'use strict'; /** * 调差表(多期独立单价使用) 数据模型 * * @author Mai * @date 2018/8/13 * @version */ const materialConst = require('../const/material'); module.exports = app => { class MaterialExponentNode extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'material_exponent_node'; } async updateNode(data) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } if (!data.baseNodes || !data.addNodes || !data.delNodes) { throw '参数错误'; } // 判断t_type是否为费用,且存在quantity,m_spread值 const transaction = await this.db.beginTransaction(); try { if (data.remove_node && data.remove_node.length > 0 && this.ctx.material.pre_exponent_node) { // 移除pre_exponent_node值 const newPreExponentNode = this.ctx.material.pre_exponent_node ? this.ctx.material.pre_exponent_node.split(',').filter(item => !data.remove_node.includes(item)).join(',') : ''; await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, pre_exponent_node: newPreExponentNode }); this.ctx.material.pre_exponent_node = newPreExponentNode; } // 清空nodes并把总金额插回到exponent_stage或者material表里 if (data.exponent_node.length === 0 && this.ctx.material.pre_exponent_node) throw '上期存在已调用分项,无法清空'; const exponentList = await transaction.select(this.ctx.service.materialExponent.tableName, { where: { tid: this.ctx.tender.id } }); if (data.exponent_node.length > 0) { // 之前不存在已调用分项且本次有新增分项 const oldMsShardList = !this.ctx.material.exponent_node && this.ctx.material.is_stage_self ? await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id } }) : []; if (!this.ctx.material.exponent_node) { await transaction.delete(this.ctx.service.materialExponentShard.tableName, { mid: this.ctx.material.id }); const insertBaseDatas = []; for (const base of data.baseNodes) { base.tid = this.ctx.tender.id; base.mid = this.ctx.material.id; base.ex_calc = JSON.stringify(base.ex_calc); insertBaseDatas.push(base); } if (insertBaseDatas.length > 0) await transaction.insert(this.tableName, insertBaseDatas); } else { const updateBaseNodes = []; const oldBaseDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id, node: '-1' } }); for (const base of data.baseNodes) { const old = this._.find(oldBaseDatas, { id: base.id }); if (old) { old.ex_calc = JSON.stringify(base.ex_calc); old.contract_tp = base.contract_tp; old.qc_tp = base.qc_tp; old.gather_tp = base.gather_tp; updateBaseNodes.push(old); } } if (updateBaseNodes.length > 0) await transaction.updateRows(this.tableName, updateBaseNodes); } const oldDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } }); const insertDatas = []; for (const add of data.addNodes) { add.tid = this.ctx.tender.id; add.mid = this.ctx.material.id; add.ex_calc = JSON.stringify(add.ex_calc); insertDatas.push(add); } if (insertDatas.length > 0) { await transaction.insert(this.tableName, insertDatas); const newNodeDatas = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } }); const insertExponentShardDatas = []; if (this.ctx.material.is_stage_self) { const msList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }); for (const node of newNodeDatas) { const ms = this._.find(msList, { id: node.ms_id }); for (const ex of exponentList) { const exponentShardOlds = this._.filter(oldMsShardList, { ms_id: ms.id, me_id: ex.id }); insertExponentShardDatas.push({ tid: this.ctx.tender.id, mid: this.ctx.material.id, ms_id: ms.id, mn_id: node.id, me_id: ex.id, type: ex.type, m_price: !this.ctx.material.exponent_node ? exponentShardOlds.m_price : 0, remark: ex.remark, }); } } } else { for (const node of newNodeDatas) { for (const ex of exponentList) { insertExponentShardDatas.push({ tid: this.ctx.tender.id, mid: this.ctx.material.id, ms_id: null, mn_id: node.id, me_id: ex.id, type: ex.type, m_price: !this.ctx.material.exponent_node ? ex.m_price : 0, remark: ex.remark, }); } } } if (insertExponentShardDatas.length > 0) await transaction.insert(this.ctx.service.materialExponentShard.tableName, insertExponentShardDatas); } const delDatas = []; for (const del of data.delNodes) { const olds = this._.filter(oldDatas, { node: del }); for (const old of olds) { delDatas.push(old.id); } } if (delDatas.length > 0) { await transaction.delete(this.tableName, { id: delDatas }); await transaction.delete(this.ctx.service.materialExponentShard.tableName, { me_id: delDatas }); } // 重算所有并更新到material表、material_stage表、material_exponent_node表 await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id); await this.ctx.service.materialExponent.calcTpByMaterialExponentNode(transaction, this.ctx.material.id); await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, exponent_node: data.exponent_node.join(',') }); } else { // 清空所有节点并重复赋值给期或者总材料节点 await transaction.delete(this.tableName, { mid: this.ctx.material.id }); await transaction.delete(this.ctx.service.materialExponentShard.tableName, { mid: this.ctx.material.id }); await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, exponent_node: '' }); if (this.ctx.material.is_stage_self) { const msList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }); for (const ms of msList) { const insertExponentShardDatas = []; for (const ex of exponentList) { insertExponentShardDatas.push({ tid: this.ctx.tender.id, mid: this.ctx.material.id, ms_id: ms.id, mn_id: null, me_id: ex.id, type: ex.type, m_price: 0, remark: ex.remark, }); } if (insertExponentShardDatas.length > 0) await transaction.insert(this.ctx.service.materialExponentShard.tableName, insertExponentShardDatas); const baseNode = this._.find(data.baseNodes, { ms_id: ms.id }); if (!baseNode) throw '标段(不含新增分项)数据不存在'; await this.ctx.service.materialExponentShard.calcMaterialExTp(transaction, baseNode.ex_calc, this.ctx.material.id, ms.id); } await this.ctx.service.materialExponent.calcTpByMaterialStageExponent(transaction, this.ctx.material.id); } else { const baseNode = data.baseNodes[0]; if (!baseNode) throw '标段(不含新增分项)数据不存在'; await this.ctx.service.materialExponent.calcMaterialExTp(transaction, baseNode.ex_calc); } } await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async calcStageExTpByNode(transaction, mid, ms_id) { const mnList = await transaction.select(this.tableName, { where: { ms_id } }); let stage_ex_tp = 0; let stage_ex_tax_tp = 0; for (const mn of mnList) { stage_ex_tp = this.ctx.helper.add(stage_ex_tp, mn.ex_tp ? mn.ex_tp : 0); stage_ex_tax_tp = this.ctx.helper.add(stage_ex_tax_tp, mn.ex_tax_tp ? mn.ex_tax_tp : 0); } await transaction.update(this.ctx.service.materialStage.tableName, { id: ms_id, ex_tp: stage_ex_tp, ex_tax_tp: stage_ex_tax_tp, }); } async getPreNodeData(mid, tid) { const materials = await this.ctx.service.material.getAllDataByCondition({ where: { tid }, order: ['order'], }); const preMaterial = materials.length > 1 ? materials[materials.length - 2] : null; // if (!preMaterial) throw '上期不存在'; const preNodes = preMaterial ? await this.getAllDataByCondition({ where: { mid: preMaterial.id } }) : []; return preNodes; } } return MaterialExponentNode; };