// 计算变更令,正负变更数 const defaultInfo = require('../app/const/tender_info'); const BaseUtil = require('./baseUtils'); const querySql = BaseUtil.querySql; const ZhCalc = BaseUtil.ZhCalc; const audit = require('../app/const/audit'); const checkChange = async function(change, decimal) { const changeBills = await querySql('Select * From zh_change_audit_list where cid = ?', [change.cid]); let p_tp = 0, n_tp = 0; for (const cb of changeBills) { cb.tp = ZhCalc.mul(cb.spamount, cb.unit_price, change.tp_decimal || decimal.tp); if (cb.spamount > 0) { p_tp = ZhCalc.add(p_tp, cb.tp); } else if (cb.spamount < 0){ n_tp = ZhCalc.add(n_tp, cb.tp); } } await querySql('Update zh_change Set positive_tp = ?, negative_tp = ? Where cid = ?', [p_tp, n_tp, change.cid]); console.log(`Update Change ${change.cid}: p_tp(${p_tp}), n_tp(${n_tp})`); }; const checkStage = async function(stage, decimal, preStage) { let stageChange; if (stage.status === 3) { stageChange = await querySql('Select scf.*, cal.unit_price From zh_stage_change_final scf Left Join zh_change_audit_list cal ON scf.cbid = cal.id where sid = ?', [stage.id]); } else { const stageChangeAll = await querySql('SELECT sc.*, cal.unit_price FROM zh_stage_change sc Left Join zh_change_audit_list cal ON sc.cbid = cal.id WHERE sid = ?', [stage.id]); stageChange = BaseUtil.filterLastestData(stageChangeAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder'); } if (stageChange.length === 0) return; stage.positive_qc_tp = 0; stage.pre_positive_qc_tp = preStage ? ZhCalc.add(stage.positive_qc_tp, stage.pre_positive_qc_tp) : 0; stage.negative_qc_tp = 0; stage.pre_negative_qc_tp = preStage ? ZhCalc.add(stage.negative_qc_tp, stage.pre_negative_qc_tp) : 0; for (const sc of stageChange) { if (sc.no_value || !sc.qty) continue; const tp = ZhCalc.mul(sc.unit_price, sc.qty, decimal.tp); if (sc.minus) { stage.negative_qc_tp = ZhCalc.add(tp, stage.negative_qc_tp); } else { stage.positive_qc_tp = ZhCalc.add(tp, stage.positive_qc_tp); } } await querySql('Update zh_stage Set positive_qc_tp = ?, pre_positive_qc_tp = ?, negative_qc_tp = ?, pre_negative_qc_tp = ? Where id = ?', [stage.positive_qc_tp, stage.pre_positive_qc_tp, stage.negative_qc_tp, stage.pre_negative_qc_tp, stage.id]); console.log(`Update Stage ${stage.order}: p_tp(${stage.positive_qc_tp}), pre_p_tp(${stage.pre_positive_qc_tp}), n_tp(${stage.negative_qc_tp}), pre_n_tp(${stage.pre_negative_qc_tp})`); }; const doComplete = async function() { try { const tender = await querySql('Select * From zh_tender'); for (const t of tender) { console.log(`Update Tender ${t.id}:`); const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]); const decimal = info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.parseInfo.decimal; const changes = await querySql('Select * From zh_change where tid = ?', [t.id]); for (const c of changes) { await checkChange(c, decimal); } const stage = await querySql('Select * From zh_stage where tid = ? order by `order` asc', [t.id]); for (const [i, s] of stage.entries()) { await checkStage(s, decimal, i > 1 ? stage[i-1] : null); } } } catch (err) { console.log(err); } BaseUtil.closePool(); }; doComplete();