|
|
@ -164,13 +164,65 @@ class CouponRecService implements CouponRecServiceInterface |
|
|
$carts = $this->shopCartService->allForUser($userId, $marketId); |
|
|
$carts = $this->shopCartService->allForUser($userId, $marketId); |
|
|
$totalAmount = $carts['total']; |
|
|
$totalAmount = $carts['total']; |
|
|
|
|
|
|
|
|
// 获取用户可用优惠券(1)
|
|
|
|
|
|
$coupons = CouponRec::query() |
|
|
|
|
|
->join('lanzu_coupon as coupon', 'coupon.id', '=', '') |
|
|
|
|
|
->where(['user_id' => $userId]) |
|
|
|
|
|
->where('number_remain', '>', 0) |
|
|
|
|
|
->whereIn('status', [0,1]) |
|
|
|
|
|
->get(); |
|
|
|
|
|
|
|
|
// 获取购物车中商品和店铺的类别
|
|
|
|
|
|
$storeCategoryIds = []; |
|
|
|
|
|
$goodsCategoryIds = []; |
|
|
|
|
|
foreach ($carts 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']); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
$categoryIds = array_merge($storeCategoryIds, $goodsCategoryIds); |
|
|
|
|
|
|
|
|
|
|
|
$coupon = ApplicationContext::getContainer()->get(Coupon::class); |
|
|
|
|
|
$couponRec = ApplicationContext::getContainer()->get(Coupon::class); |
|
|
|
|
|
$couponTable = $coupon->getTable(); |
|
|
|
|
|
$receiveTable = $couponRec->getTable(); |
|
|
|
|
|
|
|
|
|
|
|
$currentTime = time(); |
|
|
|
|
|
|
|
|
|
|
|
// 获取用户已领取的优惠券,同时是在使用期内的优惠券
|
|
|
|
|
|
$builder = $couponRec->join($couponTable, "{$couponTable}.id", "=", "{$receiveTable}.coupon_id") |
|
|
|
|
|
->where("{$receiveTable}.user_id", $userId) |
|
|
|
|
|
->where("{$receiveTable}.number_remain", ">", 0) |
|
|
|
|
|
->whereIn("{$receiveTable}.status", [0,1]) |
|
|
|
|
|
->where("{$couponTable}.usable_start_time", "<=", $currentTime) |
|
|
|
|
|
->where("{$couponTable}.usable_end_time", ">=", $currentTime); |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($marketId)) { # 市场限制
|
|
|
|
|
|
$builder->where(function ($query) use ($marketId, $couponTable) { |
|
|
|
|
|
return $query->whereJsonContains("{$couponTable}.market_ids", [(string)$marketId]) |
|
|
|
|
|
->orWhereJsonLength("{$couponTable}.market_ids", '=', 0); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($categoryIds)) { # 分类限制
|
|
|
|
|
|
$builder->where(function ($query) use ($categoryIds, $couponTable) { |
|
|
|
|
|
return $query->whereJsonContains("{$couponTable}.category_ids", $categoryIds) |
|
|
|
|
|
->orWhereJsonLength("{$couponTable}.category_ids", '=', 0); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$couponReceives = $builder->orderByRaw("{$couponTable}.usable_end_time ASC")->get(); |
|
|
|
|
|
|
|
|
|
|
|
// 循环摘出失效不可用的优惠券
|
|
|
|
|
|
foreach ($couponReceives as $key => &$couponReceive) { |
|
|
|
|
|
|
|
|
|
|
|
// 有活动商品不可用优惠券,全部都不可用了
|
|
|
|
|
|
|
|
|
|
|
|
// 不满订单金额
|
|
|
|
|
|
|
|
|
|
|
|
// 今日已使用
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|