stage_audit.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').flow;
  10. module.exports = app => {
  11. class StageAudit 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_audit';
  21. }
  22. /**
  23. * 获取 审核人信息
  24. *
  25. * @param {Number} stageId - 期id
  26. * @param {Number} auditorId - 审核人id
  27. * @param {Number} times - 第几次审批
  28. * @returns {Promise<*>}
  29. */
  30. async getAuditor(stageId, auditorId, times = 1) {
  31. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  32. 'FROM ?? AS la, ?? AS pa ' +
  33. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?' +
  34. ' and la.`aid` = pa.`id`';
  35. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  36. return await this.db.queryOne(sql, sqlParam);
  37. }
  38. /**
  39. * 获取 审核列表信息
  40. *
  41. * @param {Number} stageId - 期id
  42. * @param {Number} times - 第几次审批
  43. * @returns {Promise<*>}
  44. */
  45. async getAuditors(stageId, times = 1) {
  46. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  47. 'FROM ?? AS la, ?? AS pa ' +
  48. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id`';
  49. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  50. return await this.db.query(sql, sqlParam);
  51. }
  52. /**
  53. * 获取 当前审核人
  54. *
  55. * @param {Number} stageId - 期id
  56. * @param {Number} times - 第几次审批
  57. * @returns {Promise<*>}
  58. */
  59. async getCurAuditor(stageId, times = 1) {
  60. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  61. 'FROM ?? AS la, ?? AS pa ' +
  62. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  63. ' and la.`aid` = pa.`id`';
  64. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  65. return await this.db.queryOne(sql, sqlParam);
  66. }
  67. /**
  68. * 获取 最新审核顺序
  69. *
  70. * @param {Number} stageId - 期id
  71. * @param {Number} times - 第几次审批
  72. * @returns {Promise<number>}
  73. */
  74. async getNewOrder(stageId, times = 1) {
  75. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  76. const sqlParam = ['order', this.tableName, stageId, times];
  77. const result = await this.db.queryOne(sql, sqlParam);
  78. return result && result.max_order ? result.max_order + 1 : 1;
  79. }
  80. /**
  81. * 新增审核人
  82. *
  83. * @param {Number} stageId - 期id
  84. * @param {Number} auditorId - 审核人id
  85. * @param {Number} times - 第几次审批
  86. * @returns {Promise<number>}
  87. */
  88. async addAuditor(stageId, auditorId, times = 1) {
  89. const newOrder = await this.getNewOrder(stageId, times);
  90. const data = {
  91. tid: this.ctx.tender.id,
  92. sid: stageId,
  93. aid: auditorId,
  94. times: times,
  95. order: newOrder,
  96. status: auditConst.status.uncheck,
  97. };
  98. const result = await this.db.insert(this.tableName, data);
  99. return result.effectRows = 1;
  100. }
  101. /**
  102. * 移除审核人时,同步其后审核人order
  103. * @param transaction - 事务
  104. * @param {Number} stageId - 标段id
  105. * @param {Number} auditorId - 审核人id
  106. * @param {Number} times - 第几次审批
  107. * @returns {Promise<*>}
  108. * @private
  109. */
  110. async _syncOrderByDelete(transaction, stageId, order, times) {
  111. this.initSqlBuilder();
  112. this.sqlBuilder.setAndWhere('sid', {
  113. value: stageId,
  114. operate: '='
  115. });
  116. this.sqlBuilder.setAndWhere('order', {
  117. value: order,
  118. operate: '>=',
  119. });
  120. this.sqlBuilder.setAndWhere('times', {
  121. value: times,
  122. operate: '=',
  123. });
  124. this.sqlBuilder.setUpdateData('order', {
  125. value: 1,
  126. selfOperate: '-',
  127. });
  128. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  129. const data = await transaction.query(sql, sqlParam);
  130. return data;
  131. }
  132. /**
  133. * 移除审核人
  134. *
  135. * @param {Number} stageId - 期id
  136. * @param {Number} auditorId - 审核人id
  137. * @param {Number} times - 第几次审批
  138. * @returns {Promise<boolean>}
  139. */
  140. async deleteAuditor(stageId, auditorId, times = 1) {
  141. console.log('delete-1');
  142. const transaction = await this.db.beginTransaction();
  143. try {
  144. const condition = {sid: stageId, aid: auditorId, times: times};
  145. const auditor = await this.getDataByCondition(condition);
  146. if (!auditor) {
  147. throw '该审核人不存在';
  148. }
  149. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  150. await transaction.delete(this.tableName, condition);
  151. await transaction.commit();
  152. } catch(err) {
  153. await transaction.rollback();
  154. throw err;
  155. }
  156. return true;
  157. }
  158. /**
  159. * 开始审批
  160. *
  161. * @param {Number} stageId - 期id
  162. * @param {Number} times - 第几次审批
  163. * @returns {Promise<boolean>}
  164. */
  165. async start(stageId, times = 1) {
  166. const audit = await this.getDataByCondition({sid: stageId, times: times, order: 1});
  167. if (!audit) {
  168. throw '审核人信息错误';
  169. }
  170. const transaction = await this.db.beginTransaction();
  171. try {
  172. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  173. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: auditConst.status.checking});
  174. // todo 更新标段stage状态
  175. //await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: auditConst.status.checking});
  176. await transaction.commit();
  177. } catch(err) {
  178. await transaction.rollback();
  179. throw err;
  180. }
  181. return true;
  182. }
  183. /**
  184. * 审批
  185. * @param {Number} stageId - 标段id
  186. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  187. * @param {Number} times - 第几次审批
  188. * @returns {Promise<void>}
  189. */
  190. async check(stageId, checkType, opinion, times = 1) {
  191. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  192. throw '提交数据错误';
  193. }
  194. const transaction = await this.db.beginTransaction();
  195. try {
  196. // 整理当前流程审核人状态更新
  197. const time = new Date();
  198. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  199. if (!audit) {
  200. throw '审核数据错误';
  201. }
  202. // 更新当前审核流程
  203. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  204. if (checkType === auditConst.status.checked) {
  205. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  206. // 无下一审核人表示,审核结束
  207. if (nextAudit) {
  208. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  209. } else {
  210. // 同步 期信息
  211. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkType});
  212. }
  213. } else {
  214. // 同步 期信息
  215. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, times: times+1, status: checkType});
  216. // 拷贝新一次审核流程列表
  217. const auditors = await this.getAllDataByCondition({
  218. where: {sid: stageId, times: times},
  219. columns: ['tid', 'sid', 'aid', 'order']
  220. });
  221. for (const a of auditors) {
  222. a.times = times + 1;
  223. a.status = auditConst.status.uncheck;
  224. }
  225. await transaction.insert(this.tableName, auditors);
  226. }
  227. await transaction.commit();
  228. } catch (err) {
  229. await transaction.rollback();
  230. throw err;
  231. }
  232. }
  233. /**
  234. * 获取审核人需要审核的标段列表
  235. *
  236. * @param auditorId
  237. * @returns {Promise<*>}
  238. */
  239. async getAuditStage(auditorId) {
  240. const sql = 'SELECT la.`aid`, la.`times`, la.`order`, la.`begin_time`, la.`tid`, la.`sid`, s.`order`, t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  241. 'FROM ?? AS la, ?? AS s, ?? As t ' +
  242. 'WHERE la.`aid` = ? and la.`status` = ?' +
  243. ' and la.`sid` = s.`id` and la.`tid` = t.`id`';
  244. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking];
  245. return await this.db.query(sql, sqlParam);
  246. }
  247. }
  248. return StageAudit;
  249. };