| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | 
							- const BaseUtil = require('./baseUtils');
 
- const querySql = BaseUtil.querySql;
 
- const sp_status = {
 
-     sqspr: 1, // 授权审批人
 
-     gdspl: 2, // 固定审批流
 
-     gdzs: 3, // 固定终审
 
- };
 
- // 审批类型
 
- const sp_type = {
 
-     advance: 1,
 
-     ledger: 2,
 
-     revise: 3,
 
-     stage: 4,
 
-     change: 5,
 
-     material: 6,
 
- };
 
- const errorStageTimes = [];
 
- const updateShenpiAudit = async function (tender, type) {
 
-     const shenpiData = await querySql('Select * From zh_shenpi_audit where tid = ? AND sp_type = ? AND sp_status = ? ORDER BY id', [tender.id, type, sp_status.gdspl]);
 
-     if (shenpiData.length === 0) return;
 
-     for (const [i, spd] of shenpiData.entries()) {
 
-         if (spd.audit_order > 0) continue;
 
-         await querySql('UPDATE zh_shenpi_audit SET audit_order = ? WHERE id = ?', [i+1, spd.id]);
 
-     }
 
- };
 
- const updateShenpi = async function (tender) {
 
-     updateShenpiAudit(tender, sp_type.advance);
 
-     updateShenpiAudit(tender, sp_type.ledger);
 
-     updateShenpiAudit(tender, sp_type.revise);
 
-     updateShenpiAudit(tender, sp_type.stage);
 
-     updateShenpiAudit(tender, sp_type.change);
 
-     updateShenpiAudit(tender, sp_type.material);
 
- };
 
- const updateStageTimes = async function (stage, times) {
 
-     const auditData = await querySql('SELECT * FROM zh_stage_audit WHERE sid = ? AND times = ? ORDER BY `order`', [stage.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: stage.tid, sid: stage.id, times });
 
-     for (const [i, aid] of auditIds.entries()) {
 
-         for (const id of aid.ids) {
 
-             await querySql('UPDATE zh_stage_audit SET audit_order = ? WHERE id = ?', [i+1, id]);
 
-         }
 
-     }
 
- };
 
- const updateStage = async function (stage) {
 
-     for (let i = 1; i <= stage.times; i++) {
 
-         await updateStageTimes(stage, 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}):`);
 
-             await updateShenpi(t);
 
-             const stages = await querySql('Select * From zh_stage where tid = ?', [t.id]);
 
-             for (const s of stages) {
 
-                 await updateStage(s);
 
-             }
 
-         }
 
-     } catch (err) {
 
-         console.log(err);
 
-     }
 
-     BaseUtil.closePool();
 
-     if (errorStageTimes.length > 0) {
 
-         console.log('ERROR stage 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}):`);
 
-             await updateShenpi(t);
 
-             const stages = await querySql('Select * From zh_stage where tid = ?', [t.id]);
 
-             for (const s of stages) {
 
-                 await updateStage(s);
 
-             }
 
-         }
 
-     } catch (err) {
 
-         console.log(err);
 
-     }
 
-     BaseUtil.closePool();
 
-     if (errorStageTimes.length > 0) {
 
-         console.log('ERROR stage info: ');
 
-         console.log(errorStageTimes);
 
-     }
 
- };
 
- const tenderId = process.argv[3];
 
- if (tenderId) {
 
-     doCompleteTest(tenderId);
 
- } else {
 
-     doComplete()
 
- }
 
 
  |