safe_inspection_check.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').inspection.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* SafeInspectionCheck(next) {
  22. try {
  23. // 获取revise
  24. if (!this.subProject.page_show.safeInspection) {
  25. throw '该功能已关闭';
  26. }
  27. const qiid = this.params.qiid || this.request.body.qiid;
  28. if (!qiid) {
  29. throw '您访问的质量巡检不存在';
  30. }
  31. const inspection = yield this.service.safeInspection.getDataById(qiid);
  32. if (!inspection) throw '质量巡检数据有误';
  33. yield this.service.safeInspectionAudit.prepareDraftFlow(inspection);
  34. // 读取原报、审核人数据
  35. yield this.service.safeInspection.loadUser(inspection);
  36. // 权限相关
  37. // todo 校验权限 (标段参与人、分享)
  38. const accountId = this.session.sessionUser.accountId,
  39. auditorIds = _.map(inspection.auditors, 'aid'),
  40. shareIds = [];
  41. const permission = this.session.sessionUser.permission;
  42. if (accountId === inspection.uid) { // 原报
  43. inspection.filePermission = true;
  44. } else if (this.permission.safe_inspection.view_all) {
  45. inspection.filePermission = true;
  46. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  47. if (inspection.status === status.uncheck) {
  48. throw '您无权查看该数据';
  49. }
  50. inspection.filePermission = true;
  51. } else if (inspection.status === status.checkNo && inspection.uid !== accountId) {
  52. const preAuditors = yield this.service.safeInspectionAudit.getAuditors(inspection.id, inspection.times - 1);
  53. const preAuditorIds = _.map(preAuditors, 'aid');
  54. if (preAuditorIds.indexOf(accountId) === -1) {
  55. throw '您无权查看该数据';
  56. }
  57. inspection.filePermission = true;
  58. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  59. inspection.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  60. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  61. if (inspection.status === status.uncheck) {
  62. throw '您无权查看该数据';
  63. }
  64. inspection.filePermission = false;
  65. } else { // 其他不可见
  66. throw '您无权查看该数据';
  67. }
  68. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  69. inspection.readOnly = !((inspection.status === status.uncheck || inspection.status === status.checkNo) && accountId === inspection.uid);
  70. inspection.rectificationPower = inspection.status === status.rectification && inspection.curAuditorIds.indexOf(accountId) !== -1;
  71. inspection.shenpiPower = (inspection.status === status.checking || inspection.status === status.checkNoPre) && inspection.curAuditorIds.indexOf(accountId) !== -1;
  72. this.inspection = inspection;
  73. // 根据状态判断是否需要更新审批人列表
  74. if ((inspection.status === status.uncheck || inspection.status === status.checkNo) && this.tender.info.shenpi.safe_inspection !== shenpiConst.sp_status.sqspr) {
  75. const shenpi_status = this.tender.info.shenpi.safe_inspection;
  76. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  77. const where = { qiid: inspection.id, times: inspection.times, is_rectification: 0, is_old: 0 };
  78. if (inspection.flow_version === 1) where.flow_stage = 1;
  79. const auditList = yield this.service.safeInspectionAudit.getAllDataByCondition({ where, orders: [['order', 'asc']] });
  80. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  81. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: inspection.tid, sp_type: shenpiConst.sp_type.safe_inspection, sp_status: shenpi_status } });
  82. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  83. let sameAudit = auditList.length === shenpiList.length;
  84. if (sameAudit) {
  85. for (const audit of auditList) {
  86. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  87. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  88. sameAudit = false;
  89. break;
  90. }
  91. }
  92. }
  93. if (!sameAudit) {
  94. yield this.service.safeInspectionAudit.updateNewAuditList(inspection, shenpiList);
  95. yield this.service.safeInspection.loadUser(inspection);
  96. }
  97. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  98. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: inspection.tid, sp_type: shenpiConst.sp_type.safe_inspection, sp_status: shenpi_status });
  99. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  100. const lastAuditors = auditList.filter(x => { return x.order === auditList[auditList.length - 1].order; });
  101. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  102. yield this.service.safeInspectionAudit.updateLastAudit(inspection, auditList, shenpiInfo.audit_id);
  103. yield this.service.safeInspection.loadUser(inspection);
  104. } else if (!shenpiInfo) {
  105. // 不存在终审人的状态下这里恢复为授权审批人
  106. this.tender.info.shenpi.safe_inspection = shenpiConst.sp_status.sqspr;
  107. }
  108. }
  109. }
  110. if (inspection.flow_version === 1) {
  111. inspection.canUpload = !inspection.readOnly || inspection.rectificationPower || inspection.shenpiPower || (inspection.status === status.checked && inspection.uid === accountId);
  112. }
  113. yield next;
  114. } catch (err) {
  115. console.log(err);
  116. // 输出错误到日志
  117. if (err.stack) {
  118. this.logger.error(err);
  119. } else {
  120. this.getLogger('fail').info(JSON.stringify({
  121. error: err,
  122. project: this.session.sessionProject,
  123. user: this.session.sessionUser,
  124. body: this.session.body,
  125. }));
  126. }
  127. // 重定向值标段管理
  128. this.redirect(this.request.headers.referer);
  129. }
  130. };
  131. };