0, 'data' => null, ]; } $extWhere = []; if($extType){ $extWhere['ext_type'] = $extType; } $coupon_task = CouponAutoSend::find()->where(array_merge(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'event' => $type], $extWhere))->asArray()->all(); if (empty($coupon_task)) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : HANDLE EMPTY DATA -----------'); return [ 'code' => 0, 'data' => null, ]; } $return = []; foreach ($coupon_task as $task) { if (!$task['coupon_id']) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' EMPTY COUPON ID -----------'); continue; } $coupon_ids = Json::decode($task['coupon_id']); $coupon_ids_count = count($coupon_ids); if (!$coupon_ids || $coupon_ids_count < 1) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' EMPTY COUPON ID -----------'); continue; } \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ', COUPON_IDS: ' . implode(",", $coupon_ids) . ' -----------'); // 优惠券发放 foreach ($coupon_ids as $coupon) { // 充值类型必须达到一定金额 if ($task['event'] == CouponAutoSend::EVENT_RECHARGE && $recharge_money < $task['amount']) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' CAN NOT REACH THE LIMIT MONEY -----------'); continue; } // 单人总获取数量 if ($task['day_count'] > 0) { $coupon_count = UserCoupon::find()->where(['store_id' => $store_id, 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon]) ->andWhere(['or', ['user_id' => $user_id], ['give_user_id' => $user_id]])->count(); if ($coupon_count >= $task['day_count']) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ' TOTAL COUNT OVERRUN -----------'); continue; } } // 单人每日取数量 if ($task['day_num'] > 0) { $start_time = strtotime(date('Y-m-d 00:00:00')); $end_time = strtotime(date('Y-m-d 23:59:59')); $coupon_count = UserCoupon::find()->where(['store_id' => $store_id, 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon]) ->andWhere(['or', ['user_id' => $user_id], ['give_user_id' => $user_id]]) ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]])->count(); if ($coupon_count >= $task['day_num']) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ' ONE DAY COUNT OVERRUN -----------'); continue; } } // 最多发放次数 if ($task['send_times'] > 0) { $coupon_count = UserCoupon::find()->where(['store_id' => $store_id, 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon])->count(); if ($coupon_count >= $task['send_times']) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' SEND TIMES OVERRUN -----------'); continue; } } $res = CouponForm::userAddCoupon($user_id, $coupon, $task['id'], $type); if ($res['code'] > 0) { \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ', COUPON ID:' . $coupon. ' SEND FAIL: ' . $res['msg'] . '-----------'); } else { $return[] = $res; \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ', COUPON ID:' . $coupon. ' SEND SUCCESS -----------'); } } } \Yii::error('----------- COUPON AUTO SEND PROGRAM END -----------'); return count($return) > 0 ? $return[0] : ['code' => 0, 'data' => null]; } }