stage_detail.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ' WHERE tid = ? AND sid = ?' +
  35. ' GROUP BY `lid`, `uuid`' +
  36. ' ) As MaxFilter ' +
  37. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  38. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  39. const sqlParam = [tid, sid, tid, sid];
  40. if (!lid) {
  41. return await this.db.query(sql, sqlParam);
  42. } else if (lid instanceof Array) {
  43. sqlParam.push(lid.join(', '));
  44. return await this.db.query(sql, sqlParam);
  45. } else {
  46. sqlParam.push(lid);
  47. return await this.db.queryOne(sql, sqlParam);
  48. }
  49. }
  50. /**
  51. * 查询 某期 某轮审批 某人数据
  52. * @param {Number} tid - 标段id
  53. * @param {Number} sid - 期id
  54. * @param {Number} times - 第几轮
  55. * @param {Number} order - 流程
  56. * @param {Number|Array} lid - 台账节点id(可以为空)
  57. * @returns {Promise<*>}
  58. */
  59. async getAuditorStageData(tid, sid, times, order, lid) {
  60. const lidSql = lid ? ' And Bills.lid in (?)' : '';
  61. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  62. ' INNER JOIN ( ' +
  63. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  64. ' WHERE `tid` = ? And `sid` = ? And (`times` < ? OR (`times` = ? And `order` <= ?))' + lidSql +
  65. ' GROUP BY `lid`, `uuid`' +
  66. ' ) As MaxFilter ' +
  67. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  68. ' WHERE Bills.tid = ? And Bills.sid = ?' + lidSql;
  69. const sqlParam = [tid, sid, times, times, order, tid, sid];
  70. if (!lid) {
  71. return await this.db.query(sql, sqlParam);
  72. } else if (lid instanceof Array) {
  73. sqlParam.push(lid.join(', '));
  74. return await this.db.query(sql, sqlParam);
  75. } else {
  76. sqlParam.push(lid);
  77. return await this.db.queryOne(sql, sqlParam);
  78. }
  79. }
  80. /**
  81. * 获取 中间计量 用户最新输入数据
  82. * @param {Number} tid - 标段id
  83. * @param {Number} sid - 期id
  84. * @param {Number} lid - 关联的台账节点id
  85. * @param {String|Array[String]} uuid - 中间计量单 唯一id
  86. * @returns {Promise<*>}
  87. */
  88. async getLastestImStageData(tid, sid, lid, uuid) {
  89. if (uuid instanceof Array) {
  90. if (uuid.length === 0) { return []; }
  91. let uuidSql = '';
  92. for (const u of uuid) {
  93. uuidSql = uuidSql === '' ? this.db.escape(u) : uuidSql + ',' + this.db.escape(u);
  94. }
  95. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  96. ' INNER JOIN ( ' +
  97. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  98. ' WHERE lid = ? And uuid In (' + uuidSql + ')' +
  99. ' GROUP BY `lid`, `uuid`' +
  100. ' ) As MaxFilter ' +
  101. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  102. ' WHERE Bills.tid = ? And Bills.sid = ?';
  103. const sqlParam = [lid, tid, sid];
  104. return await this.db.query(sql, sqlParam);
  105. } else {
  106. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  107. ' INNER JOIN ( ' +
  108. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  109. ' WHERE lid = ? And uuid = ?' +
  110. ' GROUP BY `lid`, `uuid`' +
  111. ' ) As MaxFilter ' +
  112. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  113. ' WHERE Bills.tid = ? And Bills.sid = ?';
  114. const sqlParam = [lid, uuid, tid, sid];
  115. return await this.db.queryOne(sql, sqlParam);
  116. }
  117. }
  118. /**
  119. *
  120. * @param data
  121. * @returns {Promise<void>}
  122. */
  123. async saveDetailData(data) {
  124. if (data.uuid) {
  125. const org = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, data.lid, data.uuid);
  126. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  127. if (org.times === this.ctx.stage.times && org.order === order) {
  128. delete org.flow;
  129. const newData = this._.assign(org, data);
  130. await this.db.update(this.tableName, newData);
  131. return newData;
  132. } else {
  133. const nd = this._.assign(org, data);
  134. delete nd.id;
  135. nd.times = this.ctx.stage.times;
  136. nd.order = order;
  137. await this.db.insert(this.tableName, nd);
  138. return nd;
  139. }
  140. } else {
  141. data.uuid = this.uuid.v4();
  142. data.tid = this.ctx.tender.id;
  143. data.sid = this.ctx.stage.id;
  144. data.times = this.ctx.stage.times;
  145. data.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  146. await this.db.insert(this.tableName, data);
  147. return data;
  148. }
  149. }
  150. async saveDetailDatas(data) {
  151. if (!data instanceof Array) {
  152. throw '数据错误';
  153. }
  154. const transaction = await this.db.beginTransaction();
  155. const result = [];
  156. try {
  157. for (const d of data) {
  158. if (d.uuid) {
  159. const od = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid, d.uuid);
  160. delete od.flow;
  161. if (od.times === this.ctx.stage.curTimes && od.order === this.ctx.stage.curOrder) {
  162. d.id = od.id;
  163. await transaction.update(this.tableName, d);
  164. result.push(d);
  165. } else {
  166. const nd = this._.assign(od, d);
  167. delete nd.id;
  168. nd.times = this.ctx.stage.curTimes;
  169. nd.order = this.ctx.stage.curOrder;
  170. await transaction.insert(this.tableName, nd);
  171. result.push(nd);
  172. }
  173. } else {
  174. d.uuid = this.uuid.v4();
  175. d.tid = this.ctx.tender.id;
  176. d.sid = this.ctx.stage.id;
  177. d.times = this.ctx.stage.curTimes;
  178. d.order = this.ctx.stage.curOrder;
  179. await transaction.insert(this.tableName, d);
  180. result.push(d);
  181. }
  182. }
  183. await transaction.commit();
  184. return result;
  185. } catch (err) {
  186. await transaction.rollback();
  187. throw err;
  188. }
  189. }
  190. }
  191. return StageDetail;
  192. };