AutoSendCoupon.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use app\models\CouponAutoSend;
  9. use app\models\UserCoupon;
  10. use app\modules\admin\models\CouponForm;
  11. use yii\helpers\Json;
  12. class AutoSendCoupon
  13. {
  14. /**
  15. * 优惠券自动发放
  16. * @param $type
  17. * @param $user_id
  18. * @param $store_id
  19. * @param $recharge_money
  20. */
  21. public static function send($user_id, $type, $store_id, $recharge_money = 0, $extType = 0) {
  22. \Yii::error('----------- COUPON AUTO SEND PROGRAM START -----------');
  23. \Yii::warning([$user_id, $type, $store_id, $recharge_money]);
  24. if (!in_array($type, [1, 2, 3, 4, 5, 6]) || empty($user_id)) {
  25. \Yii::error('----------- COUPON AUTO SEND PROGRAM : PARAMS ERROR -----------');
  26. return [
  27. 'code' => 0,
  28. 'data' => null,
  29. ];
  30. }
  31. $extWhere = [];
  32. if($extType){
  33. $extWhere['ext_type'] = $extType;
  34. }
  35. $coupon_task = CouponAutoSend::find()->where(array_merge(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0,
  36. 'event' => $type], $extWhere))->asArray()->all();
  37. if (empty($coupon_task)) {
  38. \Yii::error('----------- COUPON AUTO SEND PROGRAM : HANDLE EMPTY DATA -----------');
  39. return [
  40. 'code' => 0,
  41. 'data' => null,
  42. ];
  43. }
  44. $return = [];
  45. foreach ($coupon_task as $task) {
  46. if (!$task['coupon_id']) {
  47. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' EMPTY COUPON ID -----------');
  48. continue;
  49. }
  50. $coupon_ids = Json::decode($task['coupon_id']);
  51. $coupon_ids_count = count($coupon_ids);
  52. if (!$coupon_ids || $coupon_ids_count < 1) {
  53. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' EMPTY COUPON ID -----------');
  54. continue;
  55. }
  56. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ', COUPON_IDS: ' . implode(",", $coupon_ids) . ' -----------');
  57. // 优惠券发放
  58. foreach ($coupon_ids as $coupon) {
  59. // 充值类型必须达到一定金额
  60. if ($task['event'] == CouponAutoSend::EVENT_RECHARGE && $recharge_money < $task['amount']) {
  61. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' CAN NOT REACH THE LIMIT MONEY -----------');
  62. continue;
  63. }
  64. // 单人总获取数量
  65. if ($task['day_count'] > 0) {
  66. $coupon_count = UserCoupon::find()->where(['store_id' => $store_id,
  67. 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon])
  68. ->andWhere(['or', ['user_id' => $user_id], ['give_user_id' => $user_id]])->count();
  69. if ($coupon_count >= $task['day_count']) {
  70. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ' TOTAL COUNT OVERRUN -----------');
  71. continue;
  72. }
  73. }
  74. // 单人每日取数量
  75. if ($task['day_num'] > 0) {
  76. $start_time = strtotime(date('Y-m-d 00:00:00'));
  77. $end_time = strtotime(date('Y-m-d 23:59:59'));
  78. $coupon_count = UserCoupon::find()->where(['store_id' => $store_id,
  79. 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon])
  80. ->andWhere(['or', ['user_id' => $user_id], ['give_user_id' => $user_id]])
  81. ->andWhere(['and', ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]])->count();
  82. if ($coupon_count >= $task['day_num']) {
  83. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ' ONE DAY COUNT OVERRUN -----------');
  84. continue;
  85. }
  86. }
  87. // 最多发放次数
  88. if ($task['send_times'] > 0) {
  89. $coupon_count = UserCoupon::find()->where(['store_id' => $store_id, 'coupon_auto_send_id' => $task['id'], 'type' => 1, 'coupon_id' => $coupon])->count();
  90. if ($coupon_count >= $task['send_times']) {
  91. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ' SEND TIMES OVERRUN -----------');
  92. continue;
  93. }
  94. }
  95. $res = CouponForm::userAddCoupon($user_id, $coupon, $task['id'], $type);
  96. if ($res['code'] > 0) {
  97. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ', COUPON ID:' . $coupon. ' SEND FAIL: ' . $res['msg'] . '-----------');
  98. } else {
  99. $return[] = $res;
  100. \Yii::error('----------- COUPON AUTO SEND PROGRAM : TASK ID ' . $task['id'] . ',USER_ID : ' . $user_id . ', COUPON ID:' . $coupon. ' SEND SUCCESS -----------');
  101. }
  102. }
  103. }
  104. \Yii::error('----------- COUPON AUTO SEND PROGRAM END -----------');
  105. return count($return) > 0 ? $return[0] : ['code' => 0, 'data' => null];
  106. }
  107. }