GoodsService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. $datas = [];
  258. $sumPrice = 0;
  259. for ($i = 1; $i <= $splitNum; $i++) {
  260. if ($i < $splitNum) {
  261. $price = round($goods['price'] / $splitNum, 0);
  262. $sumPrice += $price;
  263. } else {
  264. $price = round($goods['price'] - $sumPrice, 0);
  265. }
  266. $sourcePrice = round($goods['source_price'] / $splitNum, 0);
  267. $sellPrice = round($goods['sell_price'] / $splitNum, 0);
  268. $datas[] = [
  269. 'user_id' => $goods['user_id'],
  270. 'shop_id' => $goods['shop_id'],
  271. 'goods_name' => $goods['goods_name'],
  272. 'code' => $goods['code'] . '-' . $i,
  273. 'source_price' => $sourcePrice,
  274. 'sell_price' => $sellPrice,
  275. 'real_price' => ($price - $sellPrice + $sourcePrice),
  276. 'price' => $price,
  277. 'fee' => 0,
  278. 'thumb' => $goods['thumb'],
  279. 'albums' => $goods['albums'],
  280. 'content' => $goods['content'],
  281. 'split_num' => $goods['split_num'],
  282. 'split_price' => $goods['split_price'],
  283. 'last_sell_time' => time(),
  284. 'create_time' => time(),
  285. 'update_time' => time(),
  286. 'mark' => 1,
  287. 'status' => 1,
  288. ];
  289. }
  290. DB::beginTransaction();
  291. if ($datas) {
  292. if (!$this->model->insert($datas)) {
  293. DB::rollBack();
  294. return false;
  295. }
  296. if (!$this->model->where(['id' => $goods['id'], 'mark' => 1])->update(['status' => 2, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  297. DB::rollBack();
  298. return false;
  299. }
  300. if ($info && !TradeModel::where(['id' => $info['id'], 'mark' => 1])->update(['is_split' => 1, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  301. DB::rollBack();
  302. return false;
  303. }
  304. }
  305. DB::commit();
  306. return true;
  307. }
  308. /**
  309. * 手动拆分
  310. * @param $goodsId
  311. * @return bool
  312. */
  313. public function splitGoods($goodsId)
  314. {
  315. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  316. $splitNum = isset($goods['split_num']) ? $goods['split_num'] : 0;
  317. $splitPrice = isset($goods['split_price']) ? $goods['split_price'] : 0;
  318. $sellPrice = isset($goods['sell_price']) ? $goods['sell_price'] : 0;
  319. $sourcePrice = isset($goods['source_price']) ? $goods['source_price'] : 0;
  320. $price = isset($goods['price']) ? $goods['price'] : 0;
  321. if ($splitNum <= 0) {
  322. $this->error = 2066;
  323. return false;
  324. }
  325. $tradeInfo = TradeModel::where(['goods_id' => $goodsId, 'mark' => 1])
  326. ->whereIn('status', [1, 2, 3, 4])
  327. ->orderBy('create_time', 'desc')
  328. ->first();
  329. $tradeStatus = isset($tradeInfo['status']) ? $tradeInfo['status'] : 0;
  330. $isSell = isset($tradeInfo['is_sell']) ? $tradeInfo['is_sell'] : 0;
  331. if ($tradeInfo && in_array($tradeStatus, [1, 2, 3])) {
  332. $this->error = 2067;
  333. return false;
  334. }
  335. if ($tradeInfo && $tradeStatus==4 && $isSell != 2) {
  336. $this->error = 2068;
  337. return false;
  338. }
  339. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  340. $priceRate = $priceRate ? $priceRate : 4;
  341. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  342. $stopSplitPrice = $stopSplitPrice ? $stopSplitPrice : 500;
  343. $splitMinPrice = ConfigService::make()->getConfigByCode('split_min_price');
  344. $splitMinPrice = $splitMinPrice ? $splitMinPrice : 200;
  345. $realPrice = ($price - $sellPrice + $sourcePrice);
  346. $price = $goods['price']; // 当前价格
  347. $addPrice = intval($realPrice * $priceRate / 100, 0);
  348. if($price <= $splitMinPrice){
  349. $this->error = 2065;
  350. return false;
  351. }
  352. // 若特价拆分到停止拆分价格,且交易金额已上涨到最后一次涨价,则停止拆分
  353. if ($sellPrice == $stopSplitPrice && ($price + $addPrice >= $splitPrice)) {
  354. $this->error = 2065;
  355. return false;
  356. }
  357. $datas = [];
  358. $sumPrice = 0;
  359. for ($i = 1; $i <= $splitNum; $i++) {
  360. if ($i < $splitNum) {
  361. $price = round($goods['price'] / $splitNum, 0);
  362. $sumPrice += $price;
  363. } else {
  364. $price = round($goods['price'] - $sumPrice, 0);
  365. }
  366. $sourcePrice = round($goods['source_price'] / $splitNum, 0);
  367. $sellPrice = round($goods['sell_price'] / $splitNum, 0);
  368. $datas[] = [
  369. 'user_id' => $goods['user_id'],
  370. 'shop_id' => $goods['shop_id'],
  371. 'goods_name' => $goods['goods_name'],
  372. 'code' => $goods['code'] . '-' . $i,
  373. 'source_price' => $sourcePrice,
  374. 'sell_price' => $sellPrice,
  375. 'real_price' => ($price - $sellPrice + $sourcePrice),
  376. 'price' => $price,
  377. 'fee' => 0,
  378. 'thumb' => $goods['thumb'],
  379. 'albums' => $goods['albums'],
  380. 'content' => $goods['content'],
  381. 'split_num' => $goods['split_num'],
  382. 'split_price' => $goods['split_price'],
  383. 'last_sell_time' => time(),
  384. 'create_time' => time(),
  385. 'update_time' => time(),
  386. 'mark' => 1,
  387. 'status' => 1,
  388. ];
  389. }
  390. DB::beginTransaction();
  391. if ($datas) {
  392. if (!$this->model->insert($datas)) {
  393. $this->error = 2070;
  394. DB::rollBack();
  395. return false;
  396. }
  397. if (!$this->model->where(['id' => $goods['id'], 'mark' => 1])->update(['status' => 2, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  398. DB::rollBack();
  399. $this->error = 2070;
  400. return false;
  401. }
  402. if ($tradeInfo && !TradeModel::where(['id' => $tradeInfo['id'], 'mark' => 1])->update(['is_split' => 1, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
  403. DB::rollBack();
  404. $this->error = 2070;
  405. return false;
  406. }
  407. }
  408. $this->error = 2069;
  409. DB::commit();
  410. return true;
  411. }
  412. /**
  413. * 转场
  414. * @param $goodsId
  415. */
  416. public function change($goodsId)
  417. {
  418. $params = request()->all();
  419. $changeShopId = isset($params['change_shop_id']) ? $params['change_shop_id'] : 0;
  420. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  421. if (empty($goods)) {
  422. $this->error = 2061;
  423. return false;
  424. }
  425. $shopInfo = ShopModel::where(['id' => $changeShopId, 'mark' => 1])->first();
  426. if (empty($shopInfo)) {
  427. $this->error = 2062;
  428. return false;
  429. }
  430. if ($this->model->where(['id' => $goodsId])->update(['shop_id' => $changeShopId, 'update_time' => time(), 'remark' => '店长转场'])) {
  431. $this->error = 1002;
  432. return true;
  433. }
  434. return false;
  435. }
  436. /**
  437. * 转会员
  438. * @param $goodsId
  439. */
  440. public function switchUser($params)
  441. {
  442. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  443. $ids = isset($params['ids']) ? $params['ids'] : [];
  444. if (empty($ids)) {
  445. $this->error = 2063;
  446. return false;
  447. }
  448. $tradeList = TradeModel::whereIn('id', $ids)->where(['mark' => 1])->first();
  449. if (empty($tradeList)) {
  450. $this->error = 2061;
  451. return false;
  452. }
  453. $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->first();
  454. if (empty($memberInfo)) {
  455. $this->error = 2062;
  456. return false;
  457. }
  458. DB::beginTransaction();
  459. if (!TradeModel::whereIn('id', $ids)->update(['user_id' => $userId, 'update_time' => time(), 'remark' => '商品转会员'])) {
  460. DB::rollBack();
  461. return true;
  462. }
  463. DB::commit();
  464. $this->error = 1002;
  465. return true;
  466. }
  467. /**
  468. * 封存/解封
  469. * @param $goodsId
  470. */
  471. public function lock($goodsId)
  472. {
  473. $params = request()->all();
  474. $status = isset($params['status']) ? $params['status'] : 2;
  475. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  476. if (empty($goods)) {
  477. $this->error = 2061;
  478. return false;
  479. }
  480. if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) {
  481. $this->error = 1002;
  482. return true;
  483. }
  484. return false;
  485. }
  486. /**
  487. * 修改商品信息
  488. * @param $goodsId
  489. * @return bool
  490. */
  491. public function modify($goodsId)
  492. {
  493. $params = request()->all();
  494. $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
  495. if (empty($goods)) {
  496. $this->error = 2061;
  497. return false;
  498. }
  499. $data = [
  500. 'goods_name' => isset($params['goods_name']) ? $params['goods_name'] : '',
  501. 'update_time' => time(),
  502. ];
  503. $thumb = isset($params['thumb_path']) ? $params['thumb_path'] : '';
  504. if ($thumb) {
  505. $data['thumb'] = $thumb;
  506. }
  507. if ($this->model->where(['id' => $goodsId])->update($data)) {
  508. $this->error = 1008;
  509. return true;
  510. }
  511. $this->error = 1009;
  512. return false;
  513. }
  514. }