GoodsService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. ->where($where)
  93. ->where(function ($query) use ($params) {
  94. // 分类
  95. $categoryId = isset($params['category_id'])? intval($params['category_id']) : 0;
  96. if($categoryId>0){
  97. $query->where('a.category_id', $categoryId);
  98. }
  99. // 店铺
  100. $storeId = isset($params['store_id'])? intval($params['store_id']) : 0;
  101. if($storeId>0){
  102. $query->where('a.store_id', $storeId);
  103. }
  104. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  105. if ($keyword) {
  106. $query->where(function ($query) use ($keyword) {
  107. $query->where('a.goods_name', 'like', "%{$keyword}%")
  108. ->orWhere('a.tags','like',"%{$keyword}%");
  109. });
  110. }
  111. });
  112. return $model;
  113. }
  114. /**
  115. * 分类
  116. * @return array|mixed
  117. */
  118. public function getCategoryList()
  119. {
  120. $cacheKey = "caches:goods:categoryList";
  121. $datas = RedisService::get($cacheKey);
  122. if($datas){
  123. return $datas;
  124. }
  125. $datas = GoodsCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  126. ->select(['id','name','icon','pid','sort'])
  127. ->orderBy('sort','desc')
  128. ->orderBy('id','desc')
  129. ->get();
  130. $datas = $datas? $datas->toArray() : [];
  131. if($datas){
  132. RedisService::set($cacheKey, $datas, rand(300,600));
  133. }
  134. return $datas;
  135. }
  136. /**
  137. * 详情信息
  138. * @param $id
  139. * @return mixed
  140. */
  141. public function getInfo($id,$userId=0)
  142. {
  143. $cacheKey = "caches:goods:info_{$id}_{$userId}";
  144. $info = RedisService::get($cacheKey);
  145. if ($info) {
  146. return $info;
  147. }
  148. $info = $this->model->with(['store','category','skus'])->where(['id' => $id])->first();
  149. $info = $info ? $info->toArray() : [];
  150. if ($info) {
  151. $checkId = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$id])->value('id');
  152. $info['is_collect'] = $checkId? 1 : 0;
  153. RedisService::set($cacheKey, $info, rand(10, 20));
  154. }
  155. return $info;
  156. }
  157. /**
  158. * 收藏
  159. * @param $userId
  160. * @param $goodsId
  161. * @return array|false
  162. */
  163. public function collect($userId, $goodsId)
  164. {
  165. $info = $this->model->where(['id' => $goodsId,'status'=>1,'mark'=>1])->first();
  166. $info = $info ? $info->toArray() : [];
  167. if(empty($info)){
  168. $this->error = '商品已下架';
  169. return false;
  170. }
  171. if($id = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->value('id')){
  172. GoodsCollectModel::where(['id'=>$id])->update(['mark'=>0,'update_time'=>time()]);
  173. $this->error = '取消收藏';
  174. RedisService::clear("caches:goods:info_{$id}_{$userId}");
  175. return ['id'=>$id,'is_collect'=>0];
  176. }else{
  177. if(!$id = GoodsCollectModel::insertGetId(['user_id'=>$userId,'goods_id'=>$goodsId,'status'=>1,'mark'=>1,'create_time'=>time(),'update_time'=>time()])){
  178. $this->error = '收藏失败';
  179. return false;
  180. }
  181. $this->error = '收藏成功';
  182. RedisService::clear("caches:goods:info_{$id}_{$userId}");
  183. return ['id'=>$id,'is_collect'=>1];
  184. }
  185. }
  186. /**
  187. * @param $ids
  188. * @param $goods
  189. * @param $orderNo
  190. * @return array|false
  191. */
  192. public function getOrderGoods($ids, $goods, $orderNo, $userId=0)
  193. {
  194. $list = $this->model->whereIn('id', $ids)
  195. ->where(['status'=>1,'mark'=>1])
  196. ->select(['id as goods_id','goods_name','category_id','store_id','price','stock','unit','weight','thumb','sku_type'])
  197. ->get();
  198. $list = $list? $list->toArray() : [];
  199. if($list){
  200. $skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','price','stock'])->get()->keyBy('id');
  201. $skus = $skus?$skus->toArray() :[];
  202. $result = ['store_id'=>0,'total'=>0,'count'=>0,'goods'=>[]];
  203. // var_dump($skus);
  204. // var_dump($list);
  205. // var_dump($goods);
  206. foreach ($list as $item){
  207. $item['order_no'] = $orderNo;
  208. $id = isset($item['goods_id'])?$item['goods_id']:0;
  209. $params = isset($goods['id_'.$id])? $goods['id_'.$id]:[];
  210. $goodsId = isset($params['id'])?$params['id']:0;
  211. $storeId = isset($item['store_id'])?$item['store_id']:0;
  212. $num = isset($params['num'])?$params['num']:0;
  213. $skuId = isset($params['sku_id'])?$params['sku_id']:0;
  214. $skuType = isset($item['sku_type'])?$item['sku_type']: 1;
  215. $skuData = isset($skus[$skuId])? $skus[$skuId]:[];
  216. $skuPrice = isset($skuData['price'])?$skuData['price']:0;
  217. $price = $skuType==2 ? $skuPrice : $item['price'];
  218. unset($item['skus']);
  219. if($result['store_id'] && $storeId != $result['store_id']){
  220. $this->error = '一次只能购买同一个商家的商品,请核对后重试~';
  221. return false;
  222. }
  223. if($num>0 && $goodsId == $id && $price>0){
  224. $result['store_id'] = $storeId;
  225. $item['user_id'] = $userId;
  226. $item['sku_id'] = $skuId;
  227. $item['price'] = $price;
  228. $item['total'] = $price;
  229. $item['num'] = $num;
  230. $total = round($price * $num,2);
  231. $item['total'] = $total;
  232. $item['thumb'] = get_image_path($item['thumb']);
  233. $result['goods'][] = $item;
  234. $result['total'] += $total;
  235. $result['count']++;
  236. }
  237. }
  238. return $result;
  239. }
  240. return false;
  241. }
  242. }