954 lines
46 KiB
JavaScript
954 lines
46 KiB
JavaScript
// 限价流程同步:拉取「限价」系列钉钉流程(四个模板、仅已完结),写入限价表并导出全量明细 Excel。
|
||
//
|
||
// 用法(服务器 / 本地通用):
|
||
// node scripts/sync-limit-workflows.mjs \
|
||
// [--out-dir <目录>] [--initial-dir <目录>] [--env-file <env 文件>] [--skip-excel]
|
||
//
|
||
// 环境变量(服务器上由 shared/.env 提供):
|
||
// DATABASE_URL 业务库(限价 / 授权写入目标)
|
||
// JST_REPORT_DB_* 钉钉流程报表库(只读)
|
||
// PROD_ARCHIVE_DB_* 正式库 external.product_catalog_wide_v1(产品限价页面)
|
||
// LIMIT_SYNC_OUT_DIR Excel 输出目录(默认 <app>/导出结果)
|
||
// LIMIT_SYNC_INITIAL_DIR 限价期初数据目录(默认 <app>/期初数据,其次用仓库自带 scripts/data/limit-sync/期初数据)
|
||
// LIMIT_SYNC_ENV_FILE 额外 env 文件(服务器上一般由 systemd EnvironmentFile 注入,无需设置)
|
||
// LIMIT_SYNC_SKIP_EXCEL=1 只写库、不导出 Excel(定时任务可选)
|
||
// LIMIT_SYNC_HISTORY_KEEP 历史版本保留份数(默认 20)
|
||
//
|
||
// 输出:<out-dir>/限价流程_完整明细表.xlsx(固定文件名,每次覆盖为最新全量;
|
||
// 旧文件自动归档到 <out-dir>/历史版本/,超过保留份数自动清理)。
|
||
import envPkg from '@next/env'
|
||
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, unlinkSync } from 'node:fs'
|
||
import { dirname, isAbsolute, join, resolve } from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { randomUUID } from 'node:crypto'
|
||
import { preserveLimitFileManualPrices } from './lib/limit-file-manual-price-protection.mjs'
|
||
|
||
const APP_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
||
|
||
/** 极简参数解析:支持 `--key value`(值缺省为 true)。 */
|
||
function readArgs(argv) {
|
||
const parsed = {}
|
||
for (let index = 0; index < argv.length; index += 1) {
|
||
const token = argv[index]
|
||
if (!token.startsWith('--')) continue
|
||
const key = token.slice(2)
|
||
const next = argv[index + 1]
|
||
if (next && !next.startsWith('--')) {
|
||
parsed[key] = next
|
||
index += 1
|
||
} else {
|
||
parsed[key] = true
|
||
}
|
||
}
|
||
return parsed
|
||
}
|
||
|
||
/** 读取 `KEY=VALUE` 形式的 env 文件(不覆盖已存在的进程变量,和 systemd EnvironmentFile 行为一致)。 */
|
||
function loadEnvFile(filePath) {
|
||
if (!existsSync(filePath)) throw new Error(`env 文件不存在:${filePath}`)
|
||
const content = readFileSyncUtf8(filePath)
|
||
for (const rawLine of content.split(/\r?\n/)) {
|
||
const line = rawLine.trim()
|
||
if (!line || line.startsWith('#')) continue
|
||
const separator = line.indexOf('=')
|
||
if (separator <= 0) continue
|
||
const key = line.slice(0, separator).trim()
|
||
let value = line.slice(separator + 1).trim()
|
||
if (
|
||
(value.startsWith("'") && value.endsWith("'"))
|
||
|| (value.startsWith('"') && value.endsWith('"'))
|
||
) {
|
||
value = value.slice(1, -1)
|
||
}
|
||
if (process.env[key] === undefined) process.env[key] = value
|
||
}
|
||
}
|
||
|
||
function readFileSyncUtf8(filePath) {
|
||
return readFileSync(filePath, 'utf8')
|
||
}
|
||
|
||
function resolveFromAppRoot(value) {
|
||
const text = String(value ?? '').trim()
|
||
if (!text) return ''
|
||
return isAbsolute(text) ? text : resolve(APP_ROOT, text)
|
||
}
|
||
|
||
const args = readArgs(process.argv.slice(2))
|
||
const envFile = resolveFromAppRoot(args['env-file'] || process.env.LIMIT_SYNC_ENV_FILE || '')
|
||
if (envFile) loadEnvFile(envFile)
|
||
// 仍按 Next 的方式加载 app root 的 .env(已有变量不会被覆盖)
|
||
envPkg.loadEnvConfig(APP_ROOT)
|
||
|
||
import sql from 'mssql'
|
||
import pg from 'pg'
|
||
import * as XLSX from 'xlsx'
|
||
|
||
/** ESM 下 xlsx 的命名导出不全(readFile 取不到),统一走这个命名空间兜底。 */
|
||
const xlsx = XLSX.default ?? XLSX
|
||
|
||
const OUT_DIR = resolveFromAppRoot(args['out-dir'] || process.env.LIMIT_SYNC_OUT_DIR || join(APP_ROOT, '导出结果'))
|
||
const OUT_FILE = join(OUT_DIR, '限价流程_完整明细表.xlsx')
|
||
const HISTORY_DIR = join(OUT_DIR, '历史版本')
|
||
/** 限价期初数据(业务整理的存量限价)目录:优先显式目录,其次仓库自带样例目录。 */
|
||
const INITIAL_LIMIT_DIR = resolveFromAppRoot(
|
||
args['initial-dir'] || process.env.LIMIT_SYNC_INITIAL_DIR || join(APP_ROOT, '期初数据'),
|
||
)
|
||
const BUNDLED_INITIAL_LIMIT_DIR = join(APP_ROOT, 'scripts/data/limit-sync/期初数据')
|
||
const SKIP_EXCEL = Boolean(args['skip-excel']) || /^(1|true|yes)$/i.test(process.env.LIMIT_SYNC_SKIP_EXCEL ?? '')
|
||
const HISTORY_KEEP = Math.max(1, Number(process.env.LIMIT_SYNC_HISTORY_KEEP ?? 20) || 20)
|
||
|
||
// 数据源口径(业务确认):
|
||
// A. 「新品限价流程」→ 限价明细取自系统「产品限价页面」(按流程链接中的 SPU 关联);
|
||
// B. 「新品限价及限价调整」→ 限价明细取钉钉流程表单内的明细表;
|
||
// C. 「各中心特殊限价需求申请」→ 明细表「最低售价」作为负责人限价(2026-10-16 纳入);
|
||
// D. 「GB特殊限价需求申请」→ 明细表「期望日常限价」作为负责人限价(2026-10-16 纳入)。
|
||
// A 的「产品限价页面」数据实际来自正式库 external.product_catalog_wide_v1(本地库该视图为空),
|
||
// 没配正式库连接时退回本地 IntakeNonIntegratedProduct / IntakeNonIntegratedProductLimitRow。
|
||
// E. 「产品限价页面」上已确认(CONFIRMED)的限价全量纳入(2026-10-16),
|
||
// 给没有限价流程的货号兜底(如 CY012801);同货号冲突时以限价流程为准。
|
||
// 都只保留已完结(COMPLETED)的流程;其他限价类模板(毛利调整等)暂不抓取。
|
||
const TEMPLATE_FILTER = `(title LIKE N'%提交的新品限价及限价调整' OR title LIKE N'%提交的新品限价流程' OR title LIKE N'%提交的各中心特殊限价需求申请' OR title LIKE N'%提交的GB特殊限价需求申请') AND status = 'COMPLETED'`
|
||
|
||
// 固定 55 列,与 2026-09-28《限价流程_完整明细表》保持一致。
|
||
const COLUMNS = [
|
||
'流程名称', '标题', '流程实例ID', '流程状态', '处理结果', '发起人', '发起部门名称', '创建时间', '完成时间',
|
||
'发起人用户ID', '业务ID', '流水号', '申请类型', '品牌', '品牌限价单号', '品牌运营负责人', '部门负责人', '销售类目',
|
||
'二级类目', '品类', '类目', '商品企划', '是否龙晨简尚共有产品', '由壹点产品开发流程链接地址', '调整理由',
|
||
'限价附件', '调整限价附件', '同SPU负责人沟通记录', '来源明细表', '明细行号', 'SPU', '货号', '货号(非商品编码)',
|
||
'商品编码', '主销渠道', '最低售价', '当下限价价格', '期望限价价格', '期望日常限价', '当下限价毛利率',
|
||
'期望限价毛利率', '当前限价', '调整后限价', '当前限价毛利率', '调整后限价毛利率', '当前非SPU负责人限价',
|
||
'调整后非SPU负责人限价', '非SPU负责人限价', '主销多多百补限价', '百补限价', '期望活动性限价',
|
||
'期望活动毛利率', '近7日日销', '报备价格(商家到手价)', '开始时间', '结束时间', '图片',
|
||
// 各中心特殊限价需求申请的明细列
|
||
'主销渠道', '最低售价', '期望限价毛利率', '货号',
|
||
]
|
||
const DETAIL_COLUMNS = new Set(COLUMNS.slice(COLUMNS.indexOf('SPU')))
|
||
|
||
const STATUS_MAP = { COMPLETED: '已完成', RUNNING: '进行中', TERMINATED: '已终止', CANCELED: '已取消', DRAFT: '草稿' }
|
||
const RESULT_MAP = { agree: '同意', refuse: '拒绝' }
|
||
|
||
/** 从标题提取流程名称:「10-15+醉夏提交的新品限价及限价调整」→「新品限价及限价调整」。 */
|
||
function extractFlowName(title) {
|
||
const text = String(title ?? '')
|
||
const marker = '提交的'
|
||
return text.includes(marker) ? text.slice(text.indexOf(marker) + marker.length).trim() : text
|
||
}
|
||
|
||
/** 时间显示格式与历史导出对齐:「2026-10-15T10:56Z」→「2026-10-15 10:56」。 */
|
||
function formatDateTime(value) {
|
||
return String(value ?? '').replace('T', ' ').replace(/Z$/, '').trim()
|
||
}
|
||
|
||
/** 明细单元格取值:附件数组取文件名;对象转 JSON;其余原样。 */
|
||
function normalizeCellValue(value) {
|
||
if (Array.isArray(value)) {
|
||
const files = value.map((item) => item?.fileName).filter(Boolean)
|
||
if (files.length) return files.join(';')
|
||
return value.map((item) => (item === null || item === undefined ? '' : String(item))).join(';')
|
||
}
|
||
if (typeof value === 'object' && value !== null) return JSON.stringify(value)
|
||
return value ?? ''
|
||
}
|
||
|
||
/** 明细列在各模板中命名不同,且可能出现「列存在但值为空」,按候选顺序取第一个非空值。 */
|
||
function firstNonEmpty(record, labels) {
|
||
for (const label of labels) {
|
||
const text = String(record[label] ?? '').trim()
|
||
if (text) return text
|
||
}
|
||
return ''
|
||
}
|
||
|
||
const pool = await new sql.ConnectionPool({
|
||
server: process.env.JST_REPORT_DB_HOST.trim(),
|
||
port: Number(process.env.JST_REPORT_DB_PORT?.trim() || '1433'),
|
||
database: process.env.JST_REPORT_DB_NAME.trim(),
|
||
user: process.env.JST_REPORT_DB_USER.trim(),
|
||
password: process.env.JST_REPORT_DB_PASSWORD.trim(),
|
||
options: {
|
||
encrypt: process.env.JST_REPORT_DB_ENCRYPT?.trim() !== 'false',
|
||
trustServerCertificate: process.env.JST_REPORT_DB_TRUST_SERVER_CERTIFICATE?.trim() !== 'false',
|
||
},
|
||
requestTimeout: 300_000,
|
||
}).connect()
|
||
|
||
console.log('模式: 全量导出(新品限价及限价调整 / 新品限价流程 / 各中心特殊限价需求申请 / GB特殊限价需求申请,仅已完结)')
|
||
|
||
const flows = (await pool.request().query(`
|
||
SELECT
|
||
processInstanceId, title, status, result, originatorDeptName, originatorUserId, businessId,
|
||
createAt, updateAt, finishTime,
|
||
processCode
|
||
FROM rpt.ding_Workflow
|
||
WHERE ${TEMPLATE_FILTER}
|
||
ORDER BY createAt DESC
|
||
`)).recordset
|
||
console.log(`流程数量: ${flows.length}`)
|
||
|
||
if (!flows.length) {
|
||
console.log('没有符合条件的流程,未生成文件')
|
||
await pool.close()
|
||
process.exit(0)
|
||
}
|
||
|
||
const ids = flows.map((row) => `'${row.processInstanceId}'`).join(',')
|
||
const schemeRows = (await pool.request().query(`
|
||
SELECT processInstanceId, componentType, name, value, extValue
|
||
FROM rpt.ding_WorkflowScheme
|
||
WHERE processInstanceId IN (${ids})
|
||
ORDER BY processInstanceId, id
|
||
`)).recordset
|
||
|
||
const fieldsById = new Map()
|
||
for (const row of schemeRows) {
|
||
if (!fieldsById.has(row.processInstanceId)) fieldsById.set(row.processInstanceId, [])
|
||
fieldsById.get(row.processInstanceId).push(row)
|
||
}
|
||
const pickField = (fields, name) => fields.find((field) => field.name === name)?.value?.trim() || ''
|
||
|
||
// 发起人名字映射(本地员工目录:钉钉用户 ID → 姓名)
|
||
const nameById = new Map()
|
||
try {
|
||
const client = new pg.Client({ connectionString: process.env.DATABASE_URL })
|
||
await client.connect()
|
||
const userIds = [...new Set(flows.map((row) => String(row.originatorUserId ?? '')).filter(Boolean))]
|
||
if (userIds.length) {
|
||
const employees = await client.query(
|
||
`SELECT "dingtalkUserId", "name" FROM "EmployeeDirectory" WHERE "dingtalkUserId" = ANY($1)`,
|
||
[userIds],
|
||
)
|
||
for (const row of employees.rows) nameById.set(row.dingtalkUserId, row.name)
|
||
}
|
||
await client.end()
|
||
} catch (error) {
|
||
console.warn('员工名字映射加载失败(发起人列将留空):', error.message)
|
||
}
|
||
|
||
/** 附件字段(JSON 数组)转文件名文本。 */
|
||
const attachmentText = (raw) => {
|
||
try {
|
||
const list = JSON.parse(raw)
|
||
return Array.isArray(list) ? list.map((item) => item.fileName).filter(Boolean).join(';') : raw
|
||
} catch { return raw }
|
||
}
|
||
|
||
const rows = []
|
||
// 写库用收集器:每条明细的货号限价 / 价格授权(同价、活动)数据。
|
||
const collected = []
|
||
|
||
// 数据源 A 预扫:「新品限价流程」的限价明细取自系统「产品限价页面」。
|
||
// 从流程表单的「由壹点产品开发流程链接地址」提取 SPU(形如 /intake/non-integrated/DSG0039-1786756641270),
|
||
// 再按 SPU 批量读取限价行:优先正式库 external.product_catalog_wide_v1(产品限价页面的数据源),
|
||
// 正式库没配 / 查不到时退回本地 IntakeNonIntegratedProduct + IntakeNonIntegratedProductLimitRow。
|
||
const legacyFlowSpu = new Map()
|
||
for (const flow of flows) {
|
||
if (extractFlowName(flow.title) !== '新品限价流程') continue
|
||
const fields = fieldsById.get(flow.processInstanceId) ?? []
|
||
const link = pickField(fields, '由壹点产品开发流程链接地址')
|
||
const matched = link.match(/non-integrated\/([^/?#]+)/)
|
||
if (matched) {
|
||
// 链接形如 /non-integrated/BJ0218-1783663599778,末尾是流水时间戳;SPU 本身可能带连字符(GBGJ-SJ0380)。
|
||
const raw = decodeURIComponent(matched[1]).trim()
|
||
const spu = raw.replace(/-\d{6,}$/, '')
|
||
legacyFlowSpu.set(flow.processInstanceId, spu || raw)
|
||
}
|
||
}
|
||
|
||
const limitRowsBySpu = new Map()
|
||
if (legacyFlowSpu.size) {
|
||
const spus = [...new Set(legacyFlowSpu.values())].filter(Boolean)
|
||
/** 把一行限价按 SPU 归并(去重键:货号 + 条码),避免同 SPU 的多列/多行重复。 */
|
||
const pushLimitRow = (spu, row) => {
|
||
if (!spu) return
|
||
const list = limitRowsBySpu.get(spu) ?? []
|
||
const key = `${String(row.skuCode ?? '').trim()}|${String(row.barcode ?? '').trim()}`
|
||
if (list.some((item) => `${String(item.skuCode ?? '').trim()}|${String(item.barcode ?? '').trim()}` === key)) return
|
||
list.push(row)
|
||
limitRowsBySpu.set(spu, list)
|
||
}
|
||
|
||
let loadedFromProd = false
|
||
try {
|
||
const prodHost = (process.env.PROD_ARCHIVE_DB_HOST ?? '').trim()
|
||
if (prodHost && spus.length) {
|
||
const prod = new pg.Client({
|
||
host: prodHost,
|
||
port: Number((process.env.PROD_ARCHIVE_DB_PORT ?? '').trim() || '5432'),
|
||
database: (process.env.PROD_ARCHIVE_DB_NAME ?? '').trim(),
|
||
user: (process.env.PROD_ARCHIVE_DB_USER ?? '').trim(),
|
||
password: (process.env.PROD_ARCHIVE_DB_PASSWORD ?? '').trim(),
|
||
ssl: { rejectUnauthorized: false },
|
||
connectionTimeoutMillis: 15_000,
|
||
})
|
||
await prod.connect()
|
||
const upperToSpu = new Map(spus.map((spu) => [spu.toUpperCase(), spu]))
|
||
const catalogRows = (await prod.query(
|
||
`SELECT upper(btrim("product_business_key")) AS product_key,
|
||
upper(btrim("archive_spu")) AS archive_spu,
|
||
btrim("sku_code") AS "skuCode",
|
||
btrim("barcode") AS "barcode",
|
||
btrim("expected_daily_limit_price") AS "expectedDailyLimitPrice",
|
||
btrim("expected_limit_gross_margin_rate") AS "expectedLimitGrossMarginRate",
|
||
btrim("non_spu_owner_limit_price") AS "nonSpuOwnerLimitPrice"
|
||
FROM external.product_catalog_wide_v1
|
||
WHERE upper(btrim("product_business_key")) = ANY($1) OR upper(btrim("archive_spu")) = ANY($1)`,
|
||
[spus.map((spu) => spu.toUpperCase())],
|
||
)).rows
|
||
await prod.end()
|
||
for (const row of catalogRows) {
|
||
for (const key of new Set([row.product_key, row.archive_spu])) {
|
||
pushLimitRow(upperToSpu.get(key), row)
|
||
}
|
||
}
|
||
loadedFromProd = [...limitRowsBySpu.values()].some((list) => list.length > 0)
|
||
const matchedSpus = spus.filter((spu) => (limitRowsBySpu.get(spu) ?? []).length > 0).length
|
||
console.log(
|
||
`产品限价页面(正式库 external.product_catalog_wide_v1):SPU ${spus.length} 个,命中 ${matchedSpus} 个,明细 ${catalogRows.length} 行`,
|
||
)
|
||
if (!loadedFromProd) limitRowsBySpu.clear()
|
||
}
|
||
} catch (error) {
|
||
limitRowsBySpu.clear()
|
||
console.warn('正式库产品限价页面读取失败,改用本地库:', error.message)
|
||
}
|
||
|
||
if (!loadedFromProd) {
|
||
// 本地库兜底:直接读非集成产品主档 + 限价明细表(本地库常常是空的)。
|
||
try {
|
||
const client = new pg.Client({ connectionString: process.env.DATABASE_URL })
|
||
await client.connect()
|
||
if (spus.length) {
|
||
const products = (await client.query(
|
||
`SELECT "id", "spu", "spuBase" FROM "IntakeNonIntegratedProduct"
|
||
WHERE "deletedAt" IS NULL AND ("spu" = ANY($1) OR "spuBase" = ANY($1))`,
|
||
[spus],
|
||
)).rows
|
||
const productIds = products.map((row) => row.id)
|
||
if (productIds.length) {
|
||
const limitRows = (await client.query(
|
||
`SELECT "productId", "skuCode", "barcode", "expectedDailyLimitPrice",
|
||
"expectedLimitGrossMarginRate", "nonSpuOwnerLimitPrice"
|
||
FROM "IntakeNonIntegratedProductLimitRow" WHERE "productId" = ANY($1)`,
|
||
[productIds],
|
||
)).rows
|
||
const productById = new Map(products.map((row) => [row.id, row]))
|
||
for (const limitRow of limitRows) {
|
||
const product = productById.get(limitRow.productId)
|
||
if (!product) continue
|
||
for (const spu of new Set([product.spu, product.spuBase])) {
|
||
pushLimitRow(spu, limitRow)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
await client.end()
|
||
} catch (error) {
|
||
console.warn('产品限价数据读取失败(新品限价流程的明细将留空):', error.message)
|
||
}
|
||
}
|
||
}
|
||
console.log(`新品限价流程 ${legacyFlowSpu.size} 条,其中关联到产品限价明细 ${[...legacyFlowSpu.values()].filter((spu) => (limitRowsBySpu.get(spu) ?? []).length > 0).length} 条`)
|
||
|
||
// —— 数据源 E 预扫:产品限价页面上「已确认」的限价(正式库 external.product_catalog_wide_v1)——
|
||
// 这类货号没有限价流程(例如 CY012801),只靠流程抓不到;按货号兜底写入,冲突时仍以流程为准。
|
||
const pageLimitItems = []
|
||
try {
|
||
const prodHost = (process.env.PROD_ARCHIVE_DB_HOST ?? '').trim()
|
||
if (!prodHost) {
|
||
console.log('产品限价页面(已确认限价):未配置正式库连接,跳过')
|
||
} else {
|
||
const prod = new pg.Client({
|
||
host: prodHost,
|
||
port: Number((process.env.PROD_ARCHIVE_DB_PORT ?? '').trim() || '5432'),
|
||
database: (process.env.PROD_ARCHIVE_DB_NAME ?? '').trim(),
|
||
user: (process.env.PROD_ARCHIVE_DB_USER ?? '').trim(),
|
||
password: (process.env.PROD_ARCHIVE_DB_PASSWORD ?? '').trim(),
|
||
ssl: { rejectUnauthorized: false },
|
||
connectionTimeoutMillis: 15_000,
|
||
})
|
||
await prod.connect()
|
||
const confirmedRows = (await prod.query(
|
||
// 确认时间:视图里存的是 UTC 墙钟(Prisma 语义),+8 小时换成北京时间墙钟,
|
||
// 与流程完成时间的存储口径一致(页面直存直显)。
|
||
`SELECT btrim("sku_code") AS "skuCode",
|
||
btrim("barcode") AS "barcode",
|
||
btrim("product_business_key") AS "spu",
|
||
btrim("expected_daily_limit_price") AS "ownerLimitPrice",
|
||
btrim("non_spu_owner_limit_price") AS "nonSpuOwnerLimitPrice",
|
||
to_char("limit_confirmed_at" + interval '8 hours', 'YYYY-MM-DD HH24:MI:SS') AS "confirmedAt"
|
||
FROM external.product_catalog_wide_v1
|
||
WHERE "limit_price_status" = 'CONFIRMED'
|
||
AND COALESCE(btrim("sku_code"), '') <> ''
|
||
AND COALESCE(btrim("expected_daily_limit_price"), '') <> ''`,
|
||
)).rows
|
||
await prod.end()
|
||
// 同一货号多条(多规格条码)时保留确认时间最新的一条,价格一致时取任意
|
||
const byStyle = new Map()
|
||
for (const row of confirmedRows) {
|
||
// 注意:toNumberOrNull 在文件后面才定义,这里不能用(TDZ),直接就地判断
|
||
const price = Number(String(row.ownerLimitPrice ?? '').replace(/[^\d.-]/g, ''))
|
||
if (!Number.isFinite(price) || price <= 0) continue
|
||
const current = byStyle.get(row.skuCode)
|
||
const currentAt = current?.confirmedAt ? new Date(current.confirmedAt).getTime() : 0
|
||
const nextAt = row.confirmedAt ? new Date(row.confirmedAt).getTime() : 0
|
||
if (!current || nextAt >= currentAt) byStyle.set(row.skuCode, row)
|
||
}
|
||
pageLimitItems.push(...byStyle.values())
|
||
console.log(
|
||
`产品限价页面(已确认限价):${confirmedRows.length} 行,去重后 ${pageLimitItems.length} 个货号`,
|
||
)
|
||
}
|
||
} catch (error) {
|
||
console.warn('产品限价页面(已确认限价)读取失败,本次不纳入:', error.message)
|
||
}
|
||
|
||
// —— 数据源 F:限价期初数据(业务整理的存量限价 Excel,最低优先级兜底)——
|
||
// 取「期初数据」目录里最新的一份;同货号多行取更新日期最新的一条;限价为空/0 的剔除。
|
||
const initialLimitItems = []
|
||
let initialLimitFileName = ''
|
||
try {
|
||
// 先看显式/环境变量指定的目录,没有再回落到仓库自带的 scripts/data 目录
|
||
const candidateDirs = [...new Set([INITIAL_LIMIT_DIR, BUNDLED_INITIAL_LIMIT_DIR].filter(Boolean))]
|
||
const candidates = candidateDirs.flatMap((dir) => (
|
||
existsSync(dir)
|
||
? readdirSync(dir)
|
||
.filter((name) => /^限价期初数据.*\.xlsx$/.test(name) && !name.startsWith('~$'))
|
||
.map((name) => ({ name, dir, path: join(dir, name) }))
|
||
: []
|
||
)).sort((a, b) => statSync(b.path).mtimeMs - statSync(a.path).mtimeMs)
|
||
if (!candidates.length) {
|
||
console.log(`限价期初数据:${candidateDirs.join(' / ')} 里没有「限价期初数据*.xlsx」,跳过`)
|
||
} else {
|
||
initialLimitFileName = candidates[0].name
|
||
console.log(`限价期初数据目录:${candidates[0].dir}`)
|
||
const workbook = xlsx.readFile(candidates[0].path)
|
||
const sheetName = workbook.SheetNames[0]
|
||
const sheetRows = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { defval: '' })
|
||
const byStyle = new Map()
|
||
for (const row of sheetRows) {
|
||
const styleCode = String(row['货号'] ?? '').trim()
|
||
const price = Number(String(row['负责人限价'] ?? '').replace(/[^\d.-]/g, ''))
|
||
if (!styleCode || !Number.isFinite(price) || price <= 0) continue
|
||
const updatedAt = String(row['更新日期'] ?? '').trim()
|
||
const current = byStyle.get(styleCode)
|
||
if (!current || updatedAt >= current.updatedAt) {
|
||
byStyle.set(styleCode, {
|
||
styleCode,
|
||
spu: String(row['SPU'] ?? '').trim(),
|
||
ownerLimitPrice: price,
|
||
nonOwnerLimitPrice: String(row['非负责人限价'] ?? '').trim(),
|
||
updatedAt,
|
||
owner: String(row['限价负责人'] ?? '').trim(),
|
||
})
|
||
}
|
||
}
|
||
initialLimitItems.push(...byStyle.values())
|
||
console.log(
|
||
`限价期初数据(${initialLimitFileName}):${sheetRows.length} 行,去重后有价货号 ${initialLimitItems.length} 个`,
|
||
)
|
||
}
|
||
} catch (error) {
|
||
console.warn('限价期初数据读取失败,本次不纳入:', error.message)
|
||
}
|
||
|
||
for (const flow of flows) {
|
||
const fields = fieldsById.get(flow.processInstanceId) ?? []
|
||
const flowBase = {
|
||
流程名称: extractFlowName(flow.title),
|
||
标题: flow.title,
|
||
流程实例ID: flow.processInstanceId,
|
||
流程状态: STATUS_MAP[flow.status] ?? flow.status ?? '',
|
||
处理结果: RESULT_MAP[flow.result] ?? flow.result ?? '',
|
||
发起人: nameById.get(String(flow.originatorUserId ?? '')) ?? '',
|
||
发起部门名称: flow.originatorDeptName ?? '',
|
||
创建时间: formatDateTime(flow.createAt),
|
||
完成时间: formatDateTime(flow.finishTime),
|
||
发起人用户ID: String(flow.originatorUserId ?? ''),
|
||
业务ID: flow.businessId ?? '',
|
||
流水号: flow.businessId ?? '',
|
||
申请类型: pickField(fields, '申请类型'),
|
||
品牌: pickField(fields, '品牌'),
|
||
品牌限价单号: pickField(fields, '品牌限价单号'),
|
||
品牌运营负责人: pickField(fields, '品牌运营负责人'),
|
||
部门负责人: pickField(fields, '部门负责人'),
|
||
销售类目: pickField(fields, '销售类目'),
|
||
二级类目: pickField(fields, '二级类目'),
|
||
品类: pickField(fields, '品类'),
|
||
类目: pickField(fields, '类目'),
|
||
商品企划: pickField(fields, '商品企划'),
|
||
是否龙晨简尚共有产品: pickField(fields, '是否龙晨简尚共有产品'),
|
||
由壹点产品开发流程链接地址: pickField(fields, '由壹点产品开发流程链接地址'),
|
||
调整理由: pickField(fields, '调整理由'),
|
||
限价附件: attachmentText(pickField(fields, '限价附件')),
|
||
调整限价附件: attachmentText(pickField(fields, '调整限价附件')),
|
||
同SPU负责人沟通记录: pickField(fields, '同SPU负责人沟通记录'),
|
||
}
|
||
|
||
const flowName = extractFlowName(flow.title)
|
||
let producedInThisFlow = 0
|
||
if (flowName === '新品限价流程') {
|
||
// 数据源 A:明细取自系统「产品限价页面」(按流程链接中的 SPU 关联)。
|
||
const spu = legacyFlowSpu.get(flow.processInstanceId) ?? ''
|
||
const systemLimits = spu ? (limitRowsBySpu.get(spu) ?? []) : []
|
||
systemLimits.forEach((limit, index) => {
|
||
rows.push({
|
||
...flowBase,
|
||
来源明细表: '产品限价页面',
|
||
明细行号: index + 1,
|
||
SPU: spu,
|
||
'货号(非商品编码)': limit.skuCode ?? '',
|
||
商品编码: limit.barcode ?? '',
|
||
期望日常限价: limit.expectedDailyLimitPrice ?? '',
|
||
期望限价毛利率: limit.expectedLimitGrossMarginRate ?? '',
|
||
非SPU负责人限价: limit.nonSpuOwnerLimitPrice ?? '',
|
||
})
|
||
collected.push({
|
||
flow,
|
||
applyType: flowBase.申请类型,
|
||
spu,
|
||
styleCode: String(limit.skuCode ?? '').trim(),
|
||
barcode: String(limit.barcode ?? '').trim(),
|
||
expectedDailyLimitPrice: String(limit.expectedDailyLimitPrice ?? '').trim(),
|
||
adjustedLimitPrice: '',
|
||
startDate: '',
|
||
endDate: '',
|
||
})
|
||
producedInThisFlow += 1
|
||
})
|
||
} else {
|
||
// 数据源 B:解析钉钉流程表单内的限价明细表。
|
||
for (const field of fields) {
|
||
if (field.componentType !== 'TableField' || !field.value) continue
|
||
let table = []
|
||
try { table = JSON.parse(field.value) } catch { continue }
|
||
if (!Array.isArray(table)) continue
|
||
table.forEach((tableRow, tableIndex) => {
|
||
const cells = tableRow?.rowValue ?? []
|
||
const record = { ...flowBase, 来源明细表: field.name, 明细行号: tableIndex + 1 }
|
||
for (const cell of cells) {
|
||
// 钉钉表格里存在复合列:label 是数组(如 ["开始时间","结束时间"]),value 按位置对应。
|
||
const composite = Array.isArray(cell.label)
|
||
const labels = composite ? cell.label : [cell.label]
|
||
labels.forEach((rawLabel, labelIndex) => {
|
||
const label = String(rawLabel ?? '').trim()
|
||
if (!label || !DETAIL_COLUMNS.has(label)) return
|
||
const rawValue = composite
|
||
? (Array.isArray(cell.value) ? cell.value[labelIndex] : cell.value)
|
||
: cell.value
|
||
record[label] = normalizeCellValue(rawValue)
|
||
})
|
||
}
|
||
rows.push(record)
|
||
// 货号列命名:新品限价=「货号(非商品编码)」;各中心特殊限价 / GB特殊限价=「货号」。
|
||
// 限价列命名:新品限价及限价调整 / GB特殊限价=「期望日常限价」;各中心特殊限价=「最低售价」。
|
||
// 「调整限价明细表」的子表只填「调整后限价」(申请类型=调整原货号限价),作为兜底列一起取,
|
||
// 否则这类流程的调价会被漏掉(同一流程内同一货号不会同时出现这两列,无冲突)。
|
||
const limitPriceLabels = flowName.includes('各中心')
|
||
? ['最低售价', '期望日常限价', '调整后限价']
|
||
: ['期望日常限价', '最低售价', '调整后限价']
|
||
collected.push({
|
||
flow,
|
||
applyType: flowBase.申请类型,
|
||
spu: String(record['SPU'] ?? '').trim(),
|
||
styleCode: firstNonEmpty(record, ['货号(非商品编码)', '货号']),
|
||
barcode: String(record['商品编码'] ?? '').trim(),
|
||
expectedDailyLimitPrice: firstNonEmpty(record, limitPriceLabels),
|
||
adjustedLimitPrice: String(record['调整后限价'] || record['调整后非SPU负责人限价'] || '').trim(),
|
||
startDate: String(record['开始时间'] ?? '').trim(),
|
||
endDate: String(record['结束时间'] ?? '').trim(),
|
||
})
|
||
producedInThisFlow += 1
|
||
})
|
||
}
|
||
}
|
||
|
||
// 没有线上明细表的流程(如「新品限价流程」,限价明细在附件 Excel 中)也要保留一行,
|
||
// 与历史导出格式一致:来源明细表填附件文件名,明细列为空,避免整条流程丢失。
|
||
if (producedInThisFlow === 0) {
|
||
let attachmentName = ''
|
||
for (const name of ['限价附件', '调整限价附件']) {
|
||
const raw = pickField(fields, name)
|
||
try {
|
||
const list = JSON.parse(raw)
|
||
if (Array.isArray(list) && list[0]?.fileName) { attachmentName = String(list[0].fileName); break }
|
||
} catch {}
|
||
}
|
||
rows.push({ ...flowBase, 来源明细表: attachmentName, 明细行号: '' })
|
||
}
|
||
}
|
||
console.log(`更新流程: ${flows.length} 条, 明细行: ${rows.length} 行`)
|
||
|
||
// —— 写入数据库:货号负责人限价 + 价格授权(同价 / 活动)——
|
||
const isOverrideApplyType = (applyType) => applyType !== '申请与SPU负责人同价' && applyType !== '活动限价'
|
||
const toDateOrNull = (value) => {
|
||
const text = String(value ?? '').trim()
|
||
if (!text) return null
|
||
const normalized = text.includes('T') ? text : (text.length <= 10 ? `${text}T00:00:00Z` : text.replace(' ', 'T'))
|
||
const date = new Date(normalized)
|
||
return Number.isNaN(date.getTime()) ? null : date
|
||
}
|
||
/** 写库用的墙钟时间字符串:报表里的时间即北京时间墙钟,直存直显(不做时区换算)。 */
|
||
const toWallClockString = (value) => {
|
||
const text = String(value ?? '').trim()
|
||
if (!text) return null
|
||
const matched = text.match(/(\d{4})[-/](\d{1,2})[-/](\d{1,2})(?:[T ](\d{1,2}):(\d{2})(?::(\d{2}))?)?/)
|
||
if (!matched) return null
|
||
const pad = (part, fallback) => String(part ?? fallback).padStart(2, '0')
|
||
return `${matched[1]}-${pad(matched[2])}-${pad(matched[3])} ${pad(matched[4], '00')}:${pad(matched[5], '00')}:${pad(matched[6], '00')}`
|
||
}
|
||
const toNumberOrNull = (value) => {
|
||
const text = String(value ?? '').trim()
|
||
if (!text) return null
|
||
const parsed = Number(text.replace(/[^\d.-]/g, ''))
|
||
return Number.isFinite(parsed) ? parsed : null
|
||
}
|
||
/**
|
||
* 非负责人限价(最新口径,与《限价抬头及逻辑》一致):
|
||
* 负责人限价 × 1.05,且不低于 +10 元。历史文件/页面里的「非负责人限价」不采用。
|
||
*/
|
||
const deriveNonOwnerLimitPrice = (ownerLimitPrice) => {
|
||
const owner = toNumberOrNull(ownerLimitPrice)
|
||
if (owner === null) return ''
|
||
return (owner + Math.max(owner * 0.05, 10)).toFixed(2)
|
||
}
|
||
|
||
/** 导出用:产品限价页面已确认的限价(含是否被流程覆盖),写库那段填充。 */
|
||
let pageLimitSheetRows = []
|
||
/** 导出用:限价期初数据(含是否被流程/页面覆盖),写库那段填充。 */
|
||
let initialLimitSheetRows = []
|
||
/** 货号 → 最终生效来源(flow / page / initial),写库那段填充,导出标记用。 */
|
||
let finalLimitSourceByStyle = new Map()
|
||
|
||
let writeClient = null
|
||
try {
|
||
if (!String(process.env.DATABASE_URL ?? '').trim()) {
|
||
throw new Error('缺少 DATABASE_URL:服务器上请通过 shared/.env(systemd EnvironmentFile)注入')
|
||
}
|
||
const client = new pg.Client({ connectionString: process.env.DATABASE_URL })
|
||
writeClient = client
|
||
await client.connect()
|
||
const target = (await client.query(
|
||
`SELECT current_database() AS db, COALESCE(inet_server_addr()::text, 'local') AS host`,
|
||
)).rows[0]
|
||
console.log(`写库目标:${target.host}/${target.db}(货号限价 + 价格授权)`)
|
||
|
||
// 全量重建:每次同步生成最新镜像(两表只由本脚本维护)。
|
||
// 生产库上必须保证「要么全量成功、要么保留旧数据」,所以整段包在事务里。
|
||
await client.query('BEGIN')
|
||
|
||
// 明细没有货号时,用商品编码(条码)匹配产品档案里的货号
|
||
const missingBarcodes = [...new Set(collected.filter((item) => !item.styleCode && item.barcode).map((item) => item.barcode))]
|
||
const barcodeToStyle = new Map()
|
||
if (missingBarcodes.length) {
|
||
const archive = (await client.query(
|
||
`SELECT "barcode", "sku" FROM "SupplyChainProductArchive" WHERE "barcode" = ANY($1)`,
|
||
[missingBarcodes],
|
||
)).rows
|
||
for (const row of archive) barcodeToStyle.set(row.barcode, row.sku)
|
||
}
|
||
let matchedByBarcode = 0
|
||
for (const item of collected) {
|
||
if (item.styleCode || !item.barcode) continue
|
||
const style = barcodeToStyle.get(item.barcode)
|
||
if (style) { item.styleCode = style; matchedByBarcode += 1 }
|
||
}
|
||
|
||
// 部分模板(如 GB特殊限价)的「货号」列实际填的是商品编码:若该值不是档案里的任何货号、
|
||
// 但能匹配到条码,则统一换算为该条码对应的货号,保证限价表按货号维度对齐。
|
||
const allStyleCodes = [
|
||
...new Set([
|
||
...collected.map((item) => item.styleCode),
|
||
...initialLimitItems.map((item) => item.styleCode),
|
||
].filter(Boolean)),
|
||
]
|
||
let matchedByStyleBarcode = 0
|
||
if (allStyleCodes.length) {
|
||
const knownSkus = new Set(
|
||
(await client.query(
|
||
`SELECT "sku" FROM "SupplyChainProductArchive" WHERE "sku" = ANY($1)`,
|
||
[allStyleCodes],
|
||
)).rows.map((row) => row.sku),
|
||
)
|
||
const barcodeLikeCodes = allStyleCodes.filter((code) => !knownSkus.has(code))
|
||
if (barcodeLikeCodes.length) {
|
||
const pairs = (await client.query(
|
||
`SELECT "barcode", "sku" FROM "SupplyChainProductArchive" WHERE "barcode" = ANY($1)`,
|
||
[barcodeLikeCodes],
|
||
)).rows
|
||
const barcodeToSku = new Map(pairs.map((row) => [row.barcode, row.sku]))
|
||
for (const item of collected) {
|
||
if (!item.styleCode) continue
|
||
const sku = barcodeToSku.get(item.styleCode)
|
||
if (sku) { item.styleCode = sku; matchedByStyleBarcode += 1 }
|
||
}
|
||
for (const item of initialLimitItems) {
|
||
const sku = barcodeToSku.get(item.styleCode)
|
||
if (sku) { item.styleCode = sku; matchedByStyleBarcode += 1 }
|
||
}
|
||
}
|
||
}
|
||
|
||
const initiatorOf = (flow) => nameById.get(String(flow.originatorUserId ?? '')) ?? ''
|
||
|
||
// —— 决出每个货号的最终生效限价 ——
|
||
// 优先级:限价流程 > 产品限价页面 > 限价期初数据;
|
||
// 例外(2026-10-16 确认):期初数据的「更新日期」晚于流程完成时间时,以期初数据为准(谁新用谁)。
|
||
/** 墙钟时间字符串(北京时间口径)转毫秒,用于跨来源比较新旧。 */
|
||
const wallClockMs = (value) => {
|
||
const matched = String(value ?? '').match(/(\d{4})-(\d{1,2})-(\d{1,2})(?:[T ](\d{1,2}):(\d{2})(?::(\d{2}))?)?/)
|
||
if (!matched) return null
|
||
return Date.UTC(
|
||
Number(matched[1]), Number(matched[2]) - 1, Number(matched[3]),
|
||
Number(matched[4] ?? 0), Number(matched[5] ?? 0), Number(matched[6] ?? 0),
|
||
)
|
||
}
|
||
|
||
// 流程限价:同价 / 活动类不写负责人限价;同货号取审核时间最新的流程;价格为 0 / 空的剔除。
|
||
const limitItems = collected
|
||
.filter((item) => {
|
||
if (!item.styleCode || !isOverrideApplyType(item.applyType)) return false
|
||
const price = toNumberOrNull(item.expectedDailyLimitPrice)
|
||
return price !== null && price > 0
|
||
})
|
||
.sort((a, b) => (toDateOrNull(a.flow.finishTime)?.getTime() ?? 0) - (toDateOrNull(b.flow.finishTime)?.getTime() ?? 0))
|
||
const flowLimitStyles = new Set(limitItems.map((item) => item.styleCode))
|
||
|
||
const finalLimitByStyle = new Map()
|
||
for (const item of limitItems) {
|
||
finalLimitByStyle.set(item.styleCode, {
|
||
styleCode: item.styleCode,
|
||
ownerLimitPrice: toNumberOrNull(item.expectedDailyLimitPrice),
|
||
processInstanceId: item.flow.processInstanceId,
|
||
processNo: item.flow.businessId ?? '',
|
||
applyType: item.applyType ?? '',
|
||
initiator: initiatorOf(item.flow),
|
||
approvedAt: toWallClockString(item.flow.finishTime),
|
||
source: 'flow',
|
||
dateMs: wallClockMs(item.flow.finishTime),
|
||
})
|
||
}
|
||
for (const item of pageLimitItems) {
|
||
if (finalLimitByStyle.has(item.skuCode)) continue
|
||
finalLimitByStyle.set(item.skuCode, {
|
||
styleCode: item.skuCode,
|
||
ownerLimitPrice: toNumberOrNull(item.ownerLimitPrice),
|
||
processInstanceId: '',
|
||
processNo: '',
|
||
applyType: '产品限价页面',
|
||
initiator: '',
|
||
approvedAt: toWallClockString(item.confirmedAt),
|
||
source: 'page',
|
||
dateMs: wallClockMs(item.confirmedAt),
|
||
})
|
||
}
|
||
for (const item of initialLimitItems) {
|
||
const candidate = {
|
||
styleCode: item.styleCode,
|
||
ownerLimitPrice: item.ownerLimitPrice,
|
||
processInstanceId: '',
|
||
processNo: '',
|
||
applyType: '限价期初数据',
|
||
initiator: item.owner,
|
||
approvedAt: toWallClockString(item.updatedAt),
|
||
source: 'initial',
|
||
dateMs: wallClockMs(item.updatedAt),
|
||
}
|
||
const current = finalLimitByStyle.get(item.styleCode)
|
||
if (!current) {
|
||
finalLimitByStyle.set(item.styleCode, candidate)
|
||
continue
|
||
}
|
||
// 只有对上「流程限价」时按时间比:期初更新日期更晚 → 期初生效
|
||
if (
|
||
current.source === 'flow'
|
||
&& candidate.dateMs !== null
|
||
&& current.dateMs !== null
|
||
&& candidate.dateMs > current.dateMs
|
||
) {
|
||
finalLimitByStyle.set(item.styleCode, candidate)
|
||
}
|
||
}
|
||
// 本批限价.xlsx 的人工指定价格优先;保护范围由独立模块限定为获准的 81 个货号。
|
||
await preserveLimitFileManualPrices(client, finalLimitByStyle)
|
||
// 页面人工上传的指定限价优先级最高;每日流程同步只刷新其他来源,不能覆盖人工值。
|
||
const manualLimitRows = (await client.query(
|
||
`SELECT "styleCode", "ownerLimitPrice", "processInstanceId", "processNo",
|
||
"applyType", "initiator", "approvedAt"
|
||
FROM "SupplyChainStyleLimitPrice"
|
||
WHERE "applyType" = '人工指定限价'`,
|
||
)).rows
|
||
for (const row of manualLimitRows) {
|
||
finalLimitByStyle.set(row.styleCode, {
|
||
styleCode: row.styleCode,
|
||
ownerLimitPrice: toNumberOrNull(row.ownerLimitPrice),
|
||
processInstanceId: row.processInstanceId ?? '',
|
||
processNo: row.processNo ?? '',
|
||
applyType: '人工指定限价',
|
||
initiator: row.initiator ?? '',
|
||
approvedAt: row.approvedAt,
|
||
source: 'manual',
|
||
dateMs: row.approvedAt instanceof Date ? row.approvedAt.getTime() : null,
|
||
})
|
||
}
|
||
finalLimitSourceByStyle = new Map([...finalLimitByStyle].map(([styleCode, item]) => [styleCode, item.source]))
|
||
|
||
// 一次性写入最终生效限价(全表重建,直接 upsert 覆盖)
|
||
let limitUpserts = 0
|
||
for (const item of finalLimitByStyle.values()) {
|
||
await client.query(
|
||
`INSERT INTO "SupplyChainStyleLimitPrice"
|
||
("id","styleCode","ownerLimitPrice","processInstanceId","processNo","applyType","initiator","approvedAt","createdAt","updatedAt")
|
||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8, now(), now())
|
||
ON CONFLICT ("styleCode") DO UPDATE SET
|
||
"ownerLimitPrice" = EXCLUDED."ownerLimitPrice",
|
||
"processInstanceId" = EXCLUDED."processInstanceId",
|
||
"processNo" = EXCLUDED."processNo",
|
||
"applyType" = EXCLUDED."applyType",
|
||
"initiator" = EXCLUDED."initiator",
|
||
"approvedAt" = EXCLUDED."approvedAt",
|
||
"updatedAt" = now()`,
|
||
[
|
||
randomUUID(), item.styleCode, item.ownerLimitPrice, item.processInstanceId,
|
||
item.processNo, item.applyType, item.initiator, item.approvedAt,
|
||
],
|
||
)
|
||
limitUpserts += 1
|
||
}
|
||
|
||
// 2) 价格授权:同价申请(批准时间~结束时间)/ 活动限价(开始~结束,调整后限价)
|
||
const authItems = collected.filter(
|
||
(item) => item.styleCode && (item.applyType === '申请与SPU负责人同价' || item.applyType === '活动限价'),
|
||
)
|
||
let authUpserts = 0
|
||
for (const item of authItems) {
|
||
await client.query(
|
||
`INSERT INTO "SupplyChainLimitFlowAuth"
|
||
("id","processInstanceId","processNo","applyType","styleCode","spu","approvedAt","startDate","endDate","adjustedLimitPrice","createdAt","updatedAt")
|
||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10, now(), now())
|
||
ON CONFLICT ("processInstanceId","styleCode","applyType") DO UPDATE SET
|
||
"processNo" = EXCLUDED."processNo",
|
||
"spu" = EXCLUDED."spu",
|
||
"approvedAt" = EXCLUDED."approvedAt",
|
||
"startDate" = EXCLUDED."startDate",
|
||
"endDate" = EXCLUDED."endDate",
|
||
"adjustedLimitPrice" = EXCLUDED."adjustedLimitPrice",
|
||
"updatedAt" = now()`,
|
||
[
|
||
randomUUID(), item.flow.processInstanceId, item.flow.businessId ?? '', item.applyType, item.styleCode,
|
||
item.spu ?? '', toWallClockString(item.flow.finishTime),
|
||
toWallClockString(item.applyType === '活动限价' ? item.startDate : item.flow.finishTime),
|
||
toWallClockString(item.endDate),
|
||
item.applyType === '活动限价' ? toNumberOrNull(item.adjustedLimitPrice) : null,
|
||
],
|
||
)
|
||
authUpserts += 1
|
||
}
|
||
|
||
await client.query('COMMIT')
|
||
await client.end()
|
||
pageLimitSheetRows = pageLimitItems.map((item) => {
|
||
const source = finalLimitSourceByStyle.get(item.skuCode)
|
||
return {
|
||
货号: item.skuCode,
|
||
商品编码: item.barcode ?? '',
|
||
SPU: item.spu ?? '',
|
||
期望日常限价: item.ownerLimitPrice ?? '',
|
||
// 统一按最新口径派生,不用页面里的历史「非SPU负责人限价」
|
||
非负责人限价: deriveNonOwnerLimitPrice(item.ownerLimitPrice),
|
||
确认时间: toWallClockString(item.confirmedAt) ?? '',
|
||
限价来源: '产品限价页面',
|
||
是否被限价流程覆盖: source === 'flow'
|
||
? '是(以流程为准)'
|
||
: source === 'manual'
|
||
? '否(以本批人工指定限价为准)'
|
||
: source === 'initial'
|
||
? '否(以期初数据为准:期初更新日期更新)'
|
||
: '否(按页面限价)',
|
||
}
|
||
})
|
||
const pageBeatenByInitial = pageLimitSheetRows.filter((row) => row.是否被限价流程覆盖.startsWith('否(以期初数据为准')).length
|
||
initialLimitSheetRows = initialLimitItems.map((item) => ({
|
||
货号: item.styleCode,
|
||
SPU: item.spu,
|
||
负责人限价: item.ownerLimitPrice,
|
||
// 统一按最新口径派生,不用期初数据里的历史「非负责人限价」
|
||
非负责人限价: deriveNonOwnerLimitPrice(item.ownerLimitPrice),
|
||
更新日期: item.updatedAt,
|
||
限价负责人: item.owner,
|
||
是否被流程或页面覆盖:
|
||
finalLimitSourceByStyle.get(item.styleCode) === 'flow'
|
||
? '是(以流程为准)'
|
||
: finalLimitSourceByStyle.get(item.styleCode) === 'manual'
|
||
? '是(以本批人工指定限价为准)'
|
||
: finalLimitSourceByStyle.get(item.styleCode) === 'page'
|
||
? '是(以产品限价页面为准)'
|
||
: '否(按期初数据)',
|
||
}))
|
||
const initialWonOverFlow = initialLimitSheetRows.filter((row) => (
|
||
finalLimitSourceByStyle.get(row.货号) === 'initial'
|
||
&& flowLimitStyles.has(row.货号)
|
||
)).length
|
||
console.log(
|
||
`写库完成:货号限价 ${limitUpserts} 条(含条码匹配货号 ${matchedByBarcode} 条、货号列按商品编码换算 ${matchedByStyleBarcode} 条)`
|
||
+ `;最终生效来源:流程 ${[...finalLimitSourceByStyle.values()].filter((source) => source === 'flow').length} 个、`
|
||
+ `产品限价页面 ${[...finalLimitSourceByStyle.values()].filter((source) => source === 'page').length} 个、`
|
||
+ `限价期初数据 ${[...finalLimitSourceByStyle.values()].filter((source) => source === 'initial').length} 个、`
|
||
+ `本批人工指定限价 ${[...finalLimitSourceByStyle.values()].filter((source) => source === 'manual').length} 个`
|
||
+ `(其中 ${initialWonOverFlow} 个是「期初更新日期比流程新」赢下的、${pageBeatenByInitial} 个页面价被更新的期初价覆盖)`
|
||
+ `,价格授权 ${authUpserts} 条`,
|
||
)
|
||
} catch (error) {
|
||
try { await writeClient?.query('ROLLBACK') } catch {}
|
||
try { await writeClient?.end() } catch {}
|
||
console.warn('写库失败(已回滚,正式库保留旧数据;Excel 导出继续):', error.message)
|
||
}
|
||
|
||
if (SKIP_EXCEL) {
|
||
console.log('已按 LIMIT_SYNC_SKIP_EXCEL 跳过 Excel 导出')
|
||
await pool.close()
|
||
process.exit(0)
|
||
}
|
||
|
||
const workbook = XLSX.utils.book_new()
|
||
const sheet = XLSX.utils.json_to_sheet(rows, { header: COLUMNS })
|
||
XLSX.utils.book_append_sheet(workbook, sheet, '限价流程明细')
|
||
|
||
// 第二个页签:产品限价页面已确认的限价(没有限价流程的货号靠它兜底)
|
||
if (pageLimitSheetRows.length) {
|
||
const pageSheet = XLSX.utils.json_to_sheet(pageLimitSheetRows, {
|
||
header: ['货号', '商品编码', 'SPU', '期望日常限价', '非负责人限价', '确认时间', '限价来源', '是否被限价流程覆盖'],
|
||
})
|
||
XLSX.utils.book_append_sheet(workbook, pageSheet, '产品限价页面限价')
|
||
}
|
||
|
||
// 第三个页签:限价期初数据(存量限价兜底)
|
||
if (initialLimitSheetRows.length) {
|
||
const initialSheet = XLSX.utils.json_to_sheet(initialLimitSheetRows, {
|
||
header: ['货号', 'SPU', '负责人限价', '非负责人限价', '更新日期', '限价负责人', '是否被流程或页面覆盖'],
|
||
})
|
||
XLSX.utils.book_append_sheet(workbook, initialSheet, '限价期初数据')
|
||
}
|
||
|
||
// 固定文件名覆盖式输出:旧文件先归档到「历史版本」,保证导出目录里永远只有一份最新全量表。
|
||
mkdirSync(OUT_DIR, { recursive: true })
|
||
if (existsSync(OUT_FILE)) {
|
||
mkdirSync(HISTORY_DIR, { recursive: true })
|
||
const now = new Date()
|
||
const archiveStamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}-${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}`
|
||
renameSync(OUT_FILE, `${HISTORY_DIR}/限价流程_完整明细表_归档_${archiveStamp}.xlsx`)
|
||
// 只保留最近 N 份归档,避免服务器上每天 28MB 无限累积
|
||
const archives = readdirSync(HISTORY_DIR)
|
||
.filter((name) => /^限价流程_完整明细表_归档_.*\.xlsx$/.test(name))
|
||
.map((name) => ({ name, path: join(HISTORY_DIR, name), mtime: statSync(join(HISTORY_DIR, name)).mtimeMs }))
|
||
.sort((a, b) => b.mtime - a.mtime)
|
||
for (const stale of archives.slice(HISTORY_KEEP)) {
|
||
unlinkSync(stale.path)
|
||
}
|
||
}
|
||
XLSX.writeFile(workbook, OUT_FILE)
|
||
console.log(`已导出: ${OUT_FILE}`)
|
||
console.log(`历史版本保留:${HISTORY_KEEP} 份`)
|
||
|
||
await pool.close()
|