| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 | 
							- '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;
 
-             }
 
-         }
 
-         /**
 
-          * 修改工料信息
 
-          * @param {Object} data 工料内容
 
-          * @return {void}
 
-          */
 
-         async saveDatas(datas) {
 
-             if (!this.ctx.tender || !this.ctx.material) {
 
-                 throw '数据错误';
 
-             }
 
-             // 判断是否可修改
 
-             // 判断t_type是否为费用
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 for (const data of datas) {
 
-                     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 {
 
-                 // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
 
-                 // 还要找出删除的工料,更新单价数量
 
-                 const list = [];
 
-                 const delList = [];
 
-                 const materialBills = data.materialBills;
 
-                 const mb_idList = [];
 
-                 for (const xmj of data.addXmj) {
 
-                     const mlList = await this.getAllDataByCondition({
 
-                         where: {
 
-                             mid: this.ctx.material.id,
 
-                             gcl_id: xmj.gcl_id,
 
-                             xmj_id: xmj.id,
 
-                             mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
 
-                         },
 
-                     });
 
-                     const mbIdList = this._.map(mlList, 'mb_id');
 
-                     mb_idList.push(...mbIdList);
 
-                     delList.push(...this._.map(mlList, 'id'));
 
-                     for (const mb of materialBills) {
 
-                         const newLists = {
 
-                             tid: this.ctx.tender.id,
 
-                             order: this.ctx.material.order,
 
-                             mid: this.ctx.material.id,
 
-                             mb_id: mb.mb_id,
 
-                             gcl_id: xmj.gcl_id,
 
-                             xmj_id: xmj.id,
 
-                             mx_id: xmj.mx_id ? xmj.mx_id : '',
 
-                             gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
 
-                             quantity: mb.quantity,
 
-                             in_time: new Date(),
 
-                         };
 
-                         list.push(newLists);
 
-                         mb_idList.push(mb.mb_id);
 
-                     }
 
-                 }
 
-                 // 删除工料清单关联
 
-                 if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
 
-                 // 新增工料清单关联
 
-                 if (list.length > 0) {
 
-                     const result = await transaction.insert(this.tableName, list);
 
-                     if (result.affectedRows === 0) {
 
-                         throw '新增工料数据失败';
 
-                     }
 
-                 }
 
-                 // 重算工料和总金额
 
-                 const calcMBIdList = this._.uniq(mb_idList);
 
-                 if (calcMBIdList.length > 0) {
 
-                     for (const select of calcMBIdList) {
 
-                         await this.calcQuantityByML(transaction, select);
 
-                     }
 
-                 }
 
-                 // throw 'fail';
 
-                 // 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
 
-          * @return {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),
 
-                 m_tp: this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(mb_quantity.quantity, 3), mbInfo.m_spread), 2),
 
-             };
 
-             await transaction.update(this.ctx.service.materialBills.tableName, updateData);
 
-             // 计算本期总金额
 
-             const sql2 = 'SELECT SUM(`m_tp`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
 
-             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
 
-          * @return {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 && ml.mx_id !== '') {
 
-                     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 copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
 
-         }
 
-     }
 
-     return MaterialList;
 
- };
 
 
  |