| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 | 'use strict';/** * * * @author Mai * @date * @version */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.getSubtotal(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.getLastestStageData2(this.stage.tid, this.stage.id);        const preStageBills = this.preRelaStage            ? await 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', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'postil'], prefix: '', relaId: 'lid' },            { data: preStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], 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;            if (!l.is_tp) {                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_qty = this.ctx.helper.add(l.contract_qty, l.qc_qty);            l.gather_tp = this.ctx.helper.add(l.contract_tp, l.qc_tp);            l.pre_gather_qty = this.ctx.helper.add(l.pre_contract_qty, l.pre_qc_qty);            l.pre_gather_tp = this.ctx.helper.add(l.pre_contract_tp, l.pre_qc_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_qc_minus_qty = this.ctx.helper.add(l.pre_qc_minus_qty, l.qc_minus_qty);            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.getLastestStageData2(this.stage.tid, this.stage.id);        const preStagePos = this.preRelaStage            ? await 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', 'qc_minus_qty', 'postil', 'pid'], prefix: '', relaId: 'pid' },            { data: preStagePos, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty', 'pid'], 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.pre_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_qc_minus_qty = this.ctx.helper.add(p.qc_minus_qty, p.pre_qc_minus_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 = this.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 || t.qc_minus_qty) {                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, qc_minus_qty: t.qc_minus_qty,                    postil: t.postil,                });            }            if (t.end_contract_qty || t.end_contract_tp || t.end_qc_qty || t.end_qc_tp || t.qc_minus_qty) {                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_minus_qty: t.end_qc_minus_qty,                    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 || t.qc_minus_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.id, lid: t.id,                    contract_qty: t.contract_qty, contract_expr: t.contract_expr, qc_qty: t.qc_qty, qc_minus_qty: t.qc_minus_qty, postil: t.postil,                });            }            if (t.end_contract_qty || t.end_qc_qty || t.qc_minus_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.id, lid: t.lid,                    contract_qty: t.end_contract_qty, qc_qty: t.end_qc_qty, qc_minus_qty: t.end_qc_minus_qty,                })            }        }    }    async _getCacheStageIm() {        this.stageIm = [];        this.stageImBills = [];        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);        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_img_remark: i.calc_img_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, qc_minus_jl: i.qc_minus_jl,                pre_jl: i.pre_jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl, pre_qc_minus_jl: i.pre_qc_minus_jl,                end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl, end_qc_minus_jl: i.end_qc_minus_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, qc_minus_jl: i.qc_minus_jl,                pre_jl: i.pre_jl, pre_contract_jl: i.pre_contract_jl, pre_qc_jl: i.pre_qc_jl, pre_qc_minus_jl: i.pre_qc_minus_jl,                end_jl: i.end_jl, end_contract_jl: i.end_contract_jl, end_qc_jl: i.end_qc_jl, end_qc_minus_jl: i.end_qc_minus_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);        await this._getCacheStageIm();    }};module.exports = app => {    class StageRela extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'stage_rela';        }        _analysisStageRela(data) {            const datas = data instanceof Array ? data : [data];            for (const d of datas) {                d.cache_tp = d.cache_tp ? JSON.parse(d.cache_tp) : {};                d.cache_org_tp = d.cache_org_tp ? JSON.parse(d.cache_org_tp) : {};            }        }        async getStageRela(sid) {            const result = await this.getAllDataByCondition({                where: { sid },                orders: [['add_time', 'asc']],            });            this._analysisStageRela(result);            return result;        }        async getHistoryStageRela(tid, sorder) {            const sql = 'SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sorder < ? ORDER BY add_time desc';            const result = await this.db.query(sql, [tid, sorder]);            this._analysisStageRela(result);            return result;        }        async getPreRelaStage(tid, sorder, rela_tid) {            const sql = 'SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND rela_tid = ? And sorder < ? ORDER BY sorder desc';            return await this.db.queryOne(sql, [tid, rela_tid, sorder]);        }        // async _getCacheOrgTp(stage) {        //     const bg = await this.ctx.service.stageChange.getSubtotal(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.getLastestStageData2(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();            const tender = await this.ctx.service.tender.getDataById(relaStage.tid);            if (!tender) throw '关联标段不存在';            const conn = await this.db.beginTransaction();            try {                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(calcModel.cache_tp), cache_org_tp: JSON.stringify(calcModel.cache_org_tp),                };                const addRela = await conn.insert(this.tableName, data);                if (calcModel.stageBills && calcModel.stageBills.length > 0) await conn.insert(this.ctx.service.stageRelaBills.tableName, calcModel.stageBills);                if (calcModel.stageBillsFinal && calcModel.stageBillsFinal.length > 0) await conn.insert(this.ctx.service.stageRelaBillsFinal.tableName, calcModel.stageBillsFinal);                if (calcModel.stagePos && calcModel.stagePos.length > 0) await conn.insert(this.ctx.service.stageRelaPos.tableName, calcModel.stagePos);                if (calcModel.stagePosFinal && calcModel.stagePosFinal.length > 0) await conn.insert(this.ctx.service.stageRelaPosFinal.tableName, calcModel.stagePosFinal);                if (calcModel.stageIm && calcModel.stageIm.length > 0) await conn.insert(this.ctx.service.stageRelaIm.tableName, calcModel.stageIm);                if (calcModel.stageImBills && 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();                return addRela.insertId;            } catch (err) {                await conn.rollback();                throw err;            }        }        async deleteStageRela(id) {            const rela = await this.getDataById(id);            if (!rela) throw '关联标段不存在';            const conn = await this.db.beginTransaction();            try {                await conn.delete(this.tableName, { id: rela.id });                await conn.delete(this.ctx.service.stageRelaBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                await conn.delete(this.ctx.service.stageRelaBillsFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                await conn.delete(this.ctx.service.stageRelaPos.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                await conn.delete(this.ctx.service.stageRelaPosFinal.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                await conn.delete(this.ctx.service.stageRelaIm.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                await conn.delete(this.ctx.service.stageRelaImBills.tableName, {sid: rela.sid, rela_tid: rela.rela_tid});                // await conn.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, check_calc: 1 });                await conn.commit();            } catch (err) {                await conn.rollback();                throw err;            }        }        async updateStageRela(id) {            const rela = await this.getDataById(id);            const relaStage = await this.ctx.service.stage.getDataById(rela.rela_sid);            const conn = await this.db.beginTransaction();            try {                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(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 (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 (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();            } catch (err) {                await conn.rollback();                throw err;            }        }        async getSumCacheTp (sid) {            const result = {};            const relas = await this.getAllDataByCondition({ where: { sid } });            if (relas.length === 0) return result;            this._analysisStageRela(relas);            for (const r of relas) {                result.contract_tp = this.ctx.helper.add(result.contract_tp, r.cache_tp.contract_tp);                result.qc_tp = this.ctx.helper.add(result.qc_tp, r.cache_tp.qc_tp);                result.gather_tp = this.ctx.helper.add(result.gather_tp, r.cache_tp.gather_tp);                result.gather_tp_100 = this.ctx.helper.add(result.gather_tp_100, r.cache_tp.gather_tp_100);                result.bg_common = this.ctx.helper.add(result.bg_common, r.cache_tp.bg_common);                result.bg_more = this.ctx.helper.add(result.bg_more, r.cache_tp.bg_more);                result.bg_great = this.ctx.helper.add(result.bg_more, r.cache_tp.bg_more);            }            return result;        }    }    return StageRela;};
 |