const BaseUtil = require('./baseUtils'); const querySql = BaseUtil.querySql; const errorStageTimes = []; const updateMaterialTimes = async function(material, times) { const auditData = await querySql('SELECT * FROM zh_material_audit WHERE mid = ? AND times = ? ORDER BY `order`', [material.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) errorStageTimes.push({ tid: material.tid, mid: material.id, times }); for (const [i, aid] of auditIds.entries()) { for (const id of aid.ids) { await querySql('UPDATE zh_material_audit SET audit_order = ? WHERE id = ?', [i+1, id]); } } }; const updateMaterial = async function (material) { for (let i = 1; i <= material.times; i++) { await updateMaterialTimes(material, i); } }; 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}):`); const materials = await querySql('Select * From zh_material where tid = ?', [t.id]); for (const s of materials) { await updateMaterial(s); } } } catch (err) { console.log(err); } BaseUtil.closePool(); if (errorStageTimes.length > 0) { console.log('ERROR material info: '); console.log(errorStageTimes); } }; 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}):`); const materials = await querySql('Select * From zh_material where tid = ?', [t.id]); for (const s of materials) { await updateMaterial(s); } } } catch (err) { console.log(err); } BaseUtil.closePool(); if (errorStageTimes.length > 0) { console.log('ERROR material info: '); console.log(errorStageTimes); } }; const tenderId = process.argv[3]; if (tenderId) { doCompleteTest(tenderId); } else { doComplete(); }