GoodsService.php 23 KB

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