stage_detail.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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|Array[String]} uuid - 中间计量单 唯一id
  85. * @returns {Promise<*>}
  86. */
  87. async getLastestImStageData(tid, sid, lid, uuid) {
  88. if (uuid instanceof Array) {
  89. if (uuid.length === 0) { return []; }
  90. let uuidSql = '';
  91. for (const u of uuid) {
  92. uuidSql = uuidSql === '' ? this.db.escape(u) : uuidSql + ',' + this.db.escape(u);
  93. }
  94. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  95. ' INNER JOIN ( ' +
  96. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  97. ' WHERE lid = ? And uuid In (' + uuidSql + ')' +
  98. ' GROUP BY `lid`, `uuid`' +
  99. ' ) As MaxFilter ' +
  100. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  101. ' WHERE Bills.tid = ? And Bills.sid = ?';
  102. const sqlParam = [lid, tid, sid];
  103. return await this.db.query(sql, sqlParam);
  104. } else {
  105. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  106. ' INNER JOIN ( ' +
  107. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  108. ' WHERE lid = ? And uuid = ?' +
  109. ' GROUP BY `lid`, `uuid`' +
  110. ' ) As MaxFilter ' +
  111. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  112. ' WHERE Bills.tid = ? And Bills.sid = ?';
  113. const sqlParam = [lid, uuid, tid, sid];
  114. return await this.db.queryOne(sql, sqlParam);
  115. }
  116. }
  117. /**
  118. *
  119. * @param data
  120. * @returns {Promise<void>}
  121. */
  122. async saveDetailData(data) {
  123. if (data.uuid) {
  124. const org = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, data.lid, data.uuid);
  125. const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  126. if (org.times === this.ctx.stage.times && org.order === order) {
  127. delete org.flow;
  128. const newData = this._.assign(org, data);
  129. await this.db.update(this.tableName, newData);
  130. return newData;
  131. } else {
  132. const nd = this._.assign(org, data);
  133. delete nd.id;
  134. nd.times = this.ctx.stage.times;
  135. nd.order = order;
  136. await this.db.insert(this.tableName, nd);
  137. return nd;
  138. }
  139. } else {
  140. data.uuid = this.uuid.v4();
  141. data.tid = this.ctx.tender.id;
  142. data.sid = this.ctx.stage.id;
  143. data.times = this.ctx.stage.times;
  144. data.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  145. await this.db.insert(this.tableName, data);
  146. return data;
  147. }
  148. }
  149. async saveDetailDatas(data) {
  150. if (!data instanceof Array) {
  151. throw '数据错误';
  152. }
  153. const transaction = await this.db.beginTransaction();
  154. const result = [];
  155. try {
  156. for (const d of data) {
  157. if (d.uuid) {
  158. const od = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid, d.uuid);
  159. delete od.flow;
  160. if (od.times === this.ctx.stage.curTimes && od.order === this.ctx.stage.curOrder) {
  161. d.id = od.id;
  162. await transaction.update(this.tableName, d);
  163. result.push(d);
  164. } else {
  165. const nd = this._.assign(od, d);
  166. delete nd.id;
  167. nd.times = this.ctx.stage.curTimes;
  168. nd.order = this.ctx.stage.curOrder;
  169. await transaction.insert(this.tableName, nd);
  170. result.push(nd);
  171. }
  172. } else {
  173. d.uuid = this.uuid.v4();
  174. d.tid = this.ctx.tender.id;
  175. d.sid = this.ctx.stage.id;
  176. d.times = this.ctx.stage.curTimes;
  177. d.order = this.ctx.stage.curOrder;
  178. await transaction.insert(this.tableName, d);
  179. result.push(d);
  180. }
  181. }
  182. await transaction.commit();
  183. return result;
  184. } catch (err) {
  185. await transaction.rollback();
  186. throw err;
  187. }
  188. }
  189. }
  190. return StageDetail;
  191. };