123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 'use strict';
- /**
- * 期 - 变更数据
- *
- * @author Mai
- * @date
- * @version
- */
- const defaultPid = -1; // 非pid
- module.exports = app => {
- class StageChange extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'stage_change';
- }
- /**
- * 查询 调用的变更令
- * @param {Number} tid - 标段id
- * @param {Number} sid - 期id
- * @param {Number} lid - 台账节点id
- * @param {Number} pid - 部位明细id
- * @returns {Promise<*>}
- */
- async getLastestStageData(tid, sid, lid, pid) {
- const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
- ' INNER JOIN ( ' +
- ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid` From ' + this.tableName +
- ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
- ' ) As m ' +
- ' ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid';
- const sqlParam = [tid, sid, lid, pid ? pid : -1];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 调用变更令
- * @param {Object} target - 台账|部位
- * @param {Array} changes - 调用的变更令
- * @returns {Promise<void>}
- */
- async billsChange(bills, changes) {
- const self = this;
- function getNewChange(cid, cbid, times, order, qty) {
- return {
- tid: self.ctx.tender.id,
- sid: self.ctx.stage.id,
- lid: bills.id,
- pid: -1,
- cid: cid,
- cbid: cbid,
- stimes: times,
- sorder: order,
- qty: qty
- }
- }
- const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
- if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
- throw '提交数据错误';
- }
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
- // 获取原变更令
- const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
- const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
- // 获取更新数据
- const updateChanges = [], newChanges = [];
- let billsQty = 0;
- for (const oc of oldChanges) {
- const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
- if (!nc || nc.qty !== oc.qty) {
- const qty = nc ? this.round(nc.qty, precision.value) : null;
- const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, order, qty);
- billsQty = this.ctx.helper.plus(billsQty, change.qty);
- if (oc.stimes === this.ctx.stage.times && oc.sorder === order) {
- change.id = oc.id;
- updateChanges.push(change);
- } else {
- newChanges.push(change);
- }
- } else {
- billsQty = this.ctx.helper.plus(billsQty, oc.qty);
- }
- }
- for (const c of changes) {
- const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
- if (!nc) {
- const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, order, this.round(c.qty, precision.value));
- billsQty = this.ctx.helper.plus(billsQty, change.qty);
- newChanges.push(change);
- }
- }
- // 更新数据
- const transaction = await this.db.beginTransaction();
- try {
- if (newChanges.length > 0) {
- await transaction.insert(this.tableName, newChanges);
- }
- for (const c of updateChanges) {
- await transaction.update(this.tableName, c);
- }
- const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
- await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
- return { bills: result };
- }
- async posChange(pos, changes) {
- const self = this;
- function getNewChange(cid, cbid, times, order, qty) {
- return {
- tid: self.ctx.tender.id,
- sid: self.ctx.stage.id,
- lid: pos.lid,
- pid: pos.id,
- cid: cid,
- cbid: cbid,
- stimes: times,
- sorder: order,
- qty: qty
- }
- }
- const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
- if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
- throw '提交数据错误';
- }
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
- // 获取原变更令
- const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
- const updateChanges = [], newChanges = [];
- let posQty = 0;
- for (const oc of oldChanges) {
- const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
- if (!nc || nc.qty !== oc.qty) {
- const qty = nc ? this.round(nc.qty, precision.value) : null;
- const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, qty);
- posQty = this.ctx.helper.plus(posQty, change.qty);
- if (oc.stimes === this.ctx.stage.times && oc.sorder === this.ctx.stage.flowOrder) {
- change.id = oc.id;
- updateChanges.push(change);
- } else {
- newChanges.push(change);
- }
- } else {
- posQty = this.ctx.helper.plus(posQty, oc.qty);
- }
- }
- for (const c of changes) {
- const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
- if (!nc) {
- const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, this.round(c.qty, precision.value));
- posQty = this.ctx.helper.plus(posQty, change.qty);
- newChanges.push(change);
- }
- }
- // 更新数据
- const transaction = await this.db.beginTransaction();
- try {
- if (newChanges.length > 0) {
- await transaction.insert(this.tableName, newChanges);
- }
- for (const c of updateChanges) {
- await transaction.update(this.tableName, c);
- }
- await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- // 获取返回数据
- try {
- const data = {};
- data.bills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
- data.pos = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id])
- return data;
- } catch(err) {
- throw '获取数据错误,请刷新页面';
- }
- }
- }
- return StageChange;
- };
|