import inquirer from 'inquirer'; export async function promptMissingOptions(options) { const { company, companyList, specs, specList, shops, shopList, statuses, statusList, } = options; const prompts = []; if (!company && companyList) { prompts.push({ type: 'list', name: 'company', message: '选择公司:', choices: companyList.map(c => ({ name: `[${c.id}] ${c.name}`, value: c.id })), pageSize: 15, }); } if (company) { prompts.push({ type: 'list', name: 'datePreset', message: '选择时间范围:', choices: [ { name: '最近 7 天', value: '7d' }, { name: '最近 30 天', value: '30d' }, { name: '最近 90 天', value: '90d' }, { name: '本月', value: 'thisMonth' }, { name: '上月', value: 'lastMonth' }, { name: '自定义', value: 'custom' }, ], }); { const now = new Date(); const y = now.getFullYear(); const m = String(now.getMonth() + 1).padStart(2, '0'); const d = String(now.getDate()).padStart(2, '0'); const today = `${y}-${m}-${d}`; const monthStart = `${y}-${m}-01`; const lastM = now.getMonth() === 0 ? 12 : now.getMonth(); const lastY = now.getMonth() === 0 ? y - 1 : y; const lastMonthStr = `${lastY}-${String(lastM).padStart(2, '0')}-01`; prompts.push({ type: 'input', name: 'startDate', message: '开始日期 (YYYY-MM-DD):', default: options.startDate, when: a => a.datePreset === 'custom', validate: v => /^\d{4}-\d{2}-\d{2}$/.test(v) || '格式: YYYY-MM-DD', }); prompts.push({ type: 'input', name: 'endDate', message: '结束日期 (YYYY-MM-DD):', default: options.endDate, when: a => a.datePreset === 'custom', validate: v => /^\d{4}-\d{2}-\d{2}$/.test(v) || '格式: YYYY-MM-DD', }); } if ((!specs || specs.length === 0) && specList) { prompts.push({ type: 'number', name: 'specCount', message: `随机选取几个规格刷单?(共有 ${specList.length} 个):`, default: Math.min(5, specList.length), validate: v => (v > 0 && v <= specList.length) || `请输入 1~${specList.length}`, }); } if ((!shops || shops.length === 0) && shopList) { prompts.push({ type: 'checkbox', name: 'shops', message: '选择店铺(空格勾选):', choices: shopList.map(sh => ({ name: `[${sh.id}] ${sh.name}`, value: sh.id, checked: true, })), pageSize: 15, validate: v => v.length > 0 || '至少选一个店铺', }); } if ((!statuses || statuses.length === 0) && statusList) { prompts.push({ type: 'checkbox', name: 'statuses', message: '选择订单状态(空格勾选):', choices: statusList.map(st => ({ name: `[${st.id}] ${st.status_name}`, value: st.id, checked: true, })), pageSize: 15, validate: v => v.length > 0 || '至少选一个状态', }); } prompts.push({ type: 'input', name: 'dailyRange', message: '每店每日单量区间 (如 "3-8"):', default: `${options.dailyMin ?? 1}-${options.dailyMax ?? 5}`, validate: v => /^\d+-\d+$/.test(v) || '格式: 最小-最大 (如 3-8)', }); prompts.push({ type: 'input', name: 'unitPrice', message: '单价区间 (如 "19.9-99.9",留空自动生成):', default: options.unitPrice || '', }); prompts.push({ type: 'confirm', name: 'deleteExisting', message: '是否先清空这些规格的已有订单?', default: false, }); prompts.push({ type: 'confirm', name: 'confirmInsert', message: '确认写入数据库?', default: true, }); } const answers = await inquirer.prompt(prompts); if (answers.datePreset && answers.datePreset !== 'custom') { const now = new Date(); const y = now.getFullYear(); const m = now.getMonth() + 1; const d = now.getDate(); const pad = (n) => String(n).padStart(2, '0'); switch (answers.datePreset) { case '7d': { const sd = new Date(now); sd.setDate(d - 6); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case '30d': { const sd = new Date(now); sd.setDate(d - 29); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case '90d': { const sd = new Date(now); sd.setDate(d - 89); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case 'thisMonth': answers.startDate = `${y}-${pad(m)}-01`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; case 'lastMonth': { const lm = m === 1 ? 12 : m - 1; const ly = m === 1 ? y - 1 : y; const lastMonthEnd = new Date(y, m - 1, 0); answers.startDate = `${ly}-${pad(lm)}-01`; answers.endDate = `${ly}-${pad(lm)}-${pad(lastMonthEnd.getDate())}`; break; } } delete answers.datePreset; } if (answers.dailyRange) { const parts = answers.dailyRange.split('-').map(Number); answers.dailyMin = parts[0]; answers.dailyMax = parts[1]; delete answers.dailyRange; } return answers; } function shuffle(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; } export function pickRandomSpecs(specList, count) { const shuffled = shuffle([...specList]); return shuffled.slice(0, Math.min(count, specList.length)); } export async function promptDeleteOptions(options) { const { company, companyList, specList } = options; const prompts = []; if (!company && companyList) { prompts.push({ type: 'list', name: 'company', message: '选择公司:', choices: companyList.map(c => ({ name: `[${c.id}] ${c.name}`, value: c.id })), pageSize: 15, }); } if (company) { prompts.push({ type: 'confirm', name: 'deleteAll', message: `清空该公司所有订单?`, default: false, }); prompts.push({ type: 'list', name: 'datePreset', message: '按时间范围筛选(可选):', choices: [ { name: '不限时间', value: 'all' }, { name: '最近 7 天', value: '7d' }, { name: '最近 30 天', value: '30d' }, { name: '最近 90 天', value: '90d' }, { name: '本月', value: 'thisMonth' }, { name: '自定义', value: 'custom' }, ], }); { const now = new Date(); const y = now.getFullYear(); const m = String(now.getMonth() + 1).padStart(2, '0'); const d = String(now.getDate()).padStart(2, '0'); prompts.push({ type: 'input', name: 'startDate', message: '开始日期 (YYYY-MM-DD):', when: a => a.datePreset === 'custom', validate: v => /^\d{4}-\d{2}-\d{2}$/.test(v) || '格式: YYYY-MM-DD', }); prompts.push({ type: 'input', name: 'endDate', message: '结束日期 (YYYY-MM-DD):', when: a => a.datePreset === 'custom', validate: v => /^\d{4}-\d{2}-\d{2}$/.test(v) || '格式: YYYY-MM-DD', }); } if (!options.deleteAll && specList) { prompts.push({ type: 'number', name: 'specCount', message: `随机选取几个规格删除?(共有 ${specList.length} 个,0=全部规格):`, default: 0, validate: v => (v >= 0 && v <= specList.length) || `请输入 0~${specList.length}`, }); } prompts.push({ type: 'confirm', name: 'dryRun', message: '先预览要删除的数量?', default: true, }); prompts.push({ type: 'confirm', name: 'confirmDelete', message: '确认删除?', default: false, when: a => !a.dryRun, }); } const answers = await inquirer.prompt(prompts); if (answers.datePreset && answers.datePreset !== 'all' && answers.datePreset !== 'custom') { const now = new Date(); const y = now.getFullYear(); const m = now.getMonth() + 1; const d = now.getDate(); const pad = (n) => String(n).padStart(2, '0'); switch (answers.datePreset) { case '7d': { const sd = new Date(now); sd.setDate(d - 6); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case '30d': { const sd = new Date(now); sd.setDate(d - 29); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case '90d': { const sd = new Date(now); sd.setDate(d - 89); answers.startDate = `${sd.getFullYear()}-${pad(sd.getMonth() + 1)}-${pad(sd.getDate())}`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } case 'thisMonth': answers.startDate = `${y}-${pad(m)}-01`; answers.endDate = `${y}-${pad(m)}-${pad(d)}`; break; } delete answers.datePreset; } return answers; }