'use strict'; /** * * * @author Mai * @date * @version */ const SumLoad = require('../lib/sum_load'); module.exports = app => { class ReviseBills extends app.BaseBillsService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx, { mid: 'tender_id', kid: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', isLeaf: 'is_leaf', fullPath: 'full_path', keyPre: 'revise_bills_maxLid:', uuid: true, }, 'revisePos'); this.depart = 10; this.tableName = 'revise_bills'; } async _deleteRelaData (mid, deleteData) { await this.ctx.service.revisePos.deletePosData(this.transaction, mid, this._.map(deleteData, 'id')); } /** * 新增节点 * @param {Number} tid - 台账id * @param {uuid} rid - 修订id * @param {Number} lid - 清单节点id * @returns {Promise} */ async addReviseNode(tid, rid, lid, count) { if (!rid) return null; return await this.addNodeBatch(tid, lid, {crid: rid, check_calc: 1}, count); } /** * 批量插入子项 * @param {uuid} rid - 修订id * @param {Number} lid - 节点id * @param {Object} data - 批量插入数据 * @return {Promise} */ async batchInsertChild(tid, rid, lid, data) { const result = { ledger: {}, pos: null }; if (!tid || !rid || !lid) return result; const select = await this.getDataByKid(tid, lid); if (!select) { throw '位置数据错误'; } // 计算id和order const maxId = await this._getMaxLid(tid); const lastChild = await this.getLastChildData(tid, lid); const order = lastChild ? lastChild.order : 0; // 整理数据 const bills = [], pos = [], newIds = []; for (let i = 0, iLen = data.length; i < iLen; i++) { // 合并新增数据 const qd = { crid: rid, id: this.uuid.v4(), tender_id: tid, ledger_id: maxId + i + 1, ledger_pid: select.ledger_id, is_leaf: true, order: order + i + 1, level: select.level + 1, b_code: data[i].b_code, name: data[i].name, unit: data[i].unit, unit_price: data[i].price, check_calc: 1, }; const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); qd.sgfh_qty = 0; for (const p of data[i].pos) { const inP = { id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid, add_stage: 0, add_stage_order: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId, name: p.name, drawing_code: p.drawing_code, porder: p.porder, }; if (p.quantity) { inP.sgfh_qty = this.round(p.quantity, precision.value); inP.quantity = inP.sgfh_qty; } qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty); pos.push(inP); } qd.full_path = select.full_path + '-' + qd.ledger_id; qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp); qd.quantity = qd.sgfh_qty; qd.total_price = qd.sgfh_tp; bills.push(qd); newIds.push(qd.id); } this.transaction = await this.db.beginTransaction(); try { // 更新父项isLeaf if (!lastChild) { await this.transaction.update(this.tableName, { id: select.id, is_leaf: false, unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null, }); } // 数据库创建新增节点数据 await this.transaction.insert(this.tableName, bills); if (pos.length > 0) { await this.transaction.insert(this.ctx.service.revisePos.tableName, pos); } this._cacheMaxLid(tid, maxId + data.length); await this.transaction.commit(); } catch (err) { await this.transaction.rollback(); throw err; } // 查询应返回的结果 result.ledger.create = bills; if (!lastChild) result.ledger.update = await this.getDataById(select.id); result.pos = pos; return result; } /** * 批量插入后项 * @param {Number} tid - 台账id * @param {uuid} rid - 修订id * @param {Number} lid - 节点id * @param {Object} data - 批量插入数据 * @return {Promise} */ async batchInsertNext(tid, rid, lid, data) { const result = { ledger: {}, pos: null }; if (!tid || !rid || !lid) return result; const select = await this.getDataByKid(tid, lid); if (!select) { throw '位置数据错误'; } const parentData = await this.getDataByKid(tid, select.ledger_pid); if (!parentData) { throw '位置数据错误'; } // 计算id和order const maxId = await this._getMaxLid(tid); const order = select.order; // 整理数据 const bills = [], pos = [], newIds = []; for (let i = 0, iLen = data.length; i < iLen; i++) { // 合并新增数据 const qd = { crid: rid, id: this.uuid.v4(), tender_id: tid, ledger_id: maxId + i + 1, ledger_pid: parentData.ledger_id, is_leaf: true, order: order + i + 1, level: parentData.level + 1, b_code: data[i].b_code, name: data[i].name, unit: data[i].unit, unit_price: data[i].price, check_calc: 1, }; const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); qd.sgfh_qty = 0; for (const p of data[i].pos) { const inP = { id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid, add_stage: 0, add_stage_order: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId, name: p.name, drawing_code: p.drawing_code, porder: p.porder, }; if (p.quantity) { inP.sgfh_qty = this.round(p.quantity, precision.value); inP.quantity = inP.sgfh_qty; } qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty); pos.push(inP); } qd.full_path = parentData.full_path + '-' + qd.ledger_id; qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp); qd.quantity = qd.sgfh_qty; qd.total_price = qd.sgfh_tp; bills.push(qd); newIds.push(qd.id); } this.transaction = await this.db.beginTransaction(); try { // 选中节点的所有后兄弟节点,order+粘贴节点个数 await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1); // 数据库创建新增节点数据 await this.transaction.insert(this.tableName, bills); if (pos.length > 0) { await this.transaction.insert(this.ctx.service.revisePos.tableName, pos); } this._cacheMaxLid(tid, maxId + data.length); await this.transaction.commit(); } catch (err) { await this.transaction.rollback(); throw err; } // 查询应返回的结果 result.ledger.create = bills; result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length); result.pos = pos; return result; } async sumLoad(lid, rid, tenders) { const conn = await this.db.beginTransaction(); try { const maxId = await this._getMaxLid(this.ctx.tender.id); const select = await this.getDataById(lid); const sumLoad = new SumLoad(this.ctx); const loadTree = await sumLoad.updateGatherGcl(select, maxId, tenders, { crid: rid, }); const result = loadTree.getUpdateData(); if (result.errors.length > 100) throw '您导入的数据存在大量数据错误,请您仔细检查'; this._cacheMaxLid(this.ctx.tender.id, loadTree.keyNodeId); const his = await this.ctx.service.sumLoadHistory.saveReviseHistory(this.ctx.tender.id, rid, lid, tenders, result.errors); if (result.update.length > 0) await conn.updateRows(this.tableName, result.update); if (result.create.length > 0)await conn.insert(this.tableName, result.create); await conn.commit(); return { create: result.create, update: result.update, sumLoadHis: his}; } catch (err) { await conn.rollback(); throw (err.stack ? err : '导入工程量数据出错'); } } } return ReviseBills; };