| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils;
- use app\models\CouponAutoSend;
- use app\models\UserCoupon;
- use app\modules\admin\models\CouponForm;
- use yii\helpers\Json;
- class AutoSendCoupon
- {
- /**
- * 优惠券自动发放
- * @param $type
- * @param $user_id
- * @param $store_id
- * @param $recharge_money
- */
- public static function send($user_id, $type, $store_id, $recharge_money = 0, $extType = 0) {
- \Yii::error('----------- COUPON AUTO SEND PROGRAM START -----------');
- \Yii::warning([$user_id, $type, $store_id, $recharge_money]);
- if (!in_array($type, [1, 2, 3, 4, 5, 6]) || empty($user_id)) {
- \Yii::error('----------- COUPON AUTO SEND PROGRAM : PARAMS ERROR -----------');
- return [
- 'code' => 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];
- }
- }
|