Просмотр исходного кода

1. 用户项目修复脚本
2. 三方推送,通用消息

MaiXinRong 2 лет назад
Родитель
Сommit
5289231157

+ 46 - 0
app/const/spec_3f.js

@@ -0,0 +1,46 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const pushTiming = [
+    { value:'ledger.checked', name: '台账-审批通过' },
+    { value:'stage.checked', name: '期-审批通过' },
+    { value:'stage.flow', name: '期-上报/审批' },
+    { value:'revise.checked', name: '台账修订-审批通过' },
+];
+
+const pushOperate = (function () {
+    const result = {};
+    for (const pt of pushTiming) {
+        const path = pt.value.split('.');
+        if (!result[path[0]]) result[path[0]] = {};
+        result[path[0]][path[1]] = pt.value;
+    }
+    return result;
+})();
+
+const specMsgStatus = (function () {
+    const status = { wait: 0, exec: 1, done: 2 };
+    const name = [];
+    name[status.wait] = '等待执行';
+    name[status.exec] = '等待中';
+    name[status.done] = '执行完成';
+    return { status, name };
+})();
+
+const pullClass = [
+    { value: 'gs-da', name: '甘肃档案' },
+];
+const pullType = [
+    { value: 'sync-ledger', name: '同步台账' },
+];
+
+module.exports = {
+    pushTiming, pushOperate, specMsgStatus, pullClass, pullType,
+};

+ 3 - 0
app/service/ledger_audit.js

@@ -15,6 +15,7 @@ const SmsAliConst = require('../const/sms_alitemplate');
 const wxConst = require('../const/wechat_template');
 const shenpiConst = require('../const/shenpi');
 const pushType = require('../const/audit').pushType;
+const pushOperate = require('../const/spec_3f').pushOperate;
 
 module.exports = app => {
     class LedgerAudit extends app.BaseService {
@@ -390,6 +391,8 @@ module.exports = app => {
                             begin_time: Date.parse(begin_audit.begin_time),
                         };
                         await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
+                        // 审批通过 - 检查三方特殊推送
+                        await this.ctx.service.specMsg.addLedgerMsg(transaction, pid, this.ctx.tender, pushOperate.ledger.checked);
                     }
                 } else {
                     // 同步标段信息

+ 3 - 0
app/service/revise_audit.js

@@ -15,6 +15,7 @@ const wxConst = require('../const/wechat_template');
 const shenpiConst = require('../const/shenpi');
 const pushType = require('../const/audit').pushType;
 const RevisePrice = require('../lib/revise_price');
+const pushOperate = require('../const/spec_3f').pushOperate;
 
 module.exports = app => {
     class ReviseAudit extends app.BaseService {
@@ -493,6 +494,8 @@ module.exports = app => {
                             code: this.ctx.session.sessionProject.code,
                         };
                         await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
+                        // 审批通过 - 检查三方特殊推送
+                        await this.ctx.service.specMsg.addReviseMsg(transaction, pid, revise, pushOperate.ledger.checked);
                     }
                 } else {
                     // 同步修订信息

+ 58 - 0
app/service/spec_msg.js

@@ -0,0 +1,58 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+module.exports = app => {
+    class SpecPull extends app.BaseService {
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 's2b_spec_msg';
+        }
+
+        async tenderNeedMsg(pid, tid) {
+            const specProj = await this.db.get('zh_s2b_spec_proj', { id: pid });
+            if (!specProj || !specProj.is_push) return false;
+            switch (specProj.push_tender_type) {
+                case 0: return true;
+                case 1:
+                    const filter = specProj.filter_tender ? specProj.filter_tender.split(',') : [];
+                    return filter.indexOf(tid + '') < 0;
+                case 2:
+                    const access = specProj.access_tender ? specProj.access_tender.split(',') : [];
+                    return access.indexOf(tid + '') >= 0;
+            }
+        }
+
+        async addLedgerMsg(transaction, pid, tender, timing) {
+            const needMsg = await this.tenderNeedMsg(pid, tender.id);
+            if (!needMsg) return;
+            await transaction.insert(this.tableName, { pid, tid: tender.id, timing });
+        }
+
+        async addReviseMsg(transaction, pid, revise, timing) {
+            const needMsg = await this.tenderNeedMsg(pid, revise.tid);
+            if (!needMsg) return;
+            await transaction.insert(this.tableName, { pid, tid: revise.tid, rid: revise.id, timing });
+        }
+
+        async addStageMsg(transaction, pid, stage, timing) {
+            const needMsg = await this.tenderNeedMsg(pid, stage.tid);
+            if (!needMsg) return;
+            await transaction.insert(this.tableName, { pid, tid: stage.tid, sid: stage.id, timing });
+        }
+    }
+
+    return SpecPull;
+};

+ 13 - 0
app/service/stage_audit.js

@@ -19,6 +19,7 @@ const pushType = require('../const/audit').pushType;
 const syncApiConst = require('../const/sync_api');
 const measureType = require('../const/tender').measureType;
 const RevisePrice = require('../lib/revise_price');
+const pushOperate = require('../const/spec_3f').pushOperate;
 
 module.exports = app => {
     class StageAudit extends app.BaseService {
@@ -348,6 +349,8 @@ module.exports = app => {
                 };
                 await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
+                // 上报/审批 - 检查三方特殊推送
+                await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
                 // todo 更新标段tender状态 ?
                 await transaction.commit();
                 // 通知发送 - 第三方更新
@@ -548,7 +551,11 @@ module.exports = app => {
                         code: this.ctx.session.sessionProject.code,
                     };
                     await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
+                    // 审批通过 - 检查三方特殊推送
+                    await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
                 }
+                // 上报/审批 - 检查三方特殊推送
+                await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
                 await transaction.commit();
             } catch (err) {
                 await transaction.rollback();
@@ -691,6 +698,8 @@ module.exports = app => {
                     code: this.ctx.session.sessionProject.code,
                 };
                 await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
+                // 上报/审批 - 检查三方特殊推送
+                await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
 
                 await transaction.commit();
             } catch (err) {
@@ -851,6 +860,8 @@ module.exports = app => {
                 };
                 await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
+                // 上报/审批 - 检查三方特殊推送
+                await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
                 await transaction.commit();
             } catch (err) {
                 await transaction.rollback();
@@ -1012,6 +1023,8 @@ module.exports = app => {
                 };
                 await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
+                // 上报/审批 - 检查三方特殊推送
+                await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
                 await transaction.commit();
                 // 通知发送 - 第三方更新
                 if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {

+ 1 - 1
db_script/baseUtils.js

@@ -4,7 +4,7 @@
 const fs = require('fs');
 const path = require('path');
 var util = require('util');
-var logPath = path.join(__dirname, 'change_rela.log');
+var logPath = path.join(__dirname, `${process.argv[1]}.log`);
 var logFile = fs.createWriteStream(logPath, { flags: 'a' });
 console.log = function() {
     logFile.write(util.format.apply(null, arguments) + '\n');

+ 138 - 0
db_script/repair1027.js

@@ -0,0 +1,138 @@
+// 计算变更令,正负变更数
+
+const defaultInfo = require('../app/const/tender_info');
+const BaseUtil = require('./baseUtils');
+const querySql = BaseUtil.querySql;
+const ZhCalc = BaseUtil.ZhCalc;
+
+const checkStage = async function(stage, decimal, preStage) {
+    const billsTable = 'zh_stage_bills_' + stage.tid % 10;
+    const stageBills = await querySql(`Select * From ${billsTable} Where sid = ?`, [stage.id]);
+
+    const history = stage.tp_history ? JSON.parse(stage.tp_history) : null;
+    if (history) {
+        for (const h of history) {
+            const sbFilter = stageBills.filter(x => {
+                return x.times < h.times || (x.times === h.times && x.order <= h.order);
+            });
+            const sbLatest = BaseUtil.filterLastestData(sbFilter, ['lid']);
+            history.positive_qc_tp = 0;
+            history.negative_qc_tp = 0;
+            for (const sbl of sbLatest) {
+                stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
+                stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_qc_tp);
+            }
+        }
+        await querySql('Update zh_stage Set tp_history = ? Where id = ?', [JSON.stringify(history), stage.id]);
+    }
+
+    const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
+    if (stageBillsLatest.length === 0) return;
+
+    stage.positive_qc_tp = 0;
+    stage.pre_positive_qc_tp = preStage ? ZhCalc.add(preStage.positive_qc_tp, preStage.pre_positive_qc_tp) : 0;
+    stage.negative_qc_tp = 0;
+    stage.pre_negative_qc_tp = preStage ? ZhCalc.add(preStage.negative_qc_tp, preStage.pre_negative_qc_tp) : 0;
+    for (const sbl of stageBillsLatest) {
+        stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
+        stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_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 checkStageBills = async function (stage, decimal, preStage, reCalcBills) {
+    const billsTable = 'zh_stage_bills_' + stage.tid % 10;
+    const billsFinalTable = 'zh_stage_bills_final_' + stage.tid % 10;
+    const ledgerTable = 'zh_ledger_' + stage.tid % 10;
+    // 计算更新stagePos相关
+    const stageChange = await querySql(`SELECT sc.*, l.unit_price FROM zh_stage_change sc Left Join ${ledgerTable} l ON sc.lid = l.id WHERE sid = ?`, [stage.id]);
+    const stageBills = await querySql(`SELECT * From ${billsTable} where sid = ?`, [stage.id]);
+    if (reCalcBills) {
+        for (const sb of stageBills) {
+            const filterTimesOrder = stageChange.filter(x => {
+                if (x.no_value) return false;
+                if (x.lid !== sb.lid) return false;
+                return x.stimes < sb.times || (x.stimes === sb.times && x.sorder <= sb.order);
+            });
+            const filterLatest = BaseUtil.filterLastestData(filterTimesOrder, ['pid', 'cbid'], 'stimes', 'sorder');
+            if (filterLatest.length === 0) continue;
+
+            sb.positive_qc_qty = 0;
+            sb.negative_qc_qty = 0;
+            for (const fl of filterLatest) {
+                if (!sb.unit_price) sb.unit_price = fl.unit_price;
+                if (fl.minus) {
+                    sb.negative_qc_qty = ZhCalc.add(sb.negative_qc_qty, fl.qty);
+                } else {
+                    sb.positive_qc_qty = ZhCalc.add(sb.positive_qc_qty, fl.qty);
+                }
+            }
+            if (sb.positive_qc_qty || sb.negative_qc_qty) {
+                sb.positive_qc_tp = ZhCalc.mul(sb.unit_price, sb.positive_qc_qty, decimal.tp);
+                sb.negative_qc_tp = ZhCalc.mul(sb.unit_price, sb.negative_qc_qty, decimal.tp);
+                await querySql(`Update ${billsTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
+                    [sb.positive_qc_qty, sb.positive_qc_tp, sb.negative_qc_qty, sb.negative_qc_tp, sb.id]);
+            }
+        }
+        console.log(`Update StageBills ${stage.order}: ${stageBills.length}`);
+    }
+    // 计算更新stagePosFinal
+    const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
+    const stageBillsLatestIndex = {};
+    stageBillsLatest.forEach(x => { stageBillsLatestIndex[x.lid] = x; });
+    const preStageBillsFinal = preStage ? await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [preStage.id]) : [];
+    const preStageBillsFinalIndex = {};
+    preStageBillsFinal.forEach(x => { preStageBillsFinalIndex[x.lid] = x; });
+    const stageBillsFinal = await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [stage.id]);
+    for (const sbf of stageBillsFinal) {
+        const curSb = stageBillsLatestIndex[sbf.lid];
+        const preSb = preStageBillsFinalIndex[sbf.lid];
+        if (preSb) {
+            if (curSb) {
+                sbf.positive_qc_qty = ZhCalc.add(preSb.positive_qc_qty, curSb.positive_qc_qty);
+                sbf.positive_qc_tp = ZhCalc.add(preSb.positive_qc_tp, curSb.positive_qc_tp);
+                sbf.negative_qc_qty = ZhCalc.add(preSb.negative_qc_qty, curSb.negative_qc_qty);
+                sbf.negative_qc_tp = ZhCalc.add(preSb.negative_qc_tp, curSb.negative_qc_tp);
+            } else {
+                sbf.positive_qc_qty = preSb.positive_qc_qty;
+                sbf.positive_qc_tp = preSb.positive_qc_tp;
+                sbf.negative_qc_qty = preSb.negative_qc_qty;
+                sbf.negative_qc_tp = preSb.negative_qc_tp;
+            }
+        } else if (curSb) {
+            sbf.positive_qc_qty = curSb.positive_qc_qty;
+            sbf.positive_qc_tp = curSb.positive_qc_tp;
+            sbf.negative_qc_qty = curSb.negative_qc_qty;
+            sbf.negative_qc_tp = curSb.negative_qc_tp;
+        }
+        if (sbf.positive_qc_qty || sbf.negative_qc_qty)
+            await querySql(`Update ${billsFinalTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
+                [sbf.positive_qc_qty, sbf.positive_qc_tp, sbf.negative_qc_qty, sbf.negative_qc_tp, sbf.id]);
+    }
+    console.log(`Update StageBillsFinal ${stage.order}: ${stageBillsFinal.length}`);
+};
+
+const doRepair1027 = async function(tid, sorder, reCalcBills) {
+    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 stage = await querySql('Select * From zh_stage where tid = ? and `order` = ?', [t.id, sorder]);
+            const preStage = sorder > 1 ? await querySql('Select * From zh_stage where tid = ? and `order` = ?', [t.id, sorder - 1]) : [];
+            await checkStageBills(stage[0], decimal, preStage[0], reCalcBills);
+            await checkStage(stage[0], decimal, preStage[0], reCalcBills);
+        }
+    } catch (err) {
+        console.log(err);
+    }
+    BaseUtil.closePool();
+};
+const tenderId = parseInt(process.argv[3]);
+const stageOrder = parseInt(process.argv[4]);
+const reCalcBills = parseInt(process.argv[5]);
+doRepair1027(tenderId, stageOrder, reCalcBills);