gorden 2 місяців тому
батько
коміт
d182ccae16

+ 4 - 3
app/admin/controller/order/PayDetailController.php

@@ -293,14 +293,15 @@ class PayDetailController extends Curd
             $data = $this->insertGiveInput($request);
             $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
 
-            $this->doInsert($data);
 
             // 金额累加到福利账户
-            $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'WELFARE')->first();
+            $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'CASH')->first();
             $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
             $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
             $memberAccount->save();
 
+            $data['pay_amount'] = 0;
+            $this->doInsert($data);
             Db::commit();
         } catch (BusinessException $customException) {
             Db::rollBack();
@@ -520,7 +521,7 @@ class PayDetailController extends Curd
         $data['join_pay_object_json'] = json_encode(['order_id' => 'SYSTEM']);
 
         $rate = 0;
-        $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
+        $extendJson['added_amount'] = $data['pay_amount'];
         $extendJson['added_rate'] = $rate;
         $data['pay_extend_json'] = json_encode($extendJson);
         $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));

+ 103 - 15
app/admin/controller/order/WholeController.php

@@ -836,6 +836,10 @@ class WholeController extends Curd
         try {
             // 使用优惠券
             $couponUseJson = [];
+            if (!empty($order->order_discount_json)) {
+                // 释放下单时选的优惠券
+                $order->order_discount_json = $this->releaseCoupon(json_decode($order->order_discount_json, true));
+            }
             if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
                 $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
                 if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
@@ -844,12 +848,12 @@ class WholeController extends Curd
                 // 组装优惠券使用数据,存主表优惠里
                 if (!empty($couponResult['use_coupon_json'])) {
                     $couponUseJson = $couponResult['use_coupon_json'];
-                    if (!empty($order->order_discount_json)) {
-                        $orderDiscountJson = json_decode($order->order_discount_json, true);
-                        if (!empty($orderDiscountJson)) {
-                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
-                        }
-                    }
+//                    if (!empty($order->order_discount_json)) {
+//                        $orderDiscountJson = json_decode($order->order_discount_json, true);
+//                        if (!empty($orderDiscountJson)) {
+//                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
+//                        }
+//                    }
                 }
             }
             // 存储优惠信息
@@ -1308,6 +1312,9 @@ class WholeController extends Curd
 
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "订单支付成功");
@@ -1441,6 +1448,10 @@ class WholeController extends Curd
         Db::beginTransaction();
         try {// 使用优惠券
             $couponUseJson = [];
+            if (!empty($order->order_discount_json)) {
+                // 释放下单时选的优惠券
+                $order->order_discount_json = $this->releaseCoupon(json_decode($order->order_discount_json, true));
+            }
             if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
                 $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
                 if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
@@ -1449,12 +1460,12 @@ class WholeController extends Curd
                 // 组装优惠券使用数据,存主表优惠里
                 if (!empty($couponResult['use_coupon_json'])) {
                     $couponUseJson = $couponResult['use_coupon_json'];
-                    if (!empty($order->order_discount_json)) {
-                        $orderDiscountJson = json_decode($order->order_discount_json, true);
-                        if (!empty($orderDiscountJson)) {
-                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
-                        }
-                    }
+//                    if (!empty($order->order_discount_json)) {
+//                        $orderDiscountJson = json_decode($order->order_discount_json, true);
+//                        if (!empty($orderDiscountJson)) {
+//                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
+//                        }
+//                    }
                 }
             }
             // 存储优惠信息
@@ -1751,6 +1762,8 @@ class WholeController extends Curd
             }
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "订单支付成功");
@@ -2489,6 +2502,9 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "创建订单成功");
@@ -2508,6 +2524,68 @@ class WholeController extends Curd
         }
     }
 
+    /**
+     * @Desc 释放下单时选的优惠券
+     * @Author Gorden
+     * @Date 2024/9/19 9:56
+     *
+     * @param $coupon
+     * @return false|string
+     */
+    private function releaseCoupon($coupon)
+    {
+        if (!empty($coupon) && is_array($coupon)) {
+            $updateData = [
+                'coupon_detail_status'=>'ACTIVED',
+                'coupon_detail_used_datetime' => ''
+            ];
+            foreach ($coupon as $key => $item) {
+                if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
+                    foreach ($item['coupon_detail_id'] as $detailId) {
+                        if (substr($detailId, 0, 4) == 'CUDT') {
+                            CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
+                                ->where('coupon_detail_id', $detailId)
+                                ->where('coupon_detail_status', 'WAITING')
+                                ->update($updateData);
+                        }
+                    }
+                    unset($coupon[$key]);
+                }
+            }
+        }
+
+        return json_encode($coupon);
+    }
+
+    /**
+     * @Desc 修改优惠券状态
+     * @Author Gorden
+     * @Date 2024/9/19 9:03
+     *
+     * @param $coupon
+     * @param $status
+     * @return void
+     */
+    private function changeOrderCouponStatus($coupon, $status)
+    {
+        if (!empty($coupon) && is_array($coupon)) {
+            $updateData['coupon_detail_status'] = $status;
+            if ($status == 'ACTIVED' || $status == 'WAITING') {
+                $updateData['coupon_detail_used_datetime'] = '';
+            } elseif ($status == 'USED') {
+                $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
+            }
+            foreach ($coupon as $item) {
+                if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
+                    foreach ($item['coupon_detail_id'] as $detailId) {
+                        CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
+                            ->where('coupon_detail_id', $detailId)->update($updateData);
+                    }
+                }
+            }
+        }
+    }
+
     /**
      * 组合支付下单
      */
@@ -2973,6 +3051,9 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "创建订单成功");
@@ -4232,7 +4313,6 @@ class WholeController extends Curd
             } else {
                 $orderIds = $goodsOrderIds;
             }
-            dump($orderIds);
         }
         if (!empty($orderId) || !empty($goodsName)) {
             $where['order_id'] = ['in', implode(',', $orderIds)];
@@ -4507,6 +4587,11 @@ class WholeController extends Curd
             return json_fail('订单不存在');
         }
 
+        $discountJson = [];
+        if (!empty($order->order_discount_json)) {
+            $discountJson = json_decode($order->order_discount_json, true);
+        }
+
         if ($order->order_status_payment == 'SUCCESS') {
             return json_success('success');
         }
@@ -4518,10 +4603,10 @@ class WholeController extends Curd
                 $result = Pay::wechat(config('payment.wxpay'))->find($order->order_groupby, 'pos');
                 $result = json_decode(json_encode($result), true);
                 if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
+                    $order->order_status_payment = 'SUCCESS';
                     if (in_array($order->order_category, ['SERVICE', 'CHNMED', 'CHNNCD', 'DISHES', 'MEALS', 'VIP'])) {
                         $order->order_is_complete = 'Y';
                         $order->order_status_system = 'DONE';
-                        $order->order_status_payment = 'SUCCESS';
                     } else {
                         $order->order_status_system = 'SENDING';
                     }
@@ -4530,6 +4615,8 @@ class WholeController extends Curd
                     OrderSheet::where('join_sheet_order_id', $orderId)->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
                     // 支付记录
                     PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_prepayid', 'WXPAY')->update(['pay_status' => 'SUCCESS']);
+                    // 优惠券标记使用
+                    $this->changeOrderCouponStatus($discountJson, 'USED');
 
                     Db::commit();
                     return json_success('success');
@@ -4550,7 +4637,8 @@ class WholeController extends Curd
                     OrderSheet::where('join_sheet_order_id', $orderId)->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
                     // 支付记录
                     PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_prepayid', 'ALIPAY')->update(['pay_status' => 'SUCCESS']);
-
+                    // 优惠券标记使用
+                    $this->changeOrderCouponStatus($discountJson, 'USED');
                     Db::commit();
                     return json_success('success');
                 }

+ 49 - 28
app/admin/service/member/MemberService.php

@@ -31,6 +31,7 @@ class MemberService
         $page = $request->get('page', 1);
         $pageSize = $request->get('pageSize', 10);
         $name = $request->get('name', '');
+        $nickname = $request->get('nickname', '');
         $mobile = $request->get('mobile', '');
         $level = $request->get('level', '');
         $isAuth = $request->get('is_auth', '');
@@ -80,6 +81,10 @@ class MemberService
                     $addtime[1] = strtotime(date('Y-m-t') . ' 23:59:59');
                 }
                 $query->whereBetween('member.member_addtimes', $addtime);
+            })->when(!empty($nickname), function ($query) use ($nickname) {
+                $query->where(function($query2) use ($nickname){
+                    $query2->where('member_info.member_info_nickname','like','%'.$nickname.'%')->orWhere('member.member_mobile','like','%'.$nickname.'%');
+                });
             })
             ->select('member_id', 'member_is_vip', 'member_is_owner', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
                 'member_info.member_info_nickname', 'member_info.member_info_headimg', 'member_info.member_info_referee', 'member_info.member_info_business', 'member_info.member_info_service', 'member_info.member_info_city', 'member_info.member_info_address', 'member_info.member_info_gender', 'member_info.member_info_birthday',
@@ -98,17 +103,17 @@ class MemberService
             $row['coupon_count'] = CouponDetail::where('join_coupon_detail_member_id', $row['member_id'])->count();
             $row['member_mobile'] = substr($row['member_mobile'], 0, 3) . '****' . substr($row['member_mobile'], 7);
             $row['info'] = [
-                'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '用户',
+                'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
                 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
                 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
                 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
                 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : '',
-                'member_info_city' => !empty($row['member_info_city']) ? explode('-',$row['member_info_city']) : '',
+                'member_info_city' => !empty($row['member_info_city']) ? explode('-', $row['member_info_city']) : '',
                 'member_info_address' => !empty($row['member_info_address']) ? $row['member_info_address'] : '',
                 'member_info_birthday' => !empty($row['member_info_birthday']) ? $row['member_info_birthday'] : '',
                 'member_info_gender' => !empty($row['member_info_gender']) ? $row['member_info_gender'] : '',
             ];
-            if (substr($row['info']['member_info_headimg'],0,2) == '..'){
+            if (substr($row['info']['member_info_headimg'], 0, 2) == '..') {
                 $row['info']['member_info_headimg'] = '';
             }
             if (!empty($row['info']['member_info_address']) && in_array($row['info']['member_info_address'], ['请选择', '省-市-区'])) {
@@ -343,7 +348,7 @@ class MemberService
             'member_info_referee' => !empty($member['info']) && !empty($member['info']['member_info_referee']) ? $member['info']['member_info_referee'] : '',
             'member_info_business' => !empty($member['info']) && !empty($member['info']['member_info_business']) ? $member['info']['member_info_business'] : '',
             'member_info_service' => !empty($member['info']) && !empty($member['info']['member_info_service']) ? $member['info']['member_info_service'] : '',
-            'member_info_city' => !empty($member['info']) && !empty($member['info']['member_info_city']) ? explode('-',$member['info']['member_info_city']) : '',
+            'member_info_city' => !empty($member['info']) && !empty($member['info']['member_info_city']) ? explode('-', $member['info']['member_info_city']) : '',
             'member_info_address' => !empty($member['info']) && !empty($member['info']['member_info_address']) ? $member['info']['member_info_address'] : '',
             'member_info_gender' => !empty($member['info']) && !empty($member['info']['member_info_gender']) ? $member['info']['member_info_gender'] : '',
             'member_info_birthday' => !empty($member['info']) && !empty($member['info']['member_info_birthday']) ? $member['info']['member_info_birthday'] : '',
@@ -363,10 +368,10 @@ class MemberService
             'member_cert_nation' => $member['cert']['member_cert_nation'] ?? '',
             'member_cert_addtimes' => $member['cert']['member_cert_addtimes'] ?? '',
         ];
-        if (!empty($member['cert']['member_cert_addtimes'])){
-            $member['cert']['member_cert_addtimes'] = date('Y-m-d',$member['cert']['member_cert_addtimes']);
-        }else{
-            $member['cert']['member_cert_addtimes'] = date('Y-m-d',strtotime($member['member_addtimes']));
+        if (!empty($member['cert']['member_cert_addtimes'])) {
+            $member['cert']['member_cert_addtimes'] = date('Y-m-d', $member['cert']['member_cert_addtimes']);
+        } else {
+            $member['cert']['member_cert_addtimes'] = date('Y-m-d', strtotime($member['member_addtimes']));
         }
         if (!empty($member['join_invite_member_id'])) {
             $inviteMember = Member::with([
@@ -434,6 +439,9 @@ class MemberService
         $account = MemberAccount::where('join_account_member_id', $memberId)
             ->where('member_account_classify', 'VIP')
             ->first();
+        if (!$account) {
+            return json_fail('暂无数据');
+        }
         $balance = $account->member_account_surplus + $account->member_account_added;
 
         $info = PayDetail::where(function ($query) use ($memberId) {
@@ -545,6 +553,9 @@ class MemberService
         $account = MemberAccount::where('join_account_member_id', $memberId)
             ->where('member_account_classify', 'WELFARE')
             ->first();
+        if (!$account) {
+            return json_fail('暂无数据');
+        }
         $balance = $account->member_account_surplus + $account->member_account_added;
 
         $info = PayDetail::where(function ($query) use ($memberId) {
@@ -657,6 +668,9 @@ class MemberService
         $account = MemberAccount::where('join_account_member_id', $memberId)
             ->where('member_account_classify', 'CASH')
             ->first();
+        if (!$account) {
+            return json_fail('暂无数据');
+        }
         $balance = $account->member_account_surplus + $account->member_account_added;
 
         $info = PayDetail::where(function ($query) use ($memberId) {
@@ -697,12 +711,12 @@ class MemberService
             if (mb_substr($item['goods_category'], 0, 7) == '自定义金额充值') {
                 $item['goods_category'] = '自定义金额充值';
             }
-            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND','GIVE'])) {
+            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE'])) {
                 $item['balance_type'] = 'add';
             } else {
                 $item['balance_type'] = 'cut';
             }
-            if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND','GIVE'])) {
+            if ($key == 0 && in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE'])) {
                 if (!empty($item['pay_extend_json'])) {
                     $extendJson = json_decode($item['pay_extend_json'], true);
                     if (!isset($extendJson['added_amount'])) {
@@ -717,7 +731,7 @@ class MemberService
                             'join_pay_member_id' => $item['join_pay_member_id'],
                             'pay_addtimes' => $item['pay_addtimes'],
                             'pay_amount' => $extendJson['added_amount'],
-                            'pay_category' => 'ADDED',
+                            'pay_category' => $item['pay_category'] == 'GIVE' ? 'GIVE' : 'ADDED',
                             'balance' => number_format($balance, 2, '.', ''),
                             'balance_type' => 'add',
                         ];
@@ -727,11 +741,14 @@ class MemberService
 
                 $item['balance'] = number_format($balance, 2, '.', '');
                 $balance = $balance - $item['pay_amount'];
-                $data[] = $item;
+                if ($item['pay_category'] != 'GIVE'){
+                    $data[] = $item;
+                }
+
                 continue;
             }
 
-            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND','GIVE'])) {
+            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE'])) {
                 if (!empty($item['pay_extend_json'])) {
                     $extendJson = json_decode($item['pay_extend_json'], true);
                     if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {
@@ -739,7 +756,7 @@ class MemberService
                             'join_pay_member_id' => $item['join_pay_member_id'],
                             'pay_addtimes' => $item['pay_addtimes'],
                             'pay_amount' => $extendJson['added_amount'],
-                            'pay_category' => 'ADDED',
+                            'pay_category' => $item['pay_category'] == 'GIVE' ? 'GIVE' : 'ADDED',
                             'balance' => number_format($balance, 2, '.', ''),
                             'balance_type' => 'add',
                         ];
@@ -753,7 +770,9 @@ class MemberService
                 $balance = $balance + $item['pay_amount'];
             }
 
-            $data[] = $item;
+            if ($item['pay_category'] != 'GIVE') {
+                $data[] = $item;
+            }
         }
 
         $total = count($data);
@@ -768,7 +787,9 @@ class MemberService
         $account = MemberAccount::where('join_account_member_id', $memberId)
             ->where('member_account_classify', 'POINTS')
             ->first();
-
+        if (!$account) {
+            return json_fail('暂无数据');
+        }
         $balance = $account->member_account_surplus + $account->member_account_added;
         $points = ClientPoints::where('join_client_points_member_id', $memberId)->get()->toArray();
         $nowPoint = 0;
@@ -912,7 +933,7 @@ class MemberService
                 'member_info_service' => $params['member_info_service'] ?? '',
                 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
                 'member_info_address' => $params['member_info_address'] ?? '',
-                'member_info_birthday'=>$params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
+                'member_info_birthday' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
                 'member_info_gender' => $params['gender'] ?? '',
             ];
             MemberInfo::insert($infoData);
@@ -1009,7 +1030,7 @@ class MemberService
                 'member_info_service' => $params['member_info_service'] ?? '',
                 'member_info_city' => !empty($params['province']) ? implode('-', $params['province']) : '',
                 'member_info_address' => $params['member_info_address'] ?? '',
-                'member_info_birthday'=>$params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
+                'member_info_birthday' => $params['birth'] ? date('Y-m-d', strtotime($params['birth'])) : '',
                 'member_info_gender' => $params['gender'] ?? '',
             ];
             if (!MemberInfo::where('join_info_member_id', $params['member_id'])->exists()) {
@@ -1018,25 +1039,25 @@ class MemberService
                 MemberInfo::where('join_info_member_id', $params['member_id'])->update($infoData);
             }
             // 福利账户
-            if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr',$memberId.'-WELFARE')->exists()){
+            if (!empty($params['member_classify']) && $params['member_classify'] == 'EMPLOY' && !MemberAccount::where('member_account_nbr', $memberId . '-WELFARE')->exists()) {
                 $welfareData = [
-                    'join_account_member_id'=>$memberId,
-                    'member_account_classify'=>'WELFARE',
-                    'member_account_status'=>'ACTIVED',
-                    'member_account_category'=>'NORMAL',
-                    'member_account_nbr'=>$memberId.'-WELFARE',
-                    'member_account_name'=>'福利账户',
-                    'member_account_addtimes'=>time()
+                    'join_account_member_id' => $memberId,
+                    'member_account_classify' => 'WELFARE',
+                    'member_account_status' => 'ACTIVED',
+                    'member_account_category' => 'NORMAL',
+                    'member_account_nbr' => $memberId . '-WELFARE',
+                    'member_account_name' => '福利账户',
+                    'member_account_addtimes' => time()
                 ];
                 MemberAccount::insert($welfareData);
             }
 
             Db::commit();
-        }catch (BusinessException $e){
+        } catch (BusinessException $e) {
             Db::rollBack();
             dump($e->getMessage() . ';line:' . $e->getLine());
             return json_fail($e->getMessage());
-        }catch (\Exception $e) {
+        } catch (\Exception $e) {
             Db::rollBack();
             dump($e->getMessage() . ';line:' . $e->getLine());
             return json_fail("数据更新失败");

+ 16 - 3
app/admin/service/order/OrderService.php

@@ -111,7 +111,6 @@ class OrderService
             $orders = Order::where('order_status_system', 'PAYING')
                 ->where('order_category', '<>', 'DISHES')     // 点餐不自动取消
                 ->where('order_category', '<>', 'VIP')      // 康养城订单不自动取消,有分次付款
-                // ->where('order_platform','<>','SYSTEM')     // 后台产生的订单不自动取消
                 ->where(function ($query) {
                     $query->where('order_platform', '<>', 'SYSTEM')->orWhereNull('order_platform');
                 })
@@ -145,6 +144,20 @@ class OrderService
                         $goodsRunning->save();
                     }
                 }
+                // 释放优惠券
+                if (!empty($order->order_discount_json)) {
+                    $orderDiscountJson = json_decode($order->order_discount_json, true);
+                    foreach ($orderDiscountJson as $discount) {
+                        if (!empty($discount['coupon_id']) && !empty($discount['coupon_detail_id'])) {
+                            foreach ($discount['coupon_detail_id'] as $detailId) {
+                                if (substr($detailId, 0, 4) == 'CUDT') {
+                                    // 恢复优惠券
+                                    CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => 'ACTIVED']);
+                                }
+                            }
+                        }
+                    }
+                }
 
                 // 订单详情表
                 OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'CANCEL']);
@@ -548,7 +561,7 @@ class OrderService
                     ->where('join_goods_coupon_id', $couponId)
                     ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
                 if ($settlementNow == 'Y' && $type == 'pay') {
-                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
+                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
                 } else {
                     $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
                 }
@@ -731,7 +744,7 @@ class OrderService
                     ->where('join_goods_coupon_id', $couponId)
                     ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
                 if ($settlementNow == 'Y') {
-                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
+                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
                 } else {
                     $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
                 }