| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date 2018/8/14
 
-  * @version
 
-  */
 
- const audit = require('../const/audit');
 
- module.exports = app => {
 
-     class ChangePlanList extends app.BaseService {
 
-         /**
 
-          * 构造函数
 
-          *
 
-          * @param {Object} ctx - egg全局变量
 
-          * @return {void}
 
-          */
 
-         constructor(ctx) {
 
-             super(ctx);
 
-             this.tableName = 'change_plan_list';
 
-         }
 
-         /**
 
-          * 取出变更令清单列表,并按台账清单在前,空白清单在后排序
 
-          * @return {void}
 
-          */
 
-         async getList(cpid, transaction = false) {
 
-             const sql = 'SELECT * FROM ?? WHERE `cpid` = ? ORDER BY `id` asc';
 
-             const sqlParam = [this.tableName, cpid];
 
-             return transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 添加空白变更清单
 
-          * @return {void}
 
-          */
 
-         async add(data) {
 
-             if (!this.ctx.tender || !this.ctx.change) {
 
-                 throw '数据错误';
 
-             }
 
-             const insertData = {
 
-                 tid: this.ctx.tender.id,
 
-                 cpid: this.ctx.change.id,
 
-             };
 
-             const newData = this._.assign(insertData, data);
 
-             // 新增工料
 
-             const result = await this.db.insert(this.tableName, newData);
 
-             if (result.affectedRows === 0) {
 
-                 throw '新增空白清单数据失败';
 
-             }
 
-             return await this.getDataById(result.insertId);
 
-         }
 
-         /**
 
-          * 批量添加空白变更清单
 
-          * @return {void}
 
-          */
 
-         async batchAdd(data) {
 
-             if (!this.ctx.tender || !this.ctx.change) {
 
-                 throw '数据错误';
 
-             }
 
-             const num = data.num ? parseInt(data.num) : 0;
 
-             if (num < 1 || num > 100) {
 
-                 throw '批量添加的空白清单数目不能小于1或大于100';
 
-             }
 
-             const insertArray = [];
 
-             for (let i = 0; i < num; i++) {
 
-                 const insertData = {
 
-                     tid: this.ctx.tender.id,
 
-                     cpid: this.ctx.change.id,
 
-                 };
 
-                 insertArray.push(insertData);
 
-             }
 
-             // 新增工料
 
-             const result = await this.db.insert(this.tableName, insertArray);
 
-             if (result.affectedRows !== num) {
 
-                 throw '批量添加空白清单数据失败';
 
-             }
 
-             // 获取刚批量添加的所有list
 
-             for (let j = 0; j < num; j++) {
 
-                 insertArray[j].id = result.insertId + j;
 
-             }
 
-             return insertArray;
 
-         }
 
-         /**
 
-          * 删除变更清单
 
-          * @param {int} id 清单id
 
-          * @return {void}
 
-          */
 
-         async del(ids) {
 
-             if (!this.ctx.tender || !this.ctx.change) {
 
-                 throw '数据错误';
 
-             }
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 判断是否可删
 
-                 await transaction.delete(this.tableName, { id: ids });
 
-                 // 重新算变更令总额
 
-                 await this.calcCamountSum(transaction);
 
-                 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.change) {
 
-                 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 this.calcCamountSum(transaction);
 
-                 await transaction.commit();
 
-                 return true;
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 修改变更清单 复制粘贴
 
-          * @param {Object} datas 修改内容
 
-          * @return {void}
 
-          */
 
-         async saveDatas(datas) {
 
-             if (!this.ctx.tender || !this.ctx.change) {
 
-                 throw '数据错误';
 
-             }
 
-             // 判断是否可修改
 
-             // 判断t_type是否为费用
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const insertArray = [];
 
-                 const updateArray = [];
 
-                 for (const d of datas) {
 
-                     if (d.id) {
 
-                         updateArray.push(d);
 
-                     } else {
 
-                         d.tid = this.ctx.tender.id;
 
-                         d.cpid = this.ctx.change.id;
 
-                         insertArray.push(d);
 
-                     }
 
-                 }
 
-                 if (insertArray.length > 0) await transaction.insert(this.tableName, insertArray);
 
-                 if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
 
-                 await this.calcCamountSum(transaction);
 
-                 await transaction.commit();
 
-                 return true;
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         async calcCamountSum(transaction, updateTpDecimal = false) {
 
-             // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
 
-             // const sqlParam = [this.tableName, this.change.cid];
 
-             // const tp = await transaction.queryOne(sql, sqlParam);
 
-             // 防止小数位不精确,采用取值计算
 
-             const sql = 'SELECT unit_price, spamount FROM ?? WHERE cpid = ?';
 
-             const sqlParam = [this.tableName, this.ctx.change.id];
 
-             const changeList = await transaction.query(sql, sqlParam);
 
-             let total_price = 0;
 
-             const tp_decimal = this.ctx.change.decimal.tp;
 
-             for (const cl of changeList) {
 
-                 total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, tp_decimal));
 
-             }
 
-             const updateData = {
 
-                 total_price,
 
-             };
 
-             // if (updateTpDecimal) {
 
-             //     updateData.tp_decimal = tp_decimal;
 
-             // }
 
-             const options = {
 
-                 where: {
 
-                     id: this.ctx.change.id,
 
-                 },
 
-             };
 
-             await transaction.update(this.ctx.service.changePlan.tableName, updateData, options);
 
-         }
 
-         async insertAuditAmount(transaction, cpId, lastAudit = false) {
 
-             const changeList = await this.getList(cpId, transaction);
 
-             const updateArray = [];
 
-             for (const c of changeList) {
 
-                 const audit_amount = c.audit_amount !== null && c.audit_amount !== '' ? c.audit_amount.split(',') : [];
 
-                 audit_amount.push(c.spamount);
 
-                 updateArray.push({
 
-                     id: c.id,
 
-                     audit_amount: audit_amount.join(','),
 
-                     samount: lastAudit ? c.spamount : null,
 
-                 });
 
-             }
 
-             if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
 
-         }
 
-         async delAuditAmount(transaction, cpId) {
 
-             const changeList = await this.getList(cpId, transaction);
 
-             const updateArray = [];
 
-             for (const c of changeList) {
 
-                 updateArray.push({
 
-                     id: c.id,
 
-                     audit_amount: null,
 
-                     spamount: c.camount,
 
-                 });
 
-             }
 
-             if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
 
-             await this.calcCamountSum(transaction);
 
-         }
 
-         /**
 
-          * 报表用
 
-          * Tony Kang
 
-          * @param {tid} tid - 标段id
 
-          * @return {void}
 
-          */
 
-         async getChangeBills(tid, onlyChecked) {
 
-             const sql = 'SELECT cb.*' +
 
-                 '  FROM ' + this.tableName + ' cb' +
 
-                 '  LEFT JOIN ' + this.ctx.service.changePlan.tableName + ' c ON cb.cpid = c.id' +
 
-                 '  WHERE c.tid = ? ' + (onlyChecked ? `and c.status = ${audit.changeApply.status.checked}` : '');
 
-             const param = [tid];
 
-             const result = await this.db.query(sql, param);
 
-             return result;
 
-         }
 
-     }
 
-     return ChangePlanList;
 
- };
 
 
  |