// 计算变更令,计价不计价金额数 const defaultInfo = require('../app/const/tender_info'); const BaseUtil = require('./baseUtils'); const _ = require('lodash'); const querySql = BaseUtil.querySql; const ZhCalc = BaseUtil.ZhCalc; const checkChange = async function(change, decimal, precision = defaultInfo.defaultInfo.precision) { const sql = 'Select * From zh_change_audit_list where cid = ?'; const sqlParam = [change.cid]; const changeBills = await querySql(sql, sqlParam); const unitPriceUnit = change.up_decimal || decimal.up; const totalPriceUnit = change.tp_decimal || decimal.tp; let positive_tp = 0; let negative_tp = 0; let valuation_tp = 0; let unvaluation_tp = 0; let new_tp = 0; // 先汇总再统计金额 const gclChangeList = _.uniq(_.map(changeBills, 'gcl_id')); for (const g of gclChangeList) { if (g) { const list = _.filter(changeBills, { gcl_id: g }); let spamount = 0; let positive_amount = 0; let negative_amount = 0; let valuation_amount = 0; let unvaluation_amount = 0; let unitPrice = 0; for (const c of list) { if (c.spamount) { const amount = ZhCalc.round(c.spamount, findDecimal(c.unit, precision)); spamount = ZhCalc.add(spamount, amount); if (amount >= 0) { positive_amount = ZhCalc.add(positive_amount, amount); } else { negative_amount = ZhCalc.add(negative_amount, amount); } unitPrice = c.unit_price; if (c.is_valuation) { valuation_amount = ZhCalc.add(valuation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision))); } else { unvaluation_amount = ZhCalc.add(unvaluation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision))); } } } const price = ZhCalc.round(ZhCalc.mul(spamount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit); // 正负数量分别汇总后计算金额,避免相互抵消。 const positive_price = ZhCalc.round(ZhCalc.mul(positive_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0; const negative_price = ZhCalc.round(ZhCalc.mul(negative_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0; positive_tp = ZhCalc.add(positive_tp, positive_price); negative_tp = ZhCalc.add(negative_tp, negative_price); const valuation_price = ZhCalc.round(ZhCalc.mul(valuation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0; const unvaluation_price = ZhCalc.round(ZhCalc.mul(unvaluation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0; valuation_tp = ZhCalc.add(valuation_tp, valuation_price); unvaluation_tp = ZhCalc.add(unvaluation_tp, unvaluation_price); new_tp = ZhCalc.add(new_tp, price); } else { const list = _.filter(changeBills, { gcl_id: g }); for (const c of list) { if (c.spamount) { const price = ZhCalc.round(ZhCalc.mul(ZhCalc.round(c.spamount, findDecimal(c.unit, precision)), ZhCalc.round(c.unit_price, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0; new_tp = ZhCalc.add(new_tp, price); if (price >= 0) { positive_tp = ZhCalc.add(positive_tp, price); } else { negative_tp = ZhCalc.add(negative_tp, price); } if (c.is_valuation) { valuation_tp = ZhCalc.add(valuation_tp, price); } else { unvaluation_tp = ZhCalc.add(unvaluation_tp, price); } } } } } const updateTpList = {}; let updateFlag = false; if (positive_tp !== change.positive_tp) { updateTpList.positive_tp = positive_tp; updateFlag = true; } if (negative_tp !== change.negative_tp) { updateTpList.negative_tp = negative_tp; updateFlag = true; } if (valuation_tp !== change.valuation_tp) { updateTpList.valuation_tp = valuation_tp; updateFlag = true; } if (unvaluation_tp !== change.unvaluation_tp) { updateTpList.unvaluation_tp = unvaluation_tp; updateFlag = true; } if (updateFlag) { const sql = 'Update zh_change Set ? Where cid = ?'; const sqlParam = [updateTpList, change.cid]; await querySql(sql, sqlParam); console.log(`Update Change ${change.cid}`); } }; 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.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal; const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision; const changes = await querySql('Select * From zh_change where tid = ?', [t.id]); for (const c of changes) { await checkChange(c, decimal, precision); } } } catch (err) { console.log(err); } BaseUtil.closePool(); }; const doCompleteTest = async function(tid) { try { const tender = await querySql('Select * From zh_tender where id = ?', [tid]); 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.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal; const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision; const changes = await querySql('Select * From zh_change where tid = ?', [t.id]); for (const c of changes) { await checkChange(c, decimal, precision); } } } catch (err) { console.log(err); } BaseUtil.closePool(); }; const tenderId = process.argv[3]; if (tenderId) { doCompleteTest(tenderId); } else { doComplete(); } function findDecimal(unit, precision) { let value = precision.other.value; for (const d in precision) { if (precision[d].unit !== undefined && precision[d].unit === unit) { value = precision[d].value; break; } } return value; }