const BaseUtil = require('./baseUtils'); const querySql = BaseUtil.querySql; const change_type = { changeProject: { table_name: 'zh_change_project', audit_table_name: 'zh_change_project_audit', tb_id: 'cpid', }, changeApply: { table_name: 'zh_change_apply', audit_table_name: 'zh_change_apply_audit', tb_id: 'caid', }, changePlan: { table_name: 'zh_change_plan', audit_table_name: 'zh_change_plan_audit', tb_id: 'cpid', }, }; const errorChangeTimes = []; const updateChangeTimes = async function(change, times, type) { const auditData = await querySql('SELECT * FROM ?? WHERE ' + change_type[type].tb_id + ' = ? AND times = ? ORDER BY `order`', [change_type[type].audit_table_name, change.id, times]); if (auditData.length === 0) return; const auditIds = [], filterIds = []; for (const ad of auditData) { if (ad.audit_order) { if (filterIds.indexOf(ad.aid) < 0) filterIds.push(ad.aid); } else { const auditId = auditIds.find(x => { return x.aid === ad.aid; }); if (auditId) { auditId.ids.push(ad.id); } else { auditIds.push({ aid: ad.aid, ids: [ad.id] }); } } } if (auditIds.length === 0) return; if (filterIds.length > 0 && auditIds.length > 0) { const object = { tid: change.tid, times }; object[change_type[type].tb_id] = change.id; errorChangeTimes.push(object); } for (const [i, aid] of auditIds.entries()) { for (const id of aid.ids) { await querySql('UPDATE ?? SET audit_order = ? WHERE id = ?', [change_type[type].audit_table_name, i + 1, id]); } } }; const updateChange = async function(change, type) { for (let i = 1; i <= change.times; i++) { await updateChangeTimes(change, i, type); } }; const doCompleteTest = async function(id) { try { const tender = await querySql('Select * From zh_tender where id > ?', [id]); for (const t of tender) { console.log(`Update Tender ${t.name}(${t.id}):`); for (const type in change_type) { const changes = await querySql('Select * From ?? where tid = ?', [change_type[type].table_name, t.id]); for (const c of changes) { await updateChange(c, type); } } } } catch (err) { console.log(err); } BaseUtil.closePool(); if (errorChangeTimes.length > 0) { console.log('ERROR change info: '); console.log(errorChangeTimes); } }; const doComplete = async function() { try { const tenders = await querySql('Select * From zh_tender'); for (const t of tenders) { console.log(`Update Tender ${t.name}(${t.id}):`); for (const type in change_type) { const changes = await querySql('Select * From ?? where tid = ?', [change_type[type].table_name, t.id]); for (const c of changes) { await updateChange(c, type); } } } } catch (err) { console.log(err); } BaseUtil.closePool(); if (errorChangeTimes.length > 0) { console.log('ERROR change info: '); console.log(errorChangeTimes); } }; const tenderId = process.argv[3]; if (tenderId) { doCompleteTest(tenderId); } else { doComplete(); }