'use strict'; /** * * * @author Mai * @date * @version */ class loadStageExcelTree { constructor(ctx) { this.ctx = ctx; this.decimal = ctx.tender.info.decimal; this.insertBills = []; this.insertPos = []; this.updateBills = []; this.updatePos = []; this.insertDgn = []; this.updateDgn = []; } init(source) { this.default = source.default; const LedgerModel = require('../lib/ledger'); this.ledgerTree = new LedgerModel.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [], }); this.pos = new LedgerModel.pos({ id: 'id', ledgerId: 'lid' }); this.ledgerTree.loadDatas(source.ledgerData); this.pos.loadDatas(source.posData); this.stageBills = source.stageBills; this.stagePos = source.stagePos; this.stageBillsDgn = source.stageBillsDgn; } findNode(node, parent) { const _ = this.ctx.helper._; const sibling = parent ? parent.children : this.ledgerTree.children; const self = this; return sibling.find(x => { if (node.is_leaf !== x.is_leaf) return false; if (node.b_code) { if (x.has_pos === undefined) { const relaPos = self.pos.getLedgerPos(x.id); x.has_pos = !!relaPos && relaPos.length > 0; } return node.b_code === _.trimEnd(x.b_code) && node.name === _.trimEnd(x.name) && node.unit === _.trimEnd(x.unit) && node.unit_price === x.unit_price && node.has_pos === x.has_pos; } else { return node.code === _.trimEnd(x.code) && node.name === _.trimEnd(x.name); } }); } loadLeaf(node, source) { const curStageBills = this.stageBills.find(csb => { return csb.lid === source.id; }); if (node.has_pos) { const sourcePos = this.pos.getLedgerPos(source.id); const sourceStagePos = this.stagePos.filter(sp => { return sp.lid === source.id; }); let contract_qty = 0; for (const p of node.pos) { if (!p.contract_qty) continue; const sp = sourcePos.find(x => { return x.name === p.name; }); if (!sp) continue; contract_qty = this.ctx.helper.add(contract_qty, p.contract_qty); let ssp = sourceStagePos.find(x => { return x.pid === sp.id; }); if (ssp) { this.updatePos.push({ id: ssp.id, contract_qty: p.contract_qty, postil: p.postil || ssp.postil || '' }); sourceStagePos.splice(sourceStagePos.indexOf(ssp), 1); } else { this.insertPos.push({ tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0, lid: source.id, pid:sp.id, contract_qty: p.contract_qty, postil: p.postil || '' }); } } for (const ssp of sourceStagePos) { contract_qty = this.ctx.helper.add(contract_qty, ssp.contract_qty); } const contract_tp = this.ctx.helper.mul(contract_qty, source.unit_price, this.decimal.tp); if (curStageBills) { this.updateBills.push({ id: curStageBills.id, contract_qty, contract_tp, postil: node.postil || curStageBills.postil || '' }); } else { if (contract_qty) this.insertBills.push({ tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0, lid: source.id, contract_qty, contract_tp, postil: node.postil || '' }); } } else { if (!node.contract_qty && !node.contract_tp) return; const contract_qty = source.is_tp ? 0 : node.contract_qty; const contract_tp = contract_qty ? this.ctx.helper.mul(contract_qty, source.unit_price, this.decimal.tp) : source.is_tp ? this.ctx.helper.round(node.contract_tp, this.decimal.tp) : 0; if (curStageBills) { this.updateBills.push({ id: curStageBills.id, contract_qty: contract_qty, contract_tp: contract_tp, postil: node.postil || curStageBills.postil || '' }); } else { this.insertBills.push({ tid: this.default.tid, sid: this.default.sid, said: this.default.said, times: 1, order: 0, lid: source.id, contract_qty: contract_qty, contract_tp: contract_tp, postil: node.postil || '' }); } } } loadDgn(node, cur) { if (!node.deal_dgn_qty1 && !node.deal_dgn_qty2 && !node.c_dgn_qty1 && !node.c_dgn_qty2) return; const dgn = this.stageBillsDgn.find(x => { return x.id === cur.id; }); if (dgn) { this.updateDgn.push({ id: cur.id, deal_dgn_qty1: node.deal_dgn_qty1, deal_dgn_qty2: node.deal_dgn_qty2, c_dgn_qty1: node.c_dgn_qty1, c_dgn_qty2: node.c_dgn_qty2 }); } else { this.insertDgn.push({ id: cur.id, tid: this.default.tid, deal_dgn_qty1: node.deal_dgn_qty1, deal_dgn_qty2: node.deal_dgn_qty2, c_dgn_qty1: node.c_dgn_qty1, c_dgn_qty2: node.c_dgn_qty2 }); } } loadNode(node, parent) { node.is_leaf = !node.children || node.children.length === 0 ? 1 : 0; node.has_pos = node.pos && node.pos.length > 0; const cur = this.findNode(node, parent); if (!cur) return; if (cur) { if (!node.b_code) this.loadDgn(node, cur); if (node.is_leaf) { this.loadLeaf(node, cur); } else { for (const c of node.children) { this.loadNode(c, cur); } } } } load(excelTree, source) { this.init(source); for (const node of excelTree.roots) { this.loadNode(node, null); } } } module.exports = app => { class StageStash extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'stage_stash'; } async getValidStash(tid) { const sql = `SELECT id, sorder, create_time, uid, uname, remark FROM ${this.tableName} WHERE tid = ? and valid and TO_DAYS(NOW()) - TO_DAYS(create_time) <= 30 ORDER BY create_time DESC`; return await this.db.query(sql, [tid]); } async addStash(stage, remark = '') { const bills = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id); const pos = await this.ctx.service.stagePos.getLastestStageData2(stage.tid, stage.id); const change = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id); const timestamp = (new Date()).getTime(); const filepath = `${this.ctx.session.sessionProject.id}-${stage.tid}-${timestamp}.json`; await this.ctx.hisOss.put(this.ctx.stashOssPath + filepath, Buffer.from(JSON.stringify({bills, pos, change}), 'utf8')); const result = await this.db.insert(this.tableName, { pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id, sorder: stage.order, uid: this.ctx.session.sessionUser.accountId, uname: this.ctx.session.sessionUser.name, filepath, info: JSON.stringify({ status: stage.status, flow: stage.curAuditorIds.length > 0 ? stage.curAuditorIds : stage.uid }), remark }); return result.insertId; } async delStash(id) { const stash = await this.getDataById(id); if (!stash) throw '暂存计量不存在'; if (stash.tid !== this.ctx.tender.id) throw '非当前标段暂存计量,不可操作'; await this.deleteById(id); await this.ctx.hisOss.delete(this.ctx.stashOssPath + stash.filepath); } async loadLedgerDataFromOss(filepath) { const File = await this.ctx.hisOss.get(this.ctx.stashOssPath + filepath); if (File.res.status !== 200) return '获取暂存计量有误'; const result = JSON.parse(File.content); return result; } async loadAndAnalysis(filepath) { const data = await this.loadLedgerDataFromOss(filepath); const billsIndex = {}; for (const b of data.bills) { billsIndex[b.lid] = b; } for (const p of data.pos) { if (!billsIndex[p.lid]) continue; if (!billsIndex[p.lid].pos) billsIndex[p.lid].pos = []; billsIndex[p.lid].pos.push(p); } for (const c of data.change) { if (!billsIndex[c.lid]) continue; if (billsIndex[c.lid].pos) { const p = billsIndex[c.lid].pos.find(x => { return x.pid === c.pid }); if (!p) continue; if (!p.change) p.change = []; p.change.push(c); } else { if (!billsIndex[c.lid].change) billsIndex[c.lid].change = []; billsIndex[c.lid].change.push(c); } } return data.bills; } async reCalcStashData(stage, data) { const decimal = this.ctx.tender.info.decimal; const insertBillsData = [], insertPosData = [], insertChangeData = []; const bills = await this.ctx.service.ledger.getAllDataByCondition({ where: { tender_id: stage.tid, is_leaf: true } }); const extraData = await this.ctx.service.ledgerExtra.getData(stage.tid, ['id', 'is_tp']); this.ctx.helper.assignRelaData(bills, [ { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' }, ]); const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: stage.tid } }); const said = this.ctx.session.sessionUser.accountId; for (const d of data) { const b = bills.find(x => { return x.id === d.lid }); if (!b) continue; const nbs = { tid: stage.tid, sid: stage.id, said, lid: b.id, times: 1, order: 0, contract_qty: 0, qc_qty: 0, qc_minus_qty: 0, positive_qc_qty: 0, negative_qc_qty: 0, contract_tp: 0, qc_tp: 0, positive_qc_tp: 0, negative_qc_tp: 0 }; if (d.pos) { for (const bp of d.pos) { const p = pos.find(x => { return x.id === bp.pid}); if (!p) continue; const nps = { tid: stage.tid, sid: stage.id, said, lid: b.id, pid: p.id, times: 1, order: 0 }; nps.contract_qty = this.ctx.helper.round(bp.contract_qty, decimal.qty); nps.qc_qty = 0; nps.qc_minus_qty = 0; nps.positive_qc_qty = 0; nps.negative_qc_qty = 0; insertPosData.push(nps); if (bp.change) { for (const c of bp.change) { if (!c.qty) continue; const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: p.id, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus, no_value: c.no_value }; const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid); ncs.qty = ncs.minus ? Math.max(validQty, c.qty) : Math.min(validQty, c.qty); insertChangeData.push(ncs); if (!ncs.no_value) { nps.qc_qty = this.ctx.helper.add(nps.qc_qty, ncs.qty); if (ncs.minus) { nps.negative_qc_qty = this.ctx.helper.add(nps.negative_qc_qty, ncs.qty); } else { nps.positive_qc_qty = this.ctx.helper.add(nps.positive_qc_qty, ncs.qty); } } else { nps.qc_minus_qty = this.ctx.helper.add(nps.qc_minus_qty, ncs.qty); } } } nbs.contract_qty = this.ctx.helper.add(nbs.contract_qty, nps.contract_qty); nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, nps.qc_qty); nbs.qc_minus_qty = this.ctx.helper.add(nbs.qc_minus_qty, nps.qc_minus_qty); nbs.positive_qc_qty = this.ctx.helper.add(nbs.positive_qc_qty, nps.positive_qc_qty); nbs.negative_qc_qty = this.ctx.helper.add(nbs.negative_qc_qty, nps.negative_qc_qty); } nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp); nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp); nbs.positive_qc_tp = this.ctx.helper.mul(nbs.positive_qc_qty, b.unit_price, decimal.tp); nbs.negative_qc_tp = this.ctx.helper.mul(nbs.negative_qc_qty, b.unit_price, decimal.tp); } else { if (b.is_tp) { nbs.contract_tp = this.ctx.helper.round(d.contract_tp, decimal.tp); } else { nbs.contract_qty = this.ctx.helper.round(d.contract_qty, decimal.qty); nbs.contract_tp = this.ctx.helper.mul(nbs.contract_qty, b.unit_price, decimal.tp); if (d.change) { for (const c of d.change) { if (!c.qty) continue; const ncs = { tid: stage.tid, sid: stage.id, lid: b.id, pid: -1, stimes: 1, sorder: 0, cid: c.cid, cbid: c.cbid, minus: c.minus, no_value: c.no_value }; const validQty = await this.ctx.service.stageChangeFinal.getChangeBillsValidQty(c.cbid); ncs.qty = ncs.minus ? Math.max(validQty, c.qty) : Math.min(validQty, c.qty); insertChangeData.push(ncs); if (!ncs.no_value) { nbs.qc_qty = this.ctx.helper.add(nbs.qc_qty, ncs.qty); if (ncs.minus) { nbs.negative_qc_qty = this.ctx.helper.add(nbs.negative_qc_qty, ncs.qty); } else { nbs.positive_qc_qty = this.ctx.helper.add(nbs.positive_qc_qty, ncs.qty); } } else { nbs.qc_minus_qty = this.ctx.helper.add(nbs.qc_minus_qty, ncs.qty); } } } nbs.qc_tp = this.ctx.helper.mul(nbs.qc_qty, b.unit_price, decimal.tp); nbs.positive_qc_tp = this.ctx.helper.mul(nbs.positive_qc_qty, b.unit_price, decimal.tp); nbs.negative_qc_tp = this.ctx.helper.mul(nbs.negative_qc_qty, b.unit_price, decimal.tp); } } insertBillsData.push(nbs); } return [insertBillsData, insertPosData, insertChangeData] } async recover(stage, id) { const stash = await this.getDataById(id); if (!stash) throw '暂存计量不存在'; if (stash.tid !== stage.tid) throw '暂存计量不可导入'; const stashData = await this.loadAndAnalysis(stash.filepath); const [insertBills, insertPos, insertChange] = await this.reCalcStashData(stage, stashData); const conn = await this.db.beginTransaction(); try { await conn.delete(this.ctx.service.stageBills.tableName, { sid: stage.id }); await conn.delete(this.ctx.service.stageChange.tableName, { sid: stage.id }); await conn.delete(this.ctx.service.stagePos.tableName, { sid: stage.id }); if (insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, insertBills); if (insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, insertPos); if (insertChange.length > 0) conn.insert(this.ctx.service.stageChange.tableName, insertChange); await conn.commit(); } catch (err) { await conn.rollback(); throw err; } }; /** * 导入Excel期计量(仅导入合同计量) * 该方法本应该独立或者在stage下,但是跟stage_stash业务非常类似,暂归类于此 * @param stage * @param sheet * @returns {Promise} */ async loadExcelSheet(stage, excelData) { const AnalysisExcel = require('../lib/analysis_excel').AnalysisStageExcelTree; const analysisExcel = new AnalysisExcel(this.ctx, this.ctx.service.ledger.setting); try { const templateId = await this.ctx.service.valuation.getValuationTemplate( this.ctx.tender.data.valuation, this.ctx.tender.data.measure_type); const tempData = await this.ctx.service.tenderNodeTemplate.getData(templateId, true); const cacheTree = analysisExcel.analysisData(excelData, tempData, { filterZeroGcl: false }); const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({ columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price'], where: { tender_id: stage.tid}, }); const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']); this.ctx.helper.assignRelaData(ledgerData, [ { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' }, ]); const posData = await this.ctx.service.pos.getAllDataByCondition({ columns: ['id', 'lid', 'name', 'porder'], where: { tid: stage.tid }, }); const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } }); const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } }); const stageBillsDgn = await this.ctx.service.stageBillsDgn.getAllDataByCondition({ where: { tid: stage.tid } }); const loadModal = new loadStageExcelTree(this.ctx); loadModal.load(cacheTree, {ledgerData, posData, stageBills, stagePos, stageBillsDgn, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } }); const conn = await this.db.beginTransaction(); try { if (loadModal.insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, loadModal.insertBills); if (loadModal.updateBills.length > 0) conn.updateRows(this.ctx.service.stageBills.tableName, loadModal.updateBills); if (loadModal.insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, loadModal.insertPos); if (loadModal.updatePos.length > 0) conn.updateRows(this.ctx.service.stagePos.tableName, loadModal.updatePos); if (loadModal.insertDgn.length > 0) conn.insert(this.ctx.service.stageBillsDgn.tableName, loadModal.insertDgn); if (loadModal.updateDgn.length > 0) conn.updateRows(this.ctx.service.stageBillsDgn.tableName, loadModal.updateDgn); await conn.commit(); } catch (err) { await conn.rollback(); this.ctx.log(err); throw '保存导入数据失败'; } } catch (err) { this.ctx.log(err); throw '解析Excel错误'; } } } return StageStash; };