GoodsService.php 14 KB

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