From 6352f0496c6a4ca165a39a67e99e83ca451f8b92 Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 9 Sep 2020 21:09:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=AF=E7=94=A8=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/v3/OrderOnlineController.php | 6 +- app/Model/v3/Market.php | 9 ++- .../v3/Implementations/CouponRecService.php | 73 +++++++++++++++++-- .../v3/Implementations/CouponService.php | 8 +- .../v3/Interfaces/CouponServiceInterface.php | 4 + 5 files changed, 85 insertions(+), 15 deletions(-) diff --git a/app/Controller/v3/OrderOnlineController.php b/app/Controller/v3/OrderOnlineController.php index 5e544e6..ecab40c 100644 --- a/app/Controller/v3/OrderOnlineController.php +++ b/app/Controller/v3/OrderOnlineController.php @@ -80,6 +80,7 @@ class OrderOnlineController extends BaseController $userId = $this->request->input('user_id'); $marketId = $this->request->input('market_id'); $shopcartIds = $this->request->input('shopcart_ids'); + //判断用户有没有绑定手机 $telExists = $this->userBindTelService->check($userId); if(!$telExists){ @@ -92,14 +93,13 @@ class OrderOnlineController extends BaseController ]) ->select('id') ->first(); - $res['location'] = $this->userAddressService->getAddressAndDistributionRrice($address->id,$marketId); + // $res['location'] = $this->userAddressService->getAddressAndDistributionRrice($address->id,$marketId); //返回预约送达时间 数组 $res['appointment_time'] = $this->appointmentTimeService->do(); // $res['store_list'] = $this->shopCartService->getGoodsByShopcartId($shopcartIds); //获取用户优惠券 - $res['coupon']['available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$userId,'',1,''); - $res['coupon']['not_available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$userId,'',1,''); + $res['coupon'] = $this->couponRecService->allForOnlineOrderAvailable($userId, $marketId); //获取配送费 $res['distribution_price'] = '5.0'; //增值服务接口 diff --git a/app/Model/v3/Market.php b/app/Model/v3/Market.php index 1c95fa7..c151487 100644 --- a/app/Model/v3/Market.php +++ b/app/Model/v3/Market.php @@ -16,17 +16,20 @@ class Market extends Model public function getProvinceNameAttribute() { - return Area::query()->find($this->attributes['province_id'])->value('name'); + $area = Area::query()->find($this->attributes['province_id']); + return $area->name ?? ''; } public function getCityNameAttribute() { - return Area::query()->find($this->attributes['city_id'])->value('name'); + $area = Area::query()->find($this->attributes['city_id']); + return $area->name ?? ''; } public function getRegionNameAttribute() { - return Area::query()->find($this->attributes['region_id'])->value('name'); + $area = Area::query()->find($this->attributes['region_id']); + return $area->name ?? ''; } public function stores() diff --git a/app/Service/v3/Implementations/CouponRecService.php b/app/Service/v3/Implementations/CouponRecService.php index aa2e49c..658a253 100644 --- a/app/Service/v3/Implementations/CouponRecService.php +++ b/app/Service/v3/Implementations/CouponRecService.php @@ -4,7 +4,9 @@ namespace App\Service\v3\Implementations; use App\Model\v3\Coupon; use App\Model\v3\CouponRec; +use App\Model\v3\GoodsActivity; use App\Service\v3\Interfaces\CouponRecServiceInterface; +use App\Service\v3\Interfaces\CouponServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; use Hyperf\DbConnection\Db; use Hyperf\Redis\Redis; @@ -20,6 +22,12 @@ class CouponRecService implements CouponRecServiceInterface */ protected $shopCartService; + /** + * @Inject + * @var CouponServiceInterface + */ + protected $couponService; + public function do() { // TODO: Implement do() method. @@ -165,32 +173,47 @@ class CouponRecService implements CouponRecServiceInterface $totalAmount = $carts['total']; // 获取购物车中商品和店铺的类别 - $storeCategoryIds = []; - $goodsCategoryIds = []; - foreach ($carts as $key => &$cart) { + $storeCategoryIds = []; // 商户类型 + $goodsCategoryIds = []; // 商品类型 + $hasActivityGoodsCannotUse = false; // 购物车中是否有不可使用优惠券的活动商品 + $goodsActivityTypes = []; // 活动商品的类型集合 + foreach ($carts['store_lists'] as $key => &$cart) { + + // 商户类型 if (isset($cart['store']['category_id']) && $cart['store']['category_id']) { array_push($storeCategoryIds, $cart['store']['category_id']); } if (isset($cart['shopping_cart']) && is_array($cart['shopping_cart'])) { foreach ($cart['shopping_cart'] as $key2 => &$goods) { + // 商品类型 if (isset($goods['goods']['category_id']) && $goods['goods']['category_id']) { array_push($goodsCategoryIds, $goods['goods']['category_id']); } + // 活动商品不可使用优惠券的情况 + if ($goods['activity_type'] == 2 && $goods['goods']['can_use_coupon'] != 1) { + $hasActivityGoodsCannotUse = true; + } + // 活动商品类型集合 + if ($goods['activity_type'] == 2) { + array_merge($goodsActivityTypes, [$goods['goods']['type']]); + } } } } $categoryIds = array_merge($storeCategoryIds, $goodsCategoryIds); $coupon = ApplicationContext::getContainer()->get(Coupon::class); - $couponRec = ApplicationContext::getContainer()->get(Coupon::class); + $couponRec = ApplicationContext::getContainer()->get(CouponRec::class); $couponTable = $coupon->getTable(); $receiveTable = $couponRec->getTable(); $currentTime = time(); // 获取用户已领取的优惠券,同时是在使用期内的优惠券 - $builder = $couponRec->join($couponTable, "{$couponTable}.id", "=", "{$receiveTable}.coupon_id") + $builder = $couponRec->with('coupon') + ->select("{$receiveTable}.*") + ->join($couponTable, "{$couponTable}.id", "=", "{$receiveTable}.coupon_id") ->where("{$receiveTable}.user_id", $userId) ->where("{$receiveTable}.number_remain", ">", 0) ->whereIn("{$receiveTable}.status", [0,1]) @@ -213,17 +236,51 @@ class CouponRecService implements CouponRecServiceInterface $couponReceives = $builder->orderByRaw("{$couponTable}.usable_end_time ASC")->get(); - // 循环摘出失效不可用的优惠券 - foreach ($couponReceives as $key => &$couponReceive) { + $available = []; + $notAvailable = []; - // 有活动商品不可用优惠券,全部都不可用了 + // 有活动商品不可用优惠券,全部都不可用了 + if ($hasActivityGoodsCannotUse) { + $notAvailable = $couponReceives; + foreach ($notAvailable as $key => &$item) { + $item['not_available_reason'] = '活动不可用'; + } + $couponReceives = []; + } + + // 循环摘出失效不可用的优惠券 + foreach ($couponReceives as $key => &$item) { // 不满订单金额 + if ($item['coupon']['full_amount'] > $totalAmount) { + $item['not_available_reason'] = '差'.bcsub($item['coupon']['full_amount'], $totalAmount, 2).'元'; + array_push($notAvailable, $item); + continue; + } // 今日已使用 + $todayUsedCouponIds = $this->couponService->allTodayCouponUsed($userId); + if (in_array($item['coupon']['id'], $todayUsedCouponIds)) { + $item['not_available_reason'] = '今日已使用'; + array_push($notAvailable, $item); + continue; + } + + // 活动商品可用,校验对应的优惠券可使用活动类型 + $intersects = array_intersect($goodsActivityTypes, $item['activity_available']); # 所有活动商品的活动类型和优惠券的可用活动类型求交集 + $canUseByTypes = array_diff($goodsActivityTypes, $intersects); # 所有活动商品的活动类型和上述交集求差集,如果为空则是可以用的,否则说明前者中有后者不能用的活动类型 + if (!empty($canUseByTypes)) { + $item['not_available_reason'] = '活动不可用'; + array_push($notAvailable, $item); + continue; + } + $item['not_available_reason'] = ''; + array_push($available, $item); } + return ['available' => $available, 'not_available' => $notAvailable]; + } } \ No newline at end of file diff --git a/app/Service/v3/Implementations/CouponService.php b/app/Service/v3/Implementations/CouponService.php index 5071541..38d83c5 100644 --- a/app/Service/v3/Implementations/CouponService.php +++ b/app/Service/v3/Implementations/CouponService.php @@ -55,7 +55,7 @@ class CouponService implements CouponServiceInterface * @param $couponRecId * @return bool */ - function cacheTodayCouponUsed($userId, $couponId, $couponRecId) + public function cacheTodayCouponUsed($userId, $couponId, $couponRecId) { $redis = ApplicationContext::getContainer()->get(Redis::class); $setRes = $redis->sAdd('coupon_' . date('Ymd') . '_used_' . $userId, $couponId); @@ -63,6 +63,12 @@ class CouponService implements CouponServiceInterface return $setRes && $expireRes; } + public function allTodayCouponUsed($userId) + { + $redis = ApplicationContext::getContainer()->get(Redis::class); + return $couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId); + } + public function orderUseCoupons($orderMainId, $couponRecs) { Db::beginTransaction(); diff --git a/app/Service/v3/Interfaces/CouponServiceInterface.php b/app/Service/v3/Interfaces/CouponServiceInterface.php index c44e41e..0e58068 100644 --- a/app/Service/v3/Interfaces/CouponServiceInterface.php +++ b/app/Service/v3/Interfaces/CouponServiceInterface.php @@ -29,6 +29,10 @@ interface CouponServiceInterface */ public function countAvailableByUser($userId); + public function cacheTodayCouponUsed($userId, $couponId, $couponRecId); + + public function allTodayCouponUsed($userId); + /** * 订单使用优惠券 * @param $orderMainId