diff --git a/app/Controller/OrderController.php b/app/Controller/OrderController.php index d91a1c8..fb2ed20 100644 --- a/app/Controller/OrderController.php +++ b/app/Controller/OrderController.php @@ -102,8 +102,8 @@ class OrderController extends BaseController * 用户取消订单 */ public function onlineCancel(){ - $globalOrderId = $this->request->input('global_order_id'); - return $this->success($this->orderService->onlineCancel($globalOrderId)); + $OrderId = $this->request->input('order_id'); + return $this->success($this->orderService->onlineCancel($OrderId)); } } \ No newline at end of file diff --git a/app/Model/CouponUserUse.php b/app/Model/CouponUserUse.php index b333751..0d53a14 100644 --- a/app/Model/CouponUserUse.php +++ b/app/Model/CouponUserUse.php @@ -6,11 +6,12 @@ namespace App\Model; class CouponUserUse extends Model { - /* 状态 */ // 正常使用 - const STATE_USE = 1; - // 已退回用户 - const STATE_REFUND = 2; + const COUPON_USE_STATE_USED = 1; + // 退回用户 + const COUPON_USE_STATE_CANCEL = 2; + + public $timestamps = false; 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 ce6acef..c5cb7bd 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -7,6 +7,7 @@ use Hyperf\DbConnection\Db; use App\Model\CouponUserRecType; use App\Model\Coupon; use App\Model\CouponRec; +use App\Model\CouponUserUse; use Hyperf\Utils\ApplicationContext; use App\TaskWorker\SSDBTask; use App\Constants\SsdbKeysPrefix; @@ -221,7 +222,42 @@ class CouponService implements CouponServiceInterface } /** - * 删除-优惠券今日使用的缓存 + * 取消订单返券 + * @param $order_id + * @return bool + */ + public function refundOrderCoupons($order_id,$user_id){ + $coupon = CouponUserUse::where([ + ['order_main_id','=',$order_id], + ['status','=',CouponUserUse::COUPON_USE_STATE_USED], + ]) + ->select('id','user_receive_id','number') + ->first(); + // 返回用户优惠券数量并更新状态 + $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_'.$user_id, + $coupon->system_coupon_id + ); + return $res; + } + + /* 删除-优惠券今日使用的缓存 * @param $userId * @param $couponId * @param $couponRecId @@ -236,8 +272,5 @@ class CouponService implements CouponServiceInterface 'coupon_'.date('Ymd').'_used_'.$userId, $couponId ); - - return $res; } - } \ No newline at end of file diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index 5dd2d84..e4155d7 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -27,6 +27,6 @@ interface CouponServiceInterface */ public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]); - - function clearTodayCouponUsed($userId, $couponId); + public function refundOrderCoupons($order_id,$user_id); + public function clearTodayCouponUsed($userId, $couponId); } diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 50779c7..8d76e7c 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -704,8 +704,14 @@ class OrderService implements OrderServiceInterface /** * @inheritDoc */ - public function onlineCancel($global_order_id){ - + public function onlineCancel($order_id){ + $order_main = OrderMain::where('id',$order_id) + ->select('global_order_id','user_id') + ->first(); + OrderMain::where('id',$order_id) + ->update(['state' => OrderMain::ORDER_STATE_CANCEL]); + $res = $this->couponService->refundOrderCoupons($order_id,$order_main->user_id); + return $res; } /** * @inheritDoc @@ -739,11 +745,11 @@ class OrderService implements OrderServiceInterface $couponUses = CouponUserUse::query() ->select('id','system_coupon_id','user_id','number','user_receive_id') ->where('order_main_id',$orderMain->id) - ->where('status',CouponUserUse::STATE_USE) + ->where('status',CouponUserUse::COUPON_USE_STATE_USED) ->select(); if(!empty($couponUse)){ foreach($couponUses as $use){ - $use->status = CouponUserUse::STATE_USE; + $use->status = CouponUserUse::COUPON_USE_STATE_USED; $use->return_time = $time; $use->update_time = $time; $use->save(); @@ -773,4 +779,5 @@ class OrderService implements OrderServiceInterface return false; } } + } \ No newline at end of file diff --git a/app/Service/OrderServiceInterface.php b/app/Service/OrderServiceInterface.php index 397bc2c..4834c91 100644 --- a/app/Service/OrderServiceInterface.php +++ b/app/Service/OrderServiceInterface.php @@ -53,7 +53,7 @@ interface OrderServiceInterface * @param $global_order_id * @return mixed */ - public function onlineCancel($global_order_id); + public function onlineCancel($order_id); /** * 线上订单退款 diff --git a/config/routes.php b/config/routes.php index 8cff5ab..b2e5598 100644 --- a/config/routes.php +++ b/config/routes.php @@ -50,6 +50,7 @@ Router::addGroup('/v1/',function (){ //订单相关 Router::post('Order/addOnline', 'App\Controller\OrderController@addOnlineOrder'); Router::post('Order/addOffline', 'App\Controller\OrderController@addOfflineOrder'); + Router::post('Order/onlineCancel', 'App\Controller\OrderController@onlineCancel'); //小程序支付相关 Router::post('wxminipay/online', 'App\Controller\PaymentController@wxminiPayOnline');