sum_load_history.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2021/7/20
  7. * @version
  8. */
  9. module.exports = app => {
  10. class SumLoadHistory extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'sum_load_history';
  20. }
  21. async getHistroy(tid, lid, type) {
  22. const his = await this.db.select(this.tableName, {
  23. where: { tid, lid, type },
  24. orders: [['load_time', 'desc']],
  25. limit: 1,
  26. offset: 0,
  27. });
  28. const result = his.length > 0 ? his[0] : null;
  29. if (result && result.tenders) result.tenders = JSON.parse(result.tenders);
  30. if (result && result.errors) result.errors = JSON.parse(result.errors);
  31. return result;
  32. }
  33. async getLedgerHistory(tid, lid) {
  34. return await this.getHistroy(tid, lid, 'ledger');
  35. }
  36. async getReviseHistory(tid, lid) {
  37. return await this.getHistroy(tid, lid, 'revise');
  38. }
  39. async getStageHistory(tid, lid) {
  40. return await this.getHistroy(tid, lid, 'stage');
  41. }
  42. async saveLedgerHistory(tid, lid, tenders, errors) {
  43. const data = {
  44. tid, lid, type: 'ledger',
  45. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  46. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  47. };
  48. await this.db.insert(this.tableName, data);
  49. data.tenders = tenders;
  50. data.errors = errors;
  51. return data;
  52. }
  53. async saveReviseHistory(tid, rid, lid, tenders, errors) {
  54. const data = {
  55. tid, lid, type: 'revise', rid,
  56. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  57. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  58. };
  59. await this.db.insert(this.tableName, data);
  60. data.tenders = tenders;
  61. data.errors = errors;
  62. return data;
  63. }
  64. async saveStageHistory(tid, sid, lid, tenders, errors, cover) {
  65. const data = {
  66. tid, lid, type: 'stage', sid,
  67. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  68. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  69. cover: cover,
  70. };
  71. await this.db.insert(this.tableName, data);
  72. data.tenders = tenders;
  73. data.errors = errors;
  74. return data;
  75. }
  76. async getReviseLastestData(rid) {
  77. const sql = 'SELECT * FROM ' + this.tableName +
  78. ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE rid = ? order by load_time desc)';
  79. const data = await this.db.query(sql, [rid]);
  80. if (data.tenders) data.tenders = JSON.parse(data.tenders);
  81. if (data.errors) data.errors = JSON.parse(data.errors);
  82. return data;
  83. }
  84. async getStageLastestData(sid) {
  85. const sql = 'SELECT * FROM ' + this.tableName +
  86. ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE sid = ? order by load_time desc)';
  87. const data = await this.db.query(sql, [sid]);
  88. if (data.tenders) data.tenders = JSON.parse(data.tenders);
  89. if (data.errors) data.errors = JSON.parse(data.errors);
  90. return data;
  91. }
  92. }
  93. return SumLoadHistory;
  94. };