GoodsService.php 21 KB

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