GoodsService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\Api;
  12. use App\Models\GoodsCategoryModel;
  13. use App\Models\GoodsCollectModel;
  14. use App\Models\GoodsModel;
  15. use App\Models\GoodsSkuModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. /**
  19. * 商品管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Api
  23. */
  24. class GoodsService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new GoodsModel();
  36. }
  37. /**
  38. * 静态入口
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = new static();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $cacheKey = "caches:goods:list_{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
  56. $datas = RedisService::get($cacheKey);
  57. if (empty($datas)) {
  58. $query = $this->getQuery($params)
  59. ->orderBy('a.create_time', 'desc')
  60. ->orderBy('a.id', 'desc');
  61. $field = ["a.*"];
  62. $list = $query->select($field)
  63. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  64. $list = $list ? $list->toArray() : [];
  65. if ($list) {
  66. $datas = [
  67. 'pageSize' => $pageSize,
  68. 'total' => isset($list['total']) ? $list['total'] : 0,
  69. 'list' => isset($list['data']) ? $list['data'] : []
  70. ];
  71. RedisService::set($cacheKey, $datas, rand(3, 5));
  72. }
  73. }
  74. return $datas;
  75. }
  76. /**
  77. * 查询条件
  78. * @param $params
  79. * @return mixed
  80. */
  81. public function getQuery($params)
  82. {
  83. $where = ['a.status' => 1, 'a.mark' => 1];
  84. $status = isset($params['status']) ? $params['status'] : 1;
  85. if ($status > 0) {
  86. $where['a.status'] = $status;
  87. } else {
  88. unset($where['a.status']);
  89. }
  90. $model = $this->model->with(['store','category'])
  91. ->from('goods as a')
  92. ->leftJoin('stores as b','b.id','=','a.store_id')
  93. ->where($where)
  94. ->where(function($query){
  95. $query->where('a.store_id',0)->orWhere('b.id','>',0);
  96. })
  97. ->where(function ($query) use ($params) {
  98. // 分类
  99. $categoryId = isset($params['category_id'])? intval($params['category_id']) : 0;
  100. if($categoryId>0){
  101. $query->where('a.category_id', $categoryId);
  102. }
  103. // 店铺
  104. $storeId = isset($params['store_id'])? intval($params['store_id']) : 0;
  105. if($storeId>0){
  106. $query->where('a.store_id', $storeId);
  107. }
  108. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  109. if ($keyword) {
  110. $query->where(function ($query) use ($keyword) {
  111. $query->where('a.goods_name', 'like', "%{$keyword}%")
  112. ->orWhere('a.tags','like',"%{$keyword}%");
  113. });
  114. }
  115. });
  116. return $model;
  117. }
  118. /**
  119. * 分类
  120. * @return array|mixed
  121. */
  122. public function getCategoryList()
  123. {
  124. $cacheKey = "caches:goods:categoryList";
  125. $datas = RedisService::get($cacheKey);
  126. if($datas){
  127. return $datas;
  128. }
  129. $datas = GoodsCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  130. ->select(['id','name','icon','pid','sort'])
  131. ->orderBy('sort','desc')
  132. ->orderBy('id','desc')
  133. ->get();
  134. $datas = $datas? $datas->toArray() : [];
  135. if($datas){
  136. RedisService::set($cacheKey, $datas, rand(300,600));
  137. }
  138. return $datas;
  139. }
  140. /**
  141. * 详情信息
  142. * @param $id
  143. * @return mixed
  144. */
  145. public function getInfo($id,$userId=0)
  146. {
  147. $cacheKey = "caches:goods:info_{$id}_{$userId}";
  148. $info = RedisService::get($cacheKey);
  149. if ($info) {
  150. return $info;
  151. }
  152. $info = $this->model->with(['store','category','skus'])->where(['id' => $id])->first();
  153. $info = $info ? $info->toArray() : [];
  154. if ($info) {
  155. $checkId = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$id])->value('id');
  156. $info['is_collect'] = $checkId? 1 : 0;
  157. RedisService::set($cacheKey, $info, rand(10, 20));
  158. }
  159. return $info;
  160. }
  161. /**
  162. * 收藏
  163. * @param $userId
  164. * @param $goodsId
  165. * @return array|false
  166. */
  167. public function collect($userId, $goodsId)
  168. {
  169. $info = $this->model->where(['id' => $goodsId,'status'=>1,'mark'=>1])->first();
  170. $info = $info ? $info->toArray() : [];
  171. if(empty($info)){
  172. $this->error = '商品已下架';
  173. return false;
  174. }
  175. if($id = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->value('id')){
  176. GoodsCollectModel::where(['id'=>$id])->update(['mark'=>0,'update_time'=>time()]);
  177. $this->error = '取消收藏';
  178. RedisService::clear("caches:goods:info_{$id}_{$userId}");
  179. return ['id'=>$id,'is_collect'=>0];
  180. }else{
  181. if(!$id = GoodsCollectModel::insertGetId(['user_id'=>$userId,'goods_id'=>$goodsId,'status'=>1,'mark'=>1,'create_time'=>time(),'update_time'=>time()])){
  182. $this->error = '收藏失败';
  183. return false;
  184. }
  185. $this->error = '收藏成功';
  186. RedisService::clear("caches:goods:info_{$id}_{$userId}");
  187. return ['id'=>$id,'is_collect'=>1];
  188. }
  189. }
  190. /**
  191. * @param $ids
  192. * @param $goods
  193. * @param $orderNo
  194. * @return array|false
  195. */
  196. public function getOrderGoods($ids, $goods, $orderNo, $userId=0)
  197. {
  198. $list = $this->model->whereIn('id', $ids)
  199. ->where(['status'=>1,'mark'=>1])
  200. ->select(['id as goods_id','goods_name','category_id','store_id','sku_type','price','stock','unit','weight','thumb','sku_type'])
  201. ->get();
  202. $list = $list? $list->toArray() : [];
  203. if($list){
  204. $skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','sku_name','price','stock'])->get()->keyBy('id');
  205. $skus = $skus?$skus->toArray() :[];
  206. $result = ['store_id'=>0,'total'=>0,'count'=>0,'goods'=>[]];
  207. foreach ($list as $item){
  208. $item['order_no'] = $orderNo;
  209. $id = isset($item['goods_id'])?$item['goods_id']:0;
  210. $goodsName = isset($item['goods_name'])?$item['goods_name']:'';
  211. $params = isset($goods['id_'.$id])? $goods['id_'.$id]:[];
  212. $goodsId = isset($params['id'])?$params['id']:0;
  213. $storeId = isset($item['store_id'])?$item['store_id']:0;
  214. $stock = isset($item['stock'])?$item['stock']:0;
  215. $num = isset($params['num'])?$params['num']:0;
  216. $skuId = isset($params['sku_id'])?$params['sku_id']:0;
  217. $skuType = isset($item['sku_type'])?$item['sku_type']: 1;
  218. $skuData = isset($skus[$skuId])? $skus[$skuId]:[];
  219. $skuPrice = isset($skuData['price'])?$skuData['price']:0;
  220. $skuStock = isset($skuData['stock'])?$skuData['stock']:0;
  221. $skuName = isset($skuData['sku_name'])?$skuData['sku_name']:'';
  222. $price = $skuType==2 ? $skuPrice : $item['price'];
  223. unset($item['skus']);
  224. if($result['store_id'] && $storeId != $result['store_id']){
  225. $this->error = '一次只能购买同一个商家的商品,请核对后重试~';
  226. return false;
  227. }
  228. if($stock<=0 || $num>$stock){
  229. $this->error = $skuId? "商品[{$goodsName}]规格[{$skuName}]库存不足~" : "商品[{$goodsName}]库存不足~";
  230. return false;
  231. }
  232. if($skuType==2 && ($skuStock<=0 || $num>$skuStock)){
  233. $this->error = "商品[{$goodsName}]规格[{$skuName}]库存不足~";
  234. return false;
  235. }
  236. if($num>0 && $goodsId == $id && $price>0){
  237. $result['store_id'] = $storeId;
  238. $item['user_id'] = $userId;
  239. $item['sku_id'] = $skuId;
  240. $item['price'] = $price;
  241. $item['total'] = $price;
  242. $item['num'] = $num;
  243. $total = round($price * $num,2);
  244. $item['total'] = $total;
  245. $item['thumb'] = get_image_path($item['thumb']);
  246. $result['goods'][] = $item;
  247. $result['total'] += $total;
  248. $result['count']++;
  249. }
  250. }
  251. return $result;
  252. }
  253. return false;
  254. }
  255. }