From 34cc643b6602d4127755a6aff63817c5d1699a2c Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Wed, 26 Aug 2020 11:56:18 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E7=89=B9=E4=BB=B7=E5=95=86=E5=93=81=E4=B8=8D=E7=BB=99=E7=94=A8?= =?UTF-8?q?=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 6 ++++-- app/Service/CouponService.php | 18 +++++++++++++++--- app/Service/CouponServiceInterface.php | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index e7fd6c4..5caf76c 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -199,8 +199,10 @@ class CouponController extends BaseController $type = $this->request->input('type', 1); # 店铺类型id $storetypeId = $this->request->input('storetype_id', 0); - - $res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId); + # 购物车商品id + $carIds = $this->request->input('car_ids', 0); + + $res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds); return $this->success($res); } diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index 483bc72..8363f31 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -3,6 +3,7 @@ namespace App\Service; use App\Model\OrderMain; +use App\Model\ShopCar; use Hyperf\Di\Annotation\Inject; use Hyperf\DbConnection\Db; use App\Model\CouponUserRecType; @@ -155,12 +156,23 @@ class CouponService implements CouponServiceInterface * 获取用户当前订单可用的优惠券列表 * 按分类(1订单 等优惠)分组返回 */ - public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId) + public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds) { - $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); - $available = []; $notAvailable = []; + //如果存在特价商品 不给用券 + $carIdsArr = explode(',',$carIds); + $shopCarExists = ShopCar::whereIn('id',$carIdsArr) + ->where('money',0.01) + ->exists(); + if($shopCarExists){ + return [ + 'available' => $available, + 'not_available' => array_values($notAvailable) + ]; + } + + $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); if (empty($orderAmount) || empty($userId)) { return [ diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index 4f60de4..669e086 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -13,7 +13,7 @@ interface CouponServiceInterface public function getUserReceiveCouponList(); - public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId); + public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds); /** * 当前订单可用优惠券列表 From 117e356d0ae0b226e39afca95e65423268d8d49e Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Wed, 26 Aug 2020 16:12:07 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=80=80=E5=8D=95=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=95=B4=E5=90=88=20=E6=9C=89=E5=A4=B1=E6=95=88=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=88=B8=E6=97=B6=E8=BF=94=E5=9B=9E=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Model/CouponUserUse.php | 6 ++++ app/Service/CouponService.php | 43 ++++++++++++++------------ app/Service/CouponServiceInterface.php | 2 +- app/Service/OrderService.php | 6 ++-- app/Service/PurchaseLimitService.php | 2 -- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/app/Model/CouponUserUse.php b/app/Model/CouponUserUse.php index 0d53a14..76064ca 100644 --- a/app/Model/CouponUserUse.php +++ b/app/Model/CouponUserUse.php @@ -13,5 +13,11 @@ class CouponUserUse extends Model public $timestamps = false; + protected $fillable = [ + 'status', + 'return_time', + 'update_time' + ]; + protected $table = 'ims_system_coupon_user_use'; } \ No newline at end of file diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index 8363f31..d64b786 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -160,18 +160,6 @@ class CouponService implements CouponServiceInterface { $available = []; $notAvailable = []; - //如果存在特价商品 不给用券 - $carIdsArr = explode(',',$carIds); - $shopCarExists = ShopCar::whereIn('id',$carIdsArr) - ->where('money',0.01) - ->exists(); - if($shopCarExists){ - return [ - 'available' => $available, - 'not_available' => array_values($notAvailable) - ]; - } - $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); if (empty($orderAmount) || empty($userId)) { @@ -218,12 +206,27 @@ class CouponService implements CouponServiceInterface foreach ($data as $key => &$item) { if (in_array($item->id, $couponIds)) { + $item->msg = '今日已使用'; $notAvailable[$item->id] = $item; } else { $available[] = $item; } } + //如果存在特价商品 不给用券 + $carIdsArr = explode(',',$carIds); + $shopCarExists = ShopCar::whereIn('id',$carIdsArr) + ->where('money',0.01) + ->exists(); + if($shopCarExists){ + var_dump(1111); + foreach ($available as $k => $v) { + $v->msg = '特价商品不可使用'; + $notAvailable[$v->id] = $v; + } + $available = []; + } + return [ 'available' => $available, 'not_available' => array_values($notAvailable) @@ -334,7 +337,7 @@ class CouponService implements CouponServiceInterface 'coupon_'.date('Ymd').'_used_'.$order_main->user_id, $coupon->system_coupon_id ); - return $res; + return $remRes; } /* 删除-优惠券今日使用的缓存 @@ -359,19 +362,22 @@ class CouponService implements CouponServiceInterface * 先查询是否正常使用优惠券 * 修改状态,退还领取记录库存,删除ssdb缓存 */ - public function orderRefundCoupon($global_order_id) + public function orderRefundCoupons($global_order_id) { + $order_main = OrderMain::where('global_order_id',$global_order_id) + ->select('id','user_id') + ->first(); $time = time(); Db::beginTransaction(); try { $couponUses = CouponUserUse::query() ->select('id','system_coupon_id','user_id','number','user_receive_id') - ->where('global_order_id',$global_order_id) + ->where('order_main_id',$order_main->id) ->where('status',CouponUserUse::COUPON_USE_STATE_USED) - ->select(); - if(!empty($couponUse)){ + ->get(); + if(!empty($couponUses)){ foreach($couponUses as $use){ - $use->status = CouponUserUse::COUPON_USE_STATE_USED; + $use->status = CouponUserUse::COUPON_USE_STATE_CANCEL; $use->return_time = $time; $use->update_time = $time; $res = $use->save(); @@ -397,6 +403,5 @@ class CouponService implements CouponServiceInterface Db::rollBack(); return false; } - return true; } } \ No newline at end of file diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index 669e086..e7c54b5 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -29,5 +29,5 @@ interface CouponServiceInterface public function refundOrderCoupons($global_order_id); public function clearTodayCouponUsed($userId, $couponId); - public function orderRefundCoupon($global_order_id); + public function orderRefundCoupons($global_order_id); } diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index fc3c3e0..a1d5734 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -733,10 +733,10 @@ class OrderService implements OrderServiceInterface OrderMain::where('global_order_id',$global_order_id) ->update(['state' => OrderMain::ORDER_STATE_CANCEL]); //撤销redis 用券记录 - $res = $this->couponService->refundOrderCoupons($global_order_id); + $redisRes = $this->couponService->orderRefundCoupons($global_order_id); //撤销特价商品购买记录 - $res = $this->purchaseLimitService->delSsdbPurchaseRecord($global_order_id); - return $res; + $ssdbRes = $this->purchaseLimitService->delSsdbPurchaseRecord($global_order_id); + return $redisRes && $ssdbRes; } /** * @inheritDoc diff --git a/app/Service/PurchaseLimitService.php b/app/Service/PurchaseLimitService.php index 717e45e..8bbd222 100644 --- a/app/Service/PurchaseLimitService.php +++ b/app/Service/PurchaseLimitService.php @@ -71,8 +71,6 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface foreach ($goods as $k2 => $v2) { $ssdb->exec('del', SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$v1['user_id'].'_'.$v2['good_id']); - var_dump($v1['user_id']); - var_dump($v1['good_id']); } } return true; From 579cd1fd60feba386a43c5fb0011ef9c8ebf58d9 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Wed, 26 Aug 2020 16:23:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E4=BF=AE=E6=94=B9=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E5=A4=B1=E6=95=88=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/CouponService.php | 48 +---------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index d64b786..10ab91d 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -219,9 +219,8 @@ class CouponService implements CouponServiceInterface ->where('money',0.01) ->exists(); if($shopCarExists){ - var_dump(1111); foreach ($available as $k => $v) { - $v->msg = '特价商品不可使用'; + $v->msg = '不可使用'; $notAvailable[$v->id] = $v; } $available = []; @@ -295,51 +294,6 @@ class CouponService implements CouponServiceInterface return $setRes&&$expireRes; } - /** - * 取消订单返券 - * @param $order_id - * @return bool - */ - public function refundOrderCoupons($global_order_id){ - $order_main = OrderMain::where('global_order_id',$global_order_id) - ->select('id','user_id') - ->first(); - $order_id = $order_main->id; - $coupon = CouponUserUse::where([ - ['order_main_id','=',$order_id], - ['status','=',CouponUserUse::COUPON_USE_STATE_USED], - ]) - ->select('id','user_receive_id','number') - ->first(); - - if (empty($coupon)) { - return ''; - } - - // 返回用户优惠券数量并更新状态 - $res = Db::update("UPDATE ims_system_coupon_user_receive SET number_remain=number_remain+{$coupon->number}, status=IF(number=number_remain,0,1), update_time=".time()."" - ." WHERE id={$coupon->user_receive_id} AND number>=(number_remain+{$coupon->number})"); - - // 更新使用记录状态为已退回 - CouponUserUse::where([ - ['id','=',$coupon->id], - ['status','=',CouponUserUse::COUPON_USE_STATE_USED], - ]) - ->update([ - 'status' => CouponUserUse::COUPON_USE_STATE_CANCEL, - 'return_time' => time(), - 'update_time' => time(), - ]); - - //删除当日 redis 使用记录缓存 - $redis = ApplicationContext::getContainer()->get(Redis::class); - $remRes = $redis->sRem( - 'coupon_'.date('Ymd').'_used_'.$order_main->user_id, - $coupon->system_coupon_id - ); - return $remRes; - } - /* 删除-优惠券今日使用的缓存 * @param $userId * @param $couponId From e37f4c2cad1fe2594302707e51d41d2a0eb71546 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Wed, 26 Aug 2020 16:30:19 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=89=B9=E4=BB=B7=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/CouponService.php | 47 +------------------------- app/Service/CouponServiceInterface.php | 2 -- 2 files changed, 1 insertion(+), 48 deletions(-) diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php index d64b786..bb4be10 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -219,9 +219,8 @@ class CouponService implements CouponServiceInterface ->where('money',0.01) ->exists(); if($shopCarExists){ - var_dump(1111); foreach ($available as $k => $v) { - $v->msg = '特价商品不可使用'; + $v->msg = '不可使用'; $notAvailable[$v->id] = $v; } $available = []; @@ -295,50 +294,6 @@ class CouponService implements CouponServiceInterface return $setRes&&$expireRes; } - /** - * 取消订单返券 - * @param $order_id - * @return bool - */ - public function refundOrderCoupons($global_order_id){ - $order_main = OrderMain::where('global_order_id',$global_order_id) - ->select('id','user_id') - ->first(); - $order_id = $order_main->id; - $coupon = CouponUserUse::where([ - ['order_main_id','=',$order_id], - ['status','=',CouponUserUse::COUPON_USE_STATE_USED], - ]) - ->select('id','user_receive_id','number') - ->first(); - - if (empty($coupon)) { - return ''; - } - - // 返回用户优惠券数量并更新状态 - $res = Db::update("UPDATE ims_system_coupon_user_receive SET number_remain=number_remain+{$coupon->number}, status=IF(number=number_remain,0,1), update_time=".time()."" - ." WHERE id={$coupon->user_receive_id} AND number>=(number_remain+{$coupon->number})"); - - // 更新使用记录状态为已退回 - CouponUserUse::where([ - ['id','=',$coupon->id], - ['status','=',CouponUserUse::COUPON_USE_STATE_USED], - ]) - ->update([ - 'status' => CouponUserUse::COUPON_USE_STATE_CANCEL, - 'return_time' => time(), - 'update_time' => time(), - ]); - - //删除当日 redis 使用记录缓存 - $redis = ApplicationContext::getContainer()->get(Redis::class); - $remRes = $redis->sRem( - 'coupon_'.date('Ymd').'_used_'.$order_main->user_id, - $coupon->system_coupon_id - ); - return $remRes; - } /* 删除-优惠券今日使用的缓存 * @param $userId diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index e7c54b5..33bf11b 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -26,8 +26,6 @@ interface CouponServiceInterface * @return mixed */ public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]); - - public function refundOrderCoupons($global_order_id); public function clearTodayCouponUsed($userId, $couponId); public function orderRefundCoupons($global_order_id); }