|
|
|
@ -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]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |