Procházet zdrojové kódy

关联台账,缓存计量单元数据,缓存中间计量数据

MaiXinRong před 3 roky
rodič
revize
2003eeca5a

+ 8 - 2
app/controller/stage_rela_controller.js

@@ -137,8 +137,14 @@ module.exports = app => {
 
         async _getStageRelaPosData(ctx, relaStage) {
             const posData = await ctx.service.pos.getPosData({tid: relaStage.rela_tid});
-            const curStageData = await ctx.service.stagePos.getLastestStageData2(relaStage.rela_tid, relaStage.rela_sid);
-            const endStageData = await ctx.service.stagePosFinal.getFinalData({id: relaStage.rela_tid}, relaStage.rela_sid);
+            const curStageData = await ctx.service.stageRelaPos.getAllDataByCondition({
+                where: {sid: ctx.stage.id, rela_tid: relaStage.rela_tid},
+            });
+            const endStageData = await ctx.service.stageRelaPosFinal.getAllDataByCondition({
+                where: {sid: ctx.stage.id, rela_tid: relaStage.rela_tid},
+            });
+            // const curStageData = await ctx.service.stagePos.getLastestStageData2(relaStage.rela_tid, relaStage.rela_sid);
+            // const endStageData = await ctx.service.stagePosFinal.getFinalData({id: relaStage.rela_tid}, relaStage.rela_sid);
             this.ctx.helper.assignRelaData(posData, [
                 { data: curStageData, fields: ['contract_qty', 'contract_expr', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
                 { data: endStageData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', ], prefix: 'end_', relaId: 'lid' },

+ 52 - 15
app/lib/stage_im.js

@@ -123,7 +123,7 @@ class StageIm {
 
     // 检查汇总节点
     async _checkGather() {
-        const gatherNodes = this.ctx.stage.im_gather_node ? this.ctx.stage.im_gather_node.split(',') : [];
+        const gatherNodes = this.stage.im_gather_node ? this.stage.im_gather_node.split(',') : [];
         for (const node of this.billsTree.datas) {
             node.check = gatherNodes.indexOf(node.id) !== -1;
         }
@@ -529,14 +529,14 @@ class StageIm {
             position: '',
             lIndex: nodeIndex,
         };
-        if (this.ctx.stage.im_gather && node.check) {
+        if (this.stage.im_gather && node.check) {
             im.bw = this._getZlGatherBw(node, peg);
             im.xm = '';
         } else {
             im.bw = this._getZlNormalBw(node, peg);
             im.xm = node.name;
         }
-        im.check = this.ctx.stage.im_gather && node.check;
+        im.check = this.stage.im_gather && node.check;
         this._generateTzGclBillsData(node, im);
         this.ImData.push(im);
         this._generateTzChangeData(node, im);
@@ -815,7 +815,7 @@ class StageIm {
                     lIndex: nodeIndex,
                     dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
                 };
-                if (this.ctx.stage.im_gather && node.check) {
+                if (this.stage.im_gather && node.check) {
                     im.check = true;
                     im.bw = this._getZlGatherBw(node, peg);
                     im.xm = '';
@@ -829,7 +829,7 @@ class StageIm {
                 nodeImData.push(im);
                 this.ImData.push(im);
             }
-            // if (!this.ctx.stage.im_gather || !node.check) {
+            // if (!this.stage.im_gather || !node.check) {
             this._generateZlLeafXmjData(p, im, 'gather_qty');
             // }
             this._generateZlChangeData(p, im);
@@ -951,19 +951,18 @@ class StageIm {
      * @param {Array} nodes
      */
     _recursiveBuildImData(nodes) {
-        const stage = this.ctx.stage;
         if (!nodes || nodes.length === 0) { return; }
         for (const node of nodes) {
             if (this.billsTree.isLeafXmj(node) ||
-                ((stage.im_type !== imType.bw.value && stage.im_type !== imType.bb.value) && stage.im_gather && node.check)
+                ((this.stage.im_type !== imType.bw.value && this.stage.im_type !== imType.bb.value) && this.stage.im_gather && node.check)
             ) {
-                if (this.ctx.stage.im_type === imType.tz.value) {
+                if (this.stage.im_type === imType.tz.value) {
                     this._generateTzImData(node);
-                } else if (this.ctx.stage.im_type === imType.zl.value) {
+                } else if (this.stage.im_type === imType.zl.value) {
                     this._generateZlImData(node);
-                } else if (this.ctx.stage.im_type === imType.bw.value) {
+                } else if (this.stage.im_type === imType.bw.value) {
                     this._generateBwImData(node);
-                } else if (this.ctx.stage.im_type === imType.bb.value) {
+                } else if (this.stage.im_type === imType.bb.value) {
                     this._generateBwBillsImData(node);
                 }
             } else {
@@ -975,25 +974,63 @@ class StageIm {
     // 生成中间计量数据
     async buildImData() {
         const self = this;
+        this.stage = this.ctx.stage;
         // 初始化
         await this._loadMainData();
         await this._loadRelaData();
-        if (this.ctx.stage.im_gather) {
+        if (this.stage.im_gather) {
             this._checkGather();
         }
         // 生成数据
         this._recursiveBuildImData(this.billsTree.children);
         // 排序
-        if (this.ctx.stage.im_type !== imType.tz.value) {
+        if (this.stage.im_type !== imType.tz.value) {
             this.ImData.sort(function(x, y) {
                 const iCode = self.ctx.helper.compareCode(x.code, y.code);
                 return iCode === 0 ? x.lIndex - y.lIndex : iCode;
             });
         }
         // 生成数据(需要缓存,并清理缓存)
-        const pre = (this.ctx.stage.im_pre && this.ctx.stage.im_pre !== '') ? this.ctx.stage.im_pre + this.splitChar : '';
+        const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
         for (const [i, im] of this.ImData.entries()) {
-            im.im_code = pre + this._getNumberFormat(this.ctx.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
+            im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
+            if (im.gclBills) {
+                for (const b of im.gclBills) {
+                    b.im_code = im.im_code;
+                }
+            }
+
+            this._getCalcMemo(im);
+            this._checkCustomDetail(im);
+            delete im.leafXmjs;
+            delete im.gclBills;
+
+            this._getChangeInfo(im);
+            delete im.changes;
+        }
+    }
+
+    buildRelaStageIm(stage, billsTree, pos) {
+        this.billsTree = billsTree;
+        this.pos = pos;
+        this.stage = stage;
+
+        const self = this;
+        // 初始化
+        if (stage.im_gather) this._checkGather();
+        // 生成数据
+        this._recursiveBuildImData(this.billsTree.children);
+        // 排序
+        if (this.stage.im_type !== imType.tz.value) {
+            this.ImData.sort(function(x, y) {
+                const iCode = self.ctx.helper.compareCode(x.code, y.code);
+                return iCode === 0 ? x.lIndex - y.lIndex : iCode;
+            });
+        }
+        // 生成数据(需要缓存,并清理缓存)
+        const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
+        for (const [i, im] of this.ImData.entries()) {
+            im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
             if (im.gclBills) {
                 for (const b of im.gclBills) {
                     b.im_code = im.im_code;

+ 3 - 3
app/service/stage_change.js

@@ -67,7 +67,7 @@ module.exports = app => {
          */
         async getAuditorStageData(tid, sid, times, order, lid, pid) {
             const sql = 'SELECT c.*,' +
-                '  oc.p_code As c_code, oc.new_code As c_new_code' +
+                '  oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
                 '  FROM ' + this.tableName + ' As c ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid` From ' + this.tableName +
@@ -86,7 +86,7 @@ module.exports = app => {
 
         async getLastestAllStageData(tid, sid) {
             const sql = 'SELECT c.*,' +
-                '  oc.p_code As c_code, oc.new_code As c_new_code' +
+                '  oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
                 '  FROM ' + this.tableName + ' As c ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid` From ' + this.tableName +
@@ -105,7 +105,7 @@ module.exports = app => {
 
         async getAuditorAllStageData(tid, sid, times, order) {
             const sql = 'SELECT c.*, ' +
-                '  oc.p_code As c_code, oc.new_code As c_new_code' +
+                '  oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
                 '  FROM ' + this.tableName + ' As c ' +
                 '  INNER JOIN ( ' +
                 '    SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid` From ' + this.tableName +

+ 403 - 118
app/service/stage_rela.js

@@ -10,6 +10,238 @@
 
 const auditConst = require('../const/audit').stage;
 const changeConst = require('../const/change');
+const ledgerModel = require('../lib/ledger');
+const StageIm = require('../lib/stage_im');
+
+class srCache {
+    constructor(ctx) {
+        this.ctx = ctx;
+    }
+
+    async _getCacheOrgTp() {
+        const bg = await this.ctx.service.stageChange.getQualityTotalPrice(this.stage);
+        const gcl100 = await this.ctx.service.stageBills.getSumTotalPriceGcl(this.stage, '^[^0-9]*1[0-9]{2}(-|$)');
+        return {
+            contract_tp: this.stage.contract_tp, qc_tp: this.stage.qc_tp,
+            gather_tp: this.ctx.helper.add(this.stage.contract_tp, this.stage.qc_tp),
+            gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
+            bg_common: bg.common, bg_more: bg.more, bg_great: bg.great,
+        };
+    }
+
+    async _loadBillsData() {
+        const decimal = this.ctx.tender.info.decimal;
+        const ledger = await this.ctx.service.ledger.getData(this.stage.tid);
+        const curBillsData = await this.ctx.service.stageBills.getLastestStageData(this.stage.tid, this.stage.id);
+        const preStageBills = this.preRelaStage
+            ? this.ctx.service.stageRelaBillsFinal.getAllDataByCondition({ where: { sid: this.preRelaStage.sid, rela_tid: this.preRelaStage.rela_tid } })
+            : [];
+        this.ctx.helper.assignRelaData(ledger, [
+            { data: curBillsData, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: '', relaId: 'lid' },
+            { data: preStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
+        ]);
+        for (const l of ledger) {
+            const db = this.dealBills.find(x => {
+                return x.code === l.b_code && x.name === l.name && x.unit === l.unit;
+            });
+            l.unit_price = db ? db.unit_price : 0;
+            l.contract_tp = this.ctx.helper.mul(l.unit_price, l.contract_qty, decimal.tp);
+            l.qc_tp = this.ctx.helper.mul(l.unit_price, l.qc_qty, decimal.tp);
+            l.gather_tp = this.ctx.helper.add(l.contract_tp, l.qc_tp);
+            l.pre_gather_tp = this.ctx.helper.add(l.pre_contract_qty, l.pre_contract_tp);
+            l.end_contract_qty = this.ctx.helper.add(l.pre_contract_qty, l.contract_qty);
+            l.end_contract_tp = this.ctx.helper.add(l.pre_contract_tp, l.contract_tp);
+            l.end_qc_qty = this.ctx.helper.add(l.pre_qc_qty, l.qc_qty);
+            l.end_qc_tp = this.ctx.helper.add(l.pre_qc_tp, l.qc_tp);
+            l.end_gather_qty = this.ctx.helper.add(l.pre_gather_qty, l.gather_qty);
+            l.end_gather_tp = this.ctx.helper.add(l.pre_gather_tp, l.gather_tp);
+        }
+        this.billsTree = new ledgerModel.billsTree(this.ctx, {
+            id: 'ledger_id',
+            pid: 'ledger_pid',
+            order: 'order',
+            level: 'level',
+            rootId: -1,
+            keys: ['id', 'tender_id', 'ledger_id'],
+            stageId: 'id',
+            calcFields: ['deal_tp', 'total_price',
+                'contract_tp', 'qc_tp', 'gather_tp',
+                'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
+                'end_contract_tp', 'end_qc_tp', 'end_gather_tp'],
+        });
+        this.billsTree.loadDatas(ledger);
+    }
+
+    async _loadPosData() {
+        const pos = await this.ctx.service.pos.getPosData({tid: this.stage.tid});
+        const curPosData = await this.ctx.service.stagePos.getLastestStageData(this.stage.tid, this.stage.id);
+        const preStagePos = this.preRelaStage
+            ? this.ctx.service.stageRelaPosFinal.getAllDataByCondition({ where: { sid: this.preRelaStage.sid, rela_tid: this.preRelaStage.rela_tid } })
+            : [];
+        this.ctx.helper.assignRelaData(pos, [
+            { data: curPosData, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: '', relaId: 'pid' },
+            { data: preStagePos, fields: ['contract_qty', 'qc_qty', 'postil'], prefix: 'pre_', relaId: 'pid' },
+        ]);
+        for (const p of pos) {
+            p.gather_qty = this.ctx.helper.add(p.contract_qty, p.qc_qty);
+            p.pre_gather_qty = this.ctx.helper.add(p.pre_contract_qty, p.qc_qty);
+            p.end_contract_qty = this.ctx.helper.add(p.contract_qty, p.pre_contract_qty);
+            p.end_qc_qty = this.ctx.helper.add(p.qc_qty, p.pre_qc_qty);
+            p.end_gather_qty = this.ctx.helper.add(p.gather_qty, p.pre_gather_qty);
+        }
+
+        this.pos = new ledgerModel.pos({ id: 'id', ledgerId: 'lid' });
+        this.pos.loadDatas(pos);
+    }
+
+    async _getCacheTp(gcl, gcl100) {
+        const helper = this.ctx.helper;
+        const result = {
+            contract_tp: gcl.contract_tp, qc_tp: gcl.qc_tp,
+            gather_tp: this.ctx.helper.add(gcl.contract_tp, gcl.qc_tp),
+            gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
+        };
+        this.changes = await this.ctx.service.stageChange.getLastestAllStageData(this.stage.tid, this.stage.id);
+        const bqData = [];
+        for (const d of this.changes) {
+            if (!d.qty) continue;
+            let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
+            if (!bd) {
+                const bills = dealBills.find(x => {
+                    return x.code === d.code && x.name === d.name && x.unit === d.unit;
+                });
+                if (!bills) continue;
+                bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
+                bqData.push(bd);
+            }
+            const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
+            bd.tp = this.ctx.helper.add(bd.tp, tp);
+        }
+        result.bg_common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
+        result.bg_more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
+        result.bg_great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
+        return result;
+    }
+
+    _getCacheBills() {
+        this.stageBills = [];
+        this.stageBillsFinal = [];
+        const gcl = {}, gcl100 = {}, gcl100reg = /^[^0-9]*1[0-9]{2}(-|$)/;
+        for (const t of this.billsTree.nodes) {
+            if (t.children && t.children.length > 0) continue;
+            if (!t.b_code) continue;
+
+            gcl.contract_tp = this.ctx.helper.add(gcl.contract_tp, t.contract_tp);
+            gcl.qc_tp = this.ctx.helper.add(gcl.qc_tp, t.qc_tp);
+            if (gcl100reg.test(t.b_code)) {
+                gcl100.contract_tp = this.ctx.helper.add(gcl100.contract_tp, t.contract_tp);
+                gcl100.qc_tp = this.ctx.helper.add(gcl100.qc_tp, t.qc_tp);
+            }
+            if (t.contract_qty || t.contract_tp || t.qc_qty || t.qc_tp) {
+                this.stageBills.push({
+                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                    rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                    lid: t.id, unit_price: t.unit_price,
+                    contract_qty: t.contract_qty, contract_tp: t.contract_tp, contract_expr: t.contract_expr,
+                    qc_qty: t.qc_qty, qc_tp: t.qc_tp,
+                    postil: t.postil,
+                });
+            }
+            if (t.end_contract_qty || t.end_contract_tp || t.end_qc_qty || t.end_qc_tp) {
+                this.stageBillsFinal.push({
+                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                    rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                    lid: t.id,
+                    contract_qty: t.end_contract_qty, contract_tp: t.end_contract_tp,
+                    qc_qty: t.end_qc_qty, qc_tp: t.end_qc_tp,
+                })
+            }
+        }
+        return [gcl, gcl100];
+    }
+
+    _getCachePos() {
+        this.stagePos = [];
+        this.stagePosFinal = [];
+        for (const t of this.pos.datas) {
+            if (t.contract_qty || t.qc_qty) {
+                this.stagePos.push({
+                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                    rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                    pid: t.pid, lid: t.id,
+                    contract_qty: t.contract_qty, contract_expr: t.contract_expr, qc_qty: t.qc_qty, postil: t.postil,
+                });
+            }
+            if (t.end_contract_qty || t.end_qc_qty) {
+                this.stagePosFinal.push({
+                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                    rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                    pid: t.pid, lid: t.lid,
+                    contract_qty: t.end_contract_qty, qc_qty: t.end_qc_qty
+                })
+            }
+        }
+    }
+
+    async _getCacheStageIm() {
+        const stageIm = new StageIm(this.ctx);
+        this.details = await this.ctx.service.stageDetail.getLastestStageData(this.stage.tid, this.stage.id);
+        stageIm.buildRelaStageIm(this.stage, this.billsTree, this.pos, this.details, this.changes);
+        this.stageIm = [];
+        this.stageImBills = [];
+        for (const i of stageIm.ImData) {
+            this.stageIm.push({
+                tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                lid: i.lid, pid: i.pid, im_id: i.id,
+                code: i.code, name: i.name, unit: i.unit, unit_price: i.unit_price,
+                peg: i.peg, drawing_code: i.drawing_code, bw: i.bw, xm: i.xm,
+                position: i.position, jldy: i.jldy,
+                dwgc: i.dwgc, fbgc: i.fbgc, fxgc: i.fxgc,
+                doc_code: i.doc_code, im_code: i.im_code,
+                calc_memo: i.calc_memo, calc_memo_remark: i.calc_memo_remark, calc_img: i.calc_img,
+                bgl_code: i.bgl_code, bgl_drawing_code: i.bgl_drawing_code,
+                jl: i.jl, contract_jl: i.contract_jl, qc_jl: i.qc_jl,
+                pre_jl: i.jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl,
+                end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl,
+                tp: i.tp, contract_tp: i.contract_tp, qc_tp: i.qc_tp,
+                pre_tp: i.pre_tp, pre_contract_tp: i.pre_contract_tp, pre_qc_tp: i.pre_qc_tp,
+                end_tp: i.end_tp, end_contract_tp: i.end_contract_tp, end_qc_tp: i.end_qc_tp,
+                quantity: i.quantity, total_price: i.total_price,
+
+            })
+        }
+        for (const i of stageIm.ImBillsData) {
+            this.stageImBills.push({
+                tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+                rela_tid: this.stage.tid, rela_sid: this.stage.id, rela_sorder: this.stage.order,
+                im_id: i.imid, bid: i.bid, im_code: i.im_code,
+                b_code: i.b_code, name: i.name, unit: i.unit, unit_price: i.unit_price,
+                jl: i.jl, contract_jl: i.contract_jl, qc_jl: i.qc_jl,
+                pre_jl: i.jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl,
+                end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl,
+                tp: i.tp, contract_tp: i.contract_tp, qc_tp: i.qc_tp,
+                pre_tp: i.pre_tp, pre_contract_tp: i.pre_contract_tp, pre_qc_tp: i.pre_qc_tp,
+                end_tp: i.end_tp, end_contract_tp: i.end_contract_tp, end_qc_tp: i.end_qc_tp,
+                quantity: i.quantity, total_price: i.total_price
+            })
+        }
+    }
+
+    async calculate(stage) {
+        this.stage = stage;
+        this.dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.stage.tid } });
+        this.preRelaStage = await this.ctx.service.stageRela.getPreRelaStage(
+            this.ctx.tender.id, this.ctx.stage.order, stage.tid);
+        this.cache_org_tp = await this._getCacheOrgTp();
+        await this._loadBillsData();
+        await this._loadPosData();
+        const [gcl, gcl100] = this._getCacheBills();
+        this._getCachePos();
+        this.cache_tp = await this._getCacheTp(gcl, gcl100);
+        this._getCacheStageIm();
+    }
+};
 
 module.exports = app => {
     class StageRela extends app.BaseService {
@@ -53,115 +285,153 @@ module.exports = app => {
             return await this.db.queryOne(sql, [tid, rela_tid, sorder]);
         }
 
-        async _getCacheOrgTp(stage) {
-            const bg = await this.ctx.service.stageChange.getQualityTotalPrice(stage);
-            const gcl100 = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
-            return {
-                contract_tp: stage.contract_tp, qc_tp: stage.qc_tp,
-                gather_tp: this.ctx.helper.add(stage.contract_tp, stage.qc_tp),
-                gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
-                bg_common: bg.common, bg_more: bg.more, bg_great: bg.great,
-            };
-        }
-
-        async _calculateRelaStage(stage, preRelaStage) {
-            const result = {}, helper = this.ctx.helper, stageBills = [];
-            const ledger = await this.ctx.service.ledger.getData(stage.tid);
-            const curBillsData = await this.ctx.service.stageBills.getLastestStageData(stage.tid, stage.id);
-            helper.assignRelaData(ledger, [
-                { data: curBillsData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
-            ]);
-            const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.stage.tid } });
-
-            const gcl = {}, gcl100 = {}, gcl100reg = /^[^0-9]*1[0-9]{2}(-|$)/;
-            for (const t of ledger) {
-                const db = dealBills.find(x => {
-                    return x.code === t.b_code && x.name === t.name && x.unit === t.unit;
-                });
-                t.unit_price = db ? db.unit_price : 0;
-                if (t.contract_qty) t.contract_tp = helper.mul(t.unit_price, t.contract_qty, this.ctx.tender.info.decimal.tp);
-                if (t.qc_qty) t.qc_tp = helper.mul(t.unit_price, t.qc_qty, this.ctx.tender.info.decimal.tp);
-                gcl.contract_tp = helper.add(gcl.contract_tp, t.contract_tp);
-                gcl.qc_tp = helper.add(gcl.qc_tp, t.qc_tp);
-                if (gcl100reg.test(t.b_code)) {
-                    gcl100.contract_tp = helper.add(gcl100.contract_tp, t.contract_tp);
-                    gcl100.qc_tp = helper.add(gcl100.qc_tp, t.qc_tp);
-                }
-                if (t.contract_qty || t.contract_tp || t.qc_qty || t.qc_tp) {
-                    stageBills.push({
-                        tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
-                        rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
-                        lid: t.id, unit_price: t.unit_price,
-                        contract_qty: t.contract_qty, contract_tp: t.contract_tp, contract_expr: t.contract_expr,
-                        qc_qty: t.qc_qty, qc_tp: t.qc_tp,
-                        postil: t.postil,
-                    });
-                }
-            }
-            result.contract_tp = gcl.contract_tp;
-            result.qc_tp = gcl.qc_tp;
-            result.gather_tp = helper.add(gcl.contract_tp, gcl.qc_tp);
-            result.gather_tp_100 = helper.add(gcl100.contract_tp, gcl100.qc_tp);
-
-            const sql = 'SELECT sc.*, c.quality, cb.code, cb.name, cb.unit FROM ' + this.ctx.service.stageChange.tableName + ' sc' +
-                '  LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
-                '  LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' cb ON sc.cbid = cb.id' +
-                '  WHERE sid = ?';
-            const data = await this.db.query(sql, [stage.id]);
-            const bqData = [];
-            for (const d of data) {
-                if (!d.qty) continue;
-                let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
-                if (!bd) {
-                    const bills = dealBills.find(x => {
-                        return x.code === d.code && x.name === d.name && x.unit === d.unit;
-                    });
-                    if (!bills) continue;
-                    bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
-                    bqData.push(bd);
-                }
-                const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
-                bd.tp = this.ctx.helper.add(bd.tp, tp);
-            }
-            result.bg_common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
-            result.bg_more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
-            result.bg_great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
-
-            const stageBillsFinal = [];
-            const preStageBills = preRelaStage
-                ? this.ctx.service.stageRelaBillsFinal.getAllDataByCondition({ where: { sid: preRelaStage.sid, rela_tid: preRelaStage.rela_tid } })
-                : [];
-            for (const b of stageBills) {
-                const p = preStageBills.find(x => { return x.lid === b.lid });
-                stageBillsFinal.push({
-                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
-                    rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
-                    lid: b.lid,
-                    contract_qty: p ? helper.add(p.contract_qty, b.contract_qty) : b.contract_qty,
-                    contract_tp: p ? helper.add(p.contract_tp, b.contract_tp) : b.contract_tp,
-                    qc_qty: p ? helper.add(p.qc_qty, b.qc_qty) : b.qc_qty,
-                    qc_tp: p ? helper.add(p.qc_tp, b.qc_tp) : b.qc_tp,
-                });
-                if (p) preStageBills.splice(preStageBills.indexOf(p), 1);
-            }
-            for (const p of preStageBills) {
-                stageBillsFinal.push({
-                    tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
-                    rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
-                    lid: b.lid,
-                    contract_qty: p.contract_qty, contract_tp: p.contract_tp,
-                    qc_qty: p.qc_qty, qc_tp: p.qc_tp,
-                });
-            }
-            return [result, stageBills, stageBillsFinal];
-        }
-
-        async calculateRelaStage(relaStage) {
-            const preRelaStage = await this.getPreRelaStage(this.ctx.tender.id, this.ctx.stage.order, relaStage.tid);
-            const cache_org_tp = await this._getCacheOrgTp(relaStage);
-            const [cache_tp, stageBills, stageBillsFinal] = await this._calculateRelaStage(relaStage, preRelaStage);
-            return { cache_tp, cache_org_tp, stageBills, stageBillsFinal }
-        }
+        // async _getCacheOrgTp(stage) {
+        //     const bg = await this.ctx.service.stageChange.getQualityTotalPrice(stage);
+        //     const gcl100 = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
+        //     return {
+        //         contract_tp: stage.contract_tp, qc_tp: stage.qc_tp,
+        //         gather_tp: this.ctx.helper.add(stage.contract_tp, stage.qc_tp),
+        //         gather_tp_100: this.ctx.helper.add(gcl100.contract_tp, gcl100.qc_tp),
+        //         bg_common: bg.common, bg_more: bg.more, bg_great: bg.great,
+        //     };
+        // }
+        //
+        // async _calculateRelaStage(stage, preRelaStage) {
+        //     const result = {}, helper = this.ctx.helper, stageBills = [];
+        //     const ledger = await this.ctx.service.ledger.getData(stage.tid);
+        //     const curBillsData = await this.ctx.service.stageBills.getLastestStageData(stage.tid, stage.id);
+        //     helper.assignRelaData(ledger, [
+        //         { data: curBillsData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid' },
+        //     ]);
+        //     const dealBills = await this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.stage.tid } });
+        //
+        //     const gcl = {}, gcl100 = {}, gcl100reg = /^[^0-9]*1[0-9]{2}(-|$)/;
+        //     for (const t of ledger) {
+        //         const db = dealBills.find(x => {
+        //             return x.code === t.b_code && x.name === t.name && x.unit === t.unit;
+        //         });
+        //         t.unit_price = db ? db.unit_price : 0;
+        //         if (t.contract_qty) t.contract_tp = helper.mul(t.unit_price, t.contract_qty, this.ctx.tender.info.decimal.tp);
+        //         if (t.qc_qty) t.qc_tp = helper.mul(t.unit_price, t.qc_qty, this.ctx.tender.info.decimal.tp);
+        //         gcl.contract_tp = helper.add(gcl.contract_tp, t.contract_tp);
+        //         gcl.qc_tp = helper.add(gcl.qc_tp, t.qc_tp);
+        //         if (gcl100reg.test(t.b_code)) {
+        //             gcl100.contract_tp = helper.add(gcl100.contract_tp, t.contract_tp);
+        //             gcl100.qc_tp = helper.add(gcl100.qc_tp, t.qc_tp);
+        //         }
+        //         if (t.contract_qty || t.contract_tp || t.qc_qty || t.qc_tp) {
+        //             stageBills.push({
+        //                 tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //                 rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //                 lid: t.id, unit_price: t.unit_price,
+        //                 contract_qty: t.contract_qty, contract_tp: t.contract_tp, contract_expr: t.contract_expr,
+        //                 qc_qty: t.qc_qty, qc_tp: t.qc_tp,
+        //                 postil: t.postil,
+        //             });
+        //         }
+        //     }
+        //     result.contract_tp = gcl.contract_tp;
+        //     result.qc_tp = gcl.qc_tp;
+        //     result.gather_tp = helper.add(gcl.contract_tp, gcl.qc_tp);
+        //     result.gather_tp_100 = helper.add(gcl100.contract_tp, gcl100.qc_tp);
+        //
+        //     const sql = 'SELECT sc.*, c.quality, cb.code, cb.name, cb.unit FROM ' + this.ctx.service.stageChange.tableName + ' sc' +
+        //         '  LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
+        //         '  LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' cb ON sc.cbid = cb.id' +
+        //         '  WHERE sid = ?';
+        //     const data = await this.db.query(sql, [stage.id]);
+        //     const bqData = [];
+        //     for (const d of data) {
+        //         if (!d.qty) continue;
+        //         let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
+        //         if (!bd) {
+        //             const bills = dealBills.find(x => {
+        //                 return x.code === d.code && x.name === d.name && x.unit === d.unit;
+        //             });
+        //             if (!bills) continue;
+        //             bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
+        //             bqData.push(bd);
+        //         }
+        //         const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
+        //         bd.tp = this.ctx.helper.add(bd.tp, tp);
+        //     }
+        //     result.bg_common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
+        //     result.bg_more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
+        //     result.bg_great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
+        //
+        //     const stageBillsFinal = [];
+        //     const preStageBills = preRelaStage
+        //         ? this.ctx.service.stageRelaBillsFinal.getAllDataByCondition({ where: { sid: preRelaStage.sid, rela_tid: preRelaStage.rela_tid } })
+        //         : [];
+        //     for (const b of stageBills) {
+        //         const p = preStageBills.find(x => { return x.lid === b.lid });
+        //         stageBillsFinal.push({
+        //             tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //             rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //             lid: b.lid,
+        //             contract_qty: p ? helper.add(p.contract_qty, b.contract_qty) : b.contract_qty,
+        //             contract_tp: p ? helper.add(p.contract_tp, b.contract_tp) : b.contract_tp,
+        //             qc_qty: p ? helper.add(p.qc_qty, b.qc_qty) : b.qc_qty,
+        //             qc_tp: p ? helper.add(p.qc_tp, b.qc_tp) : b.qc_tp,
+        //         });
+        //         if (p) preStageBills.splice(preStageBills.indexOf(p), 1);
+        //     }
+        //     for (const p of preStageBills) {
+        //         stageBillsFinal.push({
+        //             tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //             rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //             lid: p.lid,
+        //             contract_qty: p.contract_qty, contract_tp: p.contract_tp,
+        //             qc_qty: p.qc_qty, qc_tp: p.qc_tp,
+        //         });
+        //     }
+        //     return [result, stageBills, stageBillsFinal];
+        // }
+        //
+        // async _getRelaCachePos(stage, preRelaStage) {
+        //     const helper = this.ctx.helper;
+        //     const curPosData = await this.ctx.service.stagePos.getLastestStageData(stage.tid, stage.id);
+        //     const preStagePos = preRelaStage
+        //         ? this.ctx.service.stageRelaPosFinal.getAllDataByCondition({ where: { sid: preRelaStage.sid, rela_tid: preRelaStage.rela_tid } })
+        //         : [];
+        //     const stagePos = [], stagePosFinal = [];
+        //     for (const b of curPosData) {
+        //         if (!stagePos.contract_qty || !stagePos.qc_tp || !stagePos.postil) continue;
+        //         const p = preStagePos.find(x => { return x.pid === b.pid });
+        //         stagePos.push({
+        //             tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //             rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //             lid: b.lid, pid: b.pid,
+        //             contract_qty: b.contract_qty, contract_expr: b.contract_expr, qc_qty: b.qc_qty, postil: b.postil,
+        //         });
+        //         stagePosFinal.push({
+        //             tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //             rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //             lid: b.lid, pid: b.pid,
+        //             contract_qty: p ? helper.add(p.contract_qty, b.contract_qty) : b.contract_qty,
+        //             qc_qty: p ? helper.add(p.qc_qty, b.qc_qty) : b.qc_qty,
+        //         });
+        //         if (p) stagePosFinal.splice(stagePosFinal.indexOf(p), 1);
+        //     }
+        //     for (const p of stagePosFinal) {
+        //         stagePosFinal.push({
+        //             tid: this.ctx.tender.id, sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
+        //             rela_tid: stage.tid, rela_sid: stage.id, rela_sorder: stage.order,
+        //             lid: p.lid, pid: p.pid,
+        //             contract_qty: p.contract_qty, contract_tp: p.contract_tp,
+        //             qc_qty: p.qc_qty, qc_tp: p.qc_tp,
+        //         });
+        //     }
+        //     return [stagePos, stagePosFinal];
+        // }
+        //
+        // async calculateRelaStage(relaStage) {
+        //     const preRelaStage = await this.getPreRelaStage(this.ctx.tender.id, this.ctx.stage.order, relaStage.tid);
+        //     const cache_org_tp = await this._getCacheOrgTp(relaStage);
+        //     const [cache_tp, stageBills, stageBillsFinal] = await this._calculateRelaStage(relaStage, preRelaStage);
+        //     const [stagePos, stagePosFinal] = await this._getRelaCachePos(relaStage, preRelaStage);
+        //     return { cache_tp, cache_org_tp, stageBills, stageBillsFinal, stagePos, stagePosFinal }
+        // }
 
         async addStageRela(relaStage) {
             const addTime = new Date();
@@ -169,17 +439,22 @@ module.exports = app => {
             if (!tender) throw '关联标段不存在';
             const conn = await this.db.beginTransaction();
             try {
-                const result = await this.calculateRelaStage(relaStage);
+                const calcModel = new srCache(this.ctx);
+                await calcModel.calculate(relaStage);
                 const data = {
                     tid: this.ctx.tender.id,
                     sid: this.ctx.stage.id, sorder: this.ctx.stage.order,
                     rela_tid: relaStage.tid, rela_sid: relaStage.id, rela_sorder: relaStage.order, rela_tname: tender.name,
                     add_time: addTime, update_time: addTime,
-                    cache_tp: JSON.stringify(result.cache_tp), cache_org_tp: JSON.stringify(result.cache_org_tp),
+                    cache_tp: JSON.stringify(calcModel.cache_tp), cache_org_tp: JSON.stringify(calcModel.cache_org_tp),
                 };
                 await conn.insert(this.tableName, data);
-                if (result.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, result.stageBills);
-                if (result.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, result.stageBillsFinal);
+                if (calcModel.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, calcModel.stageBills);
+                if (calcModel.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, calcModel.stageBillsFinal);
+                if (calcModel.stagePos.length > 0) await conn.insert(this.ctx.service.stageRelaPos.tableName, calcModel.stagePos);
+                if (calcModel.stagePosFinal.length > 0) await conn.insert(this.ctx.service.stageRelaPosFinal.tableName, calcModel.stagePosFinal);
+                if (calcModel.stageIm.length > 0) await conn.insert(this.ctx.service.stageRelaIm.tableName, calcModel.stageIm);
+                if (calcModel.stageImBills.length > 0) await conn.insert(this.ctx.service.stageRelaImBills.tableName, calcModel.stageImBills);
                 await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });
                 await conn.commit();
             } catch (err) {
@@ -209,17 +484,27 @@ module.exports = app => {
             const relaStage = await this.ctx.service.stage.getDataById(rela.rela_sid);
             const conn = await this.db.beginTransaction();
             try {
-                const result = await this.calculateRelaStage(relaStage);
+                const calcModel = new srCache(this.ctx);
+                await calcModel.calculate(relaStage);
                 await conn.update(this.tableName, {
                     id: rela.id, update_time: new Date(),
-                    cache_tp: JSON.stringify(result.cache_tp), cache_org_tp: JSON.stringify(result.cache_org_tp)
+                    cache_tp: JSON.stringify(calcModel.cache_tp), cache_org_tp: JSON.stringify(calcModel.cache_org_tp)
                 });
 
                 await conn.delete(this.ctx.service.stageRelaBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
-                if (result.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, result.stageBills);
-
+                if (calcModel.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, calcModel.stageBills);
                 await conn.delete(this.ctx.service.stageRelaBillsFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
-                if (result.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, result.stageBillsFinal);
+                if (calcModel.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, calcModel.stageBillsFinal);
+
+                await conn.delete(this.ctx.service.stageRelaPos.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
+                if (calcModel.stagePos.length > 0) await conn.insert(this.ctx.service.stageRelaPos.tableName, calcModel.stagePos);
+                await conn.delete(this.ctx.service.stageRelaPosFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
+                if (calcModel.stagePosFinal.length > 0) await conn.insert(this.ctx.service.stageRelaPosFinal.tableName, calcModel.stagePosFinal);
+
+                await conn.delete(this.ctx.service.stageRelaIm.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
+                if (calcModel.stageIm.length > 0) await conn.insert(this.ctx.service.stageRelaIm.tableName, calcModel.stageIm);
+                await conn.delete(this.ctx.service.stageRelaImBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});
+                if (calcModel.stageImBills.length > 0) await conn.insert(this.ctx.service.stageRelaImBills.tableName, calcModel.stageImBills);
 
                 await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });
                 await conn.commit();

+ 29 - 0
app/service/stage_rela_im.js

@@ -0,0 +1,29 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2021/9/11
+ * @version
+ */
+
+module.exports = app => {
+
+    class StageRelaIm extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'stage_rela_im';
+        }
+
+    }
+
+    return StageRelaIm;
+};

+ 29 - 0
app/service/stage_rela_im_bills.js

@@ -0,0 +1,29 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2021/9/11
+ * @version
+ */
+
+module.exports = app => {
+
+    class StageRelaImBills extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'stage_rela_im_bills';
+        }
+
+    }
+
+    return StageRelaImBills;
+};

+ 29 - 0
app/service/stage_rela_pos.js

@@ -0,0 +1,29 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2021/9/11
+ * @version
+ */
+
+module.exports = app => {
+
+    class StageRelaPos extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'stage_rela_pos';
+        }
+
+    }
+
+    return StageRelaPos;
+};

+ 29 - 0
app/service/stage_rela_pos_final.js

@@ -0,0 +1,29 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2021/9/11
+ * @version
+ */
+
+module.exports = app => {
+
+    class StageRelaPosFinal extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'stage_rela_pos_final';
+        }
+
+    }
+
+    return StageRelaPosFinal;
+};

+ 83 - 17
sql/update.sql

@@ -1,23 +1,89 @@
 ALTER TABLE `zh_tender`
 ADD COLUMN `has_rela`  tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否有关联台账' AFTER `had_map`;
 
-CREATE TABLE `NewTable` (
-`id`  int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id' ,
-`tid`  int(11) UNSIGNED NOT NULL COMMENT '标段id' ,
-`sid`  int(11) UNSIGNED NOT NULL COMMENT '期id' ,
-`sorder`  tinyint(4) UNSIGNED NOT NULL COMMENT '期序号' ,
-`rela_tid`  int(11) UNSIGNED NOT NULL COMMENT '关联标段id' ,
-`rela_sid`  int(11) UNSIGNED NOT NULL COMMENT '关联期id' ,
-`rela_sorder`  tinyint(4) UNSIGNED NOT NULL COMMENT '关联期序号' ,
-`add_time`  datetime NOT NULL COMMENT '添加时间' ,
-`update_time`  datetime NOT NULL COMMENT '更新时间' ,
-`total_price`  decimal(28,6) NULL ,
-`cache_tp`  text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '缓存金额(当前标段单价计算)' ,
-`cache_org_tp`  text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '缓存金额(原关联标段单价计算)' ,
-PRIMARY KEY (`id`)
-)
-COMMENT='计量期-关联台账(含缓存数据)'
-;
+CREATE TABLE `zh_stage_rela` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
+  `tid` int(11) unsigned NOT NULL COMMENT '标段id',
+  `sid` int(11) unsigned NOT NULL COMMENT '期id',
+  `sorder` tinyint(4) unsigned NOT NULL COMMENT '期序号',
+  `rela_tid` int(11) unsigned NOT NULL COMMENT '关联标段id',
+  `rela_tname` varchar(255) CHARACTER SET utf8 NOT NULL COMMENT '关联标段-名称',
+  `rela_sid` int(11) unsigned NOT NULL COMMENT '关联期id',
+  `rela_sorder` tinyint(4) unsigned NOT NULL COMMENT '关联期序号',
+  `add_time` datetime NOT NULL COMMENT '添加时间',
+  `update_time` datetime NOT NULL COMMENT '更新时间',
+  `total_price` decimal(28,6) DEFAULT NULL,
+  `cache_tp` text CHARACTER SET utf8 COMMENT '缓存金额(当前标段单价计算)',
+  `cache_org_tp` text CHARACTER SET utf8 COMMENT '缓存金额(原关联标段单价计算)',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='计量期-关联台账(含缓存数据)';
+
+CREATE TABLE `zh_stage_rela_bills` (
+  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+  `tid` int(11) unsigned NOT NULL COMMENT '标段id',
+  `sid` int(11) unsigned NOT NULL COMMENT '期id',
+  `sorder` tinyint(4) unsigned NOT NULL COMMENT '期序号',
+  `rela_tid` int(11) unsigned NOT NULL COMMENT '关联标段id',
+  `rela_sid` int(11) unsigned NOT NULL COMMENT '关联期id',
+  `rela_sorder` tinyint(4) unsigned NOT NULL COMMENT '关联期序号',
+  `lid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '台账id',
+  `contract_qty` decimal(24,8) DEFAULT NULL COMMENT '合同计量-数量',
+  `contract_tp` decimal(24,8) DEFAULT NULL COMMENT '合同计量-金额',
+  `contract_expr` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '合同计量-公式',
+  `qc_qty` decimal(24,8) DEFAULT NULL COMMENT '数量变更-数量',
+  `qc_tp` decimal(24,8) DEFAULT NULL COMMENT '数量变更-金额',
+  `postil` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '本期批注',
+  `unit_price` decimal(24,8) DEFAULT NULL COMMENT '单价',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+CREATE TABLE `zh_stage_rela_pos` (
+  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+  `tid` int(11) unsigned NOT NULL COMMENT '标段id',
+  `sid` int(11) unsigned NOT NULL COMMENT '期id',
+  `sorder` tinyint(4) unsigned NOT NULL COMMENT '期序号',
+  `rela_tid` int(11) unsigned NOT NULL COMMENT '关联标段id',
+  `rela_sid` int(11) unsigned NOT NULL COMMENT '关联期id',
+  `rela_sorder` tinyint(4) unsigned NOT NULL COMMENT '关联期序号',
+  `lid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '台账id',
+  `pid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '计量单元id',
+  `contract_qty` decimal(24,8) DEFAULT NULL COMMENT '合同计量-数量',
+  `contract_expr` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT '合同计量-公式',
+  `qc_qty` decimal(24,8) DEFAULT NULL COMMENT '数量变更-数量',
+  `postil` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '本期批注',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+CREATE TABLE `zh_stage_rela_bills_final` (
+  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+  `tid` int(11) unsigned NOT NULL COMMENT '标段id',
+  `sid` int(11) unsigned NOT NULL COMMENT '期id',
+  `sorder` tinyint(4) unsigned NOT NULL COMMENT '期序号',
+  `rela_tid` int(11) unsigned NOT NULL COMMENT '关联标段id',
+  `rela_sid` int(11) unsigned NOT NULL COMMENT '关联期id',
+  `rela_sorder` tinyint(4) unsigned NOT NULL COMMENT '关联期序号',
+  `lid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '台账id',
+  `contract_qty` decimal(24,8) DEFAULT NULL COMMENT '合同计量-数量',
+  `contract_tp` decimal(24,8) DEFAULT NULL COMMENT '合同计量-金额',
+  `qc_qty` decimal(24,8) DEFAULT NULL COMMENT '数量变更-数量',
+  `qc_tp` decimal(24,8) DEFAULT NULL COMMENT '数量变更-金额',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+CREATE TABLE `zh_stage_rela_pos_final` (
+  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+  `tid` int(11) unsigned NOT NULL COMMENT '标段id',
+  `sid` int(11) unsigned NOT NULL COMMENT '期id',
+  `sorder` tinyint(4) unsigned NOT NULL COMMENT '期序号',
+  `rela_tid` int(11) unsigned NOT NULL COMMENT '关联标段id',
+  `rela_sid` int(11) unsigned NOT NULL COMMENT '关联期id',
+  `rela_sorder` tinyint(4) unsigned NOT NULL COMMENT '关联期序号',
+  `lid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '台账id',
+  `pid` varchar(36) CHARACTER SET ascii NOT NULL COMMENT '计量单元id',
+  `contract_qty` decimal(24,8) DEFAULT NULL COMMENT '合同计量-数量',
+  `qc_qty` decimal(24,8) DEFAULT NULL COMMENT '数量变更-数量',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 
 ALTER TABLE `zh_rpt_archive`
 CHANGE COLUMN `content` `content` JSON NULL DEFAULT NULL ;