123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 'use strict';
- /**
- * 不参与调差-清单关联表 数据模型
- *
- * @author Mai
- * @date 2018/8/13
- * @version
- */
- module.exports = app => {
- class MaterialListNotJoin extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_list_notjoin';
- }
- /**
- * 添加不参与调差的清单
- * @return {void}
- */
- async add(data, ms_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const newListNotJoin = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- gcl_id: data.gcl_id,
- xmj_id: data.id,
- mx_id: data.mx_id ? data.mx_id : '',
- type: 1,
- in_time: new Date(),
- };
- // 如果存在数量变更不参与调差,需要删除
- await transaction.delete(this.tableName, {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- gcl_id: data.gcl_id,
- xmj_id: data.id,
- mx_id: data.mx_id ? data.mx_id : '',
- type: 2,
- });
- data.xmj_id = data.id;
- data.mx_id = data.mx_id ? data.mx_id : '';
- await this.updateAllMaterials(transaction, data, 'add', ms_id);
- // 新增不参与调差清单
- const result = await transaction.insert(this.tableName, newListNotJoin);
- if (result.affectedRows === 0) {
- throw '新增不参与调差清单数据失败';
- }
- await transaction.commit();
- return await this.getDataById(result.insertId);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 添加数量变更不计量的清单
- * @return {void}
- */
- async addChange(data, ms_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const newListNotChange = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- gcl_id: data.gcl_id,
- xmj_id: data.id,
- mx_id: data.mx_id ? data.mx_id : '',
- type: 2,
- in_time: new Date(),
- };
- data.xmj_id = data.id;
- data.mx_id = data.mx_id ? data.mx_id : '';
- await this.updateAllMaterials(transaction, data, 'change', ms_id);
- // 新增不参与调差清单
- const result = await transaction.insert(this.tableName, newListNotChange);
- if (result.affectedRows === 0) {
- throw '新增数量变更不计量不参与调差数据失败';
- }
- await transaction.commit();
- return await this.getDataById(result.insertId);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 删除不参与调差的清单
- * @param {int} id 工料id
- * @return {void}
- */
- async del(id, ms_id = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const notJoinInfo = await this.getDataById(id);
- await this.updateAllMaterials(transaction, notJoinInfo, 'del', ms_id);
- // 判断是否可删
- const result = await transaction.delete(this.tableName, { id });
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改调差list和bill和material对应值
- * @param transaction
- * @returns {Promise<void>}
- */
- async updateAllMaterials(transaction, data, type, ms_id = null) {
- // 先判断material_list是否存在值并quantity不为null
- const searchSql = {
- mid: this.ctx.material.id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id,
- };
- // if (data.mx_id !== '') {
- // searchSql.mx_id = data.mx_id;
- // }
- const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
- where: searchSql,
- });
- // console.log(data);
- // console.log(materialListData);
- if (materialListData && materialListData.length !== 0) {
- // 对应修改更新本期应耗数量值和总的本期金额计算
- const mbIdList = [];
- const updateList = [];
- for (const ml of materialListData) {
- const updateData = {
- id: ml.id,
- is_join: type === 'add' ? 0 : type === 'change' ? 2 : 1,
- };
- 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');
- }
- }
- }
- /**
- * 复制上一期不参与调差的清单到下一期中
- * @param {Object} transaction - 新增一期的事务
- * @param {Object} list 上期清单
- * @param {int} mid 工料id
- * @return {void}
- */
- async copyNewStageNotJoinList(transaction, list, mid) {
- if (!this.ctx.tender) {
- throw '数据错误';
- }
- const notJoinlist = [];
- for (const mb of list) {
- const newLists = {
- tid: mb.tid,
- mid,
- gcl_id: mb.gcl_id,
- xmj_id: mb.xmj_id,
- mx_id: mb.mx_id,
- type: mb.type,
- in_time: new Date(),
- };
- notJoinlist.push(newLists);
- }
- // 复制上一期不参与调差的清单
- return notJoinlist.length > 0 ? await transaction.insert(this.tableName, notJoinlist) : true;
- }
- async getJoinMsg(transaction, mid, datas) {
- const searchData = {
- tid: this.ctx.tender.id,
- mid,
- gcl_id: datas.gcl_id,
- xmj_id: datas.xmj_id,
- mx_id: datas.mx_id,
- };
- const info = await transaction.get(this.tableName, searchData);
- if (info) {
- return info.type === 1 ? 0 : info.type === 2 ? 2 : 0;
- }
- return 1;
- }
- }
- return MaterialListNotJoin;
- };
|