stage_check.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. const status = require('../const/audit').stage.status;
  9. const reviseStatus = require('../const/audit').revise.status;
  10. const auditType = require('../const/audit').auditType;
  11. const shenpiConst = require('../const/shenpi');
  12. const _ = require('lodash');
  13. module.exports = options => {
  14. /**
  15. * 期校验 中间件
  16. * 1. 读取期数据
  17. * 2. 检验用户是否参与期(不校验具体权限)
  18. *
  19. * 写入ctx.stage数据
  20. * 其中:
  21. * stage.auditors: 审批人列表(退回原报时,加载上一流程)
  22. * stage.curAuditors: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  23. * stage.readonly: 登录人,是否可操作
  24. * stage.curTimes: 当前登录人,操作、查阅数据times
  25. * stage.curOrder: 当前登录人,操作、查阅数据order
  26. *
  27. * 该方法为通用方法,如需stage其他数据,请在controller中查询
  28. *
  29. * @param {function} next - 中间件继续执行的方法
  30. * @return {void}
  31. */
  32. return function* stageCheck(next) {
  33. try {
  34. // 读取标段数据
  35. const stageOrder = parseInt(this.params.order);
  36. if (stageOrder <= 0) {
  37. throw '您访问的期不存在';
  38. }
  39. const stage = yield this.service.stage.getDataByCondition({
  40. tid: this.tender.id,
  41. order: stageOrder,
  42. });
  43. if (!stage) {
  44. throw '期数据错误';
  45. }
  46. // 读取原报、审核人数据
  47. yield this.service.stage.loadStageUser(stage);
  48. yield this.service.stage.loadPreCheckedStage(stage);
  49. // 历史台账
  50. if (stage.status === status.checked) {
  51. stage.ledgerHis = yield this.service.ledgerHistory.getDataById(stage.his_id);
  52. }
  53. // 获取最新的期
  54. stage.highOrder = yield this.service.stage.count({
  55. tid: this.tender.id,
  56. });
  57. const materials = yield this.service.material.getAllDataByCondition({ columns: ['stage_id', 's_order'], where: { tid: this.tender.id } });
  58. stage.hadMaterial = materials.find(function(item) {
  59. return item.s_order.split(',').indexOf(stage.highOrder.toString()) !== -1;
  60. });
  61. const phasePays = yield this.service.phasePay.getAllDataByCondition({ columns: ['rela_stage'], where: { tid: this.tender.id } });
  62. stage.hadPhasePay = phasePays.find(function(pp) {
  63. pp.rela_stage = pp.rela_stage ? JSON.parse(pp.rela_stage) : [];
  64. return pp.rela_stage.find(x => { return x.stage_id === stage.id; });
  65. });
  66. // 权限相关
  67. // todo 校验权限 (标段参与人、分享、游客)
  68. const accountId = this.session.sessionUser.accountId;
  69. const shareIds = [];
  70. if (stage.status === status.uncheck) {
  71. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  72. if (!stage.readOnly) {
  73. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  74. }
  75. stage.curTimes = stage.times;
  76. stage.curOrder = 0;
  77. } else if (stage.status === status.checkNo) {
  78. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  79. const checkNoAudit = yield this.service.stageAudit.getDataByCondition({
  80. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  81. });
  82. if (!stage.readOnly) {
  83. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  84. stage.curTimes = stage.times;
  85. stage.curOrder = 0;
  86. } else {
  87. stage.curTimes = stage.times - 1;
  88. stage.curOrder = checkNoAudit.order;
  89. }
  90. } else if (stage.status === status.checked) {
  91. stage.readOnly = true;
  92. stage.curTimes = stage.times;
  93. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  94. } else if (stage.status === status.checking && stage.curAuditors[0].audit_type === auditType.key.multi) {
  95. stage.curOrder = stage.curAuditors[0].order;
  96. stage.curTimes = stage.times;
  97. const ass = stage.auditAssists.find(x => { return stage.curAuditorIds.indexOf(x.user_id) >= 0 && x.ass_user_id === accountId; });
  98. stage.readOnly = stage.curAuditorIds.indexOf(accountId) < 0 && !ass;
  99. } else {
  100. const ass = stage.auditAssists.find(x => { return stage.flowAuditorIds.indexOf(x.user_id) >= 0 && x.ass_user_id === accountId; });
  101. stage.readOnly = stage.flowAuditorIds.indexOf(accountId) < 0 && !ass;
  102. stage.curTimes = stage.times;
  103. if (!stage.readOnly) {
  104. stage.assist = ass;
  105. stage.curOrder = stage.curAuditors[0].order;
  106. } else {
  107. stage.curOrder = stage.curAuditors[0].order - 1;
  108. }
  109. if (!stage.readOnly) {
  110. if (stage.curAuditors[0].audit_type === auditType.key.and) {
  111. stage.readOnly = !_.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
  112. stage.canCheck = true;
  113. } else if (stage.curAuditors[0].audit_type === auditType.key.union) {
  114. stage.readOnly = stage.relaAuditor.status === status.checked;
  115. stage.canCheck = !stage.readOnly;
  116. }
  117. }
  118. }
  119. if (stage.readOnly) {
  120. stage.assist = accountId === stage.user_id || stage.auditorIds.indexOf(accountId) >= 0 ? null : stage.auditAssists.find(x => { return x.ass_user_id === accountId});
  121. }
  122. const permission = this.session.sessionUser.permission;
  123. if (stage.userIds.indexOf(accountId) >= 0 || this.session.sessionUser.is_admin) {
  124. stage.filePermission = true;
  125. } else {
  126. if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {// 分享人
  127. if (stage.status === status.uncheck) {
  128. throw '您无权查看该数据';
  129. }
  130. stage.filePermission = false;
  131. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  132. stage.filePermission = this.tender.touristPermission.file || stage.auditorIds.indexOf(accountId) !== -1;
  133. } else {
  134. throw '您无权查看该数据';
  135. }
  136. }
  137. // 判断stage流程可否撤回,是哪一种撤回
  138. // yield this.service.stage.doCheckStageCanCancel(stage);
  139. // 是否台账修订中
  140. const lastRevise = yield this.service.ledgerRevise.getLastestRevise(this.tender.id);
  141. stage.revising = (lastRevise && lastRevise.status !== reviseStatus.checked) || false;
  142. // 根据状态判断是否需要更新审批人列表
  143. yield this.service.stage.doCheckStageCanCancel(stage);
  144. stage.readySettle = yield this.service.settle.getReadySettle(stage.tid);
  145. this.stage = stage;
  146. // 根据状态判断是否需要更新审批人列表
  147. if ((stage.status === status.uncheck || stage.status === status.checkNo) && this.tender.info.shenpi.stage !== shenpiConst.sp_status.sqspr) {
  148. const shenpi_status = this.tender.info.shenpi.stage;
  149. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  150. const auditList = yield this.service.stageAudit.getAllDataByCondition({ where: { sid: stage.id, times: stage.times }, orders: [['order', 'asc']] });
  151. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  152. // 判断并获取审批组
  153. const group = yield this.service.shenpiGroup.getSelectGroupByStageType(this.tender.id, shenpiConst.sp_type.stage, stage.sp_group);
  154. if ((group && stage.sp_group !== group.id) || (!group && stage.sp_group !== 0)) {
  155. stage.sp_group = group ? group.id : 0;
  156. yield this.service.stage.defaultUpdate({ sp_group: stage.sp_group }, { where: { id: stage.id } });
  157. }
  158. const condition = { tid: this.tender.id, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status, sp_group: stage.sp_group };
  159. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({
  160. where: condition,
  161. orders: [['audit_order', 'asc'], ['audit_group', 'ASC'], ['audit_group_order', 'ASC']]
  162. });
  163. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  164. let sameAudit = auditList.length === shenpiList.length;
  165. if (sameAudit) {
  166. for (const audit of auditList) {
  167. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  168. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type || shenpi.audit_ledger_id !== audit.audit_ledger_id
  169. || shenpi.audit_group !== audit.audit_group || shenpi.audit_group_order !== audit.audit_group_order || shenpi.audit_group_limit !== audit.audit_group_limit
  170. || shenpi.audit_group_need !== audit.audit_group_need || shenpi.audit_checkno_valid !== audit.audit_checkno_valid
  171. ) {
  172. sameAudit = false;
  173. break;
  174. }
  175. }
  176. }
  177. if (!sameAudit) {
  178. yield this.service.stageAudit.updateNewAuditList(stage, shenpiList);
  179. yield this.service.stage.loadStageUser(stage);
  180. }
  181. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  182. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status });
  183. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  184. const lastAuditors = auditList.filter(x => { x.order === auditList[auditList.length - 1].order; });
  185. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  186. yield this.service.stageAudit.updateLastAudit(stage, auditList, shenpiInfo.audit_id);
  187. yield this.service.stage.loadStageUser(stage);
  188. } else if (!shenpiInfo) {
  189. // 不存在终审人的状态下这里恢复为授权审批人
  190. this.tender.info.shenpi.stage = shenpiConst.sp_status.sqspr;
  191. }
  192. }
  193. }
  194. yield next;
  195. } catch (err) {
  196. this.helper.log(err);
  197. // 输出错误到日志
  198. if (err.stack) {
  199. this.logger.error(err);
  200. } else {
  201. this.getLogger('fail').info(JSON.stringify({
  202. error: err,
  203. project: this.session.sessionProject,
  204. user: this.session.sessionUser,
  205. body: this.session.body,
  206. }));
  207. }
  208. // 重定向值标段管理
  209. this.redirect(this.request.headers.referer);
  210. }
  211. };
  212. };