GoodsService.php 14 KB

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