'use strict'; /** * * * @author Mai * @date * @version */ 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, revise_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_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 = await this.getDataById(newIds); if (!lastChild) { result.ledger.update = await this.getDataById(select.id); } result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds); 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_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 = await this.getDataById(newIds); result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length); result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds); return result; } /** * * @param node * @param transaction * @returns {Promise} * @private */ async _calcNode(node, transaction) { const info = this.ctx.tender.info; const precision = this.ctx.helper.findPrecision(info.precision, node.unit); const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?'; const data = await transaction.queryOne(calcQtySql, [this.ctx.service.revisePos.tableName, node.id]); data.id = node.id; data.sgfh_qty = this.round(data.sgfh_qty, precision.value); data.sjcl_qty = this.round(data.sjcl_qty, precision.value); data.qtcl_qty = this.round(data.qtcl_qty, precision.value); data.quantity = this.round(data.quantity, precision.value); data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp); data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp); data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp); data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp); const result = await transaction.update(this.tableName, data); } /** * * @param {Number} tid - 标段id * @param {Number} id - 需要计算的节点的id * @param {Object} transaction - 操作所属事务,没有则创建 * @return {Promise} */ async calc(tid, id, transaction) { const node = await transaction.get(this.tableName, {id: id}); if (!node) { throw '数据错误'; } await this._calcNode(node, transaction); } } return ReviseBills; };