| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 | 'use strict';/** * xxx_flow_cur为当前流程,审批通过时为空值 * xxx_flow_pre为上一流程, 未上报时 为空值/上一期终审,pre比cur多一个time值 * * * @author Mai * @date * @version */const auditConst = require('../const/audit.js');const commonField = ['ledger_status', 'stage_status', 'revise_status', 'stage_count', 'stage_complete_count', 'contract_tp', 'advance_tp', 'change_tp'];module.exports = app => {    class TenderCache extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'tender_cache';        }        _analysisTenderCache(tender, cache, uid) {            commonField.forEach(f => { tender[f] = cache[f]; });            tender.ledger_tp = cache.ledger_tp ? JSON.parse(cache.ledger_tp) : {};            if (!cache.stage_count || (cache.stage_count === 1 && cache.stage_status === auditConst.stage.status.uncheck && cache.stage_flow_cur_uid !== uid)) {                tender.cur_flow = JSON.parse(cache.ledger_flow_cur_info || cache.ledger_flow_pre_info);                tender.cur_flow.title = '台账';                tender.pre_flow = cache.ledger_flow_pre_info ? JSON.parse(cache.ledger_flow_pre_info) : null;                tender.stage_tp = {};                tender.progress = {                    title: '台账',                    status: auditConst.ledger.tiStatusString[cache.ledger_status],                    status_class: auditConst.ledger.tiStatusStringClass[cache.ledger_status],                };            } else if (cache.stage_status !== auditConst.stage.status.uncheck || cache.stage_flow_cur_uid === uid) {                tender.stage_status = cache.stage_status;                tender.stage_count = tender.stage_count;                tender.stage_complete_count = tender.stage_complete_count;                tender.cur_flow = cache.stage_status === auditConst.stage.status.checkNo                    ? JSON.parse(cache.stage_flow_pre_info)                    : JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);                tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;                tender.stage_tp = JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp);                tender.progress = {                    title: `第${cache.stage_count}期`,                    status: auditConst.stage.tiStatusString[cache.stage_status],                    status_class: auditConst.stage.tiStatusStringClass[cache.stage_status],                };            } else {                tender.stage_status = auditConst.stage.status.checked;                tender.stage_count = tender.stage_complete_count;                tender.stage_complete_count = tender.stage_complete_count;                tender.cur_flow = (cache.stage_status !== auditConst.stage.status.uncheck || cache.stage_flow_cur_uid !== uid)                    ? JSON.parse(cache.stage_flow_pre_info) : JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);                tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;                tender.stage_tp = JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp);                tender.progress = {                    title: `第${cache.stage_complete_count}期`,                    status: auditConst.stage.tiStatusString[auditConst.stage.status.checked],                    status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.checked],                };            }            tender.contract_price = cache.contract_price;            tender.advance_tp = cache.advance_tp;            tender.change_tp = cache.change_tp;        }        async loadTenderCache(tender, uid) {            const cache = await this.getDataById(tender.id);            if (cache) this._analysisTenderCache(tender, cache, uid);        }        async insertTenderCache(transaction, tid, uid) {            const user = await this.ctx.service.projectAccount.getAccountCacheData(uid);            user.order = 0;            user.status = auditConst.ledger.status.uncheck;            const data = {                id: tid,                ledger_status: auditConst.flow.status.uncheck,                ledger_flow_cur_uid: uid,                ledger_flow_cur_info: JSON.stringify(user),            };            await transaction.insert(this.tableName, data);        }        async updateLedgerCache4Start(transaction, tid, status, audit, tp) {            const orgCache = await this.getDataById(tid);            const preInfo = JSON.parse(orgCache.ledger_flow_cur_info);            preInfo.time = new Date();            const data = {                id: tid, ledger_status: status,                ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid, ledger_flow_pre_info: JSON.stringify(preInfo),            };            if (audit) {                const info = await this.ctx.service.projectAccount.getAccountCacheData(audit.audit_id);                info.order = audit.audit_order;                info.status = status;                data.ledger_flow_cur_uid = audit.audit_id;                data.ledger_flow_cur_info = JSON.stringify(info);            }            if (tp) data.ledger_tp = JSON.stringify(tp);            await transaction.update(this.tableName, data);        }        async updateLedgerCache(transaction, tid, status, checkType, next, tp) {            const orgCache = await this.getDataById(tid);            const preInfo = JSON.parse(orgCache.ledger_flow_cur_info);            preInfo.time = new Date();            preInfo.status = checkType;            const data = {                id: tid, ledger_status: status,                ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid, ledger_flow_pre_info: JSON.stringify(preInfo),            };            if (next) {                const info = await this.ctx.service.projectAccount.getAccountCacheData(next.audit_id);                info.status = checkType === auditConst.ledger.status.checkNo ? status : auditConst.ledger.status.uncheck;                data.ledger_flow_cur_uid = next.audit_id;                data.ledger_flow_cur_info = JSON.stringify(info);            } else {                data.ledger_flow_cur_uid = 0;                data.ledger_flow_cur_info = '';            }            if (tp) data.ledger_tp = JSON.stringify(tp);            await transaction.update(this.tableName, data);        }        async updateStageCache4Add(transaction, newStage, pcTp) {            const tp = {                contract_tp: 0, qc_tp: 0,                positive_qc_tp: 0, negative_qc_tp: 0,                yf_tp: 0, sf_tp: 0,                pre_contract_tp: newStage.pre_contract_tp, pre_qc_tp: newStage.pre_qc_tp,                pre_positive_qc_tp: newStage.pre_positive_qc_tp, pre_negative_qc_tp: newStage.pre_positive_qc_tp,                pre_yf_tp: newStage.pre_yf_tp, pre_sf_tp: newStage.pre_sf_tp,                ...pcTp,            };            const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(newStage.user_id);            cur_flow_info.order = newStage.order;            cur_flow_info.status = auditConst.stage.status.uncheck;            await transaction.update(this.tableName, {                id: newStage.tid, stage_count: newStage.order, stage_status: auditConst.stage.status.uncheck,                stage_flow_cur_uid: newStage.user_id, stage_flow_cur_info: JSON.stringify(cur_flow_info),                stage_flow_cur_tp: JSON.stringify(tp),            });        }        async updateStageCache4Del(transaction, delStage) {            if (delStage.order > 1) {                const preStage = await this.ctx.service.stage.getDataByCondition({ tid: delStage.tid, order: delStage.order - 1 });                const tp = JSON.stringify({                    contract_tp: preStage.contract_tp, qc_tp: preStage.qc_tp, contract_pc_tp: preStage.contract_pc_tp, qc_pc_tp: preStage.qc_pc_tp, pc_tp: preStage.pc_tp,                    positive_qc_tp: preStage.positive_qc_tp, positive_qc_pc_tp: preStage.positive_qc_pc_tp, negative_qc_tp: preStage.negative_qc_pc_tp, negative_qc_pc_tp: preStage.negative_qc_pc_tp,                    yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,                    pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,                    pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,                    pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,                });                const auditor = await this.ctx.service.stageAudit.getLastestAuditor(preStage.id, preStage.times, preStage.status);                const user_info = JSON.stringify({                    order: preStage.order, status: preStage.status, time: auditor.end_time,                    name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile, telephone: auditor.telephone,                });                await transaction.update(this.tableName, {                    id: delStage.tid, stage_count: preStage.order, stage_complete_count: preStage.order, stage_status: preStage.status,                    stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',                    stage_flow_pre_uid: auditor.aid, stage_flow_pre_info: user_info, stage_flow_pre_tp: tp,                });            } else {                await transaction.update(this.tableName, {                    id: delStage.tid, stage_count: 0, stage_complete_count: 0, stage_status: 0,                    stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',                    stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',                });            }        }        async updateStageCache4Start(transaction, stage, status, auditor, ledgerTp, stageTp) {            const orgCache = await this.getDataById(stage.tid);            const data = { id: stage.tid, stage_status: status };            if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);            const tp = JSON.parse(orgCache.stage_flow_cur_tp);            if (stageTp) this._.assign(tp, stageTp);            data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;            const info = JSON.parse(orgCache.stage_flow_cur_info);            info.time = new Date();            data.stage_flow_pre_info = JSON.stringify(info);            data.stage_flow_pre_tp = JSON.stringify(tp);            data.stage_flow_cur_uid = auditor.aid;            const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(auditor.aid);            cur_flow_info.order = stage.order;            cur_flow_info.status = auditConst.stage.status.checking;            data.stage_flow_cur_info = JSON.stringify(cur_flow_info);            data.stage_flow_cur_tp = JSON.stringify(tp);            await transaction.update(this.tableName, data);        }        async updateStageCache4Flow(transaction, stage, status, auditor, preAuditor, ledgerTp, stageTp, pcTp) {            const orgCache = await this.getDataById(stage.tid);            const data = { id: stage.tid, stage_status: status };            if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);            const tp = orgCache.stage_flow_cur_tp ? JSON.parse(orgCache.stage_flow_cur_tp) : (orgCache.stage_flow_pre_tp ? JSON.parse(orgCache.stage_flow_pre_tp) : {});            if (stageTp) this._.assign(tp, stageTp);            if (pcTp) this._.assign(tp, pcTp);            if (preAuditor) {                data.stage_flow_pre_uid = preAuditor.aid;                const info = await this.ctx.service.projectAccount.getAccountCacheData(preAuditor.aid);                info.order = preAuditor.order;                info.status = preAuditor.status;                info.time = new Date();                data.stage_flow_pre_info = JSON.stringify(info);            } else {                data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;                data.stage_flow_pre_info = orgCache.stage_flow_cur_info;            }            data.stage_flow_pre_tp = JSON.stringify(tp);            if (auditor) {                data.stage_flow_cur_uid = auditor.aid;                const info = await this.ctx.service.projectAccount.getAccountCacheData(auditor.aid);                info.order = auditor.order;                info.status = status;                data.stage_flow_cur_info = JSON.stringify(info);            }            if (status === auditConst.stage.status.checked && !auditor) data.stage_complete_count = stage.order;            data.stage_flow_cur_tp = JSON.stringify(tp);            await transaction.update(this.tableName, data);        }        async updateStageCache4Revise(transaction, tid, ledgerTp, pcTp) {            const orgCache = await this.getDataById(tid);            const data = { id: tid };            if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);            if (data.stage_status !== auditConst.stage.status.checked && pcTp) {                const curTp = JSON.parse(orgCache.stage_flow_cur_tp);                this._.assign(curTp, pcTp);                data.stage_flow_cur_tp = JSON.stringify(curTp);                if (orgCache.stage_status !== auditConst.stage.status.uncheck) {                    const preTp = JSON.parse(orgCache.stage_flow_pre_tp);                    this._.assign(preTp, pcTp);                    data.stage_flow_cur_tp = JSON.stringify(preTp);                }            }            await transaction.update(this.tableName, data);        }        async updateAdvanceCache(tid) {            const advance_tp = await this.ctx.service.advance.getSumAdvance(tid);            await this.db.update(this.tableName, { id: tid, advance_tp });        }        async updateChangeCache(tid) {            const sql = `SELECT SUM(cast (total_price as decimal(18,6))) AS total_price, SUM(cast (positive_tp as decimal(18,6))) AS positive_tp, SUM(cast (negative_tp as decimal(18,6))) AS negative_tp               FROM ${this.ctx.service.change.tableName} WHERE tid = ? AND status = ? And valid = 1`;            const changeSum = await this.db.queryOne(sql, [tender.id, auditConst.flow.status.checked]);            await this.db.update(this.tableName, { id: tid, change_tp: changeSum.total_price || 0 });        }        async updateContractPriceCache(tender) {            const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);            await this.db.update(this.tableName, { id: tender.id, contract_price: tenderInfo.deal_param.contractPrice || 0 });        }        async _refreshLedgerRela(tender, data) {            data.ledger_status = tender.ledger_status || 0;            if (tender.ledger_status === auditConst.ledger.status.uncheck) {                data.ledger_flow_cur_uid = tender.user_id;                const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);                user.status = tender.ledger_status;                user.order = 0;                data.ledger_flow_cur_info = JSON.stringify(user);            } else if (tender.ledger_status === auditConst.ledger.status.checked) {                const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);                data.ledger_flow_pre_uid = pre.audit_id;                data.ledger_flow_pre_info = JSON.stringify({                    order: pre.order, status: pre.status, time: pre.end_time,                    name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,                });            } else if (tender.ledger_status === auditConst.ledger.status.checkNo) {                data.ledger_flow_cur_uid = tender.user_id;                const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);                user.order = 0;                user.status = auditConst.ledger.status.uncheck;                data.ledger_flow_cur_info = JSON.stringify(user);                const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times - 1, tender.ledger_status);                data.ledger_flow_pre_uid = pre.audit_id;                data.ledger_flow_pre_info = JSON.stringify({                    order: pre.order, status: pre.status, time: pre.end_time,                    name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,                });            } else {                const cur = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);                if (cur) {                    data.ledger_flow_cur_uid = cur.audit_id;                    data.ledger_flow_cur_info = JSON.stringify({                        order: cur.order, status: cur.status, time: cur.end_time,                        name: cur.name, company: cur.company, role: cur.role, mobile: cur.mobile, telephone: cur.telephone,                    });                    if (cur.audit_order === 1) {                        data.ledger_flow_pre_uid = tender.user_id;                        const user = await await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id, { order: 0, time: cur.begin_time });                        data.ledger_flow_pre_info = JSON.stringify(user);                    } else {                        const pre = await this.ctx.service.ledgerAudit.getAuditorByOrder(tender.id, cur.audit_order - 1, cur.times);                        data.ledger_flow_pre_uid = pre.audit_id;                        data.ledger_flow_pre_info = JSON.stringify({                            order: pre.order, status: pre.status, time: pre.end_time,                            name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,                        });                    }                }            }            data.ledger_tp = JSON.stringify(await this.ctx.service.ledger.addUp({ tender_id: tender.id/* , is_leaf: true*/ }));            const revise = await this.service.ledgerRevise.getLastestRevise(tender.id);            data.revise_status = revise ? revise.status : 0;        }        async _calcTp (lastStage, tp) {            lastStage.curTimes = lastStage.times;            const tpData = await this.ctx.service.stageBills.getSumTotalPrice(lastStage);            const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(lastStage);            tp.contract_tp = tpData.contract_tp;            tp.qc_tp = tpData.qc_tp;            tp.positive_qc_tp = tpData.positive_qc_tp;            tp.negative_qc_tp = tpData.negative_qc_tp;            tp.contract_pc_tp = pcSum.contract_pc_tp;            tp.qc_pc_tp = pcSum.qc_pc_tp;            tp.pc_tp = pcSum.pc_tp;            tp.positive_qc_pc_tp = pcSum.positive_qc_pc_tp;            tp.negative_qc_pc_tp = pcSum.negative_qc_pc_tp;            const payTp = await this.ctx.service.stagePay.getSpecialTotalPrice(lastStage);            tp.yf_tp = payTp.yf;            tp.sf_tp = payTp.sf;        };        async _refreshStageRela(tender, data) {            if (tender.ledger_status === auditConst.ledger.status.checked) {                const lastStage = await this.ctx.service.stage.getLastestStage(tender.id, true);                if (!lastStage) return;                data.stage_count = lastStage.order;                data.stage_complete_count = lastStage.status === auditConst.stage.status.checked ? lastStage.order : lastStage.order - 1;                data.stage_status = lastStage.status;                const tp = {                    contract_tp: lastStage.contract_tp || 0, qc_tp: lastStage.qc_tp || 0, contract_pc_tp: lastStage.contract_pc_tp, qc_pc_tp: lastStage.qc_pc_tp, pc_tp: lastStage.pc_tp,                    positive_qc_tp: lastStage.positive_qc_tp, positive_qc_pc_tp: lastStage.positive_qc_pc_tp, negative_qc_tp: lastStage.negative_qc_pc_tp, negative_qc_pc_tp: lastStage.negative_qc_pc_tp,                    yf_tp: lastStage.yf_tp, sf_tp: lastStage.sf_tp,                    pre_contract_tp: lastStage.pre_contract_tp, pre_qc_tp: lastStage.pre_qc_tp,                    pre_positive_qc_tp: lastStage.pre_positive_qc_tp, pre_negative_qc_tp: lastStage.pre_positive_qc_tp,                    pre_yf_tp: lastStage.pre_yf_tp, pre_sf_tp: lastStage.pre_sf_tp,                };                const preStage = lastStage.order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: lastStage.tid, order: lastStage.order - 1}) : null;                if (lastStage.status === auditConst.stage.status.uncheck) {                    if (preStage) {                        const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(preStage.id, preStage.times, preStage.status);                        data.stage_flow_pre_uid = preAuditor.aid;                        data.stage_flow_pre_info = JSON.stringify({                            order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,                            name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,                        });                        data.stage_flow_pre_tp = JSON.stringify({                            contract_tp: preStage.contract_tp || 0, qc_tp: preStage.qc_tp || 0, contract_pc_tp: preStage.contract_pc_tp, qc_pc_tp: preStage.qc_pc_tp, pc_tp: preStage.pc_tp,                            positive_qc_tp: preStage.positive_qc_tp, positive_qc_pc_tp: preStage.positive_qc_pc_tp, negative_qc_tp: preStage.negative_qc_pc_tp, negative_qc_pc_tp: preStage.negative_qc_pc_tp,                            yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,                            pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,                            pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,                            pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,                        });                    }                    lastStage.curOrder = 0;                    await this._calcTp(lastStage, tp);                    data.stage_flow_cur_uid = lastStage.user_id;                    const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: lastStage.status });                    data.stage_flow_cur_info = JSON.stringify(curInfo);                    data.stage_flow_cur_tp = JSON.stringify(tp);                } else if (lastStage.status === auditConst.stage.status.checked) {                    const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times, lastStage.status);                    lastStage.curOrder = preAuditor.order;                    await this._calcTp(lastStage, tp);                    data.stage_flow_pre_uid = preAuditor.aid;                    data.stage_flow_pre_info = JSON.stringify({                        order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,                        name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,                    });                    data.stage_flow_cur_tp = JSON.stringify(tp);                    data.stage_flow_pre_tp = JSON.stringify(tp);                } else if (lastStage.status === auditConst.stage.status.checkNo) {                    lastStage.curOrder = 0;                    await this._calcTp(lastStage, tp);                    data.stage_flow_cur_uid = lastStage.user_id;                    const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: auditConst.stage.status.uncheck });                    data.stage_flow_cur_info = JSON.stringify(curInfo);                    data.stage_flow_cur_tp = JSON.stringify(tp);                    const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times - 1, lastStage.status);                    data.stage_flow_pre_uid = preAuditor.aid;                    data.stage_flow_pre_info = JSON.stringify({                        order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,                        name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,                    });                    const his = lastStage.tp_history.find(x => { return x.times === preAuditor.times && x.order === preAuditor.order; });                    if (his) {                        tp.contract_tp = his.contract_tp;                        tp.qc_tp = his.qc_tp;                        tp.positive_qc_tp = his.positive_qc_tp;                        tp.negative_qc_tp = his.negative_qc_tp;                        tp.yf_tp = his.yf_tp;                        tp.sf_tp = his.sf_tp;                    }                    data.stage_flow_pre_tp = JSON.stringify(tp);                } else {                    const curAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times, lastStage.status);                    lastStage.curOrder = curAuditor.order;                    await this._calcTp(lastStage, tp);                    data.stage_flow_cur_uid = curAuditor.aid;                    data.stage_flow_cur_info = JSON.stringify({                        order: curAuditor.order, status: curAuditor.status,                        name: curAuditor.name, company: curAuditor.company, role: curAuditor.role, mobile: curAuditor.mobile, telephone: curAuditor.telephone,                    });                    data.stage_flow_cur_tp = JSON.stringify(tp);                    const preAuditor = curAuditor.order > 1 ? await this.ctx.service.stageAudit.getAuditorByOrder(lastStage.id, lastStage.times, curAuditor.order - 1) : null;                    if (preAuditor) {                        data.stage_flow_pre_uid = preAuditor.aid;                        data.stage_flow_pre_info = JSON.stringify({                            order: preAuditor.order, status: preAuditor.status, time: curAuditor.begin_time,                            name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,                        });                    } else {                        data.stage_flow_pre_uid = lastStage.user_id;                        const preInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: auditConst.stage.status.uncheck, time: curAuditor.begin_time });                        data.stage_flow_pre_info = JSON.stringify(preInfo);                    }                    const his = preAuditor                        ? lastStage.tp_history.find(x => { return x.times === preAuditor.times && x.order === preAuditor.order; })                        : lastStage.tp_history.find(x => { return x.times === lastStage.times && x.order === 0; });                    if (his) {                        tp.contract_tp = his.contract_tp;                        tp.qc_tp = his.qc_tp;                        tp.positive_qc_tp = his.positive_qc_tp;                        tp.negative_qc_tp = his.negative_qc_tp;                        tp.yf_tp = his.yf_tp;                        tp.sf_tp = his.sf_tp;                    }                    data.stage_flow_pre_tp = JSON.stringify(tp);                }            }        }        async refreshTenderCache(tender, transaction) {            const orgCache = await this.getDataById(tender.id);            const data = { id: tender.id, ledger_status: 0,                ledger_flow_cur_uid: 0, ledger_flow_cur_info: '', ledger_flow_pre_uid: 0, ledger_flow_pre_info: '',                stage_count: 0, stage_complete_count: 0, stage_status: 0,                stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',                stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',            };            // 台账            await this._refreshLedgerRela(tender, data);            // 计量            await this._refreshStageRela(tender, data);            // 其他计算项            const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);            data.contract_price = tenderInfo.deal_param.contractPrice || 0;            data.advance_tp = await this.ctx.service.advance.getSumAdvance(tender.id);            const changeSum = await this.db.queryOne(`SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ${this.ctx.service.change.tableName} WHERE tid = ? AND status = ? And valid = 1`, [tender.id, auditConst.flow.status.checked]);            data.change_tp = changeSum.total_price || 0;            const conn = transaction || this.db;            if (orgCache) {                conn.update(this.tableName, data);            } else {                conn.insert(this.tableName, data);            }        }    }    return TenderCache;};
 |