Gorden 1 miesiąc temu
rodzic
commit
52c2d8dcb4

+ 5 - 5
app/admin/controller/coupon/CouponController.php

@@ -307,7 +307,7 @@ class CouponController extends Curd
 
         // 处理周期优惠券
         if ($params['coupon_is_period'] != 'N') {
-            $this->disposePeriod($params);
+//            $this->disposePeriod($params);
             $data['coupon_period_json'] = json_encode([
                 'length' => $params['period'],
                 'unit' => $params['period_unit'],
@@ -328,9 +328,9 @@ class CouponController extends Curd
         if ($params['period'] != 'TO_EXPIRE') {
             return $params;
         }
-        if (empty($params['coupon_validdate_end'])) {
-            throw new BusinessException("周期数据异常");
-        }
+//        if (empty($params['coupon_validdate_end'])) {
+//            throw new BusinessException("周期数据异常");
+//        }
         try {
             $endTimeUnix = strtotime($params['coupon_validdate_end']);
             if ($params['period_unit'] == 'day') {
@@ -352,7 +352,7 @@ class CouponController extends Curd
             return $params;
         } catch (\Exception $e) {
 
-            throw new BusinessException("周期数据异常");
+            throw new BusinessException($e->getMessage());
         }
     }
 

+ 2 - 1
app/admin/service/coupon/CouponDetailService.php

@@ -47,7 +47,8 @@ class CouponDetailService
                     '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'=>'SEND']),
-                    'coupon_detail_status'=>'ACTIVED'
+                    'coupon_detail_status'=>'ACTIVED',
+                    'coupon_detail_addtimes' => time()
                 ]);
         } catch (\Exception $e) {
             throw new BusinessException('写入优惠券失败');

+ 27 - 8
app/admin/service/coupon/CouponService.php

@@ -39,31 +39,43 @@ class CouponService
         Db::beginTransaction();
         try {
             $coupons = Coupon::where('coupon_is_period', 'Y')->where('coupon_status', 'ACTIVED')->get()->toArray();
+//            dump("周期全",$coupons);
             foreach ($coupons as $coupon) {
-                if (time() > strtotime($coupon['coupon_validdate_end'])) {
+                if (!empty($coupon['coupon_validdate_end']) && time() > strtotime($coupon['coupon_validdate_end'])) {
                     continue;
                 }
-                $details = CouponDetail::where('join_detail_coupon_id',$coupon['coupon_id'])
-                    ->select('join_coupon_detail_member_id')
+                $details = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
+                    ->where('join_coupon_detail_member_id','<>','')
+                    ->selectRaw('join_coupon_detail_member_id,COUNT(*) as count')
                     ->groupBy('join_coupon_detail_member_id')
                     ->get();
                 foreach ($details as $item) {
                     $detail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
-                        ->where('join_coupon_detail_member_id',$item->join_coupon_detail_member_id)
+                        ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
                         ->orderBy('coupon_detail_addtimes', 'DESC')
                         ->first();
-                    if (!empty($detail) && strtotime($detail->coupon_detail_deadline_datetime) > time()) {
+                    if (strtotime($detail->coupon_detail_deadline_datetime) > time()) {
                         continue;
+                    } elseif (empty($coupon['coupon_validdate_end']) && !empty($coupon['coupon_validdate_day'])) {
+                        $firstDetail = CouponDetail::where('join_detail_coupon_id', $coupon['coupon_id'])
+                            ->where('join_coupon_detail_member_id', $item->join_coupon_detail_member_id)
+                            ->orderBy('coupon_detail_addtimes', 'ASC')
+                            ->first();
+                        if ((time() - strtotime($firstDetail->coupon_detail_addtimes)) > (3600 * 24 * $coupon['coupon_validdate_day'])) {
+                            continue;
+                        }
                     }
+
                     $params = [
                         'coupon_detail_gain_datetime' => date('Y-m-d 00:00:00'),
                         'coupon_id' => $coupon['coupon_id'],
                         'member_id' => $detail->join_coupon_detail_member_id,
                         'coupon_detail_period_num' => $detail->coupon_detail_period_num + 1
                     ];
+                    // 超出期数
                     if (!empty($coupon['coupon_period_json'])) {
                         $periodJson = json_decode($coupon['coupon_period_json'], true);
-                        if ($periodJson['nbr'] < $params['coupon_detail_period_num']){
+                        if ($periodJson['nbr'] < $params['coupon_detail_period_num']) {
                             continue;
                         }
                         if ($periodJson['unit'] == 'day') {
@@ -89,8 +101,14 @@ class CouponService
                             }
                         }
                     }
-                    // 写入优惠券
-                    CouponDetailService::customSendCoupon($params);
+                    if (CouponDetail::whereIn('coupon_detail_status',['INIT','PENDING'])->exists()){
+                        // 匹配已发行的优惠券
+                        $params['chooseCouponNbr'] = 1;
+                        CouponDetailService::customSendCouponHave($params);
+                    }else{
+                        // 写入优惠券
+                        CouponDetailService::customSendCoupon($params);
+                    }
 
                     _syslog("周期券", "发放周期券", false, $params, 1001);
                 }
@@ -98,6 +116,7 @@ class CouponService
 
             Db::commit();
         } catch (\Exception $e) {
+            dump($e->getTrace());
             Db::rollBack();
         }
     }

+ 2 - 0
process/Task.php

@@ -32,6 +32,8 @@ class Task
 
             // 优惠券自动过期
             CouponService::checkCouponExpired();
+            // 发放周期券
+            CouponService::sendPeriodCoupon();
         });
 
         // 每天的2点执行,注意这里省略了秒位