stage_detail.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. data.code = org.code;
  133. data.name = org.name;
  134. data.unit = org.unit;
  135. data.unit_price = org.unit_price;
  136. data.uuid = org.uuid;
  137. data.tid = this.ctx.tender.id;
  138. data.sid = this.ctx.stage.id;
  139. data.times = this.ctx.stage.times;
  140. data.order = order;
  141. await this.db.insert(this.tableName, data);
  142. return data;
  143. }
  144. } else {
  145. data.uuid = this.uuid.v4();
  146. data.tid = this.ctx.tender.id;
  147. data.sid = this.ctx.stage.id;
  148. data.times = this.ctx.stage.times;
  149. data.order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
  150. await this.db.insert(this.tableName, data);
  151. return data;
  152. }
  153. }
  154. async saveDetailDatas(data) {
  155. if (!data instanceof Array) {
  156. throw '数据错误';
  157. }
  158. const transaction = await this.db.beginTransaction();
  159. const result = [];
  160. try {
  161. for (const d of data) {
  162. if (d.uuid) {
  163. const od = await this.getLastestImStageData(this.ctx.tender.id, this.ctx.stage.id, d.lid, d.uuid);
  164. delete od.flow;
  165. if (od.times === this.ctx.stage.curTimes && od.order === this.ctx.stage.curOrder) {
  166. d.id = od.id;
  167. await transaction.update(this.tableName, d);
  168. result.push(d);
  169. } else {
  170. const nd = this._.assign(od, d);
  171. delete nd.id;
  172. nd.times = this.ctx.stage.curTimes;
  173. nd.order = this.ctx.stage.curOrder;
  174. await transaction.insert(this.tableName, nd);
  175. result.push(nd);
  176. }
  177. } else {
  178. d.uuid = this.uuid.v4();
  179. d.tid = this.ctx.tender.id;
  180. d.sid = this.ctx.stage.id;
  181. d.times = this.ctx.stage.curTimes;
  182. d.order = this.ctx.stage.curOrder;
  183. await transaction.insert(this.tableName, d);
  184. result.push(d);
  185. }
  186. }
  187. await transaction.commit();
  188. return result;
  189. } catch (err) {
  190. await transaction.rollback();
  191. throw err;
  192. }
  193. }
  194. }
  195. return StageDetail;
  196. };