diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index dc8ae7c..e7fd6c4 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -199,65 +199,9 @@ class CouponController extends BaseController $type = $this->request->input('type', 1); # 店铺类型id $storetypeId = $this->request->input('storetype_id', 0); - $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); - - $available = []; - $notAvailable = []; - - if ($this->empty($orderAmount) || $this->empty($userId)) { - return $this->success([ - 'available' => $available, - 'not_available' => array_values($notAvailable) - ]); - } - - // 获取用户优惠券 - $currentTime = time(); - - $data = Db::table('ims_system_coupon_user_receive as receive') - ->select([ - 'receive.id as receive_id', - 'receive.user_id', - 'receive.number_remain', - 'coupon.id', - 'coupon.title', - 'coupon.full_amount', - 'coupon.discounts', - 'coupon.usable_start_time', - 'coupon.usable_end_time', - 'coupon.discount_type' - ]) - ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') - ->where(['receive.user_id' => $userId]) - ->whereIn('receive.status', [0,1]) - ->where('receive.number_remain', '>', 0) - ->whereIn('coupon.type', [1,$type]) - ->where('coupon.full_amount', '<=', $orderAmount) - ->where('coupon.usable_start_time', '<=', $currentTime) - ->where('coupon.usable_end_time', '>=', $currentTime) - ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain')) - ->where('coupon.market_id', 'in', [0, $marketId]) - ->whereIn('coupon.storetype_id', $storetypeIds) - ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC') - ->get(); - - // 分离用户今天用过的优惠券种类 - $container = ApplicationContext::getContainer(); - $redis = $container->get(Redis::class); - $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId); - - foreach ($data as $key => &$item) { - if (in_array($item->id, $couponIds)) { - $notAvailable[$item->id] = $item; - } else { - $available[] = $item; - } - } - - return $this->success([ - 'available' => $available, - 'not_available' => array_values($notAvailable) - ]); + + $res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId); + return $this->success($res); } diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index 42fc827..5efe903 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -154,9 +154,67 @@ class CouponService implements CouponServiceInterface * 获取用户当前订单可用的优惠券列表 * 按分类(1订单 等优惠)分组返回 */ - public function getUserAvailableCoupons() + public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId) { + $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); + $available = []; + $notAvailable = []; + + if ($this->empty($orderAmount) || $this->empty($userId)) { + return $this->success([ + 'available' => $available, + 'not_available' => array_values($notAvailable) + ]); + } + + // 获取用户优惠券 + $currentTime = time(); + + $data = Db::table('ims_system_coupon_user_receive as receive') + ->select([ + 'receive.id as receive_id', + 'receive.user_id', + 'receive.number_remain', + 'coupon.id', + 'coupon.title', + 'coupon.full_amount', + 'coupon.discounts', + 'coupon.usable_start_time', + 'coupon.usable_end_time', + 'coupon.discount_type' + ]) + ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') + ->where(['receive.user_id' => $userId]) + ->whereIn('receive.status', [0,1]) + ->where('receive.number_remain', '>', 0) + ->whereIn('coupon.type', [1,$type]) + ->where('coupon.full_amount', '<=', $orderAmount) + ->where('coupon.usable_start_time', '<=', $currentTime) + ->where('coupon.usable_end_time', '>=', $currentTime) + ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain')) + ->where('coupon.market_id', 'in', [0, $marketId]) + ->whereIn('coupon.storetype_id', $storetypeIds) + ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC') + ->get(); + + // 分离用户今天用过的优惠券种类 + $container = ApplicationContext::getContainer(); + $redis = $container->get(Redis::class); + $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId); + + foreach ($data as $key => &$item) { + if (in_array($item->id, $couponIds)) { + $notAvailable[$item->id] = $item; + } else { + $available[] = $item; + } + } + + return [ + 'available' => $available, + 'not_available' => array_values($notAvailable) + ]; } /** diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index 160a662..2789efb 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -13,7 +13,7 @@ interface CouponServiceInterface public function getUserReceiveCouponList(); - public function getUserAvailableCoupons(); + public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId); /** * 当前订单可用优惠券列表 diff --git a/app/Service/ShopCarService.php b/app/Service/ShopCarService.php index a739846..6f0536d 100644 --- a/app/Service/ShopCarService.php +++ b/app/Service/ShopCarService.php @@ -27,6 +27,21 @@ class ShopCarService implements ShopCarServiceInterface ]; return $error; } + //一个订单只能添加一个特价商品 + if($params['money'] == 0.01){ + $goods_exists = ShopCar::where([ + ['money','=',0.01], + ['user_id','=',$params['user_id']], + ['good_id','=',$params['good_id']] + ]) + ->exists(); + if($goods_exists){ + $error = [ + 'error' => '一个订单只能添加一个特价商品' + ]; + return $error; + } + } //获取主表商品表信息 $goods = Goods::where([ ['id','=',$params['good_id']],