Browse Source

用户可用优惠券

master
weigang 5 years ago
parent
commit
6352f0496c
  1. 6
      app/Controller/v3/OrderOnlineController.php
  2. 9
      app/Model/v3/Market.php
  3. 71
      app/Service/v3/Implementations/CouponRecService.php
  4. 8
      app/Service/v3/Implementations/CouponService.php
  5. 4
      app/Service/v3/Interfaces/CouponServiceInterface.php

6
app/Controller/v3/OrderOnlineController.php

@ -80,6 +80,7 @@ class OrderOnlineController extends BaseController
$userId = $this->request->input('user_id'); $userId = $this->request->input('user_id');
$marketId = $this->request->input('market_id'); $marketId = $this->request->input('market_id');
$shopcartIds = $this->request->input('shopcart_ids'); $shopcartIds = $this->request->input('shopcart_ids');
//判断用户有没有绑定手机 //判断用户有没有绑定手机
$telExists = $this->userBindTelService->check($userId); $telExists = $this->userBindTelService->check($userId);
if(!$telExists){ if(!$telExists){
@ -92,14 +93,13 @@ class OrderOnlineController extends BaseController
]) ])
->select('id') ->select('id')
->first(); ->first();
$res['location'] = $this->userAddressService->getAddressAndDistributionRrice($address->id,$marketId);
// $res['location'] = $this->userAddressService->getAddressAndDistributionRrice($address->id,$marketId);
//返回预约送达时间 数组 //返回预约送达时间 数组
$res['appointment_time'] = $this->appointmentTimeService->do(); $res['appointment_time'] = $this->appointmentTimeService->do();
// //
$res['store_list'] = $this->shopCartService->getGoodsByShopcartId($shopcartIds); $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'; $res['distribution_price'] = '5.0';
//增值服务接口 //增值服务接口

9
app/Model/v3/Market.php

@ -16,17 +16,20 @@ class Market extends Model
public function getProvinceNameAttribute() 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() 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() 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() public function stores()

71
app/Service/v3/Implementations/CouponRecService.php

@ -4,7 +4,9 @@ namespace App\Service\v3\Implementations;
use App\Model\v3\Coupon; use App\Model\v3\Coupon;
use App\Model\v3\CouponRec; use App\Model\v3\CouponRec;
use App\Model\v3\GoodsActivity;
use App\Service\v3\Interfaces\CouponRecServiceInterface; use App\Service\v3\Interfaces\CouponRecServiceInterface;
use App\Service\v3\Interfaces\CouponServiceInterface;
use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis; use Hyperf\Redis\Redis;
@ -20,6 +22,12 @@ class CouponRecService implements CouponRecServiceInterface
*/ */
protected $shopCartService; protected $shopCartService;
/**
* @Inject
* @var CouponServiceInterface
*/
protected $couponService;
public function do() public function do()
{ {
// TODO: Implement do() method. // TODO: Implement do() method.
@ -165,32 +173,47 @@ class CouponRecService implements CouponRecServiceInterface
$totalAmount = $carts['total']; $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']) { if (isset($cart['store']['category_id']) && $cart['store']['category_id']) {
array_push($storeCategoryIds, $cart['store']['category_id']); array_push($storeCategoryIds, $cart['store']['category_id']);
} }
if (isset($cart['shopping_cart']) && is_array($cart['shopping_cart'])) { if (isset($cart['shopping_cart']) && is_array($cart['shopping_cart'])) {
foreach ($cart['shopping_cart'] as $key2 => &$goods) { foreach ($cart['shopping_cart'] as $key2 => &$goods) {
// 商品类型
if (isset($goods['goods']['category_id']) && $goods['goods']['category_id']) { if (isset($goods['goods']['category_id']) && $goods['goods']['category_id']) {
array_push($goodsCategoryIds, $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); $categoryIds = array_merge($storeCategoryIds, $goodsCategoryIds);
$coupon = ApplicationContext::getContainer()->get(Coupon::class); $coupon = ApplicationContext::getContainer()->get(Coupon::class);
$couponRec = ApplicationContext::getContainer()->get(Coupon::class);
$couponRec = ApplicationContext::getContainer()->get(CouponRec::class);
$couponTable = $coupon->getTable(); $couponTable = $coupon->getTable();
$receiveTable = $couponRec->getTable(); $receiveTable = $couponRec->getTable();
$currentTime = time(); $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}.user_id", $userId)
->where("{$receiveTable}.number_remain", ">", 0) ->where("{$receiveTable}.number_remain", ">", 0)
->whereIn("{$receiveTable}.status", [0,1]) ->whereIn("{$receiveTable}.status", [0,1])
@ -213,17 +236,51 @@ class CouponRecService implements CouponRecServiceInterface
$couponReceives = $builder->orderByRaw("{$couponTable}.usable_end_time ASC")->get(); $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];
} }
} }

8
app/Service/v3/Implementations/CouponService.php

@ -55,7 +55,7 @@ class CouponService implements CouponServiceInterface
* @param $couponRecId * @param $couponRecId
* @return bool * @return bool
*/ */
function cacheTodayCouponUsed($userId, $couponId, $couponRecId)
public function cacheTodayCouponUsed($userId, $couponId, $couponRecId)
{ {
$redis = ApplicationContext::getContainer()->get(Redis::class); $redis = ApplicationContext::getContainer()->get(Redis::class);
$setRes = $redis->sAdd('coupon_' . date('Ymd') . '_used_' . $userId, $couponId); $setRes = $redis->sAdd('coupon_' . date('Ymd') . '_used_' . $userId, $couponId);
@ -63,6 +63,12 @@ class CouponService implements CouponServiceInterface
return $setRes && $expireRes; 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) public function orderUseCoupons($orderMainId, $couponRecs)
{ {
Db::beginTransaction(); Db::beginTransaction();

4
app/Service/v3/Interfaces/CouponServiceInterface.php

@ -29,6 +29,10 @@ interface CouponServiceInterface
*/ */
public function countAvailableByUser($userId); public function countAvailableByUser($userId);
public function cacheTodayCouponUsed($userId, $couponId, $couponRecId);
public function allTodayCouponUsed($userId);
/** /**
* 订单使用优惠券 * 订单使用优惠券
* @param $orderMainId * @param $orderMainId

Loading…
Cancel
Save