stage_audit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. const transaction = await this.db.beginTransaction();
  142. try {
  143. const condition = {sid: stageId, aid: auditorId, times: times};
  144. const auditor = await this.getDataByCondition(condition);
  145. if (!auditor) {
  146. throw '该审核人不存在';
  147. }
  148. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  149. await transaction.delete(this.tableName, condition);
  150. await transaction.commit();
  151. } catch(err) {
  152. await transaction.rollback();
  153. throw err;
  154. }
  155. return true;
  156. }
  157. /**
  158. * 开始审批
  159. *
  160. * @param {Number} stageId - 期id
  161. * @param {Number} times - 第几次审批
  162. * @returns {Promise<boolean>}
  163. */
  164. async start(stageId, times = 1) {
  165. const audit = await this.getDataByCondition({sid: stageId, times: times, order: 1});
  166. if (!audit) {
  167. throw '审核人信息错误';
  168. }
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  172. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: auditConst.status.checking});
  173. // 计算原报最终数据
  174. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  175. // 复制一份下一审核人数据
  176. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  177. // todo 更新标段tender状态 ?
  178. await transaction.commit();
  179. } catch(err) {
  180. await transaction.rollback();
  181. throw err;
  182. }
  183. return true;
  184. }
  185. /**
  186. * 审批
  187. * @param {Number} stageId - 标段id
  188. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  189. * @param {Number} times - 第几次审批
  190. * @returns {Promise<void>}
  191. */
  192. async check(stageId, checkData, times = 1) {
  193. console.log('check');
  194. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  195. throw '提交数据错误';
  196. }
  197. const transaction = await this.db.beginTransaction();
  198. try {
  199. // 整理当前流程审核人状态更新
  200. const time = new Date();
  201. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  202. if (!audit) {
  203. throw '审核数据错误';
  204. }
  205. // 更新当前审核流程
  206. await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
  207. if (checkType === auditConst.status.checked) {
  208. console.log('checked');
  209. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  210. // 无下一审核人表示,审核结束
  211. if (nextAudit) {
  212. // 计算该审批人最终数据
  213. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  214. // 复制一份下一审核人数据
  215. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  216. // 流程至下一审批人
  217. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  218. } else {
  219. // 本期结束
  220. // 计算并合同支付最终数据
  221. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  222. // 同步 期信息
  223. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkType});
  224. }
  225. } else {
  226. console.log('checkNo');
  227. // 同步 期信息
  228. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, times: times+1, status: checkType});
  229. // 拷贝新一次审核流程列表
  230. const auditors = await this.getAllDataByCondition({
  231. where: {sid: stageId, times: times},
  232. columns: ['tid', 'sid', 'aid', 'order']
  233. });
  234. for (const a of auditors) {
  235. a.times = times + 1;
  236. a.status = auditConst.status.uncheck;
  237. }
  238. await transaction.insert(this.tableName, auditors);
  239. // 计算该审批人最终数据
  240. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  241. // 复制一份最新数据给原报
  242. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  243. }
  244. await transaction.commit();
  245. } catch (err) {
  246. await transaction.rollback();
  247. throw err;
  248. }
  249. }
  250. /**
  251. * 获取审核人需要审核的标段列表
  252. *
  253. * @param auditorId
  254. * @returns {Promise<*>}
  255. */
  256. async getAuditStage(auditorId) {
  257. 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` ' +
  258. 'FROM ?? AS la, ?? AS s, ?? As t ' +
  259. 'WHERE la.`aid` = ? and la.`status` = ?' +
  260. ' and la.`sid` = s.`id` and la.`tid` = t.`id`';
  261. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking];
  262. return await this.db.query(sql, sqlParam);
  263. }
  264. }
  265. return StageAudit;
  266. };