| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date 2018/10/30
 
-  * @version
 
-  */
 
- const infoConst = require('../const/tender_info');
 
- const parseInfo = infoConst.parseInfo;
 
- const arrayInfo = infoConst.arrayInfo;
 
- const defaultInfo = infoConst.defaultInfo;
 
- module.exports = app => {
 
-     class TenderInfo extends app.BaseService {
 
-         /**
 
-          * 构造函数
 
-          *
 
-          * @param {Object} ctx - egg全局变量
 
-          * @return {void}
 
-          */
 
-         constructor(ctx) {
 
-             super(ctx);
 
-             this.tableName = 'tender_info';
 
-         }
 
-         /**
 
-          * 新增 标段相关信息
 
-          *
 
-          * @param tenderId - 标段Id
 
-          * @param projectId - 项目Id
 
-          * @param transaction - 事务
 
-          * @returns {Promise<void>}
 
-          */
 
-         async addTenderInfo(tenderId, projectId, transaction) {
 
-             const info = JSON.parse(JSON.stringify(defaultInfo));
 
-             info.tid = tenderId;
 
-             info.pid = projectId;
 
-             for (const pi of parseInfo) {
 
-                 info[pi] = JSON.stringify(info[pi])
 
-             }
 
-             for (const pi of arrayInfo) {
 
-                 info[pi] = JSON.stringify(info[pi])
 
-             }
 
-             if (transaction) {
 
-                 await transaction.insert(this.tableName, info);
 
-             } else {
 
-                 await this.db.insert(this.tableName, info);
 
-             }
 
-             return info;
 
-         }
 
-         /**
 
-          * 获取标段相关信息
 
-          * @param tenderId
 
-          * @returns {Promise<void>}
 
-          */
 
-         async getTenderInfo(tenderId) {
 
-             let info = await this.getDataByCondition({tid: tenderId});
 
-             // 兼容不存在info的情况
 
-             if (!info) {
 
-                 info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
 
-             }
 
-             for (const pi of parseInfo) {
 
-                 info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
 
-                 this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
 
-             }
 
-             for (const ai of arrayInfo) {
 
-                 info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
 
-             }
 
-             return info;
 
-         }
 
-         /**
 
-          * 保存标段相关信息
 
-          *
 
-          * @param data
 
-          * @returns {Promise<void>}
 
-          */
 
-         async saveTenderInfo(tenderId, data) {
 
-             for (const di in data) {
 
-                 if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
 
-                     data[di] = JSON.stringify(data[di]);
 
-                 }
 
-             }
 
-             await this.db.update(this.tableName, data, {where: {tid: tenderId}});
 
-         }
 
-         async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
 
-             const changePrecision = [];
 
-             const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
 
-             const defUnits = this._.map(newPrecision, 'units');
 
-             const otherUnits = units.filter(function (u) {return defUnits.indexOf(u) === -1});
 
-             let changeUnits = [];
 
-             for (const prop in newPrecision) {
 
-                 if (oldPrecision[prop]) {
 
-                     if (newPrecision[prop].value < oldPrecision[prop].value) {
 
-                         changePrecision.push(newPrecision[prop]);
 
-                     }
 
-                 } else {
 
-                     changePrecision.push(newPrecision[prop]);
 
-                 }
 
-             }
 
-             for (const cp of changePrecision) {
 
-                 if (cp.unit) {
 
-                     changeUnits.push(cp.unit);
 
-                 } else {
 
-                     changeUnits.push(otherUnits);
 
-                 }
 
-             }
 
-             changeUnits = this._.flatten(changeUnits);
 
-             if (changeUnits.length > 0) {
 
-                 const bills = await this.ctx.service.ledger.getAllDataByCondition({
 
-                     columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
 
-                     where: {tender_id: tenderId, unit: changeUnits, is_leaf: true}
 
-                 });
 
-                 const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
 
-                 for (const b of bills) {
 
-                     const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
 
-                     const bPos = this._.filter(pos, {lid: b.id});
 
-                     if (bPos.length > 0) {
 
-                         let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0;
 
-                         for (const p of bPos) {
 
-                             this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
 
-                             p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
 
-                             sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
 
-                             sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
 
-                             qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
 
-                             quantity = this.ctx.helper.add(quantity, p.quantity);
 
-                         }
 
-                         b.sgfh_qty = sgfh_qty;
 
-                         b.sjcl_qty = sjcl_qty;
 
-                         b.qtcl_qty = qtcl_qty;
 
-                         b.quantity = quantity;
 
-                     } else {
 
-                         this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
 
-                     }
 
-                     b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
 
-                     b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
 
-                     b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
 
-                     b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
 
-                     b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
 
-                     b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
 
-                 }
 
-                 const transaction = await this.db.beginTransaction();
 
-                 try {
 
-                     await transaction.update(this.tableName,
 
-                         {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
 
-                     if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
 
-                     if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
 
-                     await transaction.commit();
 
-                 } catch (err) {
 
-                     await transaction.rollback();
 
-                     throw err;
 
-                 }
 
-             } else {
 
-                 await this.db.update(this.tableName,
 
-                     {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
 
-             }
 
-         }
 
-         async saveDecimal(tenderId, newDecimal, oldDecimal) {
 
-             const changeBills = [], calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
 
-             if (calcUp || calcTp) {
 
-                 const bills = await this.ctx.service.ledger.getAllDataByCondition({
 
-                     columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
 
-                     where: {tender_id: tenderId, is_leaf: true}
 
-                 });
 
-                 for (const b of bills) {
 
-                     const cb = { id: b.id };
 
-                     cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
 
-                     cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
 
-                     cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
 
-                     cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
 
-                     cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
 
-                     cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
 
-                     changeBills.push(cb);
 
-                 }
 
-             }
 
-             if (changeBills.length > 0) {
 
-                 const transaction = await this.db.beginTransaction();
 
-                 try {
 
-                     await transaction.update(this.tableName,
 
-                         {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
 
-                     await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
 
-                     await transaction.commit();
 
-                 } catch(error) {
 
-                     await transaction.rollback();
 
-                     throw error;
 
-                 }
 
-             } else {
 
-                 await this.db.update(this.tableName,
 
-                     {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
 
-             }
 
-         }
 
-     }
 
-     return TenderInfo;
 
- };
 
 
  |