/** * 将 jqay.txt 转为警情案由层级字典 JSON(供后端导入 t_dict_data) * jqay.txt 可能含多个 JSON 数组拼接,自动合并去重 * 用法: node scripts/convert-jqay-to-dict.js */ const fs = require('fs'); const path = require('path'); const root = path.resolve(__dirname, '..'); const sourcePath = path.join(root, 'jqay.txt'); const outputPath = path.join( root, 'economic-crime-api/economic-crime-admin/src/main/resources/business/alarm/alarm-case-reason-dict.json', ); function parseJqayContent(text) { const trimmed = text.trim(); if (trimmed.startsWith('[')) { try { return JSON.parse(trimmed); } catch (e) { // 多个数组拼接: ][ } } const chunks = trimmed.split(/\]\s*\[/); const merged = []; chunks.forEach((chunk, index) => { let jsonText = chunk; if (index === 0) { jsonText = chunk + ']'; } else if (index === chunks.length - 1) { jsonText = '[' + chunk; } else { jsonText = '[' + chunk + ']'; } merged.push(...JSON.parse(jsonText)); }); return merged; } const raw = parseJqayContent(fs.readFileSync(sourcePath, 'utf8')); const byHandle = Object.fromEntries(raw.map((item) => [item.handleId, item])); function resolveParentCode(item) { const pid = item.pId; if (!pid || pid === 'o_') { return ''; } const parent = byHandle[pid]; return parent && parent.code ? parent.code : ''; } const seen = new Set(); const unique = []; raw.forEach((item, index) => { if (!item.code || !item.name || seen.has(item.code)) { return; } seen.add(item.code); unique.push({ code: item.code, label: item.name, parentCode: resolveParentCode(item), level: Number(item.organLevel) || 0, sortOrder: raw.length - index, }); }); fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync(outputPath, JSON.stringify(unique)); process.stdout.write(`OK raw=${raw.length} unique=${unique.length} -> ${outputPath}\n`);