GoodsService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\ShopModel;
  15. use App\Models\TradeModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 商品管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class GoodsService
  25. * @package App\Services\Common
  26. */
  27. class GoodsService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * GoodsService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new GoodsModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 列表数据
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $where = ['a.mark' => 1];
  61. $status = isset($params['status'])? $params['status'] : 0;
  62. if($status>0){
  63. $where['a.status'] = $status;
  64. }
  65. $isTrade = isset($params['is_trade'])? $params['is_trade'] : 0;
  66. if($isTrade>0){
  67. $where['a.is_trade'] = $isTrade;
  68. }
  69. $list = $this->model->from('goods as a')
  70. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  71. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  72. ->where($where)
  73. ->where(function ($query) use($params){
  74. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  75. if($keyword){
  76. $query->where('a.goods_name','like',"%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%")->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  77. }
  78. $username = isset($params['username'])? $params['username'] : '';
  79. if($username){
  80. $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  81. }
  82. })
  83. ->where(function($query) use ($params){
  84. $time = isset($params['time'])? $params['time'] : '';
  85. if($time){
  86. $query->where('a.last_sell_time','<=', $time);
  87. }
  88. })
  89. ->where(function($query) use ($params){
  90. $notUserId = isset($params['not_user_id'])? $params['not_user_id'] : 0;
  91. if($notUserId){
  92. $query->whereNotIn('a.user_id', [$notUserId]);
  93. }
  94. })
  95. ->select(['a.*','b.nickname','b.code as user_code','b.mobile as mobile','c.name as shop_name','c.code as shop_code'])
  96. ->orderBy('a.create_time','desc')
  97. ->orderBy('a.id','desc')
  98. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  99. $list = $list? $list->toArray() :[];
  100. if($list){
  101. foreach($list['data'] as &$item){
  102. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  103. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  104. $item['real_price'] = round($item['price'] - $item['sell_price'] + $item['source_price']);
  105. }
  106. }
  107. return [
  108. 'pageSize'=> $pageSize,
  109. 'total'=>isset($list['total'])? $list['total'] : 0,
  110. 'list'=> isset($list['data'])? $list['data'] : []
  111. ];
  112. }
  113. /**
  114. * 上架审核商品
  115. * @param $params
  116. * @param int $pageSize
  117. * @return array
  118. */
  119. public function getTradeGoods($params, $pageSize = 15)
  120. {
  121. $where = ['a.mark' => 1];
  122. $status = isset($params['status'])? $params['status'] : 0;
  123. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  124. $shopId = isset($params['shop_id'])? $params['shop_id'] : 0;
  125. $isSell = isset($params['is_sell'])? $params['is_sell'] : 0;
  126. if($status>0){
  127. $where['a.status'] = $status;
  128. }
  129. if($shopId>0){
  130. $where['a.shop_id'] = $shopId;
  131. }
  132. if($isSell>0){
  133. $where['a.is_sell'] = $isSell;
  134. }
  135. $list = $this->model->from('trade as a')
  136. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  137. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  138. ->leftJoin('shop as d', 'd.id', '=', 'a.shop_id')
  139. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  140. ->where($where)
  141. ->where(function ($query) use($params){
  142. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  143. if($keyword){
  144. $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  145. }
  146. })
  147. ->select(['a.*','b.nickname','b.mobile as mobile','c.nickname as sell_nickname', 'c.mobile as sell_mobile','d.name as shop_name','g.goods_name','g.thumb','g.code','g.split_stop','g.status as goods_status'])
  148. ->orderBy('a.confirm_time','desc')
  149. ->orderBy('a.id','desc')
  150. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  151. $list = $list? $list->toArray() :[];
  152. if($list){
  153. foreach($list['data'] as &$item){
  154. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  155. $item['confirm_time'] = $item['confirm_time']? datetime($item['confirm_time'],'Y-m-d H:i:s') : '';
  156. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  157. }
  158. }
  159. return [
  160. 'pageSize'=> $pageSize,
  161. 'total'=>isset($list['total'])? $list['total'] : 0,
  162. 'list'=> isset($list['data'])? $list['data'] : []
  163. ];
  164. }
  165. /**
  166. * 添加编辑
  167. * @return array
  168. * @since 2020/11/11
  169. * @author laravel开发员
  170. */
  171. public function edit()
  172. {
  173. // 请求参数
  174. $data = request()->all();
  175. // thumb处理
  176. $thumb = isset($data['thumb'])? trim($data['thumb']) : '';
  177. if ($thumb && strpos($thumb, "temp")) {
  178. $data['thumb'] = save_image($thumb, 'member');
  179. } else if($thumb){
  180. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  181. }
  182. if(!isset($data['code']) || empty($data['code'])){
  183. $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(GoodsModel::max('id')+1);
  184. }
  185. if(!isset($data['price']) || empty($data['price'])){
  186. $data['price'] = $data['sell_price'];
  187. }
  188. //
  189. // $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  190. // $feeRate = $feeRate? $feeRate : '2.5';
  191. // $data['fee'] = round($data['price'] * $feeRate/100, 0);
  192. return parent::edit($data); // TODO: Change the autogenerated stub
  193. }
  194. /**
  195. * 获取资料详情
  196. * @param $where
  197. * @param array $field
  198. */
  199. public function getInfo($where, array $field = [])
  200. {
  201. $cacheKey = "caches:goods:".(!is_array($where)? $where : md5(json_encode($where)));
  202. $info = RedisService::get($cacheKey);
  203. if($info){
  204. return $info;
  205. }
  206. $field = $field ? $field : '*';
  207. if (is_array($where)) {
  208. $info = $this->model->where($where)->select($field)->first();
  209. } else {
  210. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  211. }
  212. $info = $info ? $info->toArray() : [];
  213. if($info){
  214. $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
  215. RedisService::set($cacheKey, $info, rand(5,10));
  216. }
  217. return $info;
  218. }
  219. /**
  220. * 验证是否抢购有新的商品
  221. * @param $userId
  222. * @return mixed
  223. */
  224. public function checkNewGoods($userId)
  225. {
  226. return $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  227. ->where('create_time','>=', strtotime(date('Y-m-d')))
  228. ->count('id');
  229. }
  230. /**
  231. * 拆分
  232. * @param $info
  233. * @param $goods
  234. * @return bool
  235. */
  236. public function split($goodsId, $info=[])
  237. {
  238. $goods = $this->model->where(['id'=> $goodsId,'mark'=>1])->first();
  239. $splitNum = isset($goods['split_num'])? $goods['split_num'] : 0;
  240. if($splitNum<=0){
  241. return false;
  242. }
  243. $datas = [];
  244. $sumPrice = 0;
  245. for($i=1; $i<= $splitNum; $i++){
  246. if($i < $splitNum){
  247. $price = round($goods['price']/$splitNum, 0);
  248. $sumPrice += $price;
  249. }else{
  250. $price = round($goods['price'] - $sumPrice, 0);
  251. }
  252. $datas[] = [
  253. 'user_id'=> $goods['user_id'],
  254. 'shop_id'=> $goods['shop_id'],
  255. 'goods_name'=> $goods['goods_name'],
  256. 'code'=> $goods['code'].'-'.$i,
  257. 'source_price'=> round($goods['source_price']/$splitNum, 0),
  258. 'sell_price'=> round($goods['sell_price']/$splitNum, 0),
  259. 'price'=> $price,
  260. 'fee'=> 0,
  261. 'thumb'=> $goods['thumb'],
  262. 'albums'=> $goods['albums'],
  263. 'content'=> $goods['content'],
  264. 'split_num'=> $goods['split_num'],
  265. 'split_price'=> $goods['split_price'],
  266. 'last_sell_time'=> time(),
  267. 'create_time'=> time(),
  268. 'update_time'=> time(),
  269. 'mark'=>1,
  270. 'status'=>1,
  271. ];
  272. }
  273. DB::beginTransaction();
  274. if($datas){
  275. if(!$this->model->insert($datas)){
  276. DB::rollBack();
  277. return false;
  278. }
  279. if(!$this->model->where(['id'=> $goods['id'],'mark'=>1])->update(['status'=>2,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  280. DB::rollBack();
  281. return false;
  282. }
  283. if($info && !TradeModel::where(['id'=> $info['id'],'mark'=>1])->update(['status'=>-1,'is_split'=>1,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  284. DB::rollBack();
  285. return false;
  286. }
  287. }
  288. DB::commit();
  289. return true;
  290. }
  291. /**
  292. * 转场
  293. * @param $goodsId
  294. */
  295. public function change($goodsId)
  296. {
  297. $params = request()->all();
  298. $shopId = isset($params['shop_id'])? $params['shop_id']:0;
  299. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  300. if(empty($goods)){
  301. $this->error = 2061;
  302. return false;
  303. }
  304. $shopInfo = ShopModel::where(['id'=> $shopId,'mark'=>1])->first();
  305. if(empty($shopInfo)){
  306. $this->error = 2062;
  307. return false;
  308. }
  309. if($this->model->where(['id'=> $goodsId])->update(['shop_id'=> $shopId,'update_time'=> time(),'remark'=> '店长转场'])){
  310. $this->error = 1002;
  311. return true;
  312. }
  313. return false;
  314. }
  315. /**
  316. * 转会员
  317. * @param $goodsId
  318. */
  319. public function switchUser($params)
  320. {
  321. $userId = isset($params['user_id'])? $params['user_id']:0;
  322. $ids = isset($params['ids'])? $params['ids']:[];
  323. if(empty($ids)){
  324. $this->error = 2063;
  325. return false;
  326. }
  327. $goodsList = $this->whereIn('id',$ids)->where(['mark'=>1])->first();
  328. if(empty($goodsList)){
  329. $this->error = 2061;
  330. return false;
  331. }
  332. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->first();
  333. if(empty($memberInfo)){
  334. $this->error = 2062;
  335. return false;
  336. }
  337. if($this->model->whereIn('id', $ids)->update(['user_id'=> $userId,'update_time'=> time(),'remark'=> '商品转会员'])){
  338. $this->error = 1002;
  339. return true;
  340. }
  341. return false;
  342. }
  343. /**
  344. * 封存/解封
  345. * @param $goodsId
  346. */
  347. public function lock($goodsId)
  348. {
  349. $params = request()->all();
  350. $status = isset($params['status'])? $params['status'] : 2;
  351. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  352. if(empty($goods)){
  353. $this->error = 2061;
  354. return false;
  355. }
  356. if($this->model->where(['id'=> $goodsId])->update(['status'=> $status,'update_time'=> time(),'remark'=> '店长封存'])){
  357. $this->error = 1002;
  358. return true;
  359. }
  360. return false;
  361. }
  362. /**
  363. * 修改商品信息
  364. * @param $goodsId
  365. * @return bool
  366. */
  367. public function updateData($goodsId)
  368. {
  369. $params = request()->all();
  370. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  371. if(empty($goods)){
  372. $this->error = 2061;
  373. return false;
  374. }
  375. $data = [
  376. 'thumb'=> isset($params['thumb'])? $params['thumb']: '',
  377. 'goods_name'=> isset($params['goods_name'])? $params['goods_name']: '',
  378. 'update_time'=>time(),
  379. ];
  380. if($this->model->where(['id'=> $goodsId])->update($data)){
  381. $this->error = 1008;
  382. return true;
  383. }
  384. $this->error = 1009;
  385. return false;
  386. }
  387. }