sum_load_history.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 getBatchHistory(tid) {
  43. const sql = `SELECT * FROM ${this.tableName} WHERE id IN (SELECT MAX(id) FROM ${this.tableName} WHERE tid = ? GROUP by lid);`;
  44. const result = await this.db.query(sql, [tid]);
  45. result.forEach(x => {
  46. if (x && x.tenders) x.tenders = JSON.parse(x.tenders);
  47. delete x.errors;
  48. });
  49. return result;
  50. }
  51. async saveLedgerHistory(tid, lid, tenders, errors) {
  52. const data = {
  53. tid, lid, type: 'ledger',
  54. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  55. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  56. };
  57. await this.db.insert(this.tableName, data);
  58. data.tenders = tenders;
  59. data.errors = errors;
  60. return data;
  61. }
  62. async saveReviseHistory(tid, rid, lid, tenders, errors) {
  63. const data = {
  64. tid, lid, type: 'revise', rid,
  65. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  66. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  67. };
  68. await this.db.insert(this.tableName, data);
  69. data.tenders = tenders;
  70. data.errors = errors;
  71. return data;
  72. }
  73. async saveStageHistory(tid, sid, lid, tenders, errors, cover) {
  74. const data = {
  75. tid, lid, type: 'stage', sid,
  76. load_time: new Date(), uid: this.ctx.session.sessionUser.accountId,
  77. tenders: JSON.stringify(tenders), errors: errors ? JSON.stringify(errors) : '',
  78. cover: cover,
  79. };
  80. await this.db.insert(this.tableName, data);
  81. data.tenders = tenders;
  82. data.errors = errors;
  83. return data;
  84. }
  85. async getReviseLastestData(rid) {
  86. const sql = 'SELECT * FROM ' + this.tableName +
  87. ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE rid = ? order by load_time desc)';
  88. const data = await this.db.query(sql, [rid]);
  89. if (data.tenders) data.tenders = JSON.parse(data.tenders);
  90. if (data.errors) data.errors = JSON.parse(data.errors);
  91. return data;
  92. }
  93. async getStageLastestData(sid) {
  94. const sql = 'SELECT * FROM ' + this.tableName +
  95. ' WHERE id in ( SELECT top 1 id FROM ' + this.tableName + ' WHERE sid = ? order by load_time desc)';
  96. const data = await this.db.query(sql, [sid]);
  97. if (data.tenders) data.tenders = JSON.parse(data.tenders);
  98. if (data.errors) data.errors = JSON.parse(data.errors);
  99. return data;
  100. }
  101. }
  102. return SumLoadHistory;
  103. };