123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- class loadStageExcelTree {
- constructor(ctx) {
- this.ctx = ctx;
- this.decimal = ctx.tender.info.decimal;
- this.settleStatus = ctx.service.settle.settleStatus;
- 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 || sp.settle_status === this.settleStatus.finish) 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 || cur.settle_status === this.settleStatus.finish) 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 settleStatus = this.ctx.service.settle.settleStatus;
- 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']);
- const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
- this.ctx.helper.assignRelaData(bills, [
- { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
- { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
- ]);
- const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: stage.tid } });
- const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
- this.ctx.helper.assignRelaData(pos, [
- { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
- ]);
- const said = this.ctx.session.sessionUser.accountId;
- for (const d of data) {
- const b = bills.find(x => { return x.id === d.lid });
- if (!b || b.settle_status === settleStatus.finish) 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 || p.settle_status === settleStatus.finish) 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<void>}
- */
- 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']);
- const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
- this.ctx.helper.assignRelaData(ledgerData, [
- { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
- { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
- ]);
- const posData = await this.ctx.service.pos.getAllDataByCondition({
- columns: ['id', 'lid', 'name', 'porder'],
- where: { tid: stage.tid },
- });
- const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
- this.ctx.helper.assignRelaData(posData, [
- { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
- ]);
- 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;
- };
|