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"; } } }