CartService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\CartModel;
  13. use App\Models\GoodsModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 购物车管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class CartService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new CartModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表数据
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $cacheKey = "caches:goods:cartList_{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
  55. $datas = RedisService::get($cacheKey);
  56. if (empty($datas)) {
  57. $query = $this->getQuery($params)
  58. ->orderBy('a.create_time', 'desc')
  59. ->orderBy('a.id', 'desc');
  60. $field = ["a.*"];
  61. $list = $query->select($field)
  62. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  63. $list = $list ? $list->toArray() : [];
  64. if ($list) {
  65. $datas = [
  66. 'pageSize' => $pageSize,
  67. 'total' => isset($list['total']) ? $list['total'] : 0,
  68. 'list' => isset($list['data']) ? $list['data'] : []
  69. ];
  70. RedisService::set($cacheKey, $datas, rand(3, 5));
  71. }
  72. }
  73. return $datas;
  74. }
  75. /**
  76. * 查询条件
  77. * @param $params
  78. * @return mixed
  79. */
  80. public function getQuery($params)
  81. {
  82. $where = ['a.status' => 1, 'a.mark' => 1];
  83. $status = isset($params['status']) ? $params['status'] : 1;
  84. if ($status > 0) {
  85. $where['a.status'] = $status;
  86. } else {
  87. unset($where['a.status']);
  88. }
  89. $model = $this->model->with(['store', 'goods', 'skus'])
  90. ->from('carts as a')
  91. ->leftJoin('goods as b', 'b.id', '=', 'a.goods_id')
  92. ->where($where)
  93. ->where(function ($query) use ($params) {
  94. // 商品
  95. $goodsId = isset($params['goods_id']) ? intval($params['goods_id']) : 0;
  96. if ($goodsId > 0) {
  97. $query->where('a.goods_id', $goodsId);
  98. }
  99. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  100. if ($userId > 0) {
  101. $query->where('a.user_id', $userId);
  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('b.goods_name', 'like', "%{$keyword}%")
  112. ->orWhere('b.tags', 'like', "%{$keyword}%");
  113. });
  114. }
  115. });
  116. return $model;
  117. }
  118. /**
  119. * 添加购物车
  120. * @param $userId
  121. * @param $goodsId
  122. * @param int $skuId
  123. * @return array|false
  124. */
  125. public function add($userId, $goodsId, $skuId=0, $num=1)
  126. {
  127. $cacheKey = "caches:goods:addCart:{$userId}_{$goodsId}_{$skuId}";
  128. if(RedisService::get($cacheKey.'_lock')){
  129. $this->error = '处理中~';
  130. return false;
  131. }
  132. RedisService::set($cacheKey.'_lock', ['goods_id'=>$goodsId,'sku_id'=>$skuId], rand(3,5));
  133. $goodsInfo = GoodsModel::with(['skus'])
  134. ->where(['id' => $goodsId, 'status' => 1, 'mark' => 1])
  135. ->select(['id', 'sku_type', 'store_id'])
  136. ->first();
  137. $storeId = isset($goodsInfo['store_id']) ? $goodsInfo['store_id'] : 0;
  138. $skuType = isset($goodsInfo['sku_type']) ? $goodsInfo['sku_type'] : 0;
  139. $skus = isset($goodsInfo['skus']) ? $goodsInfo['skus'] : [];
  140. $skuId = $skuId? $skuId : (isset($skus[0]) ? $skus[0]['id'] : 0);
  141. if (empty($goodsInfo)) {
  142. RedisService::clear($cacheKey.'_lock');
  143. $this->error = '商品已下架';
  144. return false;
  145. }
  146. // 去除其他店铺购物车
  147. $this->model->where(['user_id' => $userId, 'mark' => 1])->whereNotIn('store_id', [$storeId])->update(['mark' => 0, 'update_time' => time()]);
  148. // 添加购物型
  149. if ($id = $this->model->where(['user_id' => $userId, 'goods_id' => $goodsId])->value('id')) {
  150. $this->model->where(['id' => $id])->update(['store_id' => $storeId,'num'=> DB::raw("num+{$num}"), 'sku_id' => $skuType == 1 ? 0 : $skuId, 'status' => 1, 'mark' => 1, 'create_time' => time()]);
  151. } else {
  152. $data = [
  153. 'user_id' => $userId,
  154. 'goods_id' => $goodsId,
  155. 'store_id' => $storeId,
  156. 'num'=> $num,
  157. 'sku_id' => $skuType == 1 ? 0 : $skuId,
  158. 'create_time' => time(),
  159. 'status' => 1,
  160. 'mark' => 1,
  161. ];
  162. $id = $this->model->insertGetId($data);
  163. }
  164. $this->error = '添加成功';
  165. RedisService::clear($cacheKey.'_lock');
  166. RedisService::clear("caches:goods:cartCount:{$userId}");
  167. return ['id' => $id, 'count' => $this->getCount($userId)];
  168. }
  169. /**
  170. * 更新购物车
  171. * @param $userId
  172. * @param $cartId
  173. * @return array|false
  174. */
  175. public function update($userId, $cartId, $num=1)
  176. {
  177. $cacheKey = "caches:goods:updateCart:{$userId}_{$cartId}";
  178. if(RedisService::get($cacheKey.'_lock')){
  179. $this->error = '处理中~';
  180. return false;
  181. }
  182. RedisService::set($cacheKey.'_lock', ['cart_id'=>$cartId], rand(3,5));
  183. // 去除其他店铺购物车
  184. $info = $this->model->where(['id'=>$cartId, 'user_id' => $userId, 'mark' => 1])->first();
  185. if(empty($info)){
  186. RedisService::clear($cacheKey.'_lock');
  187. $this->error = '数据不存在';
  188. return false;
  189. }
  190. if($num<=0){
  191. $this->model->where(['id' => $cartId])->update(['mark'=>0,'update_time'=>time()]);
  192. }else{
  193. $this->model->where(['id' => $cartId])->update(['num' => $num,'update_time' => time()]);
  194. }
  195. RedisService::clear($cacheKey.'_lock');
  196. RedisService::clear("caches:goods:cartCount:{$userId}");
  197. return ['id' => $cartId, 'count' => $this->getCount($userId)];
  198. }
  199. /**
  200. * 删除购物车
  201. * @param $userId
  202. * @param $cartId
  203. * @return array|false
  204. */
  205. public function deleteCart($userId, $ids=[])
  206. {
  207. if($ids){
  208. $this->model->where(['user_id'=>$userId,'mark'=>1])->whereIn('id', $ids)->update(['mark'=>0,'update_time'=>time()]);
  209. }else{
  210. $this->model->where(['user_id'=>$userId,'mark'=>1])->update(['mark' => 0,'update_time' => time()]);
  211. }
  212. RedisService::clear("caches:goods:cartCount:{$userId}");
  213. return ['ids' => $ids, 'count' => $this->getCount($userId)];
  214. }
  215. /**
  216. * 购物车数量
  217. * @param $userId
  218. * @return array|mixed
  219. */
  220. public function getCount($userId)
  221. {
  222. $cacheKey ="caches:goods:cartCount:{$userId}";
  223. $data = RedisService::get($cacheKey);
  224. if($data){
  225. return $data;
  226. }
  227. $count = $this->model->where(['user_id' => $userId, 'status' => 1, 'mark' => 1])->count('id');
  228. if($count){
  229. RedisService::set($cacheKey, $count, 300);
  230. }
  231. return $count;
  232. }
  233. }