GoodsService.php 19 KB

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