| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 'use strict';
- /**
- * 调差清单-本期调差数量表 数据模型
- *
- * @author Mai
- * @date 2018/8/13
- * @version
- */
- module.exports = app => {
- class MaterialListQty extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_list_qty';
- }
- /**
- * 增删改本期数量及同步至调差清单表
- * @return {void}
- */
- async save(data, ms_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const mx_id = data.mx_id || '';
- const condition = {
- mid: this.ctx.material.id,
- ms_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id,
- };
- const now = new Date();
- const isDelete = data.qty == null;
- let record = null;
- if (data.id > 0) {
- record = { id: data.id };
- } else {
- record = await this.getDataByCondition(condition);
- }
- if (record) {
- if (isDelete) {
- await transaction.delete(this.tableName, { id: record.id });
- record = null;
- } else {
- await transaction.update(this.tableName, {
- id: record.id,
- qty: data.qty,
- in_time: now,
- });
- }
- } else if (!isDelete) {
- const insertData = {
- tid: this.ctx.tender.id,
- ...condition,
- qty: data.qty,
- in_time: now,
- };
- const result = await transaction.insert(this.tableName, insertData);
- record = { id: result.insertId };
- }
- const newData = record ? await transaction.get(this.tableName, condition) : null;
- await this.updateAllMaterials(transaction, data, newData, ms_id);
- await transaction.commit();
- return {
- info: newData,
- materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
- };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 增删改本期数量及同步至调差清单表
- * @return {void}
- */
- async savePastes(datas, ms_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const insertDatas = [];
- const updateDatas = [];
- const delDatas = [];
- const newDatas = [];
- for (const data of datas) {
- const mx_id = data.mx_id || '';
- const condition = {
- mid: this.ctx.material.id,
- ms_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id,
- };
- const now = new Date();
- const isDelete = data.qty == null;
- let record = null;
- if (data.id > 0) {
- record = { id: data.id };
- } else {
- record = await this.getDataByCondition(condition);
- }
- if (record) {
- if (isDelete) {
- delDatas.push(record.id);
- } else {
- updateDatas.push({
- id: record.id,
- qty: data.qty,
- in_time: now,
- });
- }
- } else if (!isDelete) {
- const insertData = {
- tid: this.ctx.tender.id,
- ...condition,
- qty: data.qty,
- in_time: now,
- };
- insertDatas.push(insertData);
- }
- newDatas.push({...condition, qty: data.qty});
- }
- if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
- if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
- if (delDatas.length > 0) await transaction.delete(this.tableName, { id: delDatas });
- await this.updateAllMaterialsByPastes(transaction, datas, newDatas, ms_id);
- await transaction.commit();
- return {
- qtyList: await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } }),
- materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
- };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改调差list和bill和material对应值
- * @param transaction
- * @returns {Promise<void>}
- */
- async updateAllMaterials(transaction, data, newData, ms_id = null) {
- // 先判断material_list是否存在值并quantity不为null
- const searchSql = {
- mid: this.ctx.material.id,
- ms_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id,
- };
- const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
- where: searchSql,
- });
- if (materialListData && materialListData.length !== 0) {
- // 对应修改更新本期应耗数量值和总的本期金额计算
- const mbIdList = [];
- const updateList = [];
- for (const ml of materialListData) {
- const updateData = {
- id: ml.id,
- qty: newData ? newData.qty : null,
- };
- if (mbIdList.indexOf(ml.mb_id) === -1) {
- mbIdList.push(ml.mb_id);
- }
- updateList.push(updateData);
- }
- if (updateList.length > 0) await transaction.updateRows(this.ctx.service.materialList.tableName, updateList);
- // 重新计算金额
- for (const mb_id of mbIdList) {
- await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
- }
- }
- }
- /**
- * 修改调差list和bill和material对应值
- * @param transaction
- * @returns {Promise<void>}
- */
- async updateAllMaterialsByPastes(transaction, datas, newDatas, ms_id = null) {
- // 先判断material_list是否存在值并quantity不为null
- if (datas.length > 0) {
- const updateList = [];
- const mbIdList = [];
- for (const data of datas) {
- const searchSql = {
- mid: this.ctx.material.id,
- ms_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id,
- };
- const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
- where: searchSql,
- });
- if (materialListData && materialListData.length !== 0) {
- const newData = this._.find(newDatas, searchSql);
- for (const ml of materialListData) {
- const updateData = {
- id: ml.id,
- qty: newData ? newData.qty : null,
- };
- if (mbIdList.indexOf(ml.mb_id) === -1) {
- mbIdList.push(ml.mb_id);
- }
- updateList.push(updateData);
- }
- }
- }
- if (updateList.length > 0) await transaction.updateRows(this.ctx.service.materialList.tableName, updateList);
- // 重新计算金额
- for (const mb_id of mbIdList) {
- await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
- }
- }
- }
- }
- return MaterialListQty;
- };
|