123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- '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 MaterialMonth extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_month';
- }
- /**
- * 返回月信息价列表
- * @return {void}
- */
- async getListByMid(mid) {
- return await this.getAllDataByCondition({ where: { mid } });
- }
- /**
- * 添加月信息价 并更新 工料平均单价、本期单价,调差金额等
- * @return {void}
- */
- async add(data, monthList, mbList) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
- material_month.push(data.yearmonth);
- material_month.sort();
- if (mbList.length !== 0) {
- const insertArray = [];
- const updateArray = [];
- for (const mb of mbList) {
- const one_month = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- mb_id: mb.id,
- msg_tp: monthList.length !== 0 ? null : mb.msg_tp,
- yearmonth: data.yearmonth,
- };
- insertArray.push(one_month);
- if (monthList.length !== 0) {
- const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
- const month_num = material_month.length - this.ctx.helper.arrayCount(this._.concat(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), one_month.msg_tp), [null, '', 0]);
- const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, one_month.msg_tp), month_num), 3);
- const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
- updateArray.push({
- id: mb.id,
- msg_tp: new_msg_tp,
- msg_spread: newmsg_spread,
- m_spread: newm_spread,
- m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
- });
- }
- }
- if (insertArray.length !== 0) await transaction.insert(this.tableName, insertArray);
- if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
- }
- await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
- const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
- await transaction.commit();
- this.ctx.material.months = material_month.join(',');
- return m_tp;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 删除月信息价 并更新 工料平均单价、本期单价,调差金额等
- * @return {void}
- */
- async del(data, monthList, mbList) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
- this._.remove(material_month, function(n) {
- return data.indexOf(n) !== -1;
- });
- await transaction.delete(this.tableName, { mid: this.ctx.material.id, yearmonth: data });
- if (mbList.length !== 0) {
- const updateArray = [];
- for (const mb of mbList) {
- this._.remove(monthList, function(m) {
- return data.indexOf(m.yearmonth) !== -1;
- });
- const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
- const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
- const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
- const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
- updateArray.push({
- id: mb.id,
- msg_tp: new_msg_tp,
- msg_spread: newmsg_spread,
- m_spread: newm_spread,
- m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
- });
- }
- if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
- }
- await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
- const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
- await transaction.commit();
- this.ctx.material.months = material_month.join(',');
- return m_tp;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改月信息价值 并更新 本期单价,调差金额等
- * @return {void}
- */
- async save(data) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
- await transaction.update(this.tableName, { msg_tp: data.value }, { where: { mb_id: data.mb_id, yearmonth: data.yearmonth, mid: this.ctx.material.id } });
- const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id, mid: this.ctx.material.id } });
- const mbInfo = await transaction.get(this.ctx.service.materialBills.tableName, { id: data.mb_id });
- if (monthList.length !== 0) {
- const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
- const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
- const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
- const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
- await transaction.update(this.ctx.service.materialBills.tableName, {
- id: mbInfo.id,
- msg_tp: new_msg_tp,
- msg_spread: newmsg_spread,
- m_spread: newm_spread,
- m_tp: this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), 2),
- });
- }
- const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
- await transaction.commit();
- return m_tp;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改多个月信息价值 并更新 本期单价,调差金额等
- * @return {void}
- */
- async saveDatas(datas, mbList) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
- const updateArray = [];
- for (const data of datas) {
- for (const m of material_month) {
- const one_update = {
- row: {
- msg_tp: data[m],
- },
- where: {
- mb_id: data.mb_id,
- yearmonth: m,
- },
- };
- updateArray.push(one_update);
- }
- }
- await transaction.updateRows(this.tableName, updateArray);
- const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
- if (mbList.length !== 0) {
- const mbUpdateArray = [];
- for (const mb of mbList) {
- const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
- const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
- const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
- const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
- mbUpdateArray.push({
- id: mb.id,
- msg_tp: new_msg_tp,
- msg_spread: newmsg_spread,
- m_spread: newm_spread,
- m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
- });
- }
- if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray);
- }
- const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
- await transaction.commit();
- return m_tp;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return MaterialMonth;
- };
|