Browse Source

新客专享

gorden 5 months ago
parent
commit
dfca228361

+ 11 - 28
app/admin/controller/coupon/CouponController.php

@@ -399,35 +399,18 @@ class CouponController extends Curd
 //                                }
                                 $params['coupon_detail_period_num'] = 1;
                                 $periodJson = json_decode($coupon['coupon_period_json'], true);
-                                if ($periodJson['unit'] == 'day') {
-                                    $val = $periodJson['val'] - 1;
-                                    if ($val < 1) {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
-                                    } else {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " day"));
-                                    }
-                                } elseif ($periodJson['unit'] == 'week') {
-                                    $val = $periodJson['val'] - 1;
-                                    if ($val < 1) {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
-                                    } else {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday'))."+" . $val . ' week'));
-                                    }
-                                } elseif ($periodJson['unit'] == 'month') {
-                                    $val = $periodJson['val'] - 1;
-                                    if ($val < 1) {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
-                                    } else {
-                                        $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
-                                    }
-                                }
+//                                for ($i=0;$i<$periodJson['nbr'];$i++){
+                                CouponDetailService::sendPeriodCoupon($params);
+//                                }
+
+                                continue;
                             }
                             if ($chooseCoupon['nbr'] > 0) {
                                 // 有发行数量
                                 if ($coupon['coupon_number'] != 0) {
                                     // 查询还有多少张没领的
                                     $count = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
-                                        ->whereIn('coupon_detail_status',['INIT','PENDING'])
+                                        ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
                                         ->count();
                                     if (!isset($couponNbr[$coupon['coupon_id']])) {
                                         $couponNbr[$coupon['coupon_id']] = $count;
@@ -439,7 +422,7 @@ class CouponController extends Curd
                                     $params['chooseCouponNbr'] = $chooseCoupon['nbr'];
                                     // 匹配已发行的优惠券
                                     CouponDetailService::customSendCouponHave($params);
-                                }else{
+                                } else {
                                     for ($i = 0; $i < $chooseCoupon['nbr']; $i++) {
                                         CouponDetailService::customSendCoupon($params);
                                     }
@@ -861,16 +844,16 @@ class CouponController extends Curd
     public function disableCoupon(Request $request)
     {
         $couponId = $request->post('coupon_id');
-        if (!$couponId){
+        if (!$couponId) {
             return json_fail("参数异常");
         }
         Db::beginTransaction();
         try {
-            Coupon::where('coupon_id',$couponId)->update(['coupon_status'=>'DISABLED']);
-            CouponDetail::where('join_detail_coupon_id',$couponId)->whereIn('coupon_detail_status',['INIT','PENDING'])->update(['coupon_detail_status'=>'DISABLED']);
+            Coupon::where('coupon_id', $couponId)->update(['coupon_status' => 'DISABLED']);
+            CouponDetail::where('join_detail_coupon_id', $couponId)->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->update(['coupon_detail_status' => 'DISABLED']);
             Db::commit();
             return json_success('success');
-        }catch (\Exception $e){
+        } catch (\Exception $e) {
             Db::rollBack();
             return json_fail("禁用失败");
         }

+ 2 - 2
app/admin/controller/order/NewCustomerController.php

@@ -318,7 +318,7 @@ class NewCustomerController extends Curd
             // 分期付完
             if ($params['goods_classify'] == 'COMBINE' && $params['order_status_payment'] == 'SUCCESS' && (!empty($paidOrder) && floatval($paidOrder->order_amount_paid) >= $paidOrder->order_amount_total || floatval($params['order_amount_pay']) >= $params['order_amount_total'])) {
                 $params['member_id'] = $params['join_order_member_id'];
-//                Event::dispatch('order.newCustomer.grant', $params);
+                Event::dispatch('order.new_custom.grant', $params);
             }
 
             Db::commit();
@@ -524,7 +524,7 @@ class NewCustomerController extends Curd
             // 分期付款完成
             if ($params['goods_classify'] == 'COMBINE' && $order->order_status_payment == 'SUCCESS' && floatval($order->order_amount_paid) >= $order->order_amount_total) {
                 $params['member_id'] = $params['join_order_member_id'];
-//                Event::dispatch('order.new_customer.grant', $params);
+                Event::dispatch('order.new_custom.grant', $params);
             }
 
             Db::commit();

+ 143 - 19
app/admin/service/coupon/CouponDetailService.php

@@ -5,6 +5,7 @@ namespace app\admin\service\coupon;
 use app\model\Coupon;
 use app\model\CouponDetail;
 use app\model\SysSerial;
+use support\Db;
 use support\exception\BusinessException;
 
 class CouponDetailService
@@ -75,15 +76,46 @@ class CouponDetailService
      */
     public static function sendPeriodCoupon($params)
     {
-        $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
-        if ($coupon->coupon_is_period != 'Y') {
-            return;
-        }
-        $periodJson = json_decode($coupon->coupon_period_json, true);
-        if (!empty($periodJson['nbr'])) {
-            for ($i = 1; $i < $periodJson['nbr']; $i++) {
-                self::generatePeriod($periodJson);
+        Db::beginTransaction();
+        try {
+            $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
+            if ($coupon->coupon_is_period != 'Y') {
+                return;
+            }
+            $detailCount = -1;
+            if ($coupon->coupon_number > 0) {
+                $detailCount = CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
+            }
+            $periodJson = json_decode($coupon->coupon_period_json, true);
+            if ($detailCount != -1 && $detailCount - $periodJson['nbr'] < 0) {
+                throw new BusinessException("优惠券余量不足");
             }
+            $periodJson['now_nbr'] = 1;
+            if (!empty($periodJson['nbr'])) {
+                for ($i = 0; $i < $periodJson['nbr']; $i++) {
+                    $periodParams = self::generatePeriod($periodJson);
+                    $periodParams['gettype'] = $params['gettype'] ?? '';
+                    $periodParams['coupon_id'] = $params['coupon_id'];
+                    $periodParams['member_id'] = $params['member_id'];
+                    $periodParams['coupon_detail_period_num'] = $periodJson['now_nbr'];
+                    if ($detailCount > 0) {
+                        $periodParams['chooseCouponNbr'] = 1;
+                        self::sendPeriodCouponHave($periodParams);
+                    } else {
+                        self::sendPeriodCouponNoLimit($periodParams);
+                    }
+
+                    $periodJson['now_nbr'] += 1;
+                }
+            }
+            Db::commit();
+        } catch (BusinessException $e) {
+            Db::rollBack();
+            throw new BusinessException($e->getMessage());
+        } catch (\Exception $e) {
+            dump($e->getMessage());
+            Db::rollBack();
+            throw new BusinessException("优惠券发放失败");
         }
     }
 
@@ -97,7 +129,59 @@ class CouponDetailService
      */
     public static function sendPeriodCouponHave($params)
     {
+        $gettype = 'SEND';
+        if (!empty($params['gettype'])) {
+            $gettype = $params['gettype'];
+        }
+        try {
+            CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
+                ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
+                ->limit($params['chooseCouponNbr'])
+                ->update([
+                    'join_coupon_detail_member_id' => $params['member_id'],
+                    'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
+                    'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
+                    'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
+                    'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
+                    'coupon_detail_status' => 'ACTIVED',
+                    'coupon_detail_addtimes' => time()
+                ]);
+        } catch (\Exception $e) {
+            dump($e->getMessage());
+            throw new BusinessException('写入优惠券失败');
+        }
+    }
 
+    /**
+     * @Desc 发行量不限
+     * @Author Gorden
+     * @Date 2024/9/27 16:19
+     *
+     * @param $params
+     * @return void
+     * @throws BusinessException
+     */
+    public static function sendPeriodCouponNoLimit($params)
+    {
+        $gettype = 'SEND';
+        if (!empty($params['gettype'])) {
+            $gettype = $params['gettype'];
+        }
+        try {
+            CouponDetail::insert([
+                'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
+                'join_detail_coupon_id' => $params['coupon_id'],
+                'join_coupon_detail_member_id' => $params['member_id'],
+                'coupon_detail_status' => 'ACTIVED',
+                'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
+                'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
+                'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
+                'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
+                'coupon_detail_addtimes' => time(),
+            ]);
+        } catch (\Exception $e) {
+            throw new BusinessException('写入优惠券失败');
+        }
     }
 
     /**
@@ -105,36 +189,76 @@ class CouponDetailService
      * @Author Gorden
      * @Date 2024/9/27 13:54
      *
-     * @param $periodJson 周期JSON
+     * @param $periodJson
      * @return array
      */
     public static function generatePeriod($periodJson)
     {
+        // 当前第几期
+        $now_nbr = $periodJson['now_nbr'];
         $params = [];
         if ($periodJson['unit'] == 'day') {
             $val = $periodJson['val'] - 1;
             if ($val < 1) {
-                $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . $now_nbr . ' day'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $now_nbr . ' day'));
+                }
             } else {
-                $params['coupon_detail_gain_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . ' day'));
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime("+" . ((($val + 1) * $now_nbr)) . " day"));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . " day"));
+                }
             }
         } elseif ($periodJson['unit'] == 'week') {
             $val = $periodJson['val'] - 1;
             if ($val < 1) {
-                $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . $now_nbr . ' week'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . $now_nbr . ' week'));
+                }
             } else {
-                $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday'))."+" . $val . ' week'));
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . $val . ' week'));
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime(date('Y-m-d 00:00:00', strtotime('this week Monday')) . "+" . ((($val + 1) * $now_nbr)) . ' week'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday')) . "+" . ((($val + 1) * $now_nbr) + $val) . ' week'));
+                }
             }
         } elseif ($periodJson['unit'] == 'month') {
             $val = $periodJson['val'] - 1;
             if ($val < 1) {
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime("+" . $now_nbr . ' month'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $now_nbr . ' month'));
+                }
             } else {
-                $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
+                if ($now_nbr == 1) {
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00');
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . ' month'));
+                } else {
+                    $now_nbr -= 1;
+                    $params['coupon_detail_gain_datetime'] = date('Y-m-01 00:00:00', strtotime( "+" . ((($val + 1) * $now_nbr)) . ' month'));
+                    $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . ((($val + 1) * $now_nbr) + $val) . ' month'));
+                }
             }
         }
 

+ 3 - 2
app/admin/service/member/MemberService.php

@@ -898,7 +898,6 @@ class MemberService
 
         $info = PayDetail::where(function ($query) use ($memberId) {
             $query->where('join_pay_member_id', $memberId)
-                // ->whereIn('pay_prepayid', ['W06', 'A01'])
                 ->whereIn('pay_category', ['RECHARGE','PARTNER'])
                 ->where('pay_prepayid', '<>', $memberId . '-WELFARE')
                 ->where('pay_status', 'SUCCESS');
@@ -909,6 +908,8 @@ class MemberService
         })->orderByDesc('pay_addtimes')
             ->get()
             ->toArray();
+
+//        dump($info);
         $data = [];
         foreach ($info as $key => $item) {
             if ($item['pay_category'] == 'PARTNER'){
@@ -976,7 +977,7 @@ class MemberService
                 continue;
             }
 
-            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'PARTNER'])) {
+            if (in_array($item['pay_category'], ['RECHARGE', 'REFUND', 'GIVE', 'PARTNER','COMBINE'])) {
                 if (!empty($item['pay_extend_json'])) {
                     $extendJson = json_decode($item['pay_extend_json'], true);
                     if (isset($extendJson['added_amount']) && floatval($extendJson['added_amount']) > 0) {

+ 38 - 4
app/event/order/NewCustomerEvent.php

@@ -8,6 +8,7 @@ use app\model\CouponDetail;
 use app\model\Goods;
 use app\model\Member;
 use app\model\MemberAccount;
+use app\model\PayDetail;
 use app\model\SysSerial;
 use support\exception\BusinessException;
 use support\Log;
@@ -16,6 +17,7 @@ class NewCustomerEvent
 {
     public function grant($params)
     {
+        Log::info("进入新客专享事件", $params);
         // 余额账户
         $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'CASH')->first();
         // 分期付款
@@ -31,6 +33,28 @@ class NewCustomerEvent
                 $memberAccount->member_account_income = $memberAccount->member_account_income + $goodsAttributeJson['account']['amount'];
                 $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $goodsAttributeJson['account']['amount'];
                 $memberAccount->save();
+
+                Log::info("余额账户入账:" . $goodsAttributeJson['account']['amount']);
+
+                // PayDetail 里写条充值记录
+                PayDetail::insert([
+                    'join_pay_member_id' => $params['member_id'],
+                    'join_pay_order_id' => 'COMBINE',
+                    'join_pay_object_json' => json_encode([
+                        'recharge_config' => ['nbr' => 0],
+                        'recharge_goods_id' => $goods->goods_id,
+                        'recharge_goods_name' => $goods->goods_name
+                    ]),
+                    'pay_status' => 'SUCCESS',
+                    'pay_category' => 'RECHARGE',
+                    'pay_amount' => $goodsAttributeJson['account']['amount'],
+                    'pay_paytimes' => date('Y-m-d H:i:s'),
+                    'pay_prepayid' => $params['member_id'] . '-CASH',
+                    'pay_json_request' => '[]',
+                    'pay_json_response' => '[]',
+                    'pay_extend_json' => '[]',
+                    'pay_addtimes' => time()
+                ]);
             }
             if (!empty($goodsAttributeJson['coupon'])) {
                 foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
@@ -44,11 +68,19 @@ class NewCustomerEvent
                     }
                     // 发周期券
                     if ($couponModel->coupon_is_period == 'Y') {
-                        if (CouponDetail::where('join_coupon_detail_member_id', $params['member_id'])->where('join_detail_coupon_id', $couponModel->coupon_id)->exists()) {
-                            continue;
-                        }
-                        // TODO 发券逻辑
+//                        if (CouponDetail::where('join_coupon_detail_member_id', $params['member_id'])->where('join_detail_coupon_id', $couponModel->coupon_id)->exists()) {
+//                            continue;
+//                        }
+                        // 发券参数
+                        $couponSendParams = [
+                            'gettype' => 'COMBINE',
+                            'coupon_id' => $key,
+                            'member_id' => $params['member_id']
+                        ];
+
+                        Log::info("发周期券参数", $couponSendParams);
 
+                        CouponDetailService::sendPeriodCoupon($couponSendParams);
                         continue;
                     }
 
@@ -75,6 +107,8 @@ class NewCustomerEvent
                         'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
                         'coupon_detail_deadline_datetime' => $endDate,
                     ];
+
+                    Log::info("发普通券参数", $couponSendParams);
                     if ($couponResidue != 'all' && $couponResidue - $coupon['num'] >= 0) {
                         CouponDetailService::customSendCouponHave($couponSendParams);
                     } else {

+ 3 - 0
config/event.php

@@ -16,6 +16,9 @@ return [
     'order.partner.grant' => [
         [\app\event\order\PartnerEvent::class, 'grant']
     ],
+    'order.new_custom.grant' => [
+        [\app\event\order\NewCustomerEvent::class, 'grant']
+    ],
     'order_pay.member_level.up' => [
         [\app\event\MemberLevelEvent::class, 'payOrderLevelUp']
     ],