GoodsService.php 21 KB

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