mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 09:49:19 +08:00
refactor(订单生成器): 重命名订单生成器目录
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
export async function deleteOrders(conn, { companyId, specIds, shopIds, startDate, endDate, verbose, dryRun }) {
|
||||
const conditions = ['company_id = ?'];
|
||||
const params = [companyId];
|
||||
|
||||
if (specIds && specIds.length > 0) {
|
||||
const placeholders = specIds.map(() => '?').join(',');
|
||||
conditions.push(`product_spec_id IN (${placeholders})`);
|
||||
params.push(...specIds);
|
||||
}
|
||||
|
||||
if (shopIds && shopIds.length > 0) {
|
||||
const placeholders = shopIds.map(() => '?').join(',');
|
||||
conditions.push(`shop_id IN (${placeholders})`);
|
||||
params.push(...shopIds);
|
||||
}
|
||||
|
||||
if (startDate) {
|
||||
conditions.push('order_time >= ?');
|
||||
params.push(startDate + ' 00:00:00');
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
conditions.push('order_time <= ?');
|
||||
params.push(endDate + ' 23:59:59');
|
||||
}
|
||||
|
||||
const where = conditions.join(' AND ');
|
||||
|
||||
if (dryRun) {
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT COUNT(*) AS cnt FROM ec_order WHERE ${where}`,
|
||||
params
|
||||
);
|
||||
return { matched: rows[0].cnt, deleted: 0 };
|
||||
}
|
||||
|
||||
const [result] = await conn.execute(
|
||||
`DELETE FROM ec_order WHERE ${where}`,
|
||||
params
|
||||
);
|
||||
return { matched: result.affectedRows, deleted: result.affectedRows };
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import mysql from 'mysql2/promise';
|
||||
|
||||
export async function createConnection(opts) {
|
||||
const config = {
|
||||
host: opts.dbHost || '192.168.3.100',
|
||||
port: opts.dbPort || 3306,
|
||||
user: opts.dbUser || 'ecom_dmb_dev',
|
||||
password: opts.dbPassword || 'ecom_dmb_dev',
|
||||
database: opts.dbName || 'ecom_dmb_dev',
|
||||
};
|
||||
|
||||
const conn = await mysql.createConnection(config);
|
||||
return conn;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
export async function verifyCompany(conn, companyId) {
|
||||
const [rows] = await conn.execute('SELECT id, name FROM org_company WHERE id = ?', [companyId]);
|
||||
if (rows.length === 0) {
|
||||
throw new Error(`Company with id ${companyId} not found`);
|
||||
}
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
export async function fetchSpecs(conn, companyId, specIds, specCodes) {
|
||||
const results = [];
|
||||
|
||||
if (specIds && specIds.length > 0) {
|
||||
const placeholders = specIds.map(() => '?').join(',');
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT ps.*, p.name AS product_name, p.product_code
|
||||
FROM ec_product_spec ps
|
||||
JOIN ec_product p ON ps.product_id = p.id
|
||||
WHERE ps.company_id = ? AND ps.id IN (${placeholders})`,
|
||||
[companyId, ...specIds]
|
||||
);
|
||||
results.push(...rows);
|
||||
}
|
||||
|
||||
if (specCodes && specCodes.length > 0) {
|
||||
const placeholders = specCodes.map(() => '?').join(',');
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT ps.*, p.name AS product_name, p.product_code
|
||||
FROM ec_product_spec ps
|
||||
JOIN ec_product p ON ps.product_id = p.id
|
||||
WHERE ps.company_id = ? AND ps.spec_code IN (${placeholders})`,
|
||||
[companyId, ...specCodes]
|
||||
);
|
||||
results.push(...rows);
|
||||
}
|
||||
|
||||
if (specIds === undefined && specCodes === undefined) {
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT ps.*, p.name AS product_name, p.product_code
|
||||
FROM ec_product_spec ps
|
||||
JOIN ec_product p ON ps.product_id = p.id
|
||||
WHERE ps.company_id = ?`,
|
||||
[companyId]
|
||||
);
|
||||
results.push(...rows);
|
||||
}
|
||||
|
||||
const seen = new Set();
|
||||
return results.filter(s => {
|
||||
if (seen.has(s.id)) return false;
|
||||
seen.add(s.id);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export async function fetchShops(conn, companyId, productIds, shopIds) {
|
||||
if (shopIds && shopIds.length > 0) {
|
||||
const placeholders = shopIds.map(() => '?').join(',');
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT id, name, platform_id FROM ec_shop
|
||||
WHERE company_id = ? AND id IN (${placeholders})`,
|
||||
[companyId, ...shopIds]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
if (!productIds || productIds.length === 0) {
|
||||
const [rows] = await conn.execute(
|
||||
'SELECT id, name, platform_id FROM ec_shop WHERE company_id = ?',
|
||||
[companyId]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
const productPlaceholders = productIds.map(() => '?').join(',');
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT DISTINCT s.id, s.name, s.platform_id
|
||||
FROM ec_shop s
|
||||
JOIN ec_shop_product_relation spr ON s.id = spr.shop_id
|
||||
WHERE s.company_id = ? AND spr.product_id IN (${productPlaceholders})`,
|
||||
[companyId, ...productIds]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function fetchStatuses(conn, companyId, statusIds) {
|
||||
if (statusIds && statusIds.length > 0) {
|
||||
const placeholders = statusIds.map(() => '?').join(',');
|
||||
const [rows] = await conn.execute(
|
||||
`SELECT id, status_name FROM ec_order_status
|
||||
WHERE company_id = ? AND id IN (${placeholders})`,
|
||||
[companyId, ...statusIds]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
const [rows] = await conn.execute(
|
||||
'SELECT id, status_name FROM ec_order_status WHERE company_id = ? ORDER BY sort_order',
|
||||
[companyId]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
export function rand(min, max) {
|
||||
return Math.random() * (max - min) + min;
|
||||
}
|
||||
|
||||
export function randInt(min, max) {
|
||||
return Math.floor(rand(min, max + 1));
|
||||
}
|
||||
|
||||
export function randomDate(start, end) {
|
||||
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
|
||||
}
|
||||
|
||||
export function formatDateTime(d) {
|
||||
const y = d.getFullYear();
|
||||
const M = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const D = String(d.getDate()).padStart(2, '0');
|
||||
const h = String(d.getHours()).padStart(2, '0');
|
||||
const m = String(d.getMinutes()).padStart(2, '0');
|
||||
const s = String(d.getSeconds()).padStart(2, '0');
|
||||
return `${y}-${M}-${D} ${h}:${m}:${s}`;
|
||||
}
|
||||
|
||||
export function formatDate(d) {
|
||||
const y = d.getFullYear();
|
||||
const M = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const D = String(d.getDate()).padStart(2, '0');
|
||||
return `${y}-${M}-${D}`;
|
||||
}
|
||||
|
||||
export function generatePlatformOrderNo(companyId, seenSet) {
|
||||
for (let attempt = 0; attempt < 100; attempt++) {
|
||||
const ts = Date.now().toString(36).toUpperCase();
|
||||
const randStr = Math.random().toString(36).substring(2, 10).toUpperCase();
|
||||
const no = `${ts}-${companyId}${randStr}`;
|
||||
if (!seenSet.has(no)) {
|
||||
seenSet.add(no);
|
||||
return no;
|
||||
}
|
||||
}
|
||||
throw new Error('Unable to generate unique platform order number');
|
||||
}
|
||||
|
||||
function makeOrder(config, spec, shop, status, orderTime, seenOrderNos, remarkPool, priceRange) {
|
||||
const quantity = randInt(1, 5);
|
||||
|
||||
let unitPrice;
|
||||
if (priceRange) {
|
||||
unitPrice = Math.round(rand(priceRange[0], priceRange[1]) * 100) / 100;
|
||||
} else {
|
||||
const baseCost = Number(spec.product_cost);
|
||||
if (baseCost >= 10) {
|
||||
unitPrice = Math.round(baseCost * rand(1.3, 3.0) * 100) / 100;
|
||||
} else if (baseCost > 0) {
|
||||
unitPrice = Math.round(rand(9.9, 59.9) * 100) / 100;
|
||||
} else {
|
||||
unitPrice = Math.round(rand(9.9, 99.9) * 100) / 100;
|
||||
}
|
||||
}
|
||||
if (unitPrice < 1) unitPrice = Math.round(rand(9.9, 49.9) * 100) / 100;
|
||||
|
||||
const receivedAmount = Math.round(unitPrice * quantity * rand(0.85, 1.0) * 100) / 100;
|
||||
const platformOrderNo = generatePlatformOrderNo(config.companyId, seenOrderNos);
|
||||
const remark = remarkPool[randInt(0, remarkPool.length - 1)];
|
||||
|
||||
return {
|
||||
company_id: config.companyId,
|
||||
shop_id: shop.id,
|
||||
product_spec_id: spec.id,
|
||||
spec_code: spec.spec_code,
|
||||
status_id: status.id,
|
||||
platform_order_no: platformOrderNo,
|
||||
link_product_name: spec.product_name,
|
||||
link_spec_name: spec.name,
|
||||
received_amount: receivedAmount,
|
||||
quantity,
|
||||
unit_price: unitPrice,
|
||||
product_cost: spec.product_cost,
|
||||
shipping_cost: spec.shipping_cost,
|
||||
other_cost: spec.other_cost,
|
||||
remark,
|
||||
import_task_id: null,
|
||||
confirm: config.confirm,
|
||||
order_time: formatDateTime(orderTime),
|
||||
};
|
||||
}
|
||||
|
||||
export function* iterateOrders(config) {
|
||||
const {
|
||||
companyId, specs, shops, statuses, priceRange,
|
||||
dailyMin, dailyMax, confirm,
|
||||
startDate, endDate, seenOrderNos,
|
||||
remarkPool,
|
||||
} = config;
|
||||
|
||||
const cfg = { companyId, confirm };
|
||||
let total = 0;
|
||||
|
||||
// 单量按「店 × 天」控制;每笔订单从所选规格中随机挑选
|
||||
let current = new Date(startDate);
|
||||
const end = new Date(endDate);
|
||||
|
||||
while (current <= end) {
|
||||
const dayStart = new Date(current);
|
||||
const dayEnd = new Date(current);
|
||||
dayEnd.setHours(23, 59, 59, 999);
|
||||
|
||||
for (const shop of shops) {
|
||||
const count = randInt(dailyMin, dailyMax);
|
||||
for (let i = 0; i < count; i++) {
|
||||
const spec = specs[randInt(0, specs.length - 1)];
|
||||
const status = statuses[randInt(0, statuses.length - 1)];
|
||||
const orderTime = randomDate(dayStart, dayEnd);
|
||||
total++;
|
||||
|
||||
yield makeOrder(cfg, spec, shop, status, orderTime, seenOrderNos, remarkPool, priceRange);
|
||||
}
|
||||
}
|
||||
|
||||
current.setDate(current.getDate() + 1);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import cliProgress from 'cli-progress';
|
||||
|
||||
export const BATCH_SIZE = 1000;
|
||||
|
||||
export async function deleteExistingOrders(conn, companyId, specs, verbose) {
|
||||
let total = 0;
|
||||
for (const spec of specs) {
|
||||
const [result] = await conn.execute(
|
||||
'DELETE FROM ec_order WHERE company_id = ? AND product_spec_id = ?',
|
||||
[companyId, spec.id]
|
||||
);
|
||||
total += result.affectedRows;
|
||||
if (verbose) {
|
||||
console.log(` Deleted ${result.affectedRows} orders for spec [${spec.id}] ${spec.name}`);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
const SQL = `INSERT INTO ec_order
|
||||
(company_id, shop_id, product_spec_id, spec_code, status_id, platform_order_no,
|
||||
link_product_name, link_spec_name, received_amount, quantity, unit_price,
|
||||
product_cost, shipping_cost, other_cost, remark, import_task_id, confirm, order_time)
|
||||
VALUES ?`;
|
||||
|
||||
function toRow(o) {
|
||||
return [
|
||||
o.company_id, o.shop_id, o.product_spec_id, o.spec_code, o.status_id, o.platform_order_no,
|
||||
o.link_product_name, o.link_spec_name, o.received_amount, o.quantity, o.unit_price,
|
||||
o.product_cost, o.shipping_cost, o.other_cost,
|
||||
o.remark, null, o.confirm, o.order_time,
|
||||
];
|
||||
}
|
||||
|
||||
export async function insertBatch(conn, batch) {
|
||||
const rows = batch.map(toRow);
|
||||
const [result] = await conn.query(SQL, [rows]);
|
||||
return result.affectedRows;
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user