'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'; } _tranLedgerCache(tender, cache) { tender.cur_flow = (cache.ledger_status === auditConst.ledger.status.checkNo) ? JSON.parse(cache.ledger_flow_pre_info) : 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.stage_count = 0; tender.stage_status = cache.stage_status; tender.progress = { title: '台账', status: auditConst.ledger.tiStatusString[cache.ledger_status], status_class: auditConst.ledger.tiStatusStringClass[cache.ledger_status], }; } _analysisTenderCache(tender, cache, uid) { commonField.forEach(f => { tender[f] = cache[f]; }); tender.ledger_tp = cache.ledger_tp ? JSON.parse(cache.ledger_tp) : {}; uid = uid + ''; cache.ledger_flow_cur_uid = cache.ledger_flow_cur_uid ? cache.ledger_flow_cur_uid.split(',') : []; cache.ledger_flow_pre_uid = cache.ledger_flow_pre_uid ? cache.ledger_flow_pre_uid.split(',') : []; cache.stage_flow_cur_uid = cache.stage_flow_cur_uid ? cache.stage_flow_cur_uid.split(',') : []; cache.stage_flow_pre_uid = cache.stage_flow_pre_uid ? cache.stage_flow_pre_uid.split(',') : []; if (!cache.stage_count) { this._tranLedgerCache(tender, cache); } else if (cache.stage_count === 1 && cache.stage_status === auditConst.stage.status.uncheck && cache.stage_flow_cur_uid.indexOf(uid) < 0) { this._tranLedgerCache(tender, cache); } else if (cache.stage_status === auditConst.stage.status.uncheck) { tender.stage_status = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? auditConst.stage.status.uncheck : auditConst.stage.status.checked; tender.stage_count = tender.stage_count; tender.stage_complete_count = tender.stage_complete_count; tender.cur_flow = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info) : JSON.parse(cache.stage_flow_pre_info); tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null; tender.stage_tp = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp) : JSON.parse(cache.stage_flow_pre_tp); try { tender.progress = { title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } catch (err) { tender.progress = { title: `第${tender.stage_count}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } } else if (cache.stage_status === auditConst.stage.status.checkNo) { tender.stage_status = auditConst.stage.status.checkNo; tender.stage_count = tender.stage_count; tender.stage_complete_count = tender.stage_complete_count; tender.cur_flow = 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 = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp) : JSON.parse(cache.stage_flow_pre_tp); try { tender.progress = { title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`, status: auditConst.stage.statusButton[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.uncheck], }; } catch (err) { tender.progress = { title: `第${tender.stage_count}期`, status: auditConst.stage.statusButton[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.uncheck], }; } } else if (cache.stage_status === auditConst.stage.status.checked) { 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 = JSON.parse(cache.stage_flow_pre_info || cache.stage_flow_cur_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_pre_tp); try { tender.progress = { title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } catch (err) { tender.progress = { title: `第${tender.stage_count}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } } else { tender.stage_status = auditConst.stage.status.checking; tender.stage_count = tender.stage_count; tender.stage_complete_count = tender.stage_complete_count; tender.cur_flow = 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); try { tender.progress = { title: `第${tender.pre_flow.order === undefined ? tender.pre_flow[0].order : tender.pre_flow.order}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } catch(err) { tender.progress = { title: `第${tender.stage_count}期`, status: auditConst.stage.tiStatusString[tender.stage_status], status_class: auditConst.stage.tiStatusStringClass[tender.stage_status], }; } } 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) { try { this._analysisTenderCache(tender, cache, uid); } catch(err) { console.log(err); console.log(tender); } } } async insertTenderCache(transaction, tid, uid) { const user = await this.ctx.service.projectAccount.getAccountCacheData(uid); user.order = 0; user.time = new Date(); 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, auditors, 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 (auditors.length > 0) { const auditIds = auditors.map(x => { return x.audit_id; }); const curInfo = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds, {audit_type: auditors[0].audit_type, audit_order: 1, status}); data.ledger_flow_cur_uid = auditIds.join(','); data.ledger_flow_cur_info = JSON.stringify(curInfo); } 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 || orgCache.ledger_flow_pre_info); preInfo.time = new Date(); preInfo.status = checkType; const data = { id: tid, ledger_status: status, ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid || orgCache.ledger_flow_pre_uid, ledger_flow_pre_info: JSON.stringify(preInfo), }; if (next && next.length > 0) { const auditIds = next.map(x => { return x.audit_id; }); const infoStatus = checkType === auditConst.ledger.status.checkNo ? status : auditConst.ledger.status.uncheck; const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds, {audit_type: next[0].audit_type, audit_order: next[0].audit_order, status: infoStatus}); data.ledger_flow_cur_uid = auditIds.join(','); 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 auditors = await this.ctx.service.stageAudit.getLastestAuditors(preStage.id, preStage.times, preStage.status); const user_info = auditors.length > 0 ? JSON.stringify(auditors.map(auditor => { return { order: preStage.order, status: preStage.status, time: auditor.end_time, audit_order: auditor.audit_order, 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: auditors.map(x => { return x.aid; }).join(','), 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 updateStageCache4DelTimes(transaction, stage, times) { const data = { id: stage.tid }; const tp = { contract_tp: stage.contract_tp || 0, qc_tp: stage.qc_tp || 0, contract_pc_tp: stage.contract_pc_tp, qc_pc_tp: stage.qc_pc_tp, pc_tp: stage.pc_tp, positive_qc_tp: stage.positive_qc_tp, positive_qc_pc_tp: stage.positive_qc_pc_tp, negative_qc_tp: stage.negative_qc_pc_tp, negative_qc_pc_tp: stage.negative_qc_pc_tp, yf_tp: stage.yf_tp, sf_tp: stage.sf_tp, pre_contract_tp: stage.pre_contract_tp, pre_qc_tp: stage.pre_qc_tp, pre_positive_qc_tp: stage.pre_positive_qc_tp, pre_negative_qc_tp: stage.pre_positive_qc_tp, pre_yf_tp: stage.pre_yf_tp, pre_sf_tp: stage.pre_sf_tp, }; data.stage_flow_cur_uid = stage.user_id; const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(stage.user_id, { order: stage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, status: auditConst.stage.status.uncheck }); data.stage_flow_cur_info = JSON.stringify(curInfo); const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(stage.id, times - 1, auditConst.stage.status.checkNo); const preAuditorIds = preAuditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = preAuditorIds.join(','); data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return { order: stage.order, audit_type: preAuditor.audit_type, audit_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 = stage.tp_history.find(x => { return x.times === times && x.order === preAuditors[0].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_cur_tp = JSON.stringify(tp); data.stage_flow_pre_tp = JSON.stringify(tp); await transaction.update(this.tableName, data); } async updateStageCache4Start(transaction, stage, status, auditors, ledgerTp, stageTp) { if (!stage.isCheckFirst) return; 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); const auditIds = auditors.map(x => { return x.aid; }); data.stage_flow_cur_uid = auditIds.join(','); const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds, {order: stage.order, audit_type: auditors[0].audit_type, audit_order: 1, 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, auditors, preAuditors, ledgerTp, stageTp, pcTp) { if (!stage.isCheckFirst) return; 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 (preAuditors && preAuditors.length > 0) { const preAuditorIds = preAuditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = preAuditorIds.join(','); const info = await this.ctx.service.projectAccount.getAccountCacheDatas(preAuditorIds, { order: stage.order, status: preAuditors[0].status, audit_type:preAuditors[0].audit_type, audit_order: preAuditors[0].audit_order, time: new Date() }); data.stage_flow_pre_info = info.length > 0 ? 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 (auditors && auditors.length > 0) { const auditorIds = auditors.map(x => { return x.aid; }); data.stage_flow_cur_uid = auditorIds.join(','); const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditorIds, { order: stage.order, audit_type: auditors[0].audit_type, status, audit_order: auditors[0].audit_order }); data.stage_flow_cur_info = info.length > 0 ? JSON.stringify(info) : ''; } if (status === auditConst.stage.status.checked && (!auditors || auditors.length === 0)) data.stage_complete_count = stage.order; data.stage_flow_cur_tp = JSON.stringify(tp); await transaction.update(this.tableName, data); } async updateStageCache4MultiChecked(transaction, stage, auditors, ledgerTp, stageTp, nextStage, nextStageTp) { if (!stage.isCheckFirst) return; const orgCache = await this.getDataById(stage.tid); const data = { id: stage.tid, stage_status: auditConst.stage.status.uncheck, stage_complete_count: stage.order, stage_count: stage.order + 1 }; 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 (auditors && auditors.length > 0) { const auditorIds = auditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = auditorIds.join(','); const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditorIds, { order: stage.order, audit_type: auditors[0].audit_type, status: auditConst.stage.status.checked, audit_order: auditors[0].audit_order }); data.stage_flow_pre_info = info.length > 0 ? JSON.stringify(info) : ''; } data.stage_flow_pre_tp = JSON.stringify(tp); data.stage_flow_cur_uid = nextStage.user_id; const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(nextStage.user_id); cur_flow_info.order = nextStage.order; cur_flow_info.status = auditConst.stage.status.uncheck; data.stage_flow_cur_info = JSON.stringify(cur_flow_info); const cur_flow_tp = { contract_tp: 0, qc_tp: 0, positive_qc_tp: 0, negative_qc_tp: 0, yf_tp: 0, sf_tp: 0, ...nextStageTp, }; data.stage_flow_cur_tp = JSON.stringify(cur_flow_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; user.time = tender.create_time; 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 endLastStage = await this.ctx.service.stage.getLastestStage(tender.id, true); const lastStage = await this.ctx.service.stage.getFlowLatestStage(tender.id, true); if (!lastStage || !endLastStage) return; data.stage_count = endLastStage.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 preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(preStage.id, preStage.times, preStage.status); const preAuditorIds = preAuditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = preAuditorIds.join(','); data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return { order: preStage.order, audit_type: preAuditor.audit_type, audit_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: lastStage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, 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 preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times, lastStage.status); const preAuditorIds = preAuditors.map(x => { return x.aid; }); lastStage.curOrder = preAuditors[0].order; await this._calcTp(lastStage, tp); data.stage_flow_pre_uid = preAuditorIds.join(','); data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return { order: lastStage.order, audit_type: preAuditor.audit_type, audit_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: lastStage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, status: auditConst.stage.status.uncheck }); data.stage_flow_cur_info = JSON.stringify(curInfo); data.stage_flow_cur_tp = JSON.stringify(tp); const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times - 1, lastStage.status); const preAuditorIds = preAuditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = preAuditorIds.join(','); data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return { order: lastStage.order, audit_type: preAuditor.audit_type, audit_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 === preAuditors[0].times && x.order === preAuditors[0].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 curAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times, lastStage.status); const curAuditorIds = curAuditors.map(x => { return x.aid; }); lastStage.curOrder = curAuditors[0].order; await this._calcTp(lastStage, tp); data.stage_flow_cur_uid = curAuditorIds.join(','); data.stage_flow_cur_info = JSON.stringify(curAuditors.map(curAuditor => { return { order: lastStage.order, audit_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 preAuditors = lastStage.curOrder > 1 ? await this.ctx.service.stageAudit.getAuditorsByOrder(lastStage.id, lastStage.times, lastStage.curOrder - 1) : []; if (preAuditors.length > 0) { const preAuditorIds = preAuditors.map(x => { return x.aid; }); data.stage_flow_pre_uid = preAuditorIds.join(','); data.stage_flow_pre_info = JSON.stringify(preAuditors.map(preAuditor => { return { order: lastStage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.audit_order, status: preAuditor.status, time: preAuditor.end_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: lastStage.order, audit_order: 0, audit_type: 1, status: auditConst.stage.status.uncheck, time: curAuditors[0].begin_time }); data.stage_flow_pre_info = JSON.stringify(preInfo); } const his = preAuditors.length > 0 ? lastStage.tp_history.find(x => { return x.times === preAuditors[0].times && x.order === preAuditors[0].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; };