| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 | 
							- 'use strict';
 
- /**
 
-  * 调差清单关联工料表 数据模型
 
-  *
 
-  * @author Mai
 
-  * @date 2018/8/13
 
-  * @version
 
-  */
 
- const auditConst = require('../const/audit').material;
 
- module.exports = app => {
 
-     class MaterialList extends app.BaseService {
 
-         /**
 
-          * 构造函数
 
-          *
 
-          * @param {Object} ctx - egg全局变量
 
-          * @return {void}
 
-          */
 
-         constructor(ctx) {
 
-             super(ctx);
 
-             this.tableName = 'material_list';
 
-         }
 
-         /**
 
-          * 添加工料清单关联
 
-          * @return {void}
 
-          */
 
-         async add(data) {
 
-             if (!this.ctx.tender || !this.ctx.material) {
 
-                 throw '数据错误';
 
-             }
 
-             const list = [];
 
-             for (const mb of data.mb_id) {
 
-                 const newLists = {
 
-                     tid: this.ctx.tender.id,
 
-                     order: this.ctx.material.order,
 
-                     mid: this.ctx.material.id,
 
-                     mb_id: mb,
 
-                     gcl_id: data.gcl_id,
 
-                     xmj_id: data.xmj_id,
 
-                     mx_id: data.mx_id,
 
-                     gather_qty: data.gather_qty,
 
-                     in_time: new Date(),
 
-                 };
 
-                 list.push(newLists);
 
-             }
 
-             // 新增工料
 
-             const result = await this.db.insert(this.tableName, list);
 
-             if (result.affectedRows === 0) {
 
-                 throw '新增工料数据失败';
 
-             }
 
-             return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
 
-         }
 
-         /**
 
-          * 删除工料清单关联
 
-          * @param {int} id 工料id
 
-          * @return {void}
 
-          */
 
-         async del(id, mb_id) {
 
-             if (!this.ctx.tender || !this.ctx.material) {
 
-                 throw '数据错误';
 
-             }
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 判断是否可删
 
-                 await transaction.delete(this.tableName, { id });
 
-                 await this.calcQuantityByML(transaction, mb_id);
 
-                 await transaction.commit();
 
-                 return true;
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 修改工料清单关联信息
 
-          * @param {Object} data 工料内容
 
-          * @param {int} order 期数
 
-          * @return {void}
 
-          */
 
-         async save(data, order) {
 
-             if (!this.ctx.tender || !this.ctx.material) {
 
-                 throw '数据错误';
 
-             }
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const mb_id = data.mb_id;
 
-                 delete data.mb_id;
 
-                 await transaction.update(this.tableName, data);
 
-                 await this.calcQuantityByML(transaction, mb_id);
 
-                 await transaction.commit();
 
-                 return true;
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 应用工料清单到其它清单中
 
-          * @return {void}
 
-          */
 
-         async addOther(data) {
 
-             if (!this.ctx.tender || !this.ctx.material) {
 
-                 throw '数据错误';
 
-             }
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const list = [];
 
-                 const select = data.select;
 
-                 for (const index in data.mx_id) {
 
-                     const newLists = {
 
-                         tid: this.ctx.tender.id,
 
-                         order: this.ctx.material.order,
 
-                         mid: this.ctx.material.id,
 
-                         mb_id: select.mb_id,
 
-                         gcl_id: select.gcl_id,
 
-                         xmj_id: select.xmj_id,
 
-                         mx_id: data.mx_id[index],
 
-                         gather_qty: data.gather_qty[index],
 
-                         quantity: select.quantity,
 
-                         in_time: new Date(),
 
-                     };
 
-                     list.push(newLists);
 
-                 }
 
-                 // 新增工料
 
-                 const result = await transaction.insert(this.tableName, list);
 
-                 if (result.affectedRows === 0) {
 
-                     throw '新增工料数据失败';
 
-                 }
 
-                 await this.calcQuantityByML(transaction, select.mb_id);
 
-                 await transaction.commit();
 
-                 return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 修改material_bills的quantity值和计算本期金额
 
-          * @param transaction
 
-          * @param mb_id
 
-          * @returns {Promise<*>}
 
-          */
 
-         async calcQuantityByML(transaction, mb_id) {
 
-             // 修改material_bills值
 
-             const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
 
-             if (!mbInfo) {
 
-                 throw '不存在该工料';
 
-             }
 
-             const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
 
-             const sqlParam = [this.ctx.material.id, mb_id];
 
-             const mb_quantity = await transaction.queryOne(sql, sqlParam);
 
-             console.log(mb_quantity);
 
-             const updateData = {
 
-                 id: mb_id,
 
-                 quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
 
-             };
 
-             await transaction.update(this.ctx.service.materialBills.tableName, updateData);
 
-             // 计算本期总金额
 
-             const sql2 = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
 
-             const sqlParam2 = [this.ctx.tender.id];
 
-             const tp = await transaction.queryOne(sql2, sqlParam2);
 
-             console.log(tp);
 
-             const updateData2 = {
 
-                 id: this.ctx.material.id,
 
-                 m_tp: tp.total_price,
 
-             };
 
-             return await transaction.update(this.ctx.service.material.tableName, updateData2);
 
-         }
 
-         /**
 
-          * 获取工料清单关联表
 
-          * @param {int} tid 标段id
 
-          * @param {Object} mid 期id
 
-          * @return {void}
 
-          */
 
-         async getMaterialData(tid, mid) {
 
-             const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' +
 
-                 ' FROM ' + this.tableName + ' as ml' +
 
-                 ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
 
-                 ' ON ml.`mb_id` = mb.`id`' +
 
-                 ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
 
-             const sqlParam = [tid, mid];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 复制上一期并生成新一期清单工料关联,计算新一期小计值
 
-          * @param transaction
 
-          * @param preMaterial
 
-          * @param newMid
 
-          * @returns {Promise<void>}
 
-          */
 
-         async copyPreMaterialList(transaction, preMaterial, newMaterial) {
 
-             const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
 
-             const copyMLArray = [];
 
-             for (const ml of materialListData) {
 
-                 // 获取小计值
 
-                 let gather_qty = null;
 
-                 if (ml.mx_id !== null) {
 
-                     gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
 
-                 } else {
 
-                     gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
 
-                 }
 
-                 const newMaterialList = {
 
-                     tid: ml.tid,
 
-                     order: ml.order,
 
-                     mid: newMaterial.id,
 
-                     mb_id: ml.mb_id,
 
-                     gcl_id: ml.gcl_id,
 
-                     xmj_id: ml.xmj_id,
 
-                     mx_id: ml.mx_id,
 
-                     gather_qty,
 
-                     quantity: ml.quantity,
 
-                     is_join: ml.is_join,
 
-                     in_time: new Date(),
 
-                 };
 
-                 copyMLArray.push(newMaterialList);
 
-             }
 
-             return await transaction.insert(this.tableName, copyMLArray);
 
-         }
 
-     }
 
-     return MaterialList;
 
- };
 
 
  |