/* 修改脚本,请现在指定项目测试 例如: node db_script/sub_project local T201711273363 没有意外再全部执行 所有修改均应考虑脚本二次执行时的兼容,应检查是否已执行过,避免在生产环境运行时出现问题后需要二次执行,参见自定义分类脚本 */ const BaseUtil = require('./baseUtils'); const querySql = BaseUtil.querySql; const _ = require('lodash'); const ZhCalc = BaseUtil.ZhCalc; const calcPreHbTP = async function(subProject) { const ftList = await querySql('SELECT * FROM zh_financial_transfer where spid = ? ORDER BY id DESC', [subProject.id]); if (ftList.length > 0) { const updateFtList = []; const updateFttList = []; const fttList = await querySql('SELECT * FROM zh_financial_transfer_tender where spid = ? ORDER BY trid DESC, id ASC', [subProject.id]); if (fttList.length > 0) { for (const ftt of fttList) { const ftts = _.filter(fttList, function(item) { return item.tid === ftt.tid && item.trid < ftt.trid; }); let pre_hb_tp = 0; if (ftts.length > 0) { pre_hb_tp = ZhCalc.sum(_.map(ftts, 'hb_tp')); } if (pre_hb_tp !== ftt.pre_hb_tp) { ftt.pre_hb_tp = pre_hb_tp; updateFttList.push({ id: ftt.id, pre_hb_tp }); } } for (const ft of ftList) { const fts = _.filter(ftList, function(item) { return item.tid === ft.tid && item.id < ft.id; }); let pre_hb_tp = 0; if (fts.length > 0) { pre_hb_tp = ZhCalc.sum(_.map(fts, 'total_price')); } if (pre_hb_tp !== ft.pre_hb_tp) { ft.pre_hb_tp = pre_hb_tp; updateFtList.push({ id: ft.id, pre_hb_tp }); } } } if (updateFtList.length > 0) { const sql = 'UPDATE zh_financial_transfer SET pre_hb_tp = ? WHERE id = ?'; for (const ft of updateFtList) { await querySql(sql, [ft.pre_hb_tp, ft.id]); } } if (updateFttList.length > 0) { const sql = 'UPDATE zh_financial_transfer_tender SET pre_hb_tp = ? WHERE id = ?'; for (const ftt of updateFttList) { await querySql(sql, [ftt.pre_hb_tp, ftt.id]); } } } }; const doComplete = async function(code) { try { const filter = code ? ` where code = '${code}'` : ''; const project = await querySql('Select * From zh_project' + filter); for (const p of project) { console.log(`Update Project ${p.code}(${p.id}):`); const subProj = await querySql('SELECT * FROM zh_sub_project where project_id = ? and is_folder = 0 and is_delete = 0;', [p.id]); for (const sp of subProj) { await calcPreHbTP(sp); } console.log('END Update;'); } } catch (err) { console.log(err); } BaseUtil.closePool(); }; const projectCode = process.argv[3]; doComplete(projectCode);