stage_detail.js 5.8 KB

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