123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class RevisePos extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'revise_pos';
- }
- async getPosData(condition) {
- const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.drawing_code, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.porder, p.add_stage, p.add_times, p.add_user, s.order As add_stage_order ' +
- ' FROM ' + this.tableName + ' p ' +
- ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
- ' ON add_stage = s.id'
- + this.ctx.helper.whereSql(condition, 'p');
- return await this.db.query(sql);
- }
- /**
- * 获取 修订 清单数据
- * @param {Number}tid - 标段id
- * @param {uuid}rid - 修订id
- * @returns {Promise<void>}
- */
- async getData(tid) {
- return await this.db.select(this.tableName, {
- where: {tid: tid}
- });
- }
- async getDataByLid(tid, lid) {
- return await this.db.select(this.tableName, {
- where: {tid: tid, lid: lid}
- });
- }
- async insertLedgerPosData(transaction, tid, rid, bills, data) {
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- const insertDatas = [];
- for (const d of data) {
- const inD = {
- id: this.uuid.v4(), tid: tid, lid: bills.id, crid: rid,
- add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
- name: d.name, drawing_code: d.drawing_code,
- };
- if (d.quantity) {
- inD.sgfh_qty = this.round(d.quantity, precision.value);
- inD.quantity = inD.sgfh_qty;
- }
- insertDatas.push(inD);
- }
- await transaction.insert(this.tableName, insertDatas);
- }
- /**
- * 删除清单下部位明细数据(删除清单时调用)
- *
- * @param transaction - 事务
- * @param tid - 标段id
- * @param lid - 清单id
- * @returns {Promise<void>}
- */
- async deletePosData(transaction, tid, lid) {
- await transaction.delete(this.tableName, {tid: tid, lid: lid});
- }
- async _insertPosData(transaction, data, tid, rid) {
- data.id = this.uuid.v4();
- data.tid = tid;
- // todo 新增期
- data.add_stage = 0;
- data.add_times = 0;
- data.in_time = new Date();
- data.add_user = this.ctx.session.sessionUser.accountId;
- data.crid = rid;
- if (data.quantity) {
- const bills = await this.ctx.service.reviseBills.getDataById(data.lid);
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- data.quantity = this.round(data.quantity, precision.value);
- }
- const addRst = await transaction.insert(this.tableName, data);
- }
- async addPos(tid, rid, data) {
- const transaction = await this.db.beginTransaction();
- try {
- if (data instanceof Array) {
- for (const d of data) {
- this._insertPosData(transaction, d, tid, rid);
- }
- } else {
- this._insertPosData(transaction, data, tid, rid);
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return {pos: data};
- }
- async updatePos(tid, data) {
- const billsIds = [];
- const datas = data instanceof Array ? data : [data];
- const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
- const transaction = await this.db.beginTransaction();
- try {
- for (const d of datas) {
- const op = this._.find(orgPos, function (p) { return p.id = d.id; });
- if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
- const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- if (d.sgfh_qty !== undefined) {
- d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
- } else if (op) {
- d.sgfh_qty = op.sgfh_qty;
- }
- if (d.sjcl_qty !== undefined) {
- d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
- } else if (op) {
- d.sjcl_qty = op.sjcl_qty;
- }
- if (d.qtcl_qty !== undefined) {
- d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
- } else if (op) {
- d.qtcl_qty = op.qtcl_qty;
- }
- d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
- }
- await transaction.update(this.tableName, d, {tid: tid, id: d.id});
- if (d.quantity !== undefined && op && (billsIds.indexOf(op.lid) === -1)) {
- billsIds.push(op.lid);
- }
- }
- for (const lid of billsIds) {
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- const bills = await this.ctx.service.reviseBills.getDataById(billsIds);
- return {pos: data, ledger: {update: bills}};
- }
- async deletePos(tid, data) {
- if (!data || data.length === 0) {
- throw '提交数据错误';
- }
- const pos = await this.getPosData({tid: tid, id: data});
- const ledgerIds = this._.map(pos, 'lid');
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, {tid: tid, id: data});
- for (const lid of ledgerIds) {
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- const bills = await this.ctx.service.reviseBills.getDataById(ledgerIds);
- return {ledger: {update: bills}, pos: data};
- }
- /**
- * 保存部位明细数据
- * @param data
- * @param {Number} tid - 标段id
- * @returns {Promise<{ledger: {}, pos: null}>}
- */
- async savePosData(tid, rid, data) {
- const transaction = await this.db.beginTransaction();
- try {
- if (data.posPostType === 'add') {
- if (data.updateData instanceof Array) {
- for (const d of data.postData) {
- this._insertPosData(transaction, d, tid, rid);
- }
- } else {
- this._insertPosData(transaction, data.postData, tid, rid);
- }
- } else if (data.posPostType === 'update') {
- const datas = data.postData instanceof Array ? data.postData : [data.postData];
- result.ledger.update = [];
- const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
- for (const d of datas) {
- const op = this._.find(orgPos, function (p) { return p.id = d.id; });
- if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
- const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- if (d.sgfh_qty !== undefined) {
- d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
- } else if (op) {
- d.sgfh_qty = op.sgfh_qty;
- }
- if (d.sjcl_qty !== undefined) {
- d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
- } else if (op) {
- d.sjcl_qty = op.sjcl_qty;
- }
- if (d.qtcl_qty !== undefined) {
- d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
- } else if (op) {
- d.qtcl_qty = op.qtcl_qty;
- }
- d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
- }
- await transaction.update(this.tableName, d, {tid: tid, id: d.id});
- if (d.quantity !== undefined && op && (result.ledger.update.indexOf(op.lid) === -1)) {
- result.ledger.update.push(op.lid);
- }
- }
- for (const lid of result.ledger.update) {
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
- }
- } else if (data.posPostType === 'delete') {
- if (!data.postData || data.postData.length === 0) {
- throw '提交数据错误';
- }
- const pos = await this.getPosData({tid: tid, id: data.postData});
- const ledgerIds = this._.map(pos, 'lid');
- await transaction.delete(this.tableName, {tid: tid, id: data.postData});
- for (const lid of ledgerIds) {
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
- }
- result.ledger.update = ledgerIds;
- } else {
- throw '提交数据错误';
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- result.pos = data.postData;
- result.ledger.update = await this.ctx.service.reviseBills.getDataById(result.ledger.update);
- return result;
- }
- /**
- * 复制粘贴 部位明细数据
- * @param {Array} data - 复制粘贴的数据
- * @param {Number} tid - 标段id
- * @returns {Promise<{ledger: {}, pos: null}>}
- */
- async pastePosData(tid, rid, data) {
- if (!(data instanceof Array)) throw '提交数据错误';
- const transaction = await this.db.beginTransaction();
- const result = { ledger: {}, pos: null }, updateLid = [];
- const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
- let bills = null, precision = null;
- try {
- for (const d of data) {
- const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
- if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
- if (!bills || bills.id !== d.lid) {
- bills = await this.ctx.service.reviseBills.getDataById(d.lid);
- precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- updateLid.push(d.lid);
- }
- if (d.sgfh_qty !== undefined) {
- d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
- } else if (op) {
- d.sgfh_qty = op.sgfh_qty;
- }
- if (d.sjcl_qty !== undefined) {
- d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
- } else if (op) {
- d.sjcl_qty = op.sjcl_qty;
- }
- if (d.qtcl_qty) {
- d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
- } else if (op) {
- d.qtcl_qty = op.qtcl_qty;
- }
- d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
- }
- if (d.id) {
- await transaction.update(this.tableName, d);
- } else {
- this._insertPosData(transaction, d, tid, rid);
- }
- }
- for (const lid of updateLid) {
- await this.ctx.service.reviseBills.calc(tid, lid, transaction);
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- result.pos = data;
- if (updateLid.length > 0) {
- result.ledger.update = await this.ctx.service.reviseBills.getDataById(updateLid);
- }
- return result;
- }
- }
- return RevisePos;
- };
|