stage_detail.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. async updateStageDetail4CheckCancel(sid, newTimes, newOrder, oldTimes, oldOrder, transaction) {
  81. const oldSBLists = await this.getAllDataByCondition({ where: { sid, times: oldTimes, order: oldOrder } });
  82. const newSBLists = await this.getAllDataByCondition({ where: { sid, times: newTimes, order: newOrder } });
  83. const delidList = [];
  84. for (const o of oldSBLists) {
  85. const newSBInfo = this._.find(newSBLists, { lid: o.lid });
  86. if (newSBInfo) {
  87. // 删除
  88. delidList.push(newSBInfo.id);
  89. }
  90. }
  91. if (delidList.length > 0) await transaction.delete(this.tableName, { id: delidList });
  92. if (oldSBLists.length > 0) {
  93. await transaction.update(this.tableName, {
  94. times: newTimes,
  95. order: newOrder,
  96. }, { where: { id: this._.map(oldSBLists, 'id') } });
  97. }
  98. }
  99. /**
  100. * 获取 中间计量 用户最新输入数据
  101. * @param {Number} tid - 标段id
  102. * @param {Number} sid - 期id
  103. * @param {Number} lid - 关联的台账节点id
  104. * @param {String|Array[String]} uuid - 中间计量单 唯一id
  105. * @returns {Promise<*>}
  106. */
  107. async getLastestImStageData(tid, sid, lid, uuid) {
  108. if (uuid instanceof Array) {
  109. if (uuid.length === 0) { return []; }
  110. let uuidSql = '';
  111. for (const u of uuid) {
  112. uuidSql = uuidSql === '' ? this.db.escape(u) : uuidSql + ',' + this.db.escape(u);
  113. }
  114. const sql = 'SELECT * FROM ' + this.tableName + ' As Bills ' +
  115. ' INNER JOIN ( ' +
  116. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  117. ' WHERE lid = ? And uuid In (' + uuidSql + ')' +
  118. ' GROUP BY `lid`, `uuid`' +
  119. ' ) As MaxFilter ' +
  120. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  121. ' WHERE Bills.tid = ? And Bills.sid = ?';
  122. const sqlParam = [lid, tid, sid];
  123. return await this.db.query(sql, sqlParam);
  124. } else {
  125. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
  126. ' INNER JOIN ( ' +
  127. ' SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `lid`, `uuid` From ' + this.tableName +
  128. ' WHERE lid = ? And uuid = ?' +
  129. ' GROUP BY `lid`, `uuid`' +
  130. ' ) As MaxFilter ' +
  131. ' ON (Bills.times * ' + timesLen + ' + Bills.order) = MaxFilter.flow And Bills.lid = MaxFilter.lid And Bills.uuid = MaxFilter.uuid' +
  132. ' WHERE Bills.tid = ? And Bills.sid = ?';
  133. const sqlParam = [lid, uuid, tid, sid];
  134. return await this.db.queryOne(sql, sqlParam);
  135. }
  136. }
  137. /**
  138. *
  139. * @param data
  140. * @returns {Promise<void>}
  141. */
  142. async saveDetailData(data) {
  143. if (data.uuid) {
  144. const org = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, data.lid, data.uuid);
  145. const order = this.ctx.stage.curAuditors.length > 0 ? this.ctx.stage.curAuditors[0].order : 0;
  146. if (org.times === this.ctx.stage.times && org.order === order) {
  147. delete org.flow;
  148. const newData = this._.assign(org, data);
  149. await this.db.update(this.tableName, newData);
  150. return newData;
  151. } else {
  152. const nd = this._.assign(org, data);
  153. delete nd.id;
  154. nd.times = this.ctx.stage.times;
  155. nd.order = order;
  156. await this.db.insert(this.tableName, nd);
  157. return nd;
  158. }
  159. } else {
  160. data.uuid = this.uuid.v4();
  161. data.tid = this.ctx.tender.id;
  162. data.sid = this.ctx.stage.id;
  163. data.times = this.ctx.stage.times;
  164. data.order = this.ctx.stage.curAuditors.length > 0 ? this.ctx.stage.curAuditors[0].order : 0;
  165. await this.db.insert(this.tableName, data);
  166. return data;
  167. }
  168. }
  169. async saveDetailDatas(data) {
  170. if (!data instanceof Array) {
  171. throw '数据错误';
  172. }
  173. const transaction = await this.db.beginTransaction();
  174. const result = [];
  175. try {
  176. for (const d of data) {
  177. if (d.uuid) {
  178. const od = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid, d.uuid);
  179. delete od.flow;
  180. if (od.times === this.ctx.stage.curTimes && od.order === this.ctx.stage.curOrder) {
  181. d.id = od.id;
  182. await transaction.update(this.tableName, d);
  183. result.push(d);
  184. } else {
  185. const nd = this._.assign(od, d);
  186. delete nd.id;
  187. nd.times = this.ctx.stage.curTimes;
  188. nd.order = this.ctx.stage.curOrder;
  189. await transaction.insert(this.tableName, nd);
  190. result.push(nd);
  191. }
  192. } else {
  193. d.uuid = this.uuid.v4();
  194. d.tid = this.ctx.tender.id;
  195. d.sid = this.ctx.stage.id;
  196. d.times = this.ctx.stage.curTimes;
  197. d.order = this.ctx.stage.curOrder;
  198. await transaction.insert(this.tableName, d);
  199. result.push(d);
  200. }
  201. }
  202. await transaction.commit();
  203. return result;
  204. } catch (err) {
  205. await transaction.rollback();
  206. throw err;
  207. }
  208. }
  209. }
  210. return StageDetail;
  211. };