123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit').stage;
- const decimal = { up: 6, qty: 3 };
- module.exports = app => {
- class StageYjcl extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'stage_yjcl';
- }
- async getStageData(stage) {
- const data = await this.getAllDataByCondition({ where: { sid: stage.id } });
- if (!stage.readOnly || stage.status === auditConst.status.checked) return data;
- 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,
- });
- d.qty = h ? h.qty : 0;
- d.tp = h ? h.tp : 0;
- }
- 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.m_order) throw '新增永久材料,提交的数据错误';
- const nd = {
- uuid: this.uuid.v4(),
- add_sid: this.ctx.stage.id,
- add_sorder: this.ctx.stage.order,
- add_uid: this.ctx.session.sessionUser.accountId,
- tid: this.ctx.tender.id,
- sid: this.ctx.stage.id,
- sorder: this.ctx.stage.order,
- };
- nd.name = d.name;
- nd.m_order = d.m_order;
- nd.spec = d.spec || '';
- nd.unit = d.unit || '';
- nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
- nd.arrive_time = d.arrive_time || '';
- nd.source = d.source || '';
- nd.bills_code = d.bills_code || '';
- nd.location = d.location || '';
- nd.prepare_pos = d.prepare_pos || '';
- nd.memo = d.memo || '';
- const extraPre = 1 + this.ctx.helper.div(nd.tax, 100, 2);
- nd.arrive_qty = d.arrive_qty ? this.ctx.helper.round(d.arrive_qty, decimal.qty) : 0;
- nd.arrive_tp = d.arrive_tp ? this.ctx.helper.round(d.arrive_tp, tpDecimal) : 0;
- nd.unit_price = d.arrive_qty ? this.ctx.helper.div(d.arrive_tp, d.arrive_qty, decimal.up) : 0;
- nd.ex_tax_up = d.arrive_qty ? this.ctx.helper.div(this.ctx.helper.div(d.arrive_tp, d.arrive_qty), extraPre, decimal.up) : 0;
- // nd.unit_price = d.unit_price ? this.ctx.helper.round(d.unit_price, decimal.up) : 0;
- // 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, decimal.qty);
- // nd.arrive_tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.arrive_qty), extraPre, tpDecimal);
- // }
- nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
- // nd.tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.qty), extraPre, tpDecimal);
- nd.tp = this.ctx.helper.mul(nd.unit_price, nd.qty, tpDecimal);
- 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 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 !== undefined) nd.name = d.name;
- if (d.m_order !== undefined) nd.m_order = d.m_order;
- if (d.spec !== undefined) nd.spec = d.spec || '';
- if (d.unit !== undefined) nd.unit = d.unit || '';
- if (d.tax !== undefined) nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
- if (d.arrive_time !== undefined) nd.arrive_time = d.arrive_time || '';
- if (d.source !== undefined) nd.source = d.source || '';
- if (d.bills_code !== undefined) nd.bills_code = d.bills_code || '';
- if (d.location !== undefined) nd.location = d.location || '';
- if (d.prepare_pos !== undefined) nd.prepare_pos = d.prepare_pos || '';
- if (d.memo !== undefined)nd.memo = d.memo || '';
- if (d.arrive_qty !== undefined) nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, decimal.qty);
- if (d.arrive_tp !== undefined) nd.arrive_tp = this.ctx.helper.round(d.arrive_tp, tpDecimal);
- let unit_price = od.unit_price, ex_tax_up = od.ex_tax_up;
- if (nd.arrive_qty !== undefined || nd.arrive_tp !== undefined || nd.tax !== undefined) {
- const tp = nd.arrive_tp !== undefined ? nd.arrive_tp : od.arrive_tp;
- const qty = nd.arrive_qty !== undefined ? nd.arrive_qty : od.arrive_qty;
- const extraPre = nd.tax !== undefined ? 1 + this.ctx.helper.div(nd.tax, 100, 2) : 1 + this.ctx.helper.div(od.tax, 100, 2);
- nd.unit_price = qty ? this.ctx.helper.div(tp, qty, decimal.up) : 0;
- nd.ex_tax_up = qty ? this.ctx.helper.div(this.ctx.helper.div(tp, qty), extraPre, decimal.up) : 0;
- unit_price = nd.unit_price;
- ex_tax_up = nd.ex_tax_up;
- }
- if (d.qty !== undefined) {
- nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
- } else if (nd.unit_price) {
- nd.qty = od.qty;
- }
- if (nd.qty !== undefined) nd.tp = this.ctx.helper.mul(unit_price, nd.qty, tpDecimal);
- 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 updateHistory4TimesOrder(stage, times, order, transaction) {
- const datas = await this.getStageData(stage);
- if (datas.length === 0) return;
- const updateDatas = [];
- for (const d of datas) {
- for (const curOrder of order) {
- const history = d.shistory ? JSON.parse(d.shistory) : [];
- const his = history.find(function (x) {
- return x.stimes === times && x.sorder === curOrder;
- });
- if (his) {
- his.qty = d.qty;
- his.tp = d.tp;
- } else {
- history.push({ stimes: times || 1, sorder: curOrder || 0, qty: d.qty, tp: d.tp });
- }
- updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
- }
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- async updateHistory(stage, transaction) {
- await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
- }
- async updateHistory4CheckAgain(stage, transaction) {
- await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
- }
- async addInitialStageData(stage, preStage, transaction) {
- if (!stage || !preStage) {
- throw '标段数据有误';
- }
- const preDatas = await this.getAllDataByCondition({
- columns: ['uuid', 'add_sid', 'add_sorder', 'add_uid', 'tid', 'name', 'spec', 'm_order', 'unit', 'tax', 'arrive_time', 'source', 'bills_code', 'location', 'prepare_pos', 'memo',
- 'arrive_qty', 'arrive_tp', 'unit_price', 'ex_tax_up', 'qty', 'tp', 'pre_qty', 'pre_tp', '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.qty);
- pd.pre_qty = this.ctx.helper.add(pd.qty, pd.pre_qty);
- pd.pre_tp = this.ctx.helper.add(pd.tp, pd.pre_tp);
- delete pd.qty;
- delete pd.tp;
- pd.sid = stage.id;
- pd.sorder = stage.order;
- }
- await transaction.delete(this.tableName, { 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 } });
- 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(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),
- qty: his.length > 0 ? his[his.length - 1].qty : 0,
- tp: his.length > 0 ? his[his.length - 1].tp : 0,
- };
- updateDatas.push(ud);
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- }
- return StageYjcl;
- };
|