stage_audit.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  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. console.log(this.db.format(sql, sqlParam));
  66. return await this.db.queryOne(sql, sqlParam);
  67. }
  68. /**
  69. * 获取 最新审核顺序
  70. *
  71. * @param {Number} stageId - 期id
  72. * @param {Number} times - 第几次审批
  73. * @returns {Promise<number>}
  74. */
  75. async getNewOrder(stageId, times = 1) {
  76. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  77. const sqlParam = ['order', this.tableName, stageId, times];
  78. const result = await this.db.queryOne(sql, sqlParam);
  79. return result && result.max_order ? result.max_order + 1 : 1;
  80. }
  81. /**
  82. * 新增审核人
  83. *
  84. * @param {Number} stageId - 期id
  85. * @param {Number} auditorId - 审核人id
  86. * @param {Number} times - 第几次审批
  87. * @returns {Promise<number>}
  88. */
  89. async addAuditor(stageId, auditorId, times = 1) {
  90. const newOrder = await this.getNewOrder(stageId, times);
  91. const data = {
  92. tid: this.ctx.tender.id,
  93. sid: stageId,
  94. aid: auditorId,
  95. times: times,
  96. order: newOrder,
  97. status: auditConst.status.uncheck,
  98. };
  99. const result = await this.db.insert(this.tableName, data);
  100. return result.effectRows = 1;
  101. }
  102. /**
  103. * 移除审核人时,同步其后审核人order
  104. * @param transaction - 事务
  105. * @param {Number} stageId - 标段id
  106. * @param {Number} auditorId - 审核人id
  107. * @param {Number} times - 第几次审批
  108. * @returns {Promise<*>}
  109. * @private
  110. */
  111. async _syncOrderByDelete(transaction, stageId, order, times) {
  112. this.initSqlBuilder();
  113. this.sqlBuilder.setAndWhere('sid', {
  114. value: stageId,
  115. operate: '='
  116. });
  117. this.sqlBuilder.setAndWhere('order', {
  118. value: order,
  119. operate: '>=',
  120. });
  121. this.sqlBuilder.setAndWhere('times', {
  122. value: times,
  123. operate: '=',
  124. });
  125. this.sqlBuilder.setUpdateData('order', {
  126. value: 1,
  127. selfOperate: '-',
  128. });
  129. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  130. const data = await transaction.query(sql, sqlParam);
  131. return data;
  132. }
  133. /**
  134. * 移除审核人
  135. *
  136. * @param {Number} stageId - 期id
  137. * @param {Number} auditorId - 审核人id
  138. * @param {Number} times - 第几次审批
  139. * @returns {Promise<boolean>}
  140. */
  141. async deleteAuditor(stageId, auditorId, times = 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. // 计算原报最终数据
  175. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  176. // 复制一份下一审核人数据
  177. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  178. // todo 更新标段tender状态 ?
  179. await transaction.commit();
  180. } catch(err) {
  181. await transaction.rollback();
  182. throw err;
  183. }
  184. return true;
  185. }
  186. /**
  187. * 审批
  188. * @param {Number} stageId - 标段id
  189. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  190. * @param {Number} times - 第几次审批
  191. * @returns {Promise<void>}
  192. */
  193. async check(stageId, checkData, times = 1) {
  194. if (checkData.checkType !== auditConst.status.checked && checkData.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: checkData.checkType, opinion: checkData.opinion, end_time: time});
  207. if (checkData.checkType === auditConst.status.checked) { // 审批通过
  208. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  209. // 无下一审核人表示,审核结束
  210. if (nextAudit) {
  211. // 计算该审批人最终数据
  212. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  213. // 复制一份下一审核人数据
  214. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  215. // 流程至下一审批人
  216. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  217. } else {
  218. // 本期结束
  219. // 计算并合同支付最终数据
  220. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  221. // 同步 期信息
  222. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkData.checkType});
  223. }
  224. } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报
  225. // 同步 期信息
  226. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkData.checkType});
  227. // 拷贝新一次审核流程列表
  228. const auditors = await this.getAllDataByCondition({
  229. where: {sid: stageId, times: times},
  230. columns: ['tid', 'sid', 'aid', 'order']
  231. });
  232. for (const a of auditors) {
  233. a.times = times + 1;
  234. a.status = auditConst.status.uncheck;
  235. }
  236. await transaction.insert(this.tableName, auditors);
  237. // 计算该审批人最终数据
  238. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  239. // 复制一份最新数据给原报
  240. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  241. } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  242. // 同步 期信息
  243. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkData.checkType});
  244. // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  245. if (audit.order > 1) {
  246. // 顺移气候审核人流程顺序
  247. this.initSqlBuilder();
  248. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  249. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  250. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  251. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  252. const data = await transaction.query(sql, sqlParam);
  253. // 上一审批人,当前审批人 再次添加至流程
  254. const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  255. const newAuditors = [];
  256. newAuditors.push({
  257. tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  258. times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  259. begin_time: time,
  260. });
  261. newAuditors.push({
  262. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  263. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  264. });
  265. await transaction.insert(this.tableName, newAuditors);
  266. // 计算该审批人最终数据
  267. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  268. // 复制一份最新数据给原报
  269. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  270. } else {
  271. throw '审核数据错误';
  272. }
  273. }
  274. await transaction.commit();
  275. } catch (err) {
  276. await transaction.rollback();
  277. throw err;
  278. }
  279. }
  280. /**
  281. * 获取审核人需要审核的期列表
  282. *
  283. * @param auditorId
  284. * @returns {Promise<*>}
  285. */
  286. async getAuditStage(auditorId) {
  287. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`tid`, sa.`sid`,' +
  288. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  289. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  290. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  291. ' WHERE sa.`aid` = ? and sa.`status` = ?' +
  292. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  293. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking];
  294. return await this.db.query(sql, sqlParam);
  295. }
  296. }
  297. return StageAudit;
  298. };