GoodsService.php 19 KB

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