123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- 'use strict';
- /**
- * 奖罚金
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit').stage;
- module.exports = app => {
- class StageBonus extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'stage_bonus';
- }
- _parseData(data) {
- if (!data) return;
- const datas = data instanceof Array ? data : [data];
- for (const d of datas) {
- if (d.proof_file) {
- d.proof_file = JSON.parse(d.proof_file);
- } else {
- d.proof_file = [];
- }
- }
- }
- async getStageData(sid) {
- const data = await this.getAllDataByCondition({where: { sid: sid }});
- if (this.ctx.stage && this.ctx.stage.readOnly && this.ctx.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: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder
- });
- d.tp = h ? h.tp : null;
- }
- }
- this._parseData(data);
- return data;
- }
- async getPreStageData(sorder) {
- const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder < ? And tid = ?';
- const sqlParam = [sorder, this.ctx.tender.id];
- const data = await this.db.query(sql, sqlParam);
- this._parseData(data);
- return data;
- }
- async getEndStageData(sorder) {
- const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder <= ? And tid = ? ORDER BY `sorder`, `order`';
- const sqlParam = [sorder, this.ctx.tender.id];
- const data = await this.db.query(sql, sqlParam);
- this._parseData(data);
- return data;
- }
- async getStageDataById(bonusId) {
- const data = await this.getAllDataByCondition({ where: { id: bonusId } });
- this._parseData(data);
- return data[0];
- }
- 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 = {
- id: this.uuid.v4(),
- tid: this.ctx.tender.id,
- sid: this.ctx.stage.id,
- sorder: this.ctx.stage.order,
- uid: this.ctx.session.sessionUser.accountId,
- create_time: new Date(),
- name: d.name,
- order: d.order,
- };
- nd.b_type = d.b_type ? d.b_type : null;
- nd.tp = d.tp ? this.ctx.helper.round(d.tp, tpDecimal) : 0;
- nd.code = d.code ? d.code: null;
- nd.proof = d.proof ? d.proof : null;
- nd.real_time = d.real_time ? d.real_time : null;
- nd.memo = d.memo ? d.memo : null;
- nd.doc_co = d.doc_co ? d.doc_co : null;
- insertData.push(nd);
- }
- await this.db.insert(this.tableName, insertData);
- return insertData;
- }
- 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) {
- console.log(od);
- console.log(this.ctx.stage.id);
- if (od.sid !== this.ctx.stage.id) 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.b_type !== undefined) nd.b_type = d.b_type;
- if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, tpDecimal);
- if (d.code !== undefined) nd.code = d.code;
- if (d.proof !== undefined) nd.proof = d.proof;
- if (d.proof_file !== undefined) nd.proof_file = JSON.stringify(d.proof_file);
- if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);
- if (d.memo !== undefined) nd.memo = d.memo;
- if (d.order !== undefined) nd.order = d.order;
- if (d.doc_co !== undefined) nd.doc_co = d.doc_co;
- 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.id);
- 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 && x.sorder === order;
- });
- if (his) {
- his.tp = d.tp;
- } else {
- history.push({ stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp });
- }
- updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- 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);
- });
- updateDatas.push({
- id: d.id,
- shistory: JSON.stringify(his),
- tp: his.length > 0 ? his[his.length - 1].tp : null,
- });
- }
- await transaction.updateRows(this.tableName, updateDatas);
- }
- }
- return StageBonus;
- };
|