'use strict'; /** * 变更插入新增 - 部位明细 * * @author Mai * @date * @version */ module.exports = app => { class ChangePos extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); // this.depart = ctx.app.config.table_depart.heavy; this.tableName = 'change_pos'; } async getPosData(condition) { if (!condition.tid) throw '查询计量单元缺少必要信息'; return await this.db.select(this.tableName, { where: condition, columns: ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty', 'dagl_status', 'dagl_url', 'gxby_status', 'gxby_limit', 'dagl_limit', 'dagl_status', 'ex_memo1', 'ex_memo2', 'ex_memo3', 'ccid', 'formc'], order: [['porder', 'ASC']], }); } async _completeInsertPosData(tid, cid, data) { data.id = this.uuid.v4(); data.tid = tid; // todo 新增期 data.add_stage = 0; data.add_stage_order = 0; data.add_times = 0; data.in_time = new Date(); data.add_user = this.ctx.session.sessionUser.accountId; data.ccid = cid; data.formc = 1; } async addPos(tid, cid, data) { if (data instanceof Array) { for (const d of data) { this._completeInsertPosData(tid, cid, d); } } else { this._completeInsertPosData(tid, cid, data); } const transaction = await this.db.beginTransaction(); try { await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, [data.lid], 'gcl_id', ''); await transaction.insert(this.tableName, data); await transaction.commit(); const returnData = { pos: data }; const bills = await this.ctx.service.ledger.getDataById(data.lid); if (bills) { bills.cid = 1; returnData.ledger = { update: [bills], }; } return returnData; } catch (err) { await transaction.rollback(); throw err; } } async updatePos(tid, data) { if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) { const op = await this.getDataById(data.id); let bills = await this.ctx.service.changeLedger.getDataById(op.lid); let newBills = false; let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } }); if (bills) { newBills = true; } else { bills = await this.ctx.service.ledger.getDataById(op.lid); const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } }); billsPos = this._.concat(posData, billsPos); } const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); if (data.sgfh_qty !== undefined) { data.sgfh_qty = this.round(data.sgfh_qty, precision.value); } else if (op) { data.sgfh_qty = op.sgfh_qty; } if (data.sjcl_qty !== undefined) { data.sjcl_qty = this.round(data.sjcl_qty, precision.value); } else if (op) { data.sjcl_qty = op.sjcl_qty; } if (data.qtcl_qty !== undefined) { data.qtcl_qty = this.round(data.qtcl_qty, precision.value); } else if (op) { data.qtcl_qty = op.qtcl_qty; } data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]); const updateBills = { id: bills.id }; for (const bp of billsPos) { const calcData = bp.id === data.id ? data : bp; updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty); updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty); updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty); updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity); } const info = this.ctx.tender.info; updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp); updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp); updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp); updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp); const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, data); if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills); await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]); await transaction.commit(); updateBills.ledger_id = bills.ledger_id; return { ledger: { update: [updateBills] }, pos: data, }; } catch (err) { await transaction.rollback(); throw err; } } else { const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, data, { tid, id: data.id }); await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, [data]); await transaction.commit(); return { pos: data }; } catch (err) { await transaction.rollback(); throw err; } } } async updatePosArr(tid, data) { console.log(data); if (data.length === 0) return; const op = await this.getDataById(data[0].id) || await this.ctx.service.pos.getDataById(data[0].id); let bills = await this.ctx.service.changeLedger.getDataById(op.lid); let newBills = false; let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: op.lid } }); if (bills) { newBills = true; } else { bills = await this.ctx.service.ledger.getDataById(op.lid); const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: op.lid } }); billsPos = this._.concat(posData, billsPos); } const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); let needUpdateBills; for (const d of data) { if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) { if (d.sgfh_qty !== undefined) { d.sgfh_qty = this.round(d.sgfh_qty, precision.value); } else if (op) { d.sgfh_qty = op.sgfh_qty; } if (d.sjcl_qty !== undefined) { d.sjcl_qty = this.round(d.sjcl_qty, precision.value); } else if (op) { d.sjcl_qty = op.sjcl_qty; } if (d.qtcl_qty !== undefined) { d.qtcl_qty = this.round(d.qtcl_qty, precision.value); } else if (op) { d.qtcl_qty = op.qtcl_qty; } d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]); needUpdateBills = true; } } const updateBills = { id: bills.id }; if (needUpdateBills) { for (const bp of billsPos) { const newPos = data.find(function(x) { return x.id === bp.id; }); updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty) : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty); updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty) : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty); updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty) : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty); updateBills.quantity = newPos && newPos.quantity !== undefined ? this.ctx.helper.add(updateBills.quantity, newPos.quantity) : this.ctx.helper.add(updateBills.quantity, bp.quantity); } const info = this.ctx.tender.info; updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp); updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp); updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp); updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp); } const transaction = await this.db.beginTransaction(); try { for (const d of data) { // 区分d属于pos还是changePos,分别更新对应的表 const np = await this.getDataById(d.id); np ? await transaction.update(this.tableName, d) : await transaction.update(this.ctx.service.pos.tableName, d); } if (needUpdateBills && newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills); await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data); await transaction.commit(); updateBills.ledger_id = bills.ledger_id; return { ledger: { update: needUpdateBills ? [updateBills] : [] }, pos: data, }; } catch (err) { await transaction.rollback(); throw err; } } async deletePos(tid, data) { if (!data || data.length === 0) { throw '提交数据错误'; } const pos = await this.getPosData({tid: tid, id: data}); let bills = await this.ctx.service.changeLedger.getDataById(pos[0].lid); let newBills = false; let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } }); let cid = 1; if (bills) { newBills = true; } else { bills = await this.ctx.service.ledger.getDataById(pos[0].lid); const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: pos[0].lid } }); if (billsPos.length === data.length) { cid = null; } billsPos = this._.concat(posData, billsPos); } // const bills = await this.ctx.service.reviseBills.getDataById(pos[0].lid); // const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} }); const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null}; for (const bp of billsPos) { if (data.indexOf(bp.id) >= 0) continue; updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty); updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty); updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty); updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity); } const info = this.ctx.tender.info; updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp); updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp); updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp); updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp); const transaction = await this.db.beginTransaction(); try { await transaction.delete(this.tableName, {tid: tid, id: data}); if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills); await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, data, 'mx_id'); await transaction.commit(); updateBills.ledger_id = bills.ledger_id; updateBills.cid = cid; return { ledger: { update: [updateBills] }, pos: data, }; } catch (err) { await transaction.rollback(); throw err; } } async deleteBySettle(transaction, tid, posDatas) { if (!posDatas || posDatas.length === 0) { return; } await transaction.delete(this.tableName, { id: this._.map(posDatas, 'id') }); await this.ctx.service.changeAuditList.deleteDataByRevise(transaction, tid, this._.map(posDatas, 'id'), 'mx_id'); } /** * 复制粘贴 部位明细数据 * @param {Array} data - 复制粘贴的数据 * @param {Number} tid - 标段id * @returns {Promise<{ledger: {}, pos: null}>} */ async pastePosData(tid, cid, data) { if (!(data instanceof Array)) throw '提交数据错误'; const transaction = await this.db.beginTransaction(); const result = { ledger: {}, pos: null }; const orgPos = await this.getPosData({ tid: tid, id: this._.map(data, 'id') }); // const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid); // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); // const updateBills = {id: bills.id}; // const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} }); let bills = await this.ctx.service.changeLedger.getDataById(data[0].lid); let newBills = false; let billsPos = await this.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } }); if (bills) { newBills = true; } else { bills = await this.ctx.service.ledger.getDataById(data[0].lid); const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: tid, lid: data[0].lid } }); billsPos = this._.concat(posData, billsPos); } const updateBills = { id: bills.id }; const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); let needUpdateBills; for (const bp of billsPos) { const d = data.find(function(x) { return bp.id ? x.id === bp.id : false; }); if (d) continue; updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty); updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty); updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty); updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity); } try { for (const d of data) { const op = d.id ? this._.find(orgPos, { id: d.id }) : null; if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) { if (d.sgfh_qty !== undefined) { d.sgfh_qty = this.round(d.sgfh_qty, precision.value); } else if (op) { d.sgfh_qty = op.sgfh_qty; } updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty); if (d.sjcl_qty !== undefined) { d.sjcl_qty = this.round(d.sjcl_qty, precision.value); } else if (op) { d.sjcl_qty = op.sjcl_qty; } updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty); if (d.qtcl_qty) { d.qtcl_qty = this.round(d.qtcl_qty, precision.value); } else if (op) { d.qtcl_qty = op.qtcl_qty; } updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty); d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]); updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity); needUpdateBills = true; } if (d.id) { await transaction.update(this.tableName, d); } else { updateBills.cid = 1; await this._insertPosData(transaction, d, tid, cid); } } const info = this.ctx.tender.info; if (needUpdateBills) { updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp); updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp); updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp); updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp); if (newBills) await transaction.update(this.ctx.service.changeLedger.tableName, updateBills); updateBills.ledger_id = bills.ledger_id; } await this.ctx.service.changeAuditList.updateDataByRevisePos(transaction, tid, data); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } result.pos = data; result.ledger.update = needUpdateBills ? [updateBills] : updateBills.cid && updateBills.cid === 1 ? [{ id: bills.id, ledger_id: bills.ledger_id, cid: 1 }] : []; return result; } async _insertPosData(transaction, data, tid, cid) { data.id = this.uuid.v4(); data.tid = tid; // todo 新增期 data.add_stage = 0; data.add_stage_order = 0; data.add_times = 0; data.in_time = new Date(); data.add_user = this.ctx.session.sessionUser.accountId; data.ccid = cid; data.formc = 1; if (data.quantity) { const bills = await this.ctx.service.changeLedger.getDataById(data.lid) || await this.ctx.service.ledger.getDataById(data.lid); const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit); data.quantity = this.round(data.quantity, precision.value); } const addRst = await transaction.insert(this.tableName, data); } /** * 删除清单下部位明细数据(删除清单时调用) * * @param transaction - 事务 * @param tid - 标段id * @param lid - 清单id * @returns {Promise} */ async deletePosData(transaction, tid, lid) { await transaction.delete(this.tableName, {tid: tid, lid: lid}); } } return ChangePos; };