123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 'use strict';
- /**
- * 清单设置 数据模型
- * @author LanJianRong
- * @date 2020/6/30
- * @version
- */
- const materialConst = require('../const/material');
- module.exports = app => {
- class MaterialChecklist extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_checklist';
- }
- async resetData(pushData, removeData, updateData) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- if (pushData.length > 0) {
- const insertDatas = [];
- for (const p of pushData) {
- p.mid = this.ctx.material.id;
- p.tid = this.ctx.tender.id;
- insertDatas.push(p);
- }
- await transaction.insert(this.tableName, insertDatas);
- }
- if (removeData.length > 0) {
- for (const r of removeData) {
- await transaction.delete(this.tableName, { id: r });
- }
- }
- if (updateData.length > 0) {
- await transaction.updateRows(this.tableName, updateData);
- }
- await transaction.commit();
- const materialChecklistData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
- const self = this;
- return await materialChecklistData.sort(function(a, b) {
- return self.ctx.helper.compareCode(a.b_code, b.b_code);
- });
- } catch (err) {
- console.log(err);
- await transaction.rollback();
- throw err;
- }
- }
- async updateHadBills(transaction, id, had_bills) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- return await transaction.update(this.tableName, { id, had_bills });
- }
- async addExportCB(addChecklist, addBillsList) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- if (addChecklist.length > 0) {
- const insertDatas = [];
- for (const p of addChecklist) {
- p.mid = this.ctx.material.id;
- p.tid = this.ctx.tender.id;
- insertDatas.push(p);
- }
- await transaction.insert(this.tableName, insertDatas);
- }
- if (addBillsList.length > 0) {
- let order = await this.ctx.service.materialBills._getMaxOrder(this.ctx.tender.id);
- const pushBills = [];
- for (const b of addBillsList) {
- const newBills = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- code: b.code,
- name: b.name,
- unit: b.unit,
- order: order + 1,
- in_time: new Date(),
- };
- pushBills.push(newBills);
- ++order;
- }
- // 新增工料
- const result = await transaction.insert(this.ctx.service.materialBills.tableName, pushBills);
- // 获取刚批量添加的所有list
- for (let j = 0; j < pushBills.length; j++) {
- pushBills[j].id = result.insertId + j;
- }
- if (this.ctx.material.is_stage_self) {
- await this.ctx.service.materialStageBills.adds(transaction, pushBills);
- }
- const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
- if (material_month.length > 0) {
- const insertArray = [];
- for (const pb of pushBills) {
- for (const ym of material_month) {
- const one_month = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- mb_id: pb.id,
- msg_tp: null,
- yearmonth: ym,
- };
- insertArray.push(one_month);
- }
- }
- if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
- }
- }
- await transaction.commit();
- const materialChecklistData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
- const searchsql = { tid: this.ctx.tender.id };
- let midList = [];
- if (this.ctx.material.highOrder !== this.ctx.material.order) {
- midList = await this.ctx.service.material.getPreMidList(this.ctx.tender.id, this.ctx.material.order);
- searchsql.mid = midList;
- }
- searchsql.t_type = materialConst.t_type[0].value;
- const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: searchsql, orders: [['order', 'asc']] });
- // 取对应期的截取上期的调差金额和应耗数量
- if (this.ctx.material.highOrder !== this.ctx.material.order) {
- for (const [mindex, mb] of materialBillsData.entries()) {
- const result = await this.ctx.service.materialBillsHistory.getByMbId(this.ctx.material.id, this.ctx.material.order, mb.id);
- this._.forEach(result, function(value, key) {
- if (key === 'mb_id') {
- materialBillsData[mindex].id = result ? result[key] : null;
- } else {
- materialBillsData[mindex][key] = result ? result[key] : null;
- }
- });
- }
- }
- const materialStageBillsData = this.ctx.material.is_stage_self ? await this.ctx.service.materialStageBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } }) : [];
- const self = this;
- return { materialChecklistData: await materialChecklistData.sort(function(a, b) {
- return self.ctx.helper.compareCode(a.b_code, b.b_code);
- }), materialBillsData, materialStageBillsData };
- } catch (err) {
- console.log(err);
- await transaction.rollback();
- throw err;
- }
- }
- }
- return MaterialChecklist;
- };
|