GoodsService.php 23 KB

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