cmvr-wms/scripts/projectdev/initialize-item-categories.cjs

31 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

// Explicit development-only dictionary setup. Product and purchase rows are never rewritten.
const fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict')
const {username,password,backend}=require('./vue3-test-runtime.cjs')
const target=new URL(backend);assert(['127.0.0.1','localhost'].includes(target.hostname)&&target.port==='8080','Only the local development backend is allowed')
const taxonomy=require('./item-category-taxonomy.json')
;(async()=>{
const directory=path.resolve('.codex-tmp/item-category-20261101');fs.mkdirSync(directory,{recursive:true})
const login=await fetch(backend+'/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({username,password})}).then(r=>r.json());assert(login.token,'Development login failed')
const call=async(url,method='GET',body)=>{const r=await fetch(backend+url,{method,headers:{Authorization:'Bearer '+login.token,'Content-Type':'application/json'},...(body?{body:JSON.stringify(body)}:{})}).then(r=>r.json());assert.equal(r.code,200,r.msg);return r}
const list=async type=>(await call('/system/dict/data/list?dictType='+type+'&pageSize=100&pageNum=1')).rows
const [categories,groups,products]=await Promise.all([list('wms_item_category'),list('wms_item_category_group'),call('/warehouse/product/optionselect?pageSize=2000')])
const backup=path.join(directory,'dictionary-before-setup.json');if(!fs.existsSync(backup))fs.writeFileSync(backup,JSON.stringify({categories,groups,products:products.data},null,2))
let createdGroups=0,createdCategories=0,updatedCategories=0
for(const [order,group] of taxonomy.entries()){
let parent=groups.find(row=>row.dictLabel===group.name)
if(!parent){await call('/system/dict/data','POST',{dictType:'wms_item_category_group',dictLabel:group.name,dictValue:group.name,dictSort:(order+1)*10,status:'0',isDefault:'N',remark:group.description});parent=(await list('wms_item_category_group')).find(row=>row.dictLabel===group.name);assert(parent);groups.push(parent);createdGroups++}
for(const [index,category] of group.categories.entries()){
const old=categories.find(row=>row.dictLabel===category.name),payload={...old,dictType:'wms_item_category',dictLabel:category.name,dictValue:old?.dictValue||category.name,parentCode:parent.dictCode,dictSort:(index+1)*10,status:old?.status||'0',isDefault:old?.isDefault||'N',remark:category.description}
if(old){if(old.parentCode!==payload.parentCode||old.remark!==payload.remark||old.dictSort!==payload.dictSort){await call('/system/dict/data','PUT',payload);updatedCategories++}}
else{await call('/system/dict/data','POST',payload);createdCategories++}
}
}
for(const group of groups){if(group.status==='0'&&!taxonomy.some(row=>row.name===group.dictLabel)){const leaves=(await list('wms_item_category')).filter(row=>row.parentCode===group.dictCode);if(!leaves.length)await call('/system/dict/data','PUT',{...group,status:'1',remark:(group.remark||'')+'(分类整理后停用,历史记录保留)'})}}
const afterProducts=(await call('/warehouse/product/optionselect?pageSize=2000')).data
assert.deepEqual(afterProducts.map(r=>[r.id,r.category]).sort(),products.data.map(r=>[r.id,r.category]).sort(),'Existing product classifications changed')
const options=(await call('/warehouse/product/category-options')).data
assert(options.every(row=>row.parentId&&row.parentLabel),'Ungrouped active category remains')
const result={developmentOnly:true,createdGroups,createdCategories,updatedCategories,groups:new Set(options.map(row=>row.parentId)).size,categories:options.length,productsUnchanged:true}
fs.writeFileSync(path.join(directory,'taxonomy-result.json'),JSON.stringify({result,options},null,2));console.log(JSON.stringify(result))
})().catch(e=>{console.error(e.message);process.exitCode=1})