stage_detail.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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} times - 第几轮
  53. * @param {Number} order - 流程
  54. * @param {Number|Array} lid - 台账节点id(可以为空)
  55. * @returns {Promise<*>}
  56. */
  57. async getAuditorStageData(tid, sid, times, order, lid) {
  58. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  59. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  60. ' INNER JOIN ( ' +
  61. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `uuid` From ' + this.tableName +
  62. ' WHERE `tid` = ? And `sid` = ? And (`times` < ? OR (`times` = ? And `order` <= ?))' + lidSql +
  63. ' GROUP BY `lid`, `uuid`' +
  64. ' ) As MaxFilter ' +
  65. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  66. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  67. const sqlParam = [tid, sid, times, times, order, tid, sid];
  68. if (!lid) {
  69. return await this.db.query(sql, sqlParam);
  70. } else if (lid instanceof Array) {
  71. sqlParam.push(lid.join(', '));
  72. return await this.db.query(sql, sqlParam);
  73. } else {
  74. sqlParam.push(lid);
  75. return await this.db.queryOne(sql, sqlParam);
  76. }
  77. }
  78. /**
  79. * 获取 中间计量 用户最新输入数据
  80. * @param {Number} tid - 标段id
  81. * @param {Number} sid - 期id
  82. * @param {Number} lid - 关联的台账节点id
  83. * @param {String} uuid - 中间计量单 唯一id
  84. * @returns {Promise<*>}
  85. */
  86. async getLastestImStageData(tid, sid, lid, uuid) {
  87. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  88. ' INNER JOIN ( ' +
  89. ' SELECT MAX(`times`) As `times`, MAX(`order`) As `order`, `lid`, `uuid` From ' + this.tableName +
  90. ' WHERE lid = ? And uuid = ?' +
  91. ' GROUP BY `lid`, `uuid`' +
  92. ' ) As MaxFilter ' +
  93. ' ON Bills.times = MaxFilter.times And Bills.order = MaxFilter.order And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  94. ' WHERE Bills.tid = ? And Bills.sid = ?';
  95. const sqlParam = [lid, uuid, tid, sid];
  96. return await this.db.queryOne(sql, sqlParam);
  97. }
  98. /**
  99. *
  100. * @param data
  101. * @returns {Promise<void>}
  102. */
  103. async saveDetailData(data) {
  104. if (data.uuid) {
  105. const org = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, data.lid, data.uuid);
  106. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  107. if (org.times === this.ctx.stage.times && org.order === order) {
  108. const newData = this._.assign(org, data);
  109. await this.db.update(this.tableName, newData);
  110. } else {
  111. data.uuid = org.uuid;
  112. data.tid = this.ctx.tender.id;
  113. data.sid = this.ctx.stage.id;
  114. data.times = this.ctx.stage.times;
  115. data.order = order;
  116. await this.db.insert(this.tableName, data);
  117. }
  118. } else {
  119. data.uuid = this.uuid.v4();
  120. data.tid = this.ctx.tender.id;
  121. data.sid = this.ctx.stage.id;
  122. data.times = this.ctx.stage.times;
  123. data.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  124. await this.db.insert(this.tableName, data);
  125. }
  126. }
  127. }
  128. return StageDetail;
  129. };