Files
ecom-dmb/apps/order-generator/src/generator.js
T
2026-09-16 22:43:21 +08:00

124 lines
3.7 KiB
JavaScript

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