123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const measureType = require('../const/tender').measureType;
- const timesLen = require('../const/audit').stage.timesLen;
- module.exports = app => {
- class StagePos extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.depart = 20;
- this.tableName = 'stage_pos';
- this.qtyFields = ['contract_qty', 'qc_qty', 'qc_minus_qty', 'positive_qc_qty', 'negative_qc_qty']
- }
- _getPosFilterSql(where, asTable = '') {
- let whereSql = '';
- if (!where) return whereSql;
- if (where.pid) {
- if (where.pid instanceof Array) {
- whereSql += ' And ' + asTable + 'pid in (' + this.ctx.helper.getInArrStrSqlFilter(where.pid) + ')';
- } else if (typeof where.pid === "string") {
- whereSql += ' And ' + asTable + 'pid = ' + this.db.escape(where.pid);
- }
- }
- if (where.lid) {
- if (where.lid instanceof Array) {
- whereSql += ' And ' + asTable + 'lid in (' + this.ctx.helper.getInArrStrSqlFilter(where.lid) + ')';
- } else if (typeof where.lid === "string") {
- whereSql += ' And ' + asTable + 'lid = ' + this.db.escape(where.lid);
- }
- }
- return whereSql;
- }
- /**
- * 查询期计量最后审核人数据
- * @param {Number} tid - 标段id
- * @param {Number} sid - 期id
- * @param {Number|Array} pid - 部位明细id(可以为空)
- * @returns {Promise<*>}
- */
- // async getLastestStageData(tid, sid, where) {
- // const filterSql = this._getPosFilterSql(where);
- // const sql = 'SELECT Pos.id, Pos.tid, Pos.sid, Pos.lid, Pos.pid, Pos.contract_qty, Pos.qc_qty, Pos.postil, Pos.times, Pos.order, Pos.contract_expr FROM ' +
- // ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
- // ' INNER JOIN ( ' +
- // ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
- // ' WHERE `tid` = ? And sid = ?' + filterSql +
- // ' GROUP BY `pid`' +
- // ' ) As MaxFilter ' +
- // ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
- // const sqlParam = [tid, sid, tid, sid];
- // return await this.db.query(sql, sqlParam);
- // }
- /**
- * 查询 某期 某轮审批 某审核人数据
- * @param {Number} tid - 标段id
- * @param {Number} sid - 期id
- * @param {Number} times - 期第几轮审批
- * @param {Number} order - 审核人顺序
- * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
- * @returns {Promise<*>}
- */
- // async getAuditorStageData(tid, sid, times, order, where) {
- // const filterSql = this._getPosFilterSql(where);
- // const sql = 'SELECT Pos.id, Pos.tid, Pos.sid, Pos.pid, Pos.lid, Pos.contract_qty, Pos.qc_qty, Pos.postil, Pos.times, Pos.order, Pos.contract_expr FROM ' +
- // ' (SELECT * FROM '+ this.tableName + ' WHERE tid = ? And sid = ?) As Pos ' +
- // ' INNER JOIN ( ' +
- // ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid`, `sid` From ' + this.tableName +
- // ' WHERE (`times` < ? OR (`times` = ? AND `order` <= ?)) And tid = ? And sid = ?' + filterSql +
- // ' GROUP BY `pid`' +
- // ' ) As MaxFilter ' +
- // ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid';
- // const sqlParam = [tid, sid, times, times, order, tid, sid];
- // return await this.db.query(sql, sqlParam);
- // }
- _filterLastestData(stagePos) {
- const stagePosIndex = {};
- for (const sp of stagePos) {
- const key = 'sp-' + sp.pid;
- const spi = stagePosIndex[key];
- if (spi) {
- if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp;
- } else {
- stagePosIndex[key] = sp;
- }
- }
- const result = [];
- for (const prop in stagePosIndex) {
- result.push(stagePosIndex[prop]);
- }
- return result;
- }
- async getLastestStageData2(tid, sid, where) {
- const filterSql = this._getPosFilterSql(where);
- const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, qc_minus_qty, positive_qc_qty, negative_qc_qty, postil, `times`, `order`, `contract_expr`' +
- ' FROM ' + this.departTableName(tid) +
- ' WHERE tid = ? And sid = ? ' + filterSql;
- const sqlParam = [tid, sid];
- const stagePos = await this.db.query(sql, sqlParam);
- return this._filterLastestData(stagePos);
- }
- async getAuditorStageData2(tid, sid, times, order, where) {
- const filterSql = this._getPosFilterSql(where);
- const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, qc_minus_qty, positive_qc_qty, negative_qc_qty, postil, `times`, `order`, `contract_expr`' +
- ' FROM ' + this.departTableName(tid) +
- ' WHERE tid = ? And sid = ? And (`times` < ? OR (`times` = ? AND `order` <= ?)) ' + filterSql;
- const sqlParam = [tid, sid, times, times, order];
- const stagePos = await this.db.query(sql, sqlParam);
- return this._filterLastestData(stagePos);
- }
- async updateStagePos4CheckCancel(sid, newTimes, newOrder, oldTimes, oldOrder, transaction) {
- const oldSBLists = await this.getAllDataByCondition({ where: { sid, times: oldTimes, order: oldOrder } });
- const newSBLists = await this.getAllDataByCondition({ where: { sid, times: newTimes, order: newOrder } });
- const delidList = [];
- for (const o of oldSBLists) {
- const newSBInfo = this._.find(newSBLists, { lid: o.lid, pid: o.pid });
- if (newSBInfo) {
- // 删除
- delidList.push(newSBInfo.id);
- }
- }
- if (delidList.length > 0) await transaction.delete(this.tableName, { id: delidList });
- if (oldSBLists.length > 0) {
- await transaction.update(this.tableName, {
- times: newTimes,
- order: newOrder,
- }, { where: { id: this._.map(oldSBLists, 'id') } });
- }
- }
- async getStageUsedPos(tid, sid, where) {
- const self = this;
- const stagePos = await this.getLastestStageData2(tid, sid, where);
- const pids = this._.map(stagePos, function (sp) {
- if (!self.ctx.helper.checkZero(sp.contract_qty) || !self.ctx.helper.checkZero(sp.qc_qty) || !self.ctx.helper.checkZero(sp.qc_minus_qty)) {
- return sp.pid;
- } else {
- return -1;
- }
- });
- return this._.pull(pids, -1);
- }
- /**
- * 新增部位明细数据(仅供updateStageData调用)
- *
- * @param transaction - 事务
- * @param data - 新增数据
- * @returns {Promise<{}>}
- * @private
- */
- async _addStagePosData(data) {
- let bills , precision;
- const result = {pos: [], ledger: []};
- const datas = data instanceof Array ? data : [data], calcStageBills = [];
- if (datas[0].sgfh_qty !== undefined || datas[0].sjcl_qty !== undefined || datas[0].qtcl_qty !== undefined
- || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined) {
- bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
- precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- result.ledger.push(bills.id);
- }
- const insertPos = [], insertPosStage = [];
- for (const d of datas) {
- const p = {
- id: this.uuid.v4(), tid: this.ctx.tender.id, lid: d.lid, name: d.name, porder: d.porder, position: d.position,
- add_stage: this.ctx.stage.id, add_stage_order: this.ctx.stage.order,
- add_times: this.ctx.stage.curTimes,
- add_user: this.ctx.session.sessionUser.accountId,
- };
- if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined ||
- d.sgfh_expr !== undefined || d.sjcl_expr !== undefined || d.qtcl_expr !== undefined) {
- if (d.sgfh_qty!== undefined) p.sgfh_qty = this.round(d.sgfh_qty, precision.value);
- if (d.sjcl_qty!== undefined) p.sjcl_qty = this.round(d.sjcl_qty, precision.value);
- if (d.qtcl_qty!== undefined) p.qtcl_qty = this.round(d.qtcl_qty, precision.value);
- if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
- if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
- if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
- p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
- }
- if (d.real_qty!== undefined) p.real_qty = this.round(d.real_qty, precision.value);
- insertPos.push(p);
- result.pos.push(p.id);
- if (d.contract_qty!== undefined || d.qc_qty!== undefined || d.postil!== undefined || data.contract_expr !== undefined) {
- result.stageUpdate = true;
- const ps = {
- pid: p.id,
- lid: d.lid,
- tid: this.ctx.tender.id,
- sid: this.ctx.stage.id,
- said: this.ctx.session.sessionUser.accountId,
- times: this.ctx.stage.curTimes,
- order: this.ctx.stage.curOrder,
- };
- if (d.contract_qty !== undefined) ps.contract_qty = this.round(d.contract_qty, precision.value);
- if (d.contract_expr !== undefined) ps.contract_expr = d.contract_expr;
- if (d.postil!== undefined) ps.postil = d.postil;
- insertPosStage.push(ps);
- if (d.contract_qty && calcStageBills.indexOf(ps.lid) === -1) {
- calcStageBills.push(ps.lid);
- }
- }
- }
- const transaction = await this.db.beginTransaction();
- try {
- if (insertPos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, insertPos);
- if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
- for (const lid of calcStageBills) {
- await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
- }
- await transaction.commit();
- return result;
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 更新部位明细数据(仅供updateStageData调用)
- *
- * @param transaction - 事务
- * @param data - 更新数据(允许一次性提交多条)
- * @returns {Promise<{ledger: Array, pos: Array}>}
- * @private
- */
- async _updateStagePosData(data) {
- let bills, precision, updateBills = null;
- const datas = data instanceof Array ? data : [data];
- const orgPos = await this.ctx.service.pos.getPosDataByIds(this.ctx.tender.id, this._.map(datas, 'pid'));
- const result = {ledger: [], pos: [], stageUpdate: true};
- const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
- {pid: this._.map(datas, 'pid')});
- if (datas[0].sgfh_qty !== undefined || datas[0].qtcl_qty !== undefined || datas[0].sjcl_qty !== undefined
- || datas[0].contract_qty !== undefined || datas[0].qc_qty !== undefined || datas[0].real_qty !== undefined) {
- bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
- precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- result.ledger.push(bills.id);
- }
- const updatePos = [], updatePosStage = [], insertPosStage = [];
- for (const d of datas) {
- if (d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
- || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
- || d.real_qty !== undefined
- || d.ex_memo1 !== undefined || d.ex_memo2 !== undefined || d.ex_memo3 !== undefined
- ) {
- const op = this._.find(orgPos, {id: d.pid});
- if (op.add_stage !== this.ctx.stage.id && (
- d.name !== undefined || d.drawing_code !== undefined || d.position !== undefined
- || d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined
- )) throw '不可修改数据';
- const p = {id: op.id};
- if (d.name !== undefined) p.name = d.name;
- if (d.position !== undefined) p.position = d.position;
- if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
- p.sgfh_qty = d.sgfh_qty !== undefined ? d.sgfh_qty : op.sgfh_qty;
- p.sjcl_qty = d.sjcl_qty !== undefined ? d.sjcl_qty : op.sjcl_qty;
- p.qtcl_qty = d.qtcl_qty !== undefined ? d.qtcl_qty : op.qtcl_qty;
- if (d.sgfh_expr !== undefined) p.sgfh_expr = d.sgfh_expr;
- if (d.sjcl_expr !== undefined) p.sjcl_expr = d.sjcl_expr;
- if (d.qtcl_expr !== undefined) p.qtcl_expr = d.qtcl_expr;
- p.quantity = this.ctx.helper.sum([p.sgfh_qty, p.sjcl_qty, p.qtcl_qty]);
- if (!updateBills) updateBills = {id: bills.id};
- }
- if (d.drawing_code !== undefined) p.drawing_code = d.drawing_code;
- if (d.real_qty !== undefined) p.real_qty = this.ctx.helper.round(d.real_qty, precision.value);
- if (d.ex_memo1 !== undefined) p.ex_memo1 = d.ex_memo1;
- if (d.ex_memo2 !== undefined) p.ex_memo2 = d.ex_memo2;
- if (d.ex_memo3 !== undefined) p.ex_memo3 = d.ex_memo3;
- updatePos.push(p);
- }
- if (d.contract_qty !== undefined || d.qc_qty !== undefined || d.postil !== undefined) {
- const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
- if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
- const sp = { id: osp.id, said: this.ctx.session.sessionUser.accountId };
- if (d.contract_qty !== undefined) {
- sp.contract_qty = this.ctx.helper.round(d.contract_qty, precision.value);
- }
- if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
- if (d.qc_qty !== undefined) {
- sp.qc_qty = this.ctx.helper.round(d.qc_qty, precision.value);
- }
- if (d.postil !== undefined) {
- sp.postil = d.postil;
- }
- updatePosStage.push(sp);
- } else {
- const sp = {
- pid: d.pid, lid: d.lid,
- tid: this.ctx.tender.id, sid: this.ctx.stage.id,
- said: this.ctx.session.sessionUser.accountId,
- times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
- };
- if (d.contract_qty !== undefined || osp) {
- sp.contract_qty = d.contract_qty === undefined && osp
- ? osp.contract_qty
- : this.ctx.helper.round(d.contract_qty, precision.value);
- }
- if (d.contract_expr !== undefined) sp.contract_expr = d.contract_expr;
- if (d.qc_qty || osp) {
- sp.qc_qty = d.qc_qty === undefined && osp
- ? osp.qc_qty
- : this.ctx.helper.round(d.qc_qty, precision.value);
- }
- if (d.postil || osp) {
- sp.postil = d.postil === undefined && osp ? osp.postil : d.postil;
- }
- sp.qc_minus_qty = osp ? osp.qc_minus_qty : 0;
- sp.positive_qc_qty = osp ? osp.positive_qc_qty : 0;
- sp.negative_qc_qty = osp ? osp.negative_qc_qty : 0;
- insertPosStage.push(sp);
- }
- }
- result.pos.push(d.pid);
- }
- if (updateBills) {
- const billsPos = await this.ctx.service.pos.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
- for (const bp of billsPos) {
- const newPos = updatePos.find(function (x) { return x.id === bp.id });
- const calcData = newPos ? newPos : bp;
- updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
- updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
- updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
- updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
- }
- const info = this.ctx.tender.info;
- updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
- updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
- updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
- updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
- }
- const transaction = await this.db.beginTransaction();
- try {
- if (updatePos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
- if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
- if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
- if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
- if (bills) await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 更新部位明细数据(仅供updateStageData调用)
- *
- * @param transaction - 事务
- * @param data - 更新数据(允许一次性提交多条)
- * @returns {Promise<{ledger: Array, pos: Array}>}
- * @private
- */
- async _batchUpdateStagePosData(data) {
- const datas = data instanceof Array ? data : [data];
- const result = {
- ledger: this._.uniq(this._.map(datas, 'lid')),
- pos: this._.map(datas, 'pid'),
- stageUpdate: true
- };
- const orgStagePos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id,
- {pid: this._.map(datas, 'pid')});
- const bills = await this.ctx.service.ledger.getAllDataByCondition({where: {id: result.ledger}});
- for (const b of bills) {
- b.precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, b.unit);
- }
- const updatePosStage = [], insertPosStage = [];
- for (const d of datas) {
- const b = this._.find(bills, {id: d.lid});
- const osp = this._.find(orgStagePos, function (p) { return p.pid === d.pid; });
- if (osp && osp.times === this.ctx.stage.curTimes && osp.order === this.ctx.stage.curOrder) {
- const sp = { id: osp.id, pid: osp.pid, said: this.ctx.session.sessionUser.accountId };
- if (d.contract_qty !== undefined) {
- sp.contract_qty = this.ctx.helper.round(d.contract_qty, b.precision.value);
- }
- sp.contract_expr = d.contract_expr;
- updatePosStage.push(sp);
- } else {
- const sp = {
- pid: d.pid, lid: d.lid,
- tid: this.ctx.tender.id, sid: this.ctx.stage.id,
- said: this.ctx.session.sessionUser.accountId,
- times: this.ctx.stage.curTimes, order: this.ctx.stage.curOrder
- };
- if (d.contract_qty !== undefined || osp) {
- sp.contract_qty = d.contract_qty === undefined && osp
- ? osp.contract_qty
- : this.ctx.helper.round(d.contract_qty, b.precision.value);
- sp.contract_expr = d.contract_expr;
- }
- insertPosStage.push(sp);
- }
- result.pos.push(d.pid);
- }
- const updateBillsStage = [], insertBillsStage = [], info = this.ctx.tender.info;
- for (const b of bills) {
- const stageBills = await this.ctx.service.stageBills.getLastestStageData2(b.tender_id, this.ctx.stage.id, b.id);
- const posStage = await this.getLastestStageData2(b.tender_id, this.ctx.stage.id, {lid: b.id});
- let contract_qty = 0;
- const newPosRange = insertPosStage.filter(x => {return x.lid === b.id});
- for (const nps of newPosRange) {
- contract_qty = this.ctx.helper.add(contract_qty, nps.contract_qty);
- }
- for (const ps of posStage) {
- const ips = this._.find(insertPosStage, {pid: ps.pid});
- if (!ips) {
- const ups = this._.find(updatePosStage, {id: ps.id});
- contract_qty = this.ctx.helper.add(contract_qty, ups ? ups.contract_qty : ps.contract_qty);
- }
- }
- if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
- if (contract_qty === stageBills.contract_qty) continue;
- updateBillsStage.push({
- id: stageBills.id,
- said: this.ctx.session.sessionUser.accountId,
- contract_qty: contract_qty,
- contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
- });
- } else {
- insertBillsStage.push({
- tid: this.ctx.tender.id,
- lid: b.id,
- sid: this.ctx.stage.id,
- times: this.ctx.stage.curTimes,
- order: this.ctx.stage.curOrder,
- said: this.ctx.session.sessionUser.accountId,
- contract_qty: contract_qty,
- contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
- })
- }
- }
- const transaction = await this.db.beginTransaction();
- try {
- if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
- if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
- if (updateBillsStage.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
- if (insertBillsStage.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
- await transaction.commit();
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 删除部位明细数据(仅供updateStageData调用)
- *
- * @param transaction - 事务
- * @param data - 删除的部位明细(允许一次提醒多条,也允许跨清单(但前端操作不允许))
- * @returns {Promise<{}>}
- * @private
- */
- async _deleteStagePosData(data) {
- const pos = await this.ctx.service.pos.getPosData({tid: this.ctx.tender.id, id: data});
- const result = { pos: data, isDeletePos: true };
- if (pos instanceof Array) {
- for (const p of pos) {
- if (p.add_stage !== this.ctx.stage.id /*|| p.add_times !== this.ctx.stage.curTimes || p.add_user !== this.ctx.session.sessionUser.accountId*/) {
- throw '不可删除该数据';
- }
- if (p.lid !== pos[0].lid) {
- throw '提交数据错误';
- }
- }
- } else if (pos.add_stage !== this.ctx.stage.id /*|| pos.add_times !== this.ctx.stage.curTimes || pos.add_user !== this.ctx.session.sessionUser.accountId*/) {
- throw '不可删除该数据';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 删除部位明细
- await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
- // 删除部位明细计量数据
- await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
- await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos[0].lid, transaction);
- await transaction.commit();
- // 获取需要更新的数据
- result.ledger = [pos[0].lid];
- result.stageUpdate = true;
- return result;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 根据前端提交数据,更新并计算
- *
- * @param data
- * @returns {Promise<{ledger: {}, pos: {}}>}
- */
- async updateStageData(data) {
- if ((data.updateType === 'add' || data.upateType === 'delete') && this.ctx.tender.measure_type === measureType.tz) {
- throw '台账模式下,不可在计量中新增或删除部位明细,如需操作,请进行台账修订';
- }
- let refreshData;
- if (data.updateType === 'add') {
- refreshData = await this._addStagePosData(data.updateData);
- } else if (data.updateType === 'update') {
- refreshData = await this._updateStagePosData(data.updateData);
- } else if (data.updateType === 'batchUpdate') {
- refreshData = await this._batchUpdateStagePosData(data.updateData);
- } else if (data.updateType === 'delete') {
- if (!data.updateData || data.updateData.length === 0) {
- throw '提交数据错误';
- }
- refreshData = await this._deleteStagePosData(data.updateData);
- } else {
- throw '提交数据错误';
- }
- try {
- const result = {ledger: {}, pos: {}};
- if (refreshData.ledger && refreshData.ledger.length > 0) {
- result.ledger.bills = await this.ctx.service.ledger.getDataByIds(refreshData.ledger);
- if (refreshData.stageUpdate) {
- result.ledger.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, refreshData.ledger);
- }
- }
- if (refreshData.pos) {
- if (refreshData.isDeletePos) {
- result.pos.pos = refreshData.pos;
- } else if (refreshData.pos.length > 0) {
- result.pos.pos = await this.ctx.service.pos.getPosDataWithAddStageOrder({tid: this.ctx.tender.id, id: refreshData.pos});
- if (refreshData.stageUpdate) {
- result.pos.curStageData = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: refreshData.pos});
- }
- }
- }
- return result;
- } catch(err) {
- throw '获取数据异常,请刷新页面。';
- }
- }
- async updateChangeQuantity(transaction, pos, qty, noValue, positiveQty, negativeQty) {
- let orgPos = await this.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, {pid: pos.id});
- if (orgPos.length > 1) {
- throw '数据错误';
- } else {
- orgPos = orgPos[0];
- }
- if (orgPos && orgPos.times === this.ctx.stage.curTimes && orgPos.order === this.ctx.stage.curOrder) {
- await transaction.update(this.tableName, noValue
- ? { id: orgPos.id, qc_minus_qty: qty || 0, said: this.ctx.session.sessionUser.accountId, }
- : { id: orgPos.id, qc_qty: qty, positive_qc_qty: positiveQty, negative_qc_qty: negativeQty, said: this.ctx.session.sessionUser.accountId }
- );
- } else {
- await transaction.insert(this.tableName, {
- tid: this.ctx.tender.id,
- sid: this.ctx.stage.id,
- lid: pos.lid,
- pid: pos.id,
- said: this.ctx.session.sessionUser.accountId,
- times: this.ctx.stage.curTimes,
- order: this.ctx.stage.curOrder,
- contract_qty: orgPos ? orgPos.contract_qty : 0,
- qc_qty: !noValue ? qty : (orgPos ? orgPos.qc_qty : 0),
- qc_minus_qty: noValue ? qty : (orgPos ? orgPos.qc_minus_qty : 0),
- positive_qc_qty: !noValue ? positiveQty : (orgPos ? orgPos.positive_qc_qty : 0),
- negative_qc_qty: !noValue ? negativeQty : (orgPos ? orgPos.negative_qc_qty : 0),
- });
- }
- await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
- }
- /**
- * 统计清单下部位明细合计
- * @param {Number} tid - 标段id
- * @param {Number} sid - 期id
- * @param {Number} lid - 清单节点id
- * @param transaction - 事务(不为空则在事务中查询,反之在数据库中查询)
- * @returns {Promise<*>}
- */
- async getPosGather(tid, sid, lid, transaction) {
- const calcQtySql = 'SELECT SUM(`contract_qty`) As `contract_qty`, SUM(`qc_qty`) As `qc_qty`, SUM(`qc_minus_qty`) AS `qc_minus_qty`, SUM(`positive_qc_qty`) As `positive_qc_qty`, SUM(`negative_qc_qty`) As `negative_qc_qty` FROM (' +
- ' SELECT `contract_qty`, `qc_qty`, `qc_minus_qty`, `positive_qc_qty`, `negative_qc_qty` FROM ' + this.ctx.service.stagePos.tableName + ' As Pos ' +
- ' INNER JOIN (' +
- ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `pid` ' +
- ' FROM ' + this.ctx.service.stagePos.tableName +
- ' WHERE `tid` = ? And sid = ? And `lid` = ? ' +
- ' GROUP BY `pid`' +
- ' ) As MaxFilter ' +
- ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid ' +
- ' WHERE Pos.tid = ? And Pos.sid = ? And Pos.lid = ?' +
- ' ) As Gather';
- const param = [tid, sid, lid];
- const sqlParam = param.concat(param);
- if (transaction) {
- return await transaction.queryOne(calcQtySql, sqlParam);
- } else {
- return await this.db.queryOne(calcQtySql, sqlParam);
- }
- }
- /**
- * 多期清单数据整合 (材料调差调用)
- * @param {Number} tid - 标段id
- * @param {String} stage_id_list - 期id列表
- * @param {String} comefrom - 来源(部分不调用计量不获取)
- * @returns {Promise<void>}
- */
- async getStagesData(tid, stage_id_list, comefrom = '') {
- const sids = stage_id_list.split(',');
- const result = [];
- for (const sid of sids) {
- const sql = 'SELECT id, tid, sid, pid, lid, contract_qty, qc_qty, `qc_minus_qty`, postil, `times`, `order`, `contract_expr`' +
- ' FROM ' + this.tableName +
- ' WHERE tid = ? And sid = ? ';
- const sqlParam = [tid, sid];
- const stagePos = await this.db.query(sql, sqlParam);
- const stagePosFilter = this._filterLastestData(stagePos);
- for (const sp of stagePosFilter) {
- const rsp = result.find(function (x) { return x.pid === sp.pid});
- if (rsp) {
- rsp.contract_qty = this.ctx.helper.add(rsp.contract_qty, sp.contract_qty);
- rsp.qc_qty = this.ctx.helper.add(rsp.qc_qty, sp.qc_qty);
- rsp.qc_minus_qty = this.ctx.helper.add(rsp.qc_minus_qty, sp.qc_minus_qty);
- } else if (!comefrom || (comefrom === 'list' && (sp.contract_qty || sp.qc_qty || sp.qc_minus_qty))) {
- result.push({
- id: sp.id, tid: sp.tid, lid: sp.lid, pid: sp.pid,
- contract_qty: sp.contract_qty, qc_qty: sp.qc_qty, qc_minus_qty: sp.qc_minus_qty,
- });
- }
- }
- }
- return result;
- }
- /**
- * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
- * @param {Number} tid - 标段id
- * @param {String} stage_id_list - 期id列表
- * @param {String} lid - 台账id
- * @param {String} pid - 部位id
- * @returns {Promise<void>}
- */
- async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
- stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
- const qtys = {
- gather_qty: 0,
- contract_qty: 0,
- qc_qty: 0,
- qc_minus_qty: 0,
- };
- for (const sid of stage_id_list) {
- const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
- ' (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
- ' INNER JOIN ( ' +
- ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
- ' WHERE `tid` = ? AND sid = ?' +
- ' GROUP BY `pid`' +
- ' ) As MaxFilter ' +
- ' ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
- ' WHERE Pos.lid = ? AND Pos.pid = ?';
- const sqlParam = [tid, sid, tid, sid, lid, pid];
- const result = await this.db.queryOne(sql, sqlParam);
- if (result) {
- qtys.contract_qty = this.ctx.helper.add(qtys.contract_qty, result.contract_qty);
- qtys.qc_qty = this.ctx.helper.add(qtys.qc_qty, result.qc_qty);
- qtys.qc_minus_qty = this.ctx.helper.add(qtys.qc_minus_qty, result.qc_minus_qty);
- qtys.gather_qty = this.ctx.helper.add(qtys.gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
- }
- }
- return this.ctx.helper.resetQtys(qtys);
- }
- }
- return StagePos;
- };
|