From e6ba220d8a1f69f299b29c8c2cf4734a24a156a9 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Tue, 25 Aug 2020 11:31:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=8D=95=E6=8E=A5=E5=8F=A3=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/OrderController.php | 4 +-- app/Model/CouponUserUse.php | 7 +++++ app/Service/CouponService.php | 37 ++++++++++++++++++++++++++ app/Service/CouponServiceInterface.php | 2 ++ app/Service/OrderService.php | 11 ++++++-- app/Service/OrderServiceInterface.php | 2 +- config/routes.php | 1 + 7 files changed, 59 insertions(+), 5 deletions(-) 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 a8cfcf3..0d53a14 100644 --- a/app/Model/CouponUserUse.php +++ b/app/Model/CouponUserUse.php @@ -6,5 +6,12 @@ namespace App\Model; class CouponUserUse extends Model { + // 正常使用 + 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 eb726f0..c2c016d 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; @@ -220,4 +221,40 @@ class CouponService implements CouponServiceInterface return $setRes&&$expireRes; } + /** + * 取消订单返券 + * @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; + } + } \ No newline at end of file diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php index bdd60c5..38355f2 100644 --- a/app/Service/CouponServiceInterface.php +++ b/app/Service/CouponServiceInterface.php @@ -27,4 +27,6 @@ interface CouponServiceInterface */ public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]); + public function refundOrderCoupons($order_id,$user_id); + } diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 2f74461..56a7893 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 @@ -713,4 +719,5 @@ class OrderService implements OrderServiceInterface public function onlineRefund($global_order_id){ return 123; } + } \ 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');