inspection_check.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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* InspectionCheck(next) {
  22. try {
  23. // 获取revise
  24. if (!this.subProject.page_show.quality) {
  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.qualityInspection.getDataById(qiid);
  32. if (!inspection) throw '质量巡检数据有误';
  33. // 读取原报、审核人数据
  34. yield this.service.qualityInspection.loadUser(inspection);
  35. // 权限相关
  36. // todo 校验权限 (标段参与人、分享)
  37. const accountId = this.session.sessionUser.accountId,
  38. auditorIds = _.map(inspection.auditors, 'aid'),
  39. shareIds = [];
  40. const permission = this.session.sessionUser.permission;
  41. if (accountId === inspection.uid) { // 原报
  42. inspection.filePermission = true;
  43. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  44. if (inspection.status === status.uncheck) {
  45. throw '您无权查看该数据';
  46. }
  47. inspection.filePermission = true;
  48. } else if (inspection.status === status.checkNo && inspection.uid !== accountId) {
  49. const preAuditors = yield this.service.qualityInspectionAudit.getAuditors(inspection.id, inspection.times - 1);
  50. const preAuditorIds = _.map(preAuditors, 'aid');
  51. if (preAuditorIds.indexOf(accountId) === -1) {
  52. throw '您无权查看该数据';
  53. }
  54. inspection.filePermission = true;
  55. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  56. inspection.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  57. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  58. if (inspection.status === status.uncheck) {
  59. throw '您无权查看该数据';
  60. }
  61. inspection.filePermission = false;
  62. } else { // 其他不可见
  63. throw '您无权查看该数据';
  64. }
  65. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  66. inspection.readOnly = !((inspection.status === status.uncheck || inspection.status === status.checkNo) && accountId === inspection.uid);
  67. inspection.rectificationPower = inspection.status === status.rectification && inspection.curAuditorIds.indexOf(accountId) !== -1;
  68. inspection.shenpiPower = (inspection.status === status.checking || inspection.status === status.checkNoPre) && inspection.curAuditorIds.indexOf(accountId) !== -1;
  69. this.inspection = inspection;
  70. // 根据状态判断是否需要更新审批人列表
  71. if ((inspection.status === status.uncheck || inspection.status === status.checkNo) && this.tender.info.shenpi.inspection !== shenpiConst.sp_status.sqspr) {
  72. const shenpi_status = this.tender.info.shenpi.inspection;
  73. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  74. const auditList = yield this.service.qualityInspectionAudit.getAllDataByCondition({ where: { qiid: inspection.id, times: inspection.times, is_rectification: 0 }, orders: [['order', 'asc']] });
  75. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  76. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: inspection.tid, sp_type: shenpiConst.sp_type.inspection, sp_status: shenpi_status } });
  77. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  78. let sameAudit = auditList.length === shenpiList.length;
  79. if (sameAudit) {
  80. for (const audit of auditList) {
  81. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  82. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  83. sameAudit = false;
  84. break;
  85. }
  86. }
  87. }
  88. if (!sameAudit) {
  89. yield this.service.qualityInspectionAudit.updateNewAuditList(inspection, shenpiList);
  90. yield this.service.qualityInspection.loadUser(inspection);
  91. }
  92. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  93. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: inspection.tid, sp_type: shenpiConst.sp_type.inspection, sp_status: shenpi_status });
  94. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  95. const lastAuditors = auditList.filter(x => { x.order === auditList[auditList.length - 1].order; });
  96. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  97. yield this.service.qualityInspectionAudit.updateLastAudit(inspection, auditList, shenpiInfo.audit_id);
  98. yield this.service.qualityInspection.loadUser(inspection);
  99. } else if (!shenpiInfo) {
  100. // 不存在终审人的状态下这里恢复为授权审批人
  101. this.tender.info.shenpi.inspection = shenpiConst.sp_status.sqspr;
  102. }
  103. }
  104. }
  105. yield next;
  106. } catch (err) {
  107. console.log(err);
  108. // 输出错误到日志
  109. if (err.stack) {
  110. this.logger.error(err);
  111. } else {
  112. this.getLogger('fail').info(JSON.stringify({
  113. error: err,
  114. project: this.session.sessionProject,
  115. user: this.session.sessionUser,
  116. body: this.session.body,
  117. }));
  118. }
  119. // 重定向值标段管理
  120. this.redirect(this.request.headers.referer);
  121. }
  122. };
  123. };