| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * 红包定时任务
- * - 沉睡召回:./yii hb/auto-send-sleep-recall
- * - 会员日:./yii hb/auto-send-member-day
- */
- namespace console\controllers;
- use bizHd\custom\models\Custom;
- use bizHd\hb\classes\HbAutoRuleClass;
- use bizHd\hb\classes\HbAutoSendLogClass;
- use bizHd\hb\models\HbAutoRule;
- use common\components\noticeUtil;
- use yii\console\Controller;
- use Yii;
- class HbController extends Controller
- {
- /**
- * 沉睡召回红包:每天执行一次
- * 条件:recentExpend 早于 N 天前(或从未下单),且距上次发放也已超过 N 天
- */
- public function actionAutoSendSleepRecall()
- {
- try {
- date_default_timezone_set('PRC');
- ini_set('memory_limit', '512M');
- set_time_limit(0);
- $rules = HbAutoRuleClass::getAllByCondition([
- 'type' => HbAutoRule::TYPE_SLEEP,
- 'status' => HbAutoRule::STATUS_ON,
- ]);
- if (empty($rules)) {
- echo "无开启的沉睡召回规则\n";
- return;
- }
- $totalIssued = 0;
- foreach ($rules as $rule) {
- $extra = json_decode($rule['extra'] ?? '', true);
- $days = max(1, (int)($extra['unOrderDays'] ?? 30));
- $threshold = date('Y-m-d H:i:s', strtotime("-{$days} days"));
- $shopId = (int)$rule['shopId'];
- $query = Custom::find()
- ->select('id, userId, shopId, recentExpend, member, delStatus')
- ->where(['shopId' => $shopId])
- ->andWhere(['or',
- ['recentExpend' => '0000-00-00 00:00:00'],
- ['<', 'recentExpend', $threshold],
- ])
- ->orderBy('id ASC');
- // delStatus 字段若存在则过滤已删
- try {
- $query->andWhere(['delStatus' => 0]);
- } catch (\Throwable $e) {
- // 忽略
- }
- foreach ($query->batch(200) as $customs) {
- foreach ($customs as $custom) {
- $customId = (int)$custom->id;
- $lastSend = HbAutoSendLogClass::getLastSendTime($rule['id'], $customId);
- // 冷却:上次发放后需再过 N 天
- if ($lastSend && strtotime($lastSend) > strtotime("-{$days} days")) {
- continue;
- }
- $n = HbAutoRuleClass::issueToCustom($rule, $custom->attributes);
- $totalIssued += $n;
- }
- }
- }
- echo "沉睡召回发放完成,红包张数:{$totalIssued}\n";
- Yii::info("沉睡召回发放完成,红包张数:{$totalIssued}", __METHOD__);
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- Yii::error('沉睡召回红包任务失败:' . $msg, __METHOD__);
- noticeUtil::push("沉睡召回红包任务失败:{$msg}", '15280215347');
- echo "沉睡召回红包任务失败:{$msg}\n";
- }
- }
- /**
- * 会员日红包:建议每小时执行
- * 按 extra.sendDay / sendTime / target 匹配,每月每客户只发一次
- */
- public function actionAutoSendMemberDay()
- {
- try {
- date_default_timezone_set('PRC');
- ini_set('memory_limit', '512M');
- set_time_limit(0);
- $now = time();
- $day = (int)date('j', $now);
- $hm = date('H:i', $now);
- $cycleTag = date('Ym', $now);
- $rules = HbAutoRuleClass::getAllByCondition([
- 'type' => HbAutoRule::TYPE_MEMBER_DAY,
- 'status' => HbAutoRule::STATUS_ON,
- ]);
- if (empty($rules)) {
- echo "无开启的会员日规则\n";
- return;
- }
- $totalIssued = 0;
- foreach ($rules as $rule) {
- $extra = json_decode($rule['extra'] ?? '', true) ?: [];
- $sendDay = (int)($extra['sendDay'] ?? 0);
- $sendTime = trim((string)($extra['sendTime'] ?? '10:00'));
- $target = (int)($extra['target'] ?? 1);
- if ($sendDay !== $day) {
- continue;
- }
- // 当前小时已到或超过设定时间即可发(每小时跑,靠 cycleTag 去重)
- if ($hm < $sendTime) {
- continue;
- }
- $shopId = (int)$rule['shopId'];
- $query = Custom::find()
- ->select('id, userId, shopId, member, delStatus')
- ->where(['shopId' => $shopId])
- ->orderBy('id ASC');
- try {
- $query->andWhere(['delStatus' => 0]);
- } catch (\Throwable $e) {
- }
- // target=2 仅会员(member>0)
- if ($target === 2) {
- $query->andWhere(['>', 'member', 0]);
- }
- foreach ($query->batch(200) as $customs) {
- foreach ($customs as $custom) {
- $customId = (int)$custom->id;
- if (HbAutoSendLogClass::hasCycle($rule['id'], $customId, $cycleTag)) {
- continue;
- }
- $n = HbAutoRuleClass::issueToCustom($rule, $custom->attributes, $cycleTag);
- $totalIssued += $n;
- }
- }
- }
- echo "会员日发放完成,红包张数:{$totalIssued}\n";
- Yii::info("会员日发放完成,红包张数:{$totalIssued}", __METHOD__);
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- Yii::error('会员日红包任务失败:' . $msg, __METHOD__);
- noticeUtil::push("会员日红包任务失败:{$msg}", '15280215347');
- echo "会员日红包任务失败:{$msg}\n";
- }
- }
- }
|