stage_audit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. 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. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  194. throw '提交数据错误';
  195. }
  196. const transaction = await this.db.beginTransaction();
  197. try {
  198. // 整理当前流程审核人状态更新
  199. const time = new Date();
  200. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  201. if (!audit) {
  202. throw '审核数据错误';
  203. }
  204. // 更新当前审核流程
  205. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  206. if (checkData.checkType === auditConst.status.checked) { // 审批通过
  207. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  208. // 无下一审核人表示,审核结束
  209. if (nextAudit) {
  210. // 计算该审批人最终数据
  211. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  212. // 复制一份下一审核人数据
  213. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  214. // 流程至下一审批人
  215. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  216. // 同步 期信息
  217. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: auditConst.status.checking});
  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: checkData.checkType});
  224. }
  225. } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报
  226. // 同步 期信息
  227. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkData.checkType});
  228. // 拷贝新一次审核流程列表
  229. const auditors = await this.getAllDataByCondition({
  230. where: {sid: stageId, times: times},
  231. columns: ['tid', 'sid', 'aid', 'order']
  232. });
  233. for (const a of auditors) {
  234. a.times = times + 1;
  235. a.status = auditConst.status.uncheck;
  236. }
  237. await transaction.insert(this.tableName, auditors);
  238. // 计算该审批人最终数据
  239. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  240. // 复制一份最新数据给原报
  241. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  242. } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  243. // 同步 期信息
  244. await transaction.update(this.ctx.service.stage.tableName, {id: stageId, status: checkData.checkType});
  245. // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  246. if (audit.order > 1) {
  247. // 顺移气候审核人流程顺序
  248. this.initSqlBuilder();
  249. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  250. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  251. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  252. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  253. const data = await transaction.query(sql, sqlParam);
  254. // 上一审批人,当前审批人 再次添加至流程
  255. const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  256. const newAuditors = [];
  257. newAuditors.push({
  258. tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  259. times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  260. begin_time: time,
  261. });
  262. newAuditors.push({
  263. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  264. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  265. });
  266. await transaction.insert(this.tableName, newAuditors);
  267. // 计算该审批人最终数据
  268. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  269. // 复制一份最新数据给原报
  270. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  271. } else {
  272. throw '审核数据错误';
  273. }
  274. } else {
  275. throw '无效审批操作';
  276. }
  277. await transaction.commit();
  278. } catch (err) {
  279. await transaction.rollback();
  280. throw err;
  281. }
  282. }
  283. /**
  284. * 获取审核人需要审核的期列表
  285. *
  286. * @param auditorId
  287. * @returns {Promise<*>}
  288. */
  289. async getAuditStage(auditorId) {
  290. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  291. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  292. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  293. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  294. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = s.`times`))' +
  295. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  296. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  297. return await this.db.query(sql, sqlParam);
  298. }
  299. }
  300. return StageAudit;
  301. };