123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 'use strict';
- /**
- * 单独计量单元设置-清单关联表 数据模型
- *
- * @author Mai
- * @date 2018/8/13
- * @version
- */
- module.exports = app => {
- class MaterialListSelf extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_list_self';
- }
- /**
- * 添加单独计量的清单
- * @return {void}
- */
- async add(data) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const newListSelf = {
- 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 : '',
- in_time: new Date(),
- };
- // 更新list表为is_self为1
- await transaction.update(this.ctx.service.materialList.tableName, { is_self: 1, order: this.ctx.material.order, in_time: new Date() }, {
- where: {
- 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 : '',
- },
- });
- // data.xmj_id = data.id;
- // data.mx_id = data.mx_id !== undefined ? data.mx_id : null;
- // await this.updateAllMaterials(transaction, data, 'add');
- // 新增不参与调差清单
- const result = await transaction.insert(this.tableName, newListSelf);
- if (result.affectedRows === 0) {
- throw '新增不参与调差清单数据失败';
- }
- await transaction.commit();
- return { info: await this.getDataById(result.insertId), materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id) };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 删除单独计量的清单
- * @param {int} id 工料id
- * @return {void}
- */
- async del(id, ms_id = null, gather_qty = null) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const selfInfo = await this.getDataById(id);
- selfInfo.gather_qty = gather_qty;
- await this.updateAllMaterials(transaction, selfInfo, ms_id);
- // 判断是否可删
- const result = await transaction.delete(this.tableName, { id });
- await transaction.commit();
- return 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, 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 !== null) {
- // searchSql.mx_id = data.mx_id;
- // }
- const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
- where: searchSql,
- });
- const mbIdList = [];
- if (materialListData && materialListData.length > 0) {
- for (const ml of materialListData) {
- if (mbIdList.indexOf(ml.mb_id) === -1) {
- mbIdList.push(ml.mb_id);
- }
- }
- await transaction.delete(this.ctx.service.materialList.tableName, {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id ? data.mx_id : '',
- });
- }
- const materialListGclData = await this.ctx.service.materialListGcl.getAllDataByCondition({
- where: { tid: this.ctx.tender.id, gcl_id: data.gcl_id },
- });
- const insertList = [];
- const is_join = await this.ctx.service.materialListNotjoin.getJoinMsg(transaction, this.ctx.material.id, data);
- if (this.ctx.material.is_stage_self) {
- const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
- for (const ms of materialStageList) {
- // const oneML = this._.filter(materialListData, { ms_id: ms.id });
- // const gather_qty = oneML[0].gather_qty;
- // const is_join = oneML[0].is_join;
- if (data.gather_qty['ms_id_' + ms.id] !== null) {
- for (const m of materialListGclData) {
- insertList.push({
- tid: this.ctx.tender.id,
- order: m.order,
- mid: this.ctx.material.id,
- mb_id: m.mb_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id,
- ms_id: ms.id,
- gather_qty: data.gather_qty['ms_id_' + ms.id],
- quantity: m.quantity,
- expr: m.expr,
- is_join,
- is_self: 0,
- in_time: new Date(),
- });
- }
- }
- }
- } else {
- // const gather_qty = materialListData[0].gather_qty;
- // const is_join = materialListData[0].is_join;
- for (const m of materialListGclData) {
- insertList.push({
- tid: this.ctx.tender.id,
- order: m.order,
- mid: this.ctx.material.id,
- mb_id: m.mb_id,
- gcl_id: data.gcl_id,
- xmj_id: data.xmj_id,
- mx_id: data.mx_id,
- gather_qty: data.gather_qty,
- quantity: m.quantity,
- expr: m.expr,
- is_join,
- is_self: 0,
- in_time: new Date(),
- });
- }
- }
- if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
- // 重新计算金额
- 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 copyNewStageSelfList(transaction, list, mid) {
- if (!this.ctx.tender) {
- throw '数据错误';
- }
- const selfList = [];
- 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,
- in_time: new Date(),
- };
- selfList.push(newLists);
- }
- // 复制上一期不参与调差的清单
- return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
- }
- }
- return MaterialListSelf;
- };
|