GoodsService.php 21 KB

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