stage_detail.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. /**
  3. * 期计量 中间计量 详细数据
  4. *
  5. * @author Mai
  6. * @date 2019/2/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class StageDetail extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_detail';
  20. }
  21. /**
  22. * 查询最后审核人数据
  23. * @param {Number} tid - 标段id
  24. * @param {Number} sid - 期id
  25. * @param {Number|Array} lid - 台账节点id(可以为空)
  26. * @returns {Promise<*>}
  27. */
  28. async getLastestStageData(tid, sid, lid) {
  29. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  30. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  31. ' INNER JOIN ( ' +
  32. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `uuid` From ' + this.tableName +
  33. ' GROUP BY `lid`, `uuid`' +
  34. ' ) As MaxFilter ' +
  35. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  36. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  37. const sqlParam = [tid, sid];
  38. if (!lid) {
  39. return await this.db.query(sql, sqlParam);
  40. } else if (lid instanceof Array) {
  41. sqlParam.push(lid.join(', '));
  42. return await this.db.query(sql, sqlParam);
  43. } else {
  44. sqlParam.push(lid);
  45. return await this.db.queryOne(sql, sqlParam);
  46. }
  47. }
  48. /**
  49. * 获取 中间计量 用户最新输入数据
  50. * @param {Number} tid - 标段id
  51. * @param {Number} sid - 期id
  52. * @param {Number} lid - 关联的台账节点id
  53. * @param {String} uuid - 中间计量单 唯一id
  54. * @returns {Promise<*>}
  55. */
  56. async getLastestImStageData(tid, sid, lid, uuid) {
  57. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  58. ' INNER JOIN ( ' +
  59. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `uuid` From ' + this.tableName +
  60. ' WHERE lid = ? And uuid = ?' +
  61. ' GROUP BY `lid`, `uuid`' +
  62. ' ) As MaxFilter ' +
  63. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  64. ' WHERE Bills.tid = ? And Bills.sid = ?';
  65. const sqlParam = [lid, uuid, tid, sid];
  66. return await this.db.queryOne(sql, sqlParam);
  67. }
  68. /**
  69. *
  70. * @param data
  71. * @returns {Promise<void>}
  72. */
  73. async saveDetailData(data) {
  74. if (data.uuid) {
  75. const org = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, data.lid, data.uuid);
  76. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  77. if (org.times === this.ctx.stage.times && org.order === order) {
  78. const newData = this._.assign(org, data);
  79. await this.db.update(this.tableName, newData);
  80. } else {
  81. data.uuid = org.uuid;
  82. data.tid = this.ctx.tender.id;
  83. data.sid = this.ctx.stage.id;
  84. data.times = this.ctx.stage.times;
  85. data.order = order;
  86. await this.db.insert(this.tableName, data);
  87. }
  88. } else {
  89. data.uuid = this.uuid.v4();
  90. data.tid = this.ctx.tender.id;
  91. data.sid = this.ctx.stage.id;
  92. data.times = this.ctx.stage.times;
  93. data.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  94. await this.db.insert(this.tableName, data);
  95. }
  96. }
  97. }
  98. return StageDetail;
  99. };