'use strict'; /** * * * @author Mai * @date 2021/7/20 * @version */ module.exports = app => { class SumLoadHistory extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'sum_load_history'; } async getHistroy(tid, lid, type) { const his = await this.db.select(this.tableName, { where: { tid, lid, type }, orders: [['load_time', 'desc']], limit: 1, offset: 0, }); const result = his.length > 0 ? his[0] : null; if (result && result.tenders) result.tenders = JSON.parse(result.tenders); if (result && result.errors) result.errors = JSON.parse(result.errors); return result; } async getLedgerHistory(tid, lid) { return await this.getHistroy(tid, lid, 'ledger'); } async getReviseHistory(tid, lid) { return await this.getHistroy(tid, lid, 'revise'); } async getStageHistory(tid, lid) { return await this.getHistroy(tid, lid, 'stage'); } async saveLedgerHistory(tid, lid, tenders, errors) { const data = { tid, lid, type: 'ledger', load_time: new Date(), uid: this.ctx.session.sessionUser.accountId, tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '', }; await this.db.insert(this.tableName, data); } async saveReviseHistory(tid, rid, lid, tenders, errors) { const data = { tid, lid, type: 'revise', rid, load_time: new Date(), uid: this.ctx.session.sessionUser.accountId, tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '', }; await this.db.insert(this.tableName, data); } async saveStageHistory(tid, sid, lid, tenders, errors) { const data = { tid, lid, type: 'revise', sid, load_time: new Date(), uid: this.ctx.session.sessionUser.accountId, tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '', }; await this.db.insert(this.tableName, data); } async getReviseLastestData(rid) { const sql = 'SELECT * FROM ' + this.tableName + ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE rid = ? order by load_time desc)'; return await this.db.query(sql, [rid]) } async getStageLastestData(sid) { const sql = 'SELECT * FROM ' + this.tableName + ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE sid = ? order by load_time desc)'; return await this.db.query(sql, [sid]) } } return SumLoadHistory; };