'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) { const helper = this.ctx.helper; 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); d.proof_file.forEach(f => { if (helper.canPreview(f.fileext)){ f.viewpath = f.filepath.substr(3, f.filepath.length - 3); } }); } else { d.proof_file = []; } } } async getStageData(sid, cancel = false) { const data = await this.getAllDataByCondition({where: { sid: sid }}); if (!cancel && this.ctx.stage && this.ctx.stage.readOnly && !this.ctx.tender.isTourist && 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(tid, sorder) { const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder < ? And tid = ?'; const sqlParam = [sorder, tid]; const data = await this.db.query(sql, sqlParam); this._parseData(data); return data; } async getEndStageData(tid, sorder) { const sql = 'SELECT * From ' + this.tableName + ' WHERE sorder <= ? And tid = ? ORDER BY `sorder`, `order`'; const sqlParam = [sorder, tid]; 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) { 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: { 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); this._parseData(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 !== undefined && x.sorder !== null && 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 updateHistory4CheckCancel(stage, newTimes, newOrder, transaction) { const datas = await this.getStageData(stage.id, 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.tp = d.tp; } else { history.push({ stimes: newTimes, sorder: newOrder, tp: d.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.id); 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.tp = d.tp; } else { history.push({ stimes: times, sorder: order, 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; };