优化:名录编码自动生成并隐藏维护入口

This commit is contained in:
2026-11-03 13:11:00 +08:00
parent 52d92c95d8
commit 1aeccddd4d
8 changed files with 126 additions and 36 deletions
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { configurationTables, importConfigurationTables, createConfigurationWorkbook, readConfigurationWorkbook } from '../src/configurationWorkbook.ts';
import { configurationPreviewTable, configurationTables, importConfigurationTables, createConfigurationWorkbook, readConfigurationWorkbook } from '../src/configurationWorkbook.ts';
const rule={onlyUnclassified:true,id:'rule_001',name:'指定分类',priority:-9900,stage:'all',enabled:true,target:'group',value:'G',conditions:[{field:'PDT代码',operator:'EQUALS',value:'00001'},{field:'销售片区',operator:'IS_BLANK',value:''}]};
const base={schemaVersion:3,unknown:{retain:[1,null]},masterData:{departments:[{code:'0001',name:'A',enabled:true}],groups:[{code:'002',name:'G',enabled:true,department:'A',unknown:'keep'}],products:[],projectGroups:[]},
@@ -11,8 +11,44 @@ test('all configuration sheets round-trip through a real xlsx with codes, nulls,
const tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());
for(const mode of ['MERGE','REPLACE'])assert.deepEqual(importConfigurationTables(source,tables,'all',mode).definition,source);
assert.equal(tables.find(t=>t.name==='产业部名录').rows[0][0],'0001');
assert.equal(workbook.getWorksheet('产业部名录').getColumn(1).hidden,true);
assert.deepEqual(configurationPreviewTable(tables.find(t=>t.name==='产业部名录')).headers,['名称','状态']);
assert.deepEqual(source,base);assert.equal(workbook.getWorksheet('产业部名录').getCell('A2').numFmt,'@');
});
test('all four catalogs accept name-only files and preserve existing identity, metadata and ownership',()=>{
for(const kind of ['departments','groups','products','projectGroups']){
const source={masterData:{[kind]:[{code:'existing',name:'原名称',enabled:false,department:'所属产业部',extra:'keep'}]}};
const table=configurationTables(source,kind)[0];
table.headers.shift();table.rows=kind==='groups'?[['原名称','启用','所属产业部'],['新增名称','启用','']]:[['原名称','启用'],['新增名称','启用']];
const result=importConfigurationTables(source,[table],kind,'MERGE');const rows=result.definition.masterData[kind];
assert.equal(rows[0].code,'existing');assert.equal(rows[0].extra,'keep');assert.equal(rows[0].department,'所属产业部');assert.equal(rows[0].enabled,true);
assert.match(rows[1].code,/^REF-/);assert.notEqual(rows[0].code,rows[1].code);
assert.equal(result.changes[0].added,1);assert.equal(result.changes[0].updated,1);
assert.deepEqual(importConfigurationTables(result.definition,[table],kind,'MERGE').definition,result.definition);
}
});
test('hidden identity lets an exported row be renamed, and new rows need only a name',async()=>{
const book=await createConfigurationWorkbook(base,'groups');const sheet=book.getWorksheet('业务分组名录');
sheet.getCell('B2').value='改名分组';sheet.addRow(['','新分组','启用','A']);
const tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());
const result=importConfigurationTables(base,tables,'groups','MERGE').definition.masterData.groups;
assert.equal(result.length,2);assert.equal(result[0].code,'002');assert.equal(result[0].name,'改名分组');assert.equal(result[0].unknown,'keep');
assert.match(result[1].code,/^REF-/);assert.equal(result[1].department,'A');
assert.deepEqual(configurationPreviewTable(tables[0]).rows.map(row=>row[0]),['改名分组','新分组']);
});
test('catalogs accept only the required name column without losing state or group ownership',()=>{
const source={masterData:{groups:[{code:'kept',name:'G',enabled:false,department:'A',extra:'keep'}]}};
const table={name:'业务分组名录',headers:['名称'],rows:[['G'],['新组']]};
const result=importConfigurationTables(source,[table],'groups','MERGE').definition;
assert.deepEqual(result.masterData.groups[0],source.masterData.groups[0]);assert.match(result.masterData.groups[1].code,/^REF-/);
assert.equal(result.masterData.groups[1].enabled,true);
const reordered={name:'业务分组名录',headers:['名称','编码'],rows:[['G','kept']]};
assert.deepEqual(configurationPreviewTable(reordered),{name:'业务分组名录',headers:['名称'],rows:[['G']]});
assert.deepEqual(importConfigurationTables(source,[reordered],'groups','MERGE').definition,source);
});
test('edits upsert by source key, and replace removes only records in supplied tables',()=>{
const tables=configurationTables(base,'categoryLookup');tables[0].rows[0][2]='新分组';
const imported=importConfigurationTables(base,tables,'categoryLookup','MERGE');