来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
159 lines
5.7 KiB
TypeScript
159 lines
5.7 KiB
TypeScript
/**
|
|
* 订单定时强推配置:按分公司存 system_configs,分公司覆盖 + 全局兜底,默认关闭。
|
|
* 见 docs/design/20260823-order-auto-advance-pending-backfill.md
|
|
* 闸门(过点不补跑、当天改配置次日生效)见 docs/design/20260902-order-auto-advance-no-catchup.md
|
|
*/
|
|
import { pool } from '../db';
|
|
import {
|
|
addChinaDateKeyDays,
|
|
chinaCalendarParts,
|
|
parseStoredDateKey,
|
|
} from './orderAutoAdvanceSchedule';
|
|
|
|
export interface OrderAutoAdvanceConfig {
|
|
enabled: boolean;
|
|
/** 每天强推时刻,HH:mm(24h,北京时间) */
|
|
time: string;
|
|
/** 最早允许定时执行的北京日历日(含);当天启用/改时刻会写成次日 */
|
|
earliestRunDate?: string | null;
|
|
/** 上次定时成功执行的北京日历日 */
|
|
lastRunDate?: string | null;
|
|
}
|
|
|
|
const CONFIG_KEY = 'order_auto_advance';
|
|
const CONFIG_GROUP = 'order';
|
|
const CONFIG_DESCRIPTION =
|
|
'定时强推待配送:enabled=true 时,仅在每天 time 起 15 分钟窗口内把 30/40/45 订单推进到 50 并标记待补录;当天改配置次日才跑';
|
|
|
|
export function defaultOrderAutoAdvanceConfig(): OrderAutoAdvanceConfig {
|
|
return { enabled: false, time: '08:00', earliestRunDate: null, lastRunDate: null };
|
|
}
|
|
|
|
export function parseOrderAutoAdvanceConfigValue(raw: unknown): OrderAutoAdvanceConfig {
|
|
const fallback = defaultOrderAutoAdvanceConfig();
|
|
if (raw == null) return { ...fallback };
|
|
const obj = typeof raw === 'string' ? safeParse(raw) : raw;
|
|
if (typeof obj !== 'object' || obj === null) return { ...fallback };
|
|
const rec = obj as Record<string, unknown>;
|
|
const enabled = typeof rec.enabled === 'boolean' ? rec.enabled : fallback.enabled;
|
|
const timeRaw = typeof rec.time === 'string' ? rec.time.trim() : '';
|
|
const time = /^([01]?\d|2[0-3]):[0-5]\d$/.test(timeRaw) ? timeRaw.padStart(5, '0') : fallback.time;
|
|
return {
|
|
enabled,
|
|
time,
|
|
earliestRunDate: parseStoredDateKey(rec.earliestRunDate ?? rec.earliest_run_date),
|
|
lastRunDate: parseStoredDateKey(rec.lastRunDate ?? rec.last_run_date),
|
|
};
|
|
}
|
|
|
|
function safeParse(text: string): unknown {
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function toStoredPayload(config: OrderAutoAdvanceConfig): string {
|
|
return JSON.stringify({
|
|
enabled: config.enabled,
|
|
time: config.time,
|
|
earliestRunDate: config.earliestRunDate || null,
|
|
lastRunDate: config.lastRunDate || null,
|
|
});
|
|
}
|
|
|
|
async function persistConfig(branchCode: string | null, next: OrderAutoAdvanceConfig): Promise<void> {
|
|
const payload = toStoredPayload(next);
|
|
const branch = String(branchCode || '').trim() || null;
|
|
const updated = await pool.query(
|
|
`UPDATE system_configs
|
|
SET config_value = $1::jsonb, updated_at = NOW(), description = $4
|
|
WHERE config_key = $2 AND branch_code IS NOT DISTINCT FROM $3`,
|
|
[payload, CONFIG_KEY, branch, CONFIG_DESCRIPTION]
|
|
);
|
|
if (!updated.rowCount) {
|
|
await pool.query(
|
|
`INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
|
|
VALUES ($1, $2::jsonb, $4, $5, $3)`,
|
|
[CONFIG_KEY, payload, branch, CONFIG_GROUP, CONFIG_DESCRIPTION]
|
|
);
|
|
}
|
|
}
|
|
|
|
async function readStoredConfig(
|
|
branchCode?: string | null,
|
|
options?: { fallbackToGlobal?: boolean }
|
|
): Promise<OrderAutoAdvanceConfig | null> {
|
|
const code = String(branchCode || '').trim();
|
|
if (code) {
|
|
const branchRow = await pool.query(
|
|
`SELECT config_value FROM system_configs
|
|
WHERE config_key = $1 AND branch_code = $2
|
|
LIMIT 1`,
|
|
[CONFIG_KEY, code]
|
|
);
|
|
if (branchRow.rows[0]?.config_value != null) {
|
|
return parseOrderAutoAdvanceConfigValue(branchRow.rows[0].config_value);
|
|
}
|
|
}
|
|
// 分公司调度必须显式配置;全局配置仅供全局管理入口使用,不能让未配置分公司意外开启。
|
|
if (code && options?.fallbackToGlobal === false) return null;
|
|
|
|
const globalRow = await pool.query(
|
|
`SELECT config_value FROM system_configs
|
|
WHERE config_key = $1 AND branch_code IS NULL
|
|
LIMIT 1`,
|
|
[CONFIG_KEY]
|
|
);
|
|
if (globalRow.rows[0]?.config_value != null) {
|
|
return parseOrderAutoAdvanceConfigValue(globalRow.rows[0].config_value);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export async function getOrderAutoAdvanceConfig(
|
|
branchCode?: string | null
|
|
): Promise<OrderAutoAdvanceConfig> {
|
|
const code = String(branchCode || '').trim();
|
|
return (
|
|
(await readStoredConfig(code || null, code ? { fallbackToGlobal: false } : undefined)) ||
|
|
defaultOrderAutoAdvanceConfig()
|
|
);
|
|
}
|
|
|
|
export async function setOrderAutoAdvanceConfig(
|
|
branchCode: string | null,
|
|
config: OrderAutoAdvanceConfig
|
|
): Promise<OrderAutoAdvanceConfig> {
|
|
const parsed = parseOrderAutoAdvanceConfigValue(config);
|
|
const code = String(branchCode || '').trim();
|
|
const previous =
|
|
(await readStoredConfig(branchCode, code ? { fallbackToGlobal: false } : undefined)) ||
|
|
defaultOrderAutoAdvanceConfig();
|
|
const { dateKey: today } = chinaCalendarParts();
|
|
const armed =
|
|
parsed.enabled && (!previous.enabled || previous.time !== parsed.time);
|
|
const next: OrderAutoAdvanceConfig = {
|
|
enabled: parsed.enabled,
|
|
time: parsed.time,
|
|
earliestRunDate: parsed.enabled
|
|
? armed
|
|
? addChinaDateKeyDays(today, 1)
|
|
: previous.earliestRunDate
|
|
: previous.earliestRunDate,
|
|
lastRunDate: previous.lastRunDate,
|
|
};
|
|
await persistConfig(branchCode, next);
|
|
return next;
|
|
}
|
|
|
|
export async function markOrderAutoAdvanceRan(branchCode: string, dateKey: string): Promise<void> {
|
|
const code = String(branchCode || '').trim();
|
|
const day = parseStoredDateKey(dateKey);
|
|
if (!code || !day) return;
|
|
const current =
|
|
(await readStoredConfig(code, { fallbackToGlobal: false })) || defaultOrderAutoAdvanceConfig();
|
|
await persistConfig(code, { ...current, lastRunDate: day });
|
|
}
|