GoodsService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  70. if ($shopId > 0) {
  71. $where['a.shop_id'] = $shopId;
  72. }
  73. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  74. if ($userId > 0) {
  75. $where['a.user_id'] = $userId;
  76. }
  77. $type = isset($params['type']) ? $params['type'] : 0;
  78. $list = $this->model->from('goods as a')
  79. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  80. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  81. ->where($where)
  82. ->where(function($query) use($type){
  83. if($type == 1){
  84. $query->whereIn('a.id', TradeModel::where(['is_pay'=>2,'status'=>1,'is_out'=>0,'mark'=>1])->pluck('goods_id'));
  85. }
  86. })
  87. ->where(function ($query) use ($params) {
  88. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  89. if ($keyword) {
  90. $query->where('a.goods_name', 'like', "%{$keyword}%")->orWhere('c.name', 'like', "%{$keyword}%")->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  91. }
  92. $username = isset($params['username']) ? $params['username'] : '';
  93. if ($username) {
  94. $query->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  95. }
  96. })
  97. ->where(function ($query) use ($params) {
  98. $time = isset($params['time']) ? $params['time'] : '';
  99. if ($time) {
  100. $query->where('a.last_sell_time', '<=', $time);
  101. }
  102. })
  103. ->where(function ($query) use ($params) {
  104. $notUserId = isset($params['not_user_id']) ? $params['not_user_id'] : 0;
  105. if ($notUserId) {
  106. $query->whereNotIn('a.user_id', [$notUserId]);
  107. }
  108. })
  109. ->select(['a.*', 'b.nickname', 'b.code as user_code', 'b.mobile as mobile', 'c.name as shop_name', 'c.code as shop_code'])
  110. ->orderBy('a.create_time', 'desc')
  111. ->orderBy('a.id', 'desc')
  112. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  113. $list = $list ? $list->toArray() : [];
  114. if ($list) {
  115. foreach ($list['data'] as &$item) {
  116. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  117. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  118. }
  119. }
  120. return [
  121. 'pageSize' => $pageSize,
  122. 'total' => isset($list['total']) ? $list['total'] : 0,
  123. 'list' => isset($list['data']) ? $list['data'] : []
  124. ];
  125. }
  126. /**
  127. * 上架审核商品
  128. * @param $params
  129. * @param int $pageSize
  130. * @return array
  131. */
  132. public function getTradeGoods($params, $pageSize = 15)
  133. {
  134. $where = ['a.mark' => 1];
  135. $status = isset($params['status']) ? $params['status'] : 0;
  136. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  137. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  138. $isSell = isset($params['is_sell']) ? $params['is_sell'] : 0;
  139. if ($status > 0) {
  140. $where['a.status'] = $status;
  141. }
  142. if ($shopId > 0) {
  143. $where['a.shop_id'] = $shopId;
  144. }
  145. if ($isSell > 0) {
  146. $where['a.is_sell'] = $isSell;
  147. }
  148. $list = $this->model->from('trade as a')
  149. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  150. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  151. ->leftJoin('shop as d', 'd.id', '=', 'a.shop_id')
  152. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  153. ->where($where)
  154. ->where(function ($query) use ($params) {
  155. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  156. if ($keyword) {
  157. $query->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  158. }
  159. })
  160. ->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'])
  161. ->orderBy('b.mobile', 'asc')
  162. ->orderBy('a.confirm_time', 'desc')
  163. ->orderBy('a.id', 'desc')
  164. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  165. $list = $list ? $list->toArray() : [];
  166. if ($list) {
  167. foreach ($list['data'] as &$item) {
  168. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  169. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  170. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  171. }
  172. }
  173. return [
  174. 'pageSize' => $pageSize,
  175. 'total' => isset($list['total']) ? $list['total'] : 0,
  176. 'list' => isset($list['data']) ? $list['data'] : []
  177. ];
  178. }
  179. /**
  180. * 添加编辑
  181. * @return array
  182. * @since 2020/11/11
  183. * @author laravel开发员
  184. */
  185. public function edit()
  186. {
  187. // 请求参数
  188. $data = request()->all();
  189. // thumb处理
  190. $thumb = isset($data['thumb']) ? trim($data['thumb']) : '';
  191. if ($thumb && strpos($thumb, "temp")) {
  192. $data['thumb'] = save_image($thumb, 'member');
  193. } else if ($thumb) {
  194. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  195. }
  196. if (!isset($data['code']) || empty($data['code'])) {
  197. $data['code'] = isset($data['id']) && $data['id'] ? 'G' . $data['id'] : 'G' . (GoodsModel::max('id') + 1);
  198. }
  199. if (!isset($data['price']) || empty($data['price'])) {
  200. $data['price'] = $data['sell_price'];
  201. }
  202. $data['real_price'] = ($data['price']-$data['sell_price']+$data['source_price']);
  203. // $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  204. // $feeRate = $feeRate? $feeRate : '2.5';
  205. // $data['fee'] = round($data['price'] * $feeRate/100, 0);
  206. return parent::edit($data); // TODO: Change the autogenerated stub
  207. }
  208. /**
  209. * 获取资料详情
  210. * @param $where
  211. * @param array $field
  212. */
  213. public function getInfo($where, array $field = [])
  214. {
  215. $cacheKey = "caches:goods:" . (!is_array($where) ? $where : md5(json_encode($where)));
  216. $info = RedisService::get($cacheKey);
  217. if ($info) {
  218. return $info;
  219. }
  220. $field = $field ? $field : '*';
  221. if (is_array($where)) {
  222. $info = $this->model->where($where)->select($field)->first();
  223. } else {
  224. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  225. }
  226. $info = $info ? $info->toArray() : [];
  227. if ($info) {
  228. $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
  229. RedisService::set($cacheKey, $info, rand(5, 10));
  230. }
  231. return $info;
  232. }
  233. /**
  234. * 验证是否抢购有新的商品
  235. * @param $userId
  236. * @return mixed
  237. */
  238. public function checkNewGoods($userId)
  239. {
  240. return $this->model->where(['user_id' => $userId, 'status' => 1, 'mark' => 1])
  241. ->where('create_time', '>=', strtotime(date('Y-m-d')))
  242. ->count('id');
  243. }
  244. /**
  245. * 拆分
  246. * @param $goodsId
  247. * @param array $info
  248. * @return bool
  249. */
  250. public function split($goodsId, $info = [])
  251. {
  252. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  253. $splitNum = isset($goods['split_num']) ? $goods['split_num'] : 0;
  254. if ($splitNum <= 0) {
  255. return false;
  256. }
  257. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  258. $priceRate = $priceRate? $priceRate : 4;
  259. $realPrice = $goods['real_price'];
  260. $sellPrice = $goods['sell_price']; // 特价
  261. $price = $goods['price']; // 买入价格
  262. $addPrice = intval($realPrice*$priceRate/100,0);
  263. // 涨价
  264. $goods['price'] = $price+$addPrice;
  265. $goods['real_price'] = $realPrice+$addPrice;
  266. $datas = [];
  267. $sumPrice = 0;
  268. for ($i = 1; $i <= $splitNum; $i++) {
  269. if ($i < $splitNum) {
  270. $price = round($goods['price'] / $splitNum, 0);
  271. $sumPrice += $price;
  272. } else {
  273. $price = round($goods['price'] - $sumPrice, 0);
  274. }
  275. $sourcePrice = round($goods['source_price'] / $splitNum, 0);
  276. $sellPrice = round($goods['sell_price'] / $splitNum, 0);
  277. $datas[] = [
  278. 'user_id' => $goods['user_id'],
  279. 'shop_id' => $goods['shop_id'],
  280. 'goods_name' => $goods['goods_name'],
  281. 'code' => $goods['code'] . '-' . $i,
  282. 'source_price' => $sourcePrice,
  283. 'sell_price' => $sellPrice,
  284. 'real_price' => ($price - $sellPrice + $sourcePrice),
  285. 'price' => $price,
  286. 'fee' => 0,
  287. 'thumb' => $goods['thumb'],
  288. 'albums' => $goods['albums'],
  289. 'content' => $goods['content'],
  290. 'split_num' => $goods['split_num'],
  291. 'split_price' => $goods['split_price'],
  292. 'last_sell_time' => time(),
  293. 'create_time' => time(),
  294. 'update_time' => time(),
  295. 'mark' => 1,
  296. 'status' => 1,
  297. ];
  298. }
  299. DB::beginTransaction();
  300. if ($datas) {
  301. if (!$this->model->insert($datas)) {
  302. DB::rollBack();
  303. return false;
  304. }
  305. if (!$this->model->where(['id' => $goods['id'], 'mark' => 1])->update(['status' => 2, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  306. DB::rollBack();
  307. return false;
  308. }
  309. if ($info && !TradeModel::where(['id' => $info['id'], 'mark' => 1])->update(['is_split' => 1, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  310. DB::rollBack();
  311. return false;
  312. }
  313. }
  314. DB::commit();
  315. return true;
  316. }
  317. /**
  318. * 手动拆分
  319. * @param $goodsId
  320. * @return bool
  321. */
  322. public function splitGoods($goodsId)
  323. {
  324. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  325. $splitNum = isset($goods['split_num']) ? $goods['split_num'] : 0;
  326. $splitPrice = isset($goods['split_price']) ? $goods['split_price'] : 0;
  327. $sellPrice = isset($goods['sell_price']) ? $goods['sell_price'] : 0;
  328. $sourcePrice = isset($goods['source_price']) ? $goods['source_price'] : 0;
  329. $price = isset($goods['price']) ? $goods['price'] : 0;
  330. if ($splitNum <= 0) {
  331. $this->error = 2066;
  332. return false;
  333. }
  334. $tradeInfo = TradeModel::where(['goods_id' => $goodsId, 'mark' => 1])
  335. ->whereIn('status', [1, 2, 3, 4])
  336. ->orderBy('create_time', 'desc')
  337. ->first();
  338. $tradeStatus = isset($tradeInfo['status']) ? $tradeInfo['status'] : 0;
  339. $isSell = isset($tradeInfo['is_sell']) ? $tradeInfo['is_sell'] : 0;
  340. if ($tradeInfo && in_array($tradeStatus, [1, 2, 3])) {
  341. $this->error = 2067;
  342. return false;
  343. }
  344. if ($tradeInfo && $tradeStatus==4 && $isSell != 2) {
  345. $this->error = 2068;
  346. return false;
  347. }
  348. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  349. $priceRate = $priceRate ? $priceRate : 4;
  350. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  351. $stopSplitPrice = $stopSplitPrice ? $stopSplitPrice : 500;
  352. $splitMinPrice = ConfigService::make()->getConfigByCode('split_min_price');
  353. $splitMinPrice = $splitMinPrice ? $splitMinPrice : 200;
  354. $realPrice = ($price - $sellPrice + $sourcePrice);
  355. $price = $goods['price']; // 当前价格
  356. $addPrice = intval($realPrice * $priceRate / 100, 0);
  357. if($price <= $splitMinPrice){
  358. $this->error = 2065;
  359. return false;
  360. }
  361. // 若特价拆分到停止拆分价格,且交易金额已上涨到最后一次涨价,则停止拆分
  362. if ($sellPrice == $stopSplitPrice && ($price + $addPrice >= $splitPrice)) {
  363. $this->error = 2065;
  364. return false;
  365. }
  366. $datas = [];
  367. $sumPrice = 0;
  368. for ($i = 1; $i <= $splitNum; $i++) {
  369. if ($i < $splitNum) {
  370. $price = round($goods['price'] / $splitNum, 0);
  371. $sumPrice += $price;
  372. } else {
  373. $price = round($goods['price'] - $sumPrice, 0);
  374. }
  375. $sourcePrice = round($goods['source_price'] / $splitNum, 0);
  376. $sellPrice = round($goods['sell_price'] / $splitNum, 0);
  377. $datas[] = [
  378. 'user_id' => $goods['user_id'],
  379. 'shop_id' => $goods['shop_id'],
  380. 'goods_name' => $goods['goods_name'],
  381. 'code' => $goods['code'] . '-' . $i,
  382. 'source_price' => $sourcePrice,
  383. 'sell_price' => $sellPrice,
  384. 'real_price' => ($price - $sellPrice + $sourcePrice),
  385. 'price' => $price,
  386. 'fee' => 0,
  387. 'thumb' => $goods['thumb'],
  388. 'albums' => $goods['albums'],
  389. 'content' => $goods['content'],
  390. 'split_num' => $goods['split_num'],
  391. 'split_price' => $goods['split_price'],
  392. 'last_sell_time' => time(),
  393. 'create_time' => time(),
  394. 'update_time' => time(),
  395. 'mark' => 1,
  396. 'status' => 1,
  397. ];
  398. }
  399. DB::beginTransaction();
  400. if ($datas) {
  401. if (!$this->model->insert($datas)) {
  402. $this->error = 2070;
  403. DB::rollBack();
  404. return false;
  405. }
  406. if (!$this->model->where(['id' => $goods['id'], 'mark' => 1])->update(['status' => 2, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  407. DB::rollBack();
  408. $this->error = 2070;
  409. return false;
  410. }
  411. if ($tradeInfo && !TradeModel::where(['id' => $tradeInfo['id'], 'mark' => 1])->update(['is_split' => 1, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  412. DB::rollBack();
  413. $this->error = 2070;
  414. return false;
  415. }
  416. }
  417. $this->error = 2069;
  418. DB::commit();
  419. return true;
  420. }
  421. /**
  422. * 转场
  423. * @param $goodsId
  424. */
  425. public function change($goodsId)
  426. {
  427. $params = request()->all();
  428. $changeShopId = isset($params['change_shop_id']) ? $params['change_shop_id'] : 0;
  429. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  430. if (empty($goods)) {
  431. $this->error = 2061;
  432. return false;
  433. }
  434. $shopInfo = ShopModel::where(['id' => $changeShopId, 'mark' => 1])->first();
  435. if (empty($shopInfo)) {
  436. $this->error = 2062;
  437. return false;
  438. }
  439. if ($this->model->where(['id' => $goodsId])->update(['shop_id' => $changeShopId, 'update_time' => time(), 'remark' => '店长转场'])) {
  440. $this->error = 1002;
  441. return true;
  442. }
  443. return false;
  444. }
  445. /**
  446. * 转会员
  447. * @param $goodsId
  448. */
  449. public function switchUser($params)
  450. {
  451. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  452. $ids = isset($params['ids']) ? $params['ids'] : [];
  453. if (empty($ids)) {
  454. $this->error = 2063;
  455. return false;
  456. }
  457. $tradeList = TradeModel::whereIn('id', $ids)->where(['mark' => 1])->first();
  458. if (empty($tradeList)) {
  459. $this->error = 2061;
  460. return false;
  461. }
  462. $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->first();
  463. if (empty($memberInfo)) {
  464. $this->error = 2062;
  465. return false;
  466. }
  467. DB::beginTransaction();
  468. if (!TradeModel::whereIn('id', $ids)->update(['user_id' => $userId, 'update_time' => time(), 'remark' => '商品转会员'])) {
  469. DB::rollBack();
  470. return true;
  471. }
  472. DB::commit();
  473. $this->error = 1002;
  474. return true;
  475. }
  476. /**
  477. * 封存/解封
  478. * @param $goodsId
  479. */
  480. public function lock($goodsId)
  481. {
  482. $params = request()->all();
  483. $status = isset($params['status']) ? $params['status'] : 2;
  484. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  485. if (empty($goods)) {
  486. $this->error = 2061;
  487. return false;
  488. }
  489. if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) {
  490. $this->error = 1002;
  491. return true;
  492. }
  493. return false;
  494. }
  495. /**
  496. * 修改商品信息
  497. * @param $goodsId
  498. * @return bool
  499. */
  500. public function modify($goodsId)
  501. {
  502. $params = request()->all();
  503. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  504. if (empty($goods)) {
  505. $this->error = 2061;
  506. return false;
  507. }
  508. $data = [
  509. 'goods_name' => isset($params['goods_name']) ? $params['goods_name'] : '',
  510. 'update_time' => time(),
  511. ];
  512. $thumb = isset($params['thumb_path']) ? $params['thumb_path'] : '';
  513. if ($thumb) {
  514. $data['thumb'] = $thumb;
  515. }
  516. if ($this->model->where(['id' => $goodsId])->update($data)) {
  517. $this->error = 1008;
  518. return true;
  519. }
  520. $this->error = 1009;
  521. return false;
  522. }
  523. }