| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- 'use strict';
- /**
- * 期计量 数据模型
- *
- * @author Mai
- * @date 2018/8/13
- * @version
- */
- const auditConst = require('../const/audit').material;
- const materialConst = require('../const/material');
- const MaterialCalculator = require('../lib/material_calc');
- module.exports = app => {
- class MaterialExponent extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_exponent';
- }
- /**
- * 添加指数清单
- * @return {void}
- */
- async add() {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const resultData = {};
- const newExponent = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- in_time: new Date(),
- };
- // 新增工料
- const result = await transaction.insert(this.tableName, newExponent);
- if (result.affectedRows !== 1) {
- throw '新增指数数据失败';
- }
- switch (this.ctx.material.exponentStatus) {
- case materialConst.exponent_status.shared_noNode:
- break;
- case materialConst.exponent_status.shared_node:
- case materialConst.exponent_status.self_noNode:
- case materialConst.exponent_status.self_node:
- await this.ctx.service.materialExponentShard.add(transaction, this.ctx.material, result.insertId);
- resultData.pushExponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id, me_id: result.insertId } });
- break;
- default: break;
- }
- await transaction.commit();
- resultData.info = await this.getDataById(result.insertId);
- return resultData;
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- throw error;
- }
- }
- /**
- * 删除指数清单
- * @param {int} id 工料id
- * @return {void}
- */
- async del(id) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- // 判断t_type是否为费用,且存在quantity,m_spread值
- const transaction = await this.db.beginTransaction();
- try {
- const result = {};
- const info = {};
- const meInfo = await this.getDataById(id);
- await transaction.delete(this.tableName, { id });
- if (meInfo.weight_num === null) {
- await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { mid: this.ctx.material.id, me_id: id });
- }
- switch (this.ctx.material.exponentStatus) {
- case materialConst.exponent_status.shared_noNode:
- result.ex_tp = this.ctx.material.ex_tp;
- result.ex_expr = this.ctx.material.ex_expr;
- if (meInfo.is_summary === materialConst.is_summary.yes) {
- // 金额发生变化,则重新计算本期金额
- [result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
- }
- break;
- case materialConst.exponent_status.shared_node:
- await transaction.delete(this.ctx.service.materialExponentShard.tableName, { me_id: id });
- if (meInfo.is_summary === materialConst.is_summary.yes) {
- info.nodeList = await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } });
- for (const node of info.nodeList) {
- await this.ctx.service.materialExponentShard.calcMaterialExTp(transaction, node.ex_calc ? JSON.parse(node.ex_calc) : null, this.ctx.material.id, null, node.id);
- }
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialExponentNode(transaction, this.ctx.material.id);
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, {where: {mid: this.ctx.material.id}});
- result.nodeData = await transaction.select(this.ctx.service.materialExponentNode.tableName, {where: {mid: this.ctx.material.id}});
- }
- break;
- case materialConst.exponent_status.self_noNode:
- await transaction.delete(this.ctx.service.materialExponentShard.tableName, { me_id: id });
- if (meInfo.is_summary === materialConst.is_summary.yes) {
- info.materialStages = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
- for (const ms of info.materialStages) {
- await this.ctx.service.materialExponentShard.calcMaterialExTp(transaction, ms.ex_calc ? JSON.parse(ms.ex_calc) : null, this.ctx.material.id, ms.id);
- }
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id } });
- result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
- }
- break;
- case materialConst.exponent_status.self_node:
- await transaction.delete(this.ctx.service.materialExponentShard.tableName, { me_id: id });
- if (meInfo.is_summary === materialConst.is_summary.yes) {
- info.materialStages = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
- info.materialNodes = await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } });
- for (const ms of info.materialStages) {
- const nodeList = this._.filter(info.materialNodes, { ms_id: ms.id });
- for (const node of nodeList) {
- await this.ctx.service.materialExponentShard.calcMaterialExTp(transaction, node.ex_calc ? JSON.parse(node.ex_calc) : null, this.ctx.material.id, null, node.id);
- }
- await this.ctx.service.materialExponentNode.calcStageExTpByNode(transaction, this.ctx.material.id, ms.id);
- }
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id } });
- result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
- result.nodeData = await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } });
- }
- break;
- default: break;
- }
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改指数清单信息
- * @param {Object} data 工料内容
- * @return {void}
- */
- async save(data, ms_id = null, mn_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- delete data.in_time;
- // delete data.m_tp;
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- const result = {};
- const info = {};
- switch (this.ctx.material.exponentStatus) {
- case materialConst.exponent_status.shared_noNode:
- await transaction.update(this.tableName, data);
- [result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
- break;
- case materialConst.exponent_status.shared_node:
- if (!mn_id) {
- throw '分项参数有误';
- }
- [info.needUp, info.needCalc] = await this._updateOneExponentData(transaction, data, null, mn_id);
- await this.ctx.service.materialExponentShard.update(transaction, data, null, mn_id, info.needCalc);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialExponentNode(transaction, this.ctx.material.id);
- if (info.needUp) {
- result.exponentData = await transaction.select(this.tableName, { where: { id: data.id } });
- }
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id, mn_id, me_id: data.id } });
- result.nodeData = info.needCalc ? await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id, id: mn_id } });
- break;
- case materialConst.exponent_status.self_noNode:
- if (!ms_id) {
- throw '期参数有误';
- }
- [info.needUp, info.needCalc] = await this._updateOneExponentData(transaction, data, ms_id);
- await this.ctx.service.materialExponentShard.update(transaction, data, ms_id, null, info.needCalc);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- if (info.needUp) {
- result.exponentData = await transaction.select(this.tableName, { where: { id: data.id } });
- }
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id, ms_id, me_id: data.id } });
- result.stageData = info.needCalc ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
- break;
- case materialConst.exponent_status.self_node:
- if (!ms_id && !mn_id) {
- throw '参数有误';
- }
- [info.needUp, info.needCalc] = await this._updateOneExponentData(transaction, data, ms_id, mn_id);
- await this.ctx.service.materialExponentShard.update(transaction, data, ms_id, mn_id, info.needCalc);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- if (info.needUp) {
- result.exponentData = await transaction.select(this.tableName, { where: { id: data.id } });
- }
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id, ms_id, mn_id, me_id: data.id } });
- result.stageData = info.needCalc ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
- result.nodeData = info.needCalc ? await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id, id: mn_id } });
- break;
- default: break;
- }
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _updateOneExponentData(transaction, data, ms_id = null, mn_id = null) {
- // 当以下值和exponent值不相同时,需要同步更新最新的计算表达式和金额到stage表里,无需更新到exponentShard表
- const updateColsArray = ['symbol', 'symbol_desc', 'code', 'basic_price', 'is_summary', 'basic_times'];
- const updateCalcArray = ['weight_num', 'basic_price', 'is_summary'];
- let needCalc = false;
- let needUp = false;
- const meInfo = await this.getDataById(data.id);
- if (!meInfo) {
- throw '指数工料数据不存在';
- }
- const updateData = {};
- for (const uc of updateColsArray) {
- if (data[uc] !== undefined && data[uc] !== meInfo[uc]) {
- if (updateCalcArray.includes(uc)) {
- needCalc = true;
- }
- needUp = true;
- updateData[uc] = data[uc];
- }
- }
- // 判断updateData不为空时,不需要更新
- if (needUp) {
- updateData.id = data.id;
- await transaction.update(this.tableName, updateData);
- }
- if (needCalc) {
- if (mn_id) {
- await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id, mn_id);
- } else if (ms_id && !mn_id) {
- await this.ctx.service.materialExponentShard.calcMaterialAllExTpByStage(transaction, this.ctx.material.id, ms_id);
- }
- }
- return [needUp, needCalc];
- }
- /**
- * 复制粘贴指数清单
- * @param {Object} data 工料内容
- * @return {void}
- */
- async saveDatas(datas, ms_id = null, mn_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- // 判断是否可修改
- // 判断t_type是否为费用
- const transaction = await this.db.beginTransaction();
- try {
- const result = { ex_tp: this.ctx.material.ex_tp };
- const info = {};
- if (this.ctx.material.exponentStatus !== materialConst.exponent_status.shared_noNode) {
- info.meList = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
- }
- switch (this.ctx.material.exponentStatus) {
- case materialConst.exponent_status.shared_noNode:
- await transaction.updateRows(this.tableName, datas);
- [result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
- result.exponentData = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
- break;
- case materialConst.exponent_status.shared_node:
- if (!mn_id) {
- throw '参数有误';
- }
- info.needCalc = await this._updateMoreExponentData(transaction, datas, result, info.meList, null, mn_id);
- if (info.needCalc) await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialExponentNode(transaction, this.ctx.material.id);
- result.nodeData = info.needCalc ? await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id, id: mn_id } });
- break;
- case materialConst.exponent_status.self_noNode:
- if (!ms_id) {
- throw '参数有误';
- }
- info.needCalc = await this._updateMoreExponentData(transaction, datas, result, info.meList, ms_id, null);
- if (info.needCalc) await this.ctx.service.materialExponentShard.calcMaterialAllExTpByStage(transaction, this.ctx.material.id);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- result.stageData = info.needCalc ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
- break;
- case materialConst.exponent_status.self_node:
- if (!mn_id && !ms_id) {
- throw '参数有误';
- }
- info.needCalc = await this._updateMoreExponentData(transaction, datas, result, info.meList, null, mn_id);
- if (info.needCalc) await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id);
- [result.ex_tp, result.ex_tax_tp] = await this.calcTpByMaterialStageExponent(transaction, this.ctx.material.id);
- result.nodeData = info.needCalc ? await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid: this.ctx.material.id, id: mn_id } });
- result.stageData = info.needCalc ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
- await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
- break;
- default: break;
- }
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _updateMoreExponentData(transaction, datas, result, meList, ms_id = null, mn_id = null) {
- // 判断data值是否需要更新materialExponent和materialExponentShard
- const mesCondition = { me_id: this._.map(datas, 'id') };
- if (ms_id !== null) mesCondition.ms_id = ms_id;
- if (mn_id !== null) mesCondition.mn_id = mn_id;
- const mesList = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: mesCondition });
- const materialExponentColArray = ['symbol', 'symbol_desc', 'code', 'basic_price', 'is_summary', 'basic_times'];
- const materialExponentShardColArray = ['weight_num', 'm_price', 'remark'];
- const updateCalcArray = ['weight_num', 'basic_price', 'm_price', 'is_summary'];
- const updateMeArray = [];
- const udpateMseArray = [];
- let needCalc = false;
- for (const data of datas) {
- const meInfo = this._.find(meList, { id: data.id });
- const mseInfo = this._.find(mesList, { me_id: data.id });
- for (const uc of materialExponentColArray) {
- if (data[uc] !== undefined && data[uc] !== meInfo[uc]) {
- const updateMe = this._.find(updateMeArray, { id: meInfo.id });
- if (!updateMe) {
- const newUpdateMe = { id: meInfo.id };
- newUpdateMe[uc] = data[uc];
- updateMeArray.push(newUpdateMe);
- } else {
- updateMe[uc] = data[uc];
- }
- if (updateCalcArray.includes(uc)) {
- needCalc = true;
- }
- }
- }
- for (const uc of materialExponentShardColArray) {
- if (data[uc] !== undefined && data[uc] !== mseInfo[uc]) {
- const updateMse = this._.find(udpateMseArray, { id: mseInfo.id });
- if (!updateMse) {
- const newUpdateMse = { id: mseInfo.id };
- newUpdateMse[uc] = data[uc];
- udpateMseArray.push(newUpdateMse);
- } else {
- updateMse[uc] = data[uc];
- }
- if (updateCalcArray.includes(uc)) {
- needCalc = true;
- }
- }
- }
- }
- console.log(updateMeArray, udpateMseArray);
- if (updateMeArray.length > 0) {
- await transaction.updateRows(this.tableName, updateMeArray);
- result.exponentData = await transaction.select(this.tableName, { where: { id: this._.map(updateMeArray, 'id') } });
- }
- if (udpateMseArray.length > 0) {
- await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, udpateMseArray);
- // const condition = mn_id ? { mid: this.ctx.material.id, mn_id } : { mid: this.ctx.material.id, ms_id };
- result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { id: this._.map(udpateMseArray, 'id') } });
- }
- return needCalc;
- }
- /**
- * 旧数据补充定值和历史数据
- * @returns {Promise<void>}
- */
- async addOldData() {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 先获取第一期的mid
- const first_material = await this.ctx.service.material.getDataByCondition({
- tid: this.ctx.tender.id,
- order: 1,
- });
- const insert_data = {
- tid: this.ctx.tender.id,
- mid: first_material.id,
- type: materialConst.ex_type[0].value,
- symbol: 'X',
- symbol_desc: '非可调因子',
- code: 'A',
- weight_num: 0,
- is_summary: materialConst.is_summary.yes,
- in_time: new Date(),
- };
- const result = await transaction.insert(this.tableName, insert_data);
- // 获取所有期数据
- const materials = await this.ctx.service.material.getAllDataByCondition({ where: {
- tid: this.ctx.tender.id,
- } });
- // const qi_order = high_material.status === auditConst.status.uncheck || high_material.status === auditConst.status.checkNo ? high_material.order - 1 : high_material;
- // const qi_order = high_material.status === auditConst.status.uncheck || high_material.status === auditConst.status.checkNo ? high_material.order - 1 : high_material;
- const insert_history_data = [];
- const insert_stage_data = [];
- for (const m of materials) {
- if (m.is_stage_self && m.is_new_exponent) {
- const materialStages = await this.ctx.service.materialStage.getAllDataByCondition({ where: {
- tid: this.ctx.tender.id,
- mid: m.id,
- } });
- for (const ms of materialStages) {
- const one_stage_insert = {
- tid: this.ctx.tender.id,
- mid: m.id,
- ms_id: ms.id,
- me_id: result.insertId,
- type: materialConst.ex_type[0].value,
- };
- insert_stage_data.push(one_stage_insert);
- }
- } else {
- const one_insert = {
- tid: this.ctx.tender.id,
- mid: first_material.id,
- order: m.order,
- me_id: result.insertId,
- type: materialConst.ex_type[0].value,
- weight_num: 0,
- is_summary: materialConst.is_summary.yes,
- };
- insert_history_data.push(one_insert);
- }
- }
- // for (let i = 1; i <= qi_order; i++) {
- // const one_insert = {
- // tid: this.ctx.tender.id,
- // mid: first_material.id,
- // order: i,
- // me_id: result.insertId,
- // type: materialConst.ex_type[0].value,
- // weight_num: 0,
- // is_summary: materialConst.is_summary.yes,
- // };
- // insert_history_data.push(one_insert);
- // }
- if (insert_history_data.length > 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, insert_history_data);
- if (insert_stage_data.length > 0) await transaction.insert(this.ctx.service.materialExponentShard.tableName, insert_stage_data);
- await transaction.commit();
- } catch (err) {
- console.log(err);
- await transaction.rollback();
- throw err;
- }
- }
- // 更改计算总指数金额并返回值和公式
- async calcMaterialExTp(transaction, ex_calc = (this.ctx.material.ex_calc ? JSON.parse(this.ctx.material.ex_calc) : null), mid = null, decimal = (this.ctx.material.exponent_decimal ? this.ctx.material.exponent_decimal : materialConst.exponent_decimal), rate = (this.ctx.material.rate ? this.ctx.material.exponent_rate : 10)) {
- let basic_calc = 0;
- // let basic_expr = 0;
- if (ex_calc) {
- for (const calc of ex_calc) {
- if (calc.select) {
- if (calc.code === 'zdy' && calc.result) {
- basic_calc = this.ctx.helper.add(basic_calc, calc.result);
- // basic_expr = calc.value;
- } else {
- basic_calc = this.ctx.helper.add(basic_calc, calc.value);
- // basic_expr = this.ctx.helper.add(basic_expr, calc.value);
- }
- }
- }
- }
- let expr = basic_calc + '*[';
- const exponentList = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id, is_summary: materialConst.is_summary.yes } });
- let sumByChange = 0;
- let constant = 0;
- for (const ex of exponentList) {
- if (ex.type === materialConst.ex_type[0].value) {
- constant = ex.weight_num ? ex.weight_num : 0;
- expr += constant.toString();
- } else if (ex.type === materialConst.ex_type[1].value) {
- const calc_num = ex.basic_price > 0 ? this.ctx.helper.mul(ex.weight_num, this.ctx.helper.div(ex.m_price, ex.basic_price)) : 0;
- const change = calc_num ? this.ctx.helper.round(calc_num, decimal.qty) : 0;
- // const change = ex.calc_num ? ex.calc_num : 0;
- expr = expr + (change !== 0 ? '+' + ex.weight_num.toString() + '*(' + ex.m_price.toString() + '/' + ex.basic_price.toString() + ')' : '');
- sumByChange = this.ctx.helper.add(sumByChange, change);
- }
- }
- expr += '-1]';
- expr = constant !== 0 ? expr : null;
- const ex_tp = constant !== 0 ? this.ctx.helper.round(this.ctx.helper.mul(basic_calc, this.ctx.helper.sub(this.ctx.helper.add(constant, sumByChange), 1)), decimal.tp) : null;
- const ex_tax_tp = ex_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(ex_tp, 1 + rate / 100), decimal.tp) : null;
- await transaction.update(this.ctx.service.material.tableName, {
- id: mid ? mid : this.ctx.material.id,
- ex_tp,
- ex_tax_tp,
- ex_expr: expr,
- ex_calc: JSON.stringify(ex_calc),
- });
- console.log(ex_tp, expr);
- if (!mid) {
- // 找出当前人并更新tp_data
- const tp_data = await this.ctx.service.materialAudit.getTpData(transaction, this.ctx.material.id);
- if (this.ctx.material.status === auditConst.status.uncheck || this.ctx.material.status === auditConst.status.checkNo) {
- await transaction.update(this.ctx.service.material.tableName, {
- id: this.ctx.material.id,
- tp_data: JSON.stringify(tp_data),
- });
- } else if (this.ctx.material.curAuditor) {
- await transaction.update(this.ctx.service.materialAudit.tableName, {
- id: this.ctx.material.curAuditor.id,
- tp_data: JSON.stringify(tp_data),
- });
- }
- }
- return [ex_tp, ex_tax_tp, expr];
- }
- /**
- * 更新新一期的现行价格指数,并返回指数总金额和公式
- * @param transaction
- * @param tid
- * @param mid
- * @returns {Promise<number>}
- */
- async updateNewMaterial(transaction, mid, ctx, stage_id, ex_calc, decimal, rate) {
- const stage_list = await ctx.service.stage.getStageMsgByStageId(stage_id);
- const calcBase = await ctx.service.stage.getMaterialCalcBase(stage_list, ctx.tender.info);
- const old_ex_calc = ex_calc ? JSON.parse(ex_calc) : null;
- const new_ex_calc = this._.cloneDeep(materialConst.ex_calc);
- for (const bq of new_ex_calc) {
- const calc = this._.find(calcBase, { code: bq.code }) || this._.find(calcBase, { code: 'bqwc' });
- const oldcalc = old_ex_calc ? this._.find(old_ex_calc, { code: bq.code }) : null;
- bq.value = calc.value;
- bq.select = oldcalc ? oldcalc.select : bq.select;
- }
- await this.calcMaterialExTp(transaction, new_ex_calc, mid, decimal, rate);
- return new_ex_calc;
- }
- async calcTpByMaterialExponentNode(transaction, mid) {
- // 汇总到material上
- const materialNodes = await transaction.select(this.ctx.service.materialExponentNode.tableName, { where: { mid } });
- let ex_tax_tp = 0;
- let total_ex_tp = 0;
- for (const ms of materialNodes) {
- ex_tax_tp = this.ctx.helper.add(ex_tax_tp, ms.ex_tax_tp);
- total_ex_tp = this.ctx.helper.add(total_ex_tp, ms.ex_tp || 0);
- }
- // if (updateTaxTps.length > 0) await transaction.updateRows(this.ctx.service.materialStage.tableName, updateTaxTps);
- await transaction.update(this.ctx.service.material.tableName, {
- id: mid,
- ex_tp: total_ex_tp,
- ex_tax_tp,
- });
- return [total_ex_tp, ex_tax_tp];
- }
- async calcTpByMaterialStageExponent(transaction, mid) {
- // 汇总到material上
- const materialStages = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid } });
- let ex_tax_tp = 0;
- let total_ex_tp = 0;
- for (const ms of materialStages) {
- ex_tax_tp = this.ctx.helper.add(ex_tax_tp, ms.ex_tax_tp);
- total_ex_tp = this.ctx.helper.add(total_ex_tp, ms.ex_tp || 0);
- }
- // if (updateTaxTps.length > 0) await transaction.updateRows(this.ctx.service.materialStage.tableName, updateTaxTps);
- await transaction.update(this.ctx.service.material.tableName, {
- id: mid,
- ex_tp: total_ex_tp,
- ex_tax_tp,
- });
- return [total_ex_tp, ex_tax_tp];
- }
- async resetDecimal(transaction, decimal) {
- const exponentList = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id } });
- const exponentShardList = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id } });
- const updateArray = [];
- const updateStageArray = [];
- const upColArray = ['basic_price', 'm_price'];
- // const qtyColArray = ['calc_num'];
- for (const ex of exponentList) {
- for (const col of upColArray) {
- if (ex[col] && ex[col] !== this.ctx.helper.round(ex[col], decimal.up)) {
- const upex = this._.find(updateArray, { id: ex.id });
- if (upex) {
- upex[col] = this.ctx.helper.round(ex[col], decimal.up);
- } else {
- const insertEx = {
- id: ex.id,
- };
- insertEx[col] = this.ctx.helper.round(ex[col], decimal.up);
- updateArray.push(insertEx);
- }
- }
- }
- // for (const col of qtyColArray) {
- // if (ex[col] && ex[col] !== this.ctx.helper.round(ex[col], decimal.qty)) {
- // const upex = this._.find(updateArray, { id: ex.id });
- // if (upex) {
- // upex[col] = this.ctx.helper.round(ex[col], decimal.qty);
- // } else {
- // const insertEx = {
- // id: ex.id,
- // };
- // insertEx[col] = this.ctx.helper.round(ex[col], decimal.qty);
- // updateArray.push(insertEx);
- // }
- // }
- // }
- }
- for (const ex of exponentShardList) {
- if (ex.m_price && ex.m_price !== this.ctx.helper.round(ex.m_price, decimal.up)) {
- const insertEx = {
- id: ex.id,
- };
- insertEx.m_price = this.ctx.helper.round(ex.m_price, decimal.up);
- updateStageArray.push(insertEx);
- }
- }
- if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
- if (updateStageArray.length > 0) await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, updateStageArray);
- }
- }
- return MaterialExponent;
- };
|