diff --git a/app/Constants/LogLabel.php b/app/Constants/LogLabel.php index d4b9f72..072a6f7 100644 --- a/app/Constants/LogLabel.php +++ b/app/Constants/LogLabel.php @@ -53,5 +53,5 @@ class LogLabel extends AbstractConstants * @Message("Offline Paid Complete Log Label") */ const OFFLINE_PAID_LOG = 'offline_paid_log'; - + } diff --git a/app/JsonRpc/OrderService.php b/app/JsonRpc/OrderService.php index 5ecdadf..43abb2e 100644 --- a/app/JsonRpc/OrderService.php +++ b/app/JsonRpc/OrderService.php @@ -65,6 +65,8 @@ class OrderService implements OrderServiceInterface /** * 线上订单退款 + * 申请退款 state = 8 + * 退款成功 state = 9 */ public function onlineRefund($global_order_id){ Db::beginTransaction(); @@ -75,15 +77,15 @@ class OrderService implements OrderServiceInterface "code" => 0, "result" => $this->orderService->onlineRefund($global_order_id), // 'result' => $global_order_id, - "message" => '' + "message" => '退款成功' ]; } catch (\Exception $e){ + Db::rollBack(); return [ "status" => 200, - "code" => 0, - "result" => $this->orderService->onlineRefund($global_order_id), - // 'result' => $global_order_id, - "message" => '' + "code" => ErrorCode::ORDER_FAILURE, + "result" => [], + "message" => $e->getMessage() ]; } } diff --git a/app/Model/CouponUserRec.php b/app/Model/CouponUserRec.php index c5c2608..25afd2f 100644 --- a/app/Model/CouponUserRec.php +++ b/app/Model/CouponUserRec.php @@ -6,5 +6,13 @@ namespace App\Model; class CouponUserRec extends Model { + /* 状态 */ + // 未使用 + const STATE_UNUSED = 0; + // 使用部分 + const STATE_SOME = 1; + // 已用完 + const STATE_FINISH = 2; + protected $table = 'ims_system_coupon_user_receive'; } \ No newline at end of file diff --git a/app/Model/CouponUserUse.php b/app/Model/CouponUserUse.php index a8cfcf3..b333751 100644 --- a/app/Model/CouponUserUse.php +++ b/app/Model/CouponUserUse.php @@ -6,5 +6,11 @@ namespace App\Model; class CouponUserUse extends Model { + /* 状态 */ + // 正常使用 + const STATE_USE = 1; + // 已退回用户 + const STATE_REFUND = 2; + 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..ce6acef 100644 --- a/app/Service/CouponService.php +++ b/app/Service/CouponService.php @@ -220,4 +220,24 @@ class CouponService implements CouponServiceInterface return $setRes&&$expireRes; } + /** + * 删除-优惠券今日使用的缓存 + * @param $userId + * @param $couponId + * @param $couponRecId + * @return bool + */ + function clearTodayCouponUsed($userId, $couponId) + { + + $redis = ApplicationContext::getContainer()->get(Redis::class); + + $res = $redis->sRem( + '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 bdd60c5..5dd2d84 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]); + + function clearTodayCouponUsed($userId, $couponId); } diff --git a/app/Service/OrderService.php b/app/Service/OrderService.php index 2f74461..50779c7 100644 --- a/app/Service/OrderService.php +++ b/app/Service/OrderService.php @@ -711,6 +711,66 @@ class OrderService implements OrderServiceInterface * @inheritDoc */ public function onlineRefund($global_order_id){ - return 123; + Db::beginTransaction(); + try { + + $time = time(); + // 主订单状态更新 + $orderMain = OrderMain::query() + ->where(['global_order_id' => $global_order_id, 'state' => OrderMain::ORDER_STATE_DELIVERY]) + ->first(); + + if (empty($orderMain)) { + Db::rollBack(); + return false; + } + + $orderMain->state = OrderMain::ORDER_STATE_REFUNDED; + $upOrderMain = $orderMain->save(); + + // 子订单状态更新 + $upChild = Order::query() + ->where(['order_main_id' => $orderMain->id]) + ->update(['state' => OrderMain::ORDER_STATE_REFUNDED]); + + /* 退还优惠券 */ + // 先查询是否正常使用优惠券 + // 修改状态,退还领取记录库存,删除ssdb缓存 + $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) + ->select(); + if(!empty($couponUse)){ + foreach($couponUses as $use){ + $use->status = CouponUserUse::STATE_USE; + $use->return_time = $time; + $use->update_time = $time; + $use->save(); + + $couponReceive = CouponUserRec::query() + ->where('id',$use->user_receive_id) + ->whereRaw('number >= number_remain+'.$use->number) + ->update([ + 'number_remain' => Db::raw('number_remain+'.$use->number), + 'status' => Db::raw('IF(number=number_remain,' . CouponUserRec::STATE_UNUSED . ',' . CouponUserRec::STATE_SOME . ')'), + 'update_time' => $time + ]); + + $clearUseRedis = $this->couponService->clearTodayCouponUsed($use->user_id,$use->system_coupon_id); + } + } + + // 退还订单金额到用户微信余额 + + + Db::commit(); + return true; + } catch (Exception $e) { + + $this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款','exception' => $e->getMessage()]); + Db::rollBack(); + return false; + } } } \ No newline at end of file diff --git a/app/Service/PayRefundService.php b/app/Service/PayRefundService.php new file mode 100644 index 0000000..27bd0d4 --- /dev/null +++ b/app/Service/PayRefundService.php @@ -0,0 +1,47 @@ + 0, + 'msg' => '退款成功' + ]; + + // 查询订单 + $orderMain = OrderMain::query() + ->select('id','code','order_num','money','state') + ->where('global_order_id',$global_order_id) + ->where('pay_type',OrderMain::ORDER_PAY_WX) + ->where(Db::raw('refund_time is null')) + ->first(); + if(empty($orderMain)){ + return ['status'=>1, 'msg'=>'订单不存在或已退款']; + } + $optional = []; + $result = $app->refund->byOutTradeNumber( + $orderMain->code, + $orderMain->code, + $orderMain->money * 100, + $orderMain->money * 100, + $optional + ); + return $result; + } + +} \ No newline at end of file diff --git a/app/Service/PayRefundServiceInterface.php b/app/Service/PayRefundServiceInterface.php new file mode 100644 index 0000000..7ca1de1 --- /dev/null +++ b/app/Service/PayRefundServiceInterface.php @@ -0,0 +1,8 @@ +