GoodsService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. $list = $this->model->from('goods as a')
  70. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  71. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  72. ->where($where)
  73. ->where(function ($query) use($params){
  74. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  75. if($keyword){
  76. $query->where('a.goods_name','like',"%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%")->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  77. }
  78. $username = isset($params['username'])? $params['username'] : '';
  79. if($username){
  80. $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  81. }
  82. })
  83. ->where(function($query) use ($params){
  84. $time = isset($params['time'])? $params['time'] : '';
  85. if($time){
  86. $query->where('last_sell_time','<=', $time);
  87. }
  88. })
  89. ->select(['a.*','b.nickname','b.code as user_code','b.mobile as mobile','c.name as shop_name','c.code as shop_code'])
  90. ->orderBy('a.id','desc')
  91. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  92. $list = $list? $list->toArray() :[];
  93. if($list){
  94. foreach($list['data'] as &$item){
  95. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  96. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  97. $item['real_price'] = round($item['price'] - $item['sell_price'] + $item['source_price']);
  98. }
  99. }
  100. return [
  101. 'pageSize'=> $pageSize,
  102. 'total'=>isset($list['total'])? $list['total'] : 0,
  103. 'list'=> isset($list['data'])? $list['data'] : []
  104. ];
  105. }
  106. public function getTradeGoods($params, $pageSize = 15)
  107. {
  108. $where = ['a.mark' => 1];
  109. $status = isset($params['status'])? $params['status'] : 0;
  110. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  111. $shopId = isset($params['shop_id'])? $params['shop_id'] : 0;
  112. $isSell = isset($params['is_sell'])? $params['is_sell'] : 0;
  113. if($status>0){
  114. $where['a.status'] = $status;
  115. }
  116. if($shopId>0){
  117. $where['a.shop_id'] = $shopId;
  118. }
  119. if($isSell>0){
  120. $where['a.is_sell'] = $isSell;
  121. }
  122. $list = $this->model->from('trade as a')
  123. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  124. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  125. ->leftJoin('shop as d', 'c.id', '=', 'd.shop_id')
  126. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  127. ->where($where)
  128. ->where(function ($query) use($params){
  129. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  130. if($keyword){
  131. $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  132. }
  133. })
  134. ->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'])
  135. ->orderBy('a.id','desc')
  136. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  137. $list = $list? $list->toArray() :[];
  138. if($list){
  139. foreach($list['data'] as &$item){
  140. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  141. $item['confirm_time'] = $item['confirm_time']? datetime($item['confirm_time'],'Y-m-d H.i.s') : '';
  142. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  143. }
  144. }
  145. return [
  146. 'pageSize'=> $pageSize,
  147. 'total'=>isset($list['total'])? $list['total'] : 0,
  148. 'list'=> isset($list['data'])? $list['data'] : []
  149. ];
  150. }
  151. /**
  152. * 添加编辑
  153. * @return array
  154. * @since 2020/11/11
  155. * @author laravel开发员
  156. */
  157. public function edit()
  158. {
  159. // 请求参数
  160. $data = request()->all();
  161. // thumb处理
  162. $thumb = isset($data['thumb'])? trim($data['thumb']) : '';
  163. if ($thumb && strpos($thumb, "temp")) {
  164. $data['thumb'] = save_image($thumb, 'member');
  165. } else if($thumb){
  166. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  167. }
  168. if(!isset($data['code']) || empty($data['code'])){
  169. $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(GoodsModel::max('id')+1);
  170. }
  171. if(!isset($data['price']) || empty($data['price'])){
  172. $data['price'] = $data['sell_price'];
  173. }
  174. //
  175. // $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  176. // $feeRate = $feeRate? $feeRate : '2.5';
  177. // $data['fee'] = round($data['price'] * $feeRate/100, 0);
  178. return parent::edit($data); // TODO: Change the autogenerated stub
  179. }
  180. /**
  181. * 获取资料详情
  182. * @param $where
  183. * @param array $field
  184. */
  185. public function getInfo($where, array $field = [])
  186. {
  187. $cacheKey = "caches:goods:".(!is_array($where)? $where : md5(json_encode($where)));
  188. $info = RedisService::get($cacheKey);
  189. if($info){
  190. return $info;
  191. }
  192. $field = $field ? $field : '*';
  193. if (is_array($where)) {
  194. $info = $this->model->where($where)->select($field)->first();
  195. } else {
  196. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  197. }
  198. $info = $info ? $info->toArray() : [];
  199. if($info){
  200. $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
  201. RedisService::set($cacheKey, $info, rand(5,10));
  202. }
  203. return $info;
  204. }
  205. /**
  206. * 验证是否抢购有新的商品
  207. * @param $userId
  208. * @return mixed
  209. */
  210. public function checkNewGoods($userId)
  211. {
  212. return $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  213. ->where('create_time','>=', strtotime(date('Y-m-d')))
  214. ->count('id');
  215. }
  216. /**
  217. * 拆分
  218. * @param $info
  219. * @param $goods
  220. * @return bool
  221. */
  222. public function split($goodsId)
  223. {
  224. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  225. $splitNum = isset($goods['split_num'])? $goods['split_num'] : 0;
  226. if($splitNum<=0){
  227. return false;
  228. }
  229. $datas = [];
  230. $sumPrice = 0;
  231. for($i=1; $i<= $splitNum; $i++){
  232. if($i < $splitNum){
  233. $price = round($info['price']/$splitNum, 0);
  234. $sumPrice += $price;
  235. }else{
  236. $price = round($info['price'] - $sumPrice, 0);
  237. }
  238. $datas[] = [
  239. 'user_id'=> $goods['user_id'],
  240. 'shop_id'=> $goods['shop_id'],
  241. 'goods_name'=> $goods['goods_name'],
  242. 'code'=> $goods['code'].'-'.$i,
  243. 'source_price'=> round($goods['source_price']/$splitNum, 0),
  244. 'sell_price'=> round($goods['sell_price']/$splitNum, 0),
  245. 'price'=> $price,
  246. 'fee'=> 0,
  247. 'thumb'=> $goods['thumb'],
  248. 'content'=> $goods['content'],
  249. 'split_num'=> $goods['split_num'],
  250. 'split_price'=> $goods['split_price'],
  251. 'last_sell_time'=> time(),
  252. 'create_time'=> time(),
  253. 'update_time'=> time(),
  254. 'mark'=>1,
  255. 'status'=>1,
  256. ];
  257. }
  258. DB::beginTransaction();
  259. if($datas){
  260. if(!$this->model->insert($datas)){
  261. DB::rollBack();
  262. return false;
  263. }
  264. if(!$this->model->where(['id'=> $goods['id'],'mark'=>1])->update(['status'=>2,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  265. DB::rollBack();
  266. return false;
  267. }
  268. if($info && !TradeModel::where(['id'=> $info['id'],'mark'=>1])->update(['status'=>-1,'is_split'=>1,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  269. DB::rollBack();
  270. return false;
  271. }
  272. }
  273. DB::commit();
  274. return true;
  275. }
  276. /**
  277. * 转场
  278. * @param $goodsId
  279. */
  280. public function change($goodsId)
  281. {
  282. $params = request()->all();
  283. $shopId = isset($params['shop_id'])? $params['shop_id']:0;
  284. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  285. if(empty($goods)){
  286. $this->error = 2061;
  287. return false;
  288. }
  289. $shopInfo = ShopModel::where(['id'=> $shopId,'mark'=>1])->first();
  290. if(empty($shopInfo)){
  291. $this->error = 2062;
  292. return false;
  293. }
  294. if($this->model->where(['id'=> $goodsId])->update(['shop_id'=> $shopId,'update_time'=> time(),'remark'=> '店长转场'])){
  295. $this->error = 1002;
  296. return true;
  297. }
  298. return false;
  299. }
  300. /**
  301. * 转会员
  302. * @param $goodsId
  303. */
  304. public function switchUser($params)
  305. {
  306. $userId = isset($params['user_id'])? $params['user_id']:0;
  307. $ids = isset($params['ids'])? $params['ids']:[];
  308. if(empty($ids)){
  309. $this->error = 2063;
  310. return false;
  311. }
  312. $goodsList = $this->whereIn('id',$ids)->where(['mark'=>1])->first();
  313. if(empty($goodsList)){
  314. $this->error = 2061;
  315. return false;
  316. }
  317. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->first();
  318. if(empty($memberInfo)){
  319. $this->error = 2062;
  320. return false;
  321. }
  322. if($this->model->whereIn('id', $ids)->update(['user_id'=> $userId,'update_time'=> time(),'remark'=> '商品转会员'])){
  323. $this->error = 1002;
  324. return true;
  325. }
  326. return false;
  327. }
  328. /**
  329. * 封存/解封
  330. * @param $goodsId
  331. */
  332. public function lock($goodsId)
  333. {
  334. $params = request()->all();
  335. $status = isset($params['status'])? $params['status'] : 2;
  336. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  337. if(empty($goods)){
  338. $this->error = 2061;
  339. return false;
  340. }
  341. if($this->model->where(['id'=> $goodsId])->update(['status'=> $status,'update_time'=> time(),'remark'=> '店长封存'])){
  342. $this->error = 1002;
  343. return true;
  344. }
  345. return false;
  346. }
  347. /**
  348. * 修改商品信息
  349. * @param $goodsId
  350. * @return bool
  351. */
  352. public function updateData($goodsId)
  353. {
  354. $params = request()->all();
  355. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  356. if(empty($goods)){
  357. $this->error = 2061;
  358. return false;
  359. }
  360. $data = [
  361. 'thumb'=> isset($params['thumb'])? $params['thumb']: '',
  362. 'goods_name'=> isset($params['goods_name'])? $params['goods_name']: '',
  363. 'update_time'=>time(),
  364. ];
  365. if($this->model->where(['id'=> $goodsId])->update($data)){
  366. $this->error = 1008;
  367. return true;
  368. }
  369. $this->error = 1009;
  370. return false;
  371. }
  372. /**
  373. * 审核上架
  374. * @param $goodsId
  375. * @return bool
  376. */
  377. public function sellCheck($goodsId)
  378. {
  379. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  380. if(empty($goods)){
  381. $this->error = 2061;
  382. return false;
  383. }
  384. $data = [
  385. 'is_sell'=> 2,
  386. 'update_time'=>time(),
  387. ];
  388. if($this->model->where(['id'=> $goodsId])->update($data)){
  389. $this->error = 1018;
  390. return true;
  391. }
  392. $this->error = 1019;
  393. return false;
  394. }
  395. }