material_check.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. /**
  3. *
  4. * @author EllisRan
  5. * @date
  6. * @version
  7. */
  8. const status = require('../const/audit').material.status;
  9. const shenpiConst = require('../const/shenpi');
  10. const materialConst = require('../const/material');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 材料调差校验 中间件
  15. * 1. 读取材料调差数据
  16. * 2. 检验用户是否参与材料调差(不校验具体权限)
  17. *
  18. * 写入ctx.material数据
  19. * 其中:
  20. * material.auditors: 审批人列表(退回原报时,加载上一流程)
  21. * material.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  22. * material.readonly: 登录人,是否可操作
  23. * material.curTimes: 当前登录人,操作、查阅数据times
  24. * material.curOrder: 当前登录人,操作、查阅数据order
  25. *
  26. * 该方法为通用方法,如需material其他数据,请在controller中查询
  27. *
  28. * @param {function} next - 中间件继续执行的方法
  29. * @return {void}
  30. */
  31. return function* materialCheck(next) {
  32. try {
  33. if (!this.subProject.page_show.openMaterial) {
  34. throw '该功能已关闭';
  35. }
  36. // 读取标段数据
  37. const materialOrder = parseInt(this.params.order);
  38. if (materialOrder <= 0) {
  39. throw '您访问的期不存在';
  40. }
  41. const material = yield this.service.material.getDataByCondition({
  42. tid: this.tender.id,
  43. order: materialOrder,
  44. });
  45. if (!material) {
  46. throw '材料调差期数据错误';
  47. }
  48. // 读取原报、审核人数据
  49. yield this.service.material.loadMaterialUser(material);
  50. const openMaterialTax = this.subProject.page_show.openMaterialTax;
  51. if ((material.status === status.uncheck || material.status === status.checkNo) && material.material_tax !== openMaterialTax) {
  52. yield this.service.material.updateMaterialTax(material.id, openMaterialTax);
  53. material.material_tax = openMaterialTax;
  54. }
  55. // 读取原报、审核人数据
  56. // material.auditors = yield this.service.materialAudit.getAuditors(material.id, material.times);
  57. // if (material.status > 1) {
  58. // material.auditors2 === 2 ? material.auditors : (yield this.service.materialAudit.getAuditors(material.id, material.times - 1));
  59. // }
  60. // material.curAuditor = yield this.service.materialAudit.getCurAuditor(material.id, material.times);
  61. // 权限相关
  62. // todo 校验权限 (标段参与人、分享)
  63. const accountId = this.session.sessionUser.accountId,
  64. auditorIds = _.map(material.auditors, 'aid'),
  65. shareIds = [];
  66. if (accountId === material.user_id) { // 原报
  67. material.filePermission = true;
  68. } else if (this.tender.isTourist) {
  69. material.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  70. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  71. if (material.status === status.uncheck) {
  72. throw '您无权查看该数据';
  73. }
  74. // material.readOnly = material.status !== status.checking || accountId !== material.curAuditor.aid;
  75. material.filePermission = true;
  76. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  77. if (material.status === status.uncheck) {
  78. throw '您无权查看该数据';
  79. }
  80. material.filePermission = false;
  81. } else if (this.session.sessionUser.is_admin) {
  82. material.filePermission = true;
  83. } else { // 其他不可见
  84. throw '您无权查看该数据';
  85. }
  86. // 获取最新的期
  87. material.highOrder = yield this.service.material.count({
  88. tid: this.tender.id,
  89. });
  90. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  91. material.readOnly = !(((material.status === status.uncheck || material.status === status.checkNo) && accountId === material.user_id)
  92. || (this.subProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditorIds.indexOf(accountId) !== -1));
  93. material.editForAudit = this.subProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditorIds.indexOf(accountId) !== -1;
  94. material.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  95. material.exponent_decimal = material.exponent_decimal ? JSON.parse(material.exponent_decimal) : materialConst.exponent_decimal;
  96. material.needStageExponent = !material.is_new_exponent && material.is_stage_self && !material.readOnly;
  97. material.showStageExponent = material.is_stage_self && material.is_new_exponent;
  98. material.needUpdateExponent = yield this.service.material.checkExponentUpdate(material);
  99. // 指数调差5个状态,第一个:共用期无分项,第二个:共用期有分项,第三个:独立期无分项,第四个:独立期有分项, 第五个:旧数据,非上报状态下是独立期但指数调差是共用期
  100. if (material.readOnly && material.is_stage_self && !material.is_new_exponent) {
  101. material.exponentStatus = 5;
  102. } else if (!material.is_stage_self && !material.exponent_node) {
  103. material.exponentStatus = materialConst.exponent_status.shared_noNode;// 共用期无分项
  104. } else if (!material.is_stage_self && material.exponent_node) {
  105. material.exponentStatus = materialConst.exponent_status.shared_node;// 共用期有分项
  106. } else if (material.is_stage_self && !material.exponent_node) {
  107. material.exponentStatus = materialConst.exponent_status.self_noNode;// 独立期无分项
  108. } else if (material.is_stage_self && material.exponent_node) {
  109. material.exponentStatus = materialConst.exponent_status.self_node;// 独立期有分项
  110. }
  111. // 判断stage流程可否撤回,是哪一种撤回
  112. // yield this.service.material.doCheckMaterialCanCancel(material);
  113. this.material = material;
  114. // 根据状态判断是否需要更新审批人列表
  115. if ((material.status === status.uncheck || material.status === status.checkNo) && this.tender.info.shenpi.material !== shenpiConst.sp_status.sqspr) {
  116. const shenpi_status = this.tender.info.shenpi.material;
  117. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  118. const auditList = yield this.service.materialAudit.getAllDataByCondition({ where: { mid: material.id, times: material.times }, orders: [['order', 'asc']] });
  119. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  120. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });
  121. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  122. let sameAudit = auditList.length === shenpiList.length;
  123. if (sameAudit) {
  124. for (const audit of auditList) {
  125. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  126. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  127. sameAudit = false;
  128. break;
  129. }
  130. }
  131. }
  132. if (!sameAudit) {
  133. yield this.service.materialAudit.updateNewAuditList(material, shenpiList);
  134. yield this.service.material.loadMaterialUser(material);
  135. }
  136. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  137. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });
  138. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  139. const lastAuditors = auditList.filter(x => { x.order === auditList[auditList.length - 1].order; });
  140. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  141. yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);
  142. yield this.service.material.loadMaterialUser(material);
  143. } else if (!shenpiInfo) {
  144. // 不存在终审人的状态下这里恢复为授权审批人
  145. this.tender.info.shenpi.material = shenpiConst.sp_status.sqspr;
  146. }
  147. }
  148. // const auditIdList = _.map(auditList, 'aid');
  149. // if (shenpi_status === shenpiConst.sp_status.gdspl) {
  150. // const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });
  151. // const shenpiIdList = _.map(shenpiList, 'audit_id');
  152. // // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  153. // if (!_.isEqual(auditIdList, shenpiIdList)) {
  154. // yield this.service.materialAudit.updateNewAuditList(material, shenpiIdList);
  155. // }
  156. // } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  157. // const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });
  158. // // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  159. // if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  160. // yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);
  161. // } else if (!shenpiInfo) {
  162. // // 不存在终审人的状态下这里恢复为授权审批人
  163. // this.tender.info.shenpi.material = shenpiConst.sp_status.sqspr;
  164. // }
  165. // }
  166. }
  167. yield next;
  168. } catch (err) {
  169. console.log(err);
  170. // 输出错误到日志
  171. if (err.stack) {
  172. this.logger.error(err);
  173. } else {
  174. this.getLogger('fail').info(JSON.stringify({
  175. error: err,
  176. project: this.session.sessionProject,
  177. user: this.session.sessionUser,
  178. body: this.session.body,
  179. }));
  180. }
  181. // 重定向值标段管理
  182. if (err === '您无权查看该数据') {
  183. this.tender ? this.redirect('/tender/' + this.tender.id + '/measure/material') : this.redirect(`/sp/${ctx.subProject.id}/list`);
  184. }
  185. this.redirect(this.request.headers.referer);
  186. }
  187. };
  188. };