123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit').stage;
- module.exports = app => {
- class StageJgcl extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'stage_jgcl';
- }
- async getStageData(stage, cancel = false) {
- const data = await this.getAllDataByCondition({where: { sid: stage.id }});
- if (!cancel && stage && stage.readOnly && !this.ctx.tender.isTourist && stage.status !== auditConst.status.checked) {
- for (const d of data) {
- const his = d.shistory ? JSON.parse(d.shistory) : [];
- const h = this.ctx.helper._.find(his, {
- stimes: stage.curTimes, sorder: stage.curOrder
- });
- if (h) {
- d.arrive_qty = h.arrive_qty ? h.arrive_qty : h.arraive_qty;
- d.arrive_tp = h.arrive_tp;
- d.deduct_qty = h.deduct_qty;
- d.deduct_tp = h.deduct_tp;
- } else {
- d.arrive_qty = null;
- d.arrive_tp = null;
- d.deduct_qty = null;
- d.deduct_tp = null;
- }
- }
- }
- return data;
- }
- async getPreStageData(tid, sorder) {
- const sql = 'SELECT c.uuid, Sum(c.arrive_qty) as arrive_qty, Sum(c.arrive_tp) as arrive_tp,' +
- ' Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp' +
- ' From ' + this.tableName + ' c' +
- ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
- ' WHERE s.`order` < ? And s.`tid` = ?' +
- ' GROUP By uuid';
- const sqlParam = [sorder, tid];
- const data = await this.db.query(sql, sqlParam);
- return data;
- }
- async getEndStageData(tid, sorder) {
- const sql = 'SELECT c.uuid, Sum(c.arrive_qty) as arrive_qty, Sum(c.arrive_tp) as arrive_tp,' +
- ' Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp' +
- ' From ' + this.tableName + ' c' +
- ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
- ' WHERE s.`order` <= ? And s.`tid` = ?' +
- ' GROUP By uuid';
- const sqlParam = [sorder, tid];
- const data = await this.db.query(sql, sqlParam);
- return data;
- }
- async _addDatas(data) {
- const tpDecimal = this.ctx.tender.info.decimal.extra
- ? this.ctx.tender.info.decimal.extraTp
- : this.ctx.tender.info.decimal.tp;
- const datas = data instanceof Array ? data : [data];
- const insertData = [];
- for (const d of datas) {
- if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
- const nd = {
- uuid: this.uuid.v4(),
- add_sid: this.ctx.stage.id,
- add_uid: this.ctx.session.sessionUser.accountId,
- tid: this.ctx.tender.id,
- sid: this.ctx.stage.id,
- };
- nd.name = d.name;
- nd.order = d.order;
- if (d.unit) nd.unit = d.unit;
- if (d.unit_price) nd.unit_price = d.unit_price;
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit);
- if (d.arrive_qty) {
- nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
- nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
- }
- if (d.deduct_qty) {
- nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
- nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
- }
- if (d.source) nd.source = d.source;
- if (d.bills_code) nd.bills_code = d.bills_code;
- if (d.check_code) nd.check_code = d.check_code;
- if (d.memo) nd.memo = d.memo;
- insertData.push(nd);
- }
- await this.db.insert(this.tableName, insertData);
- return await this.getAllDataByCondition({
- where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
- });
- }
- async _delDatas (data) {
- const datas = data instanceof Array ? data : [data];
- const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
- for (const od of orgDatas) {
- if (od.pre_used) throw '甲供材料往期已经计量,不可删除';
- }
- await this.db.delete(this.tableName, {id: datas});
- return datas;
- }
- async _updateDatas (data) {
- const tpDecimal = this.ctx.tender.info.decimal.extra
- ? this.ctx.tender.info.decimal.extraTp
- : this.ctx.tender.info.decimal.tp;
- const datas = data instanceof Array ? data : [data];
- const orgDatas = await this.getAllDataByCondition({
- where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') }
- });
- const info = this.ctx.tender.info;
- const uDatas = [];
- for (const d of datas) {
- const od = this.ctx.helper._.find(orgDatas, {id: d.id});
- if (!od) continue;
- const nd = {id: od.id};
- if (d.name) nd.name = d.name;
- if (d.unit !== undefined) nd.unit = d.unit;
- nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
- // if (od.pre_used === null || od.pre_used === undefined || od.pre_used === 0) {
- // if (d.unit !== undefined) nd.unit = d.unit;
- // nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
- // } else {
- // nd.unit_price = od.unit_price;
- // }
- const precision = this.ctx.helper.findPrecision(info.precision, d.unit || od.unit);
- if (d.arrive_qty !== undefined) {
- nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
- nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
- } else if (d.unit_price !== undefined) {
- nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, od.arrive_qty, tpDecimal);
- }
- if (d.deduct_qty !== undefined) {
- nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
- nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
- } else if (d.unit_price !== undefined) {
- nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, od.deduct_qty, tpDecimal);
- }
- if (d.source !== undefined) nd.source = d.source;
- if (d.bills_code !== undefined) nd.bills_code = d.bills_code;
- if (d.check_code !== undefined) nd.check_code = d.check_code;
- if (d.memo !== undefined) nd.memo = d.memo;
- if (d.order !== undefined) nd.order = d.order;
- uDatas.push(nd);
- }
- if (uDatas.length > 0) {
- await this.db.updateRows(this.tableName, uDatas);
- return uDatas;
- } else {
- return [];
- }
- }
- async updateDatas(data) {
- const result = {add: [], del: [], update: []};
- try {
- if (data.add) {
- result.add = await this._addDatas(data.add);
- }
- if (data.update) {
- result.update = await this._updateDatas(data.update);
- }
- if (data.del) {
- result.del = await this._delDatas(data.del);
- }
- return result;
- } catch (err) {
- if (err) result.err = err;
- return result;
- }
- }
- async updateHistory(stage, transaction) {
- const datas = await this.getStageData(stage);
- if (datas.length === 0) return;
- const updateDatas = [];
- const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
- for (const d of datas) {
- const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
- const his = history.find(function (x) {
- return x.stimes && x.stimes === times
- && x.sorder !== undefined && x.sorder !== null && x.sorder === order;
- });
- if (his) {
- his.arrive_qty = d.arrive_qty;
- his.arrive_tp = d.arrive_tp;
- his.deduct_qty = d.deduct_qty;
- his.deduct_tp = d.deduct_tp;
- } else {
- history.push({
- stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder,
- arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
- deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
- });
- }
- updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- async updateHistory4CheckCancel(stage, newTimes, newOrder, transaction) {
- const datas = await this.getStageData(stage, true);
- if (datas.length === 0) return;
- const updateDatas = [];
- for (const d of datas) {
- const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
- const his = history.find(function (x) {
- return x.stimes === newTimes && x.sorder === newOrder;
- });
- if (his) {
- his.arrive_qty = d.arrive_qty;
- his.arrive_tp = d.arrive_tp;
- his.deduct_qty = d.deduct_qty;
- his.deduct_tp = d.deduct_tp;
- } else {
- history.push({
- stimes: newTimes, sorder: newOrder,
- arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
- deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
- });
- }
- updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- async updateHistory4CheckAgain(stage, transaction) {
- const datas = await this.getStageData(stage);
- if (datas.length === 0) return;
- const updateDatas = [];
- const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder + 1;
- for (const d of datas) {
- const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
- const his = history.find(function (x) {
- return x.stimes && x.stimes === times
- && x.sorder && x.sorder === order;
- });
- if (his) {
- his.unit_price = d.unit_price;
- his.arrive_qty = d.arrive_qty;
- his.arrive_tp = d.arrive_tp;
- his.deduct_qty = d.deduct_qty;
- his.deduct_tp = d.deduct_tp;
- } else {
- history.push({
- stimes: times, sorder: order, unit_price: d.unit_price,
- arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
- deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
- });
- }
- updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- async addInitialStageData(stage, preStage, transaction) {
- if (!stage || !preStage) {
- throw '标段数据有误';
- }
- const preDatas = await this.getAllDataByCondition({
- columns: ['uuid', 'name', 'unit', 'unit_price', 'source', 'bills_code', 'check_code', 'memo', 'add_uid', 'add_sid', 'tid', 'order', 'arrive_qty', 'deduct_qty', 'pre_used'],
- where: { sid: preStage.id },
- });
- if (preDatas.length > 0) {
- for (const pd of preDatas) {
- pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.arrive_qty) || !this.ctx.helper.checkZero(pd.deduct_qty);
- delete pd.arrive_qty;
- delete pd.deduct_qty;
- pd.sid = stage.id;
- }
- const result = await transaction.insert(this.tableName, preDatas);
- return result.affectedRows === preDatas.length;
- } else {
- return true;
- }
- }
- async deleteStageTimesData(sid, times, transaction) {
- const datas = await this.getAllDataByCondition({where: { sid: sid }});
- if (datas.length === 0) return;
- const updateDatas = [];
- for (const d of datas) {
- const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
- const his = history.filter(function (x) {
- return x.stimes && x.stimes < times;
- });
- his.sort(function (x, y) {
- return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
- });
- const ud = {
- id: d.id,
- shistory: JSON.stringify(his),
- arrive_qty: his.length > 0 ? his[his.length - 1].arrive_qty : null,
- deduct_qty: his.length > 0 ? his[his.length - 1].deduct_qty : null,
- arrive_tp: his.length > 0 ? this.ctx.helper.mul(his[his.length - 1].arrive_qty, d.unit_price, this.ctx.tender.info.decimal.tp) : null,
- deduct_tp: his.length > 0 ? this.ctx.helper.mul(his[his.length - 1].deduct_qty, d.unit_price, this.ctx.tender.info.decimal.tp) : null,
- };
- updateDatas.push(ud);
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- }
- return StageJgcl;
- };
|