material_check.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // 读取标段数据
  34. const materialOrder = parseInt(this.params.order);
  35. if (materialOrder <= 0) {
  36. throw '您访问的期不存在';
  37. }
  38. const material = yield this.service.material.getDataByCondition({
  39. tid: this.tender.id,
  40. order: materialOrder,
  41. });
  42. if (!material) {
  43. throw '材料调差期数据错误';
  44. }
  45. const openMaterialTax = this.session.sessionProject.page_show.openMaterialTax;
  46. if ((material.status === status.uncheck || material.status === status.checkNo) && material.material_tax !== openMaterialTax) {
  47. yield this.service.material.updateMaterialTax(material.id, openMaterialTax);
  48. material.material_tax = openMaterialTax;
  49. }
  50. // 读取原报、审核人数据
  51. material.auditors = yield this.service.materialAudit.getAuditors(material.id, material.times);
  52. // if (material.status > 1) {
  53. // material.auditors2 === 2 ? material.auditors : (yield this.service.materialAudit.getAuditors(material.id, material.times - 1));
  54. // }
  55. material.curAuditor = yield this.service.materialAudit.getCurAuditor(material.id, material.times);
  56. // 权限相关
  57. // todo 校验权限 (标段参与人、分享)
  58. const accountId = this.session.sessionUser.accountId,
  59. auditorIds = _.map(material.auditors, 'aid'),
  60. shareIds = [];
  61. if (accountId === material.user_id) { // 原报
  62. // if (material.curAuditor) {
  63. // material.readOnly = material.status === status.checking && material.curAuditor.user_id === accountId;
  64. // } else {
  65. // material.readOnly = material.status !== status.uncheck && material.status !== status.checkNo;
  66. // }
  67. material.curTimes = material.times;
  68. if (material.status === status.uncheck || material.status === status.checkNo) {
  69. material.curOrder = 0;
  70. } else if (material.status === status.checked) {
  71. material.curOrder = _.max(_.map(material.auditors, 'order'));
  72. } else {
  73. material.curOrder = material.curAuditor.aid === accountId ? material.curAuditor.order : material.curAuditor.order - 1;
  74. }
  75. material.filePermission = true;
  76. } else if (this.tender.isTourist) {
  77. material.curTimes = material.times;
  78. if (material.status === status.uncheck || material.status === status.checkNo) {
  79. material.curOrder = 0;
  80. } else if (material.status === status.checked) {
  81. material.curOrder = _.max(_.map(material.auditors, 'order'));
  82. } else {
  83. material.curOrder = material.curAuditor.order;
  84. }
  85. material.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  86. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  87. if (material.status === status.uncheck) {
  88. throw '您无权查看该数据';
  89. }
  90. // material.readOnly = material.status !== status.checking || accountId !== material.curAuditor.aid;
  91. material.curTimes = material.status === status.checkNo ? material.times - 1 : material.times;
  92. if (material.status === status.checked) {
  93. material.curOrder = _.max(_.map(material.auditors, 'order'));
  94. } else if (material.status === status.checkNo) {
  95. const audit = this.service.materialAudit.getDataByCondition({
  96. mid: material.id, times: material.times, status: status.checkNo,
  97. });
  98. material.curOrder = audit.order;
  99. } else {
  100. material.curOrder = accountId === material.curAuditor.aid ? material.curAuditor.order : material.curAuditor.order - 1;
  101. }
  102. material.filePermission = true;
  103. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  104. if (material.status === status.uncheck) {
  105. throw '您无权查看该数据';
  106. }
  107. // material.readOnly = true;
  108. material.curTimes = material.status === status.checkNo ? material.times - 1 : material.times;
  109. material.curOrder = material.status === status.checked ? _.max(_.map(material.auditors, 'order')) : material.curAuditor.order - 1;
  110. material.filePermission = false;
  111. } else if (this.session.sessionUser.is_admin) {
  112. material.curTimes = material.times;
  113. if (material.status === status.uncheck || material.status === status.checkNo) {
  114. material.curOrder = 0;
  115. } else if (material.status === status.checked) {
  116. material.curOrder = _.max(_.map(material.auditors, 'order'));
  117. } else {
  118. material.curOrder = material.curAuditor.order;
  119. }
  120. material.filePermission = true;
  121. } else { // 其他不可见
  122. throw '您无权查看该数据';
  123. }
  124. // 获取最新的期
  125. material.highOrder = yield this.service.material.count({
  126. tid: this.tender.id,
  127. });
  128. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  129. material.readOnly = !(((material.status === status.uncheck || material.status === status.checkNo) && accountId === material.user_id)
  130. || (this.session.sessionProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditor.aid === accountId));
  131. material.editForAudit = this.session.sessionProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditor.aid === accountId;
  132. material.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  133. this.material = material;
  134. // 根据状态判断是否需要更新审批人列表
  135. if ((material.status === status.uncheck || material.status === status.checkNo) && this.tender.info.shenpi.material !== shenpiConst.sp_status.sqspr) {
  136. const shenpi_status = this.tender.info.shenpi.material;
  137. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  138. const auditList = yield this.service.materialAudit.getAllDataByCondition({ where: { mid: material.id, times: material.times }, orders: [['order', 'asc']] });
  139. const auditIdList = _.map(auditList, 'aid');
  140. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  141. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });
  142. const shenpiIdList = _.map(shenpiList, 'audit_id');
  143. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  144. if (!_.isEqual(auditIdList, shenpiIdList)) {
  145. yield this.service.materialAudit.updateNewAuditList(material, shenpiIdList);
  146. }
  147. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  148. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });
  149. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  150. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  151. yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);
  152. } else if (!shenpiInfo) {
  153. // 不存在终审人的状态下这里恢复为授权审批人
  154. this.tender.info.shenpi.material = shenpiConst.sp_status.sqspr;
  155. }
  156. }
  157. }
  158. yield next;
  159. } catch (err) {
  160. console.log(err);
  161. // 输出错误到日志
  162. if (err.stack) {
  163. this.logger.error(err);
  164. } else {
  165. this.getLogger('fail').info(JSON.stringify({
  166. error: err,
  167. project: this.session.sessionProject,
  168. user: this.session.sessionUser,
  169. body: this.session.body,
  170. }));
  171. }
  172. // 重定向值标段管理
  173. if (err === '您无权查看该数据') {
  174. this.tender ? this.redirect('/tender/' + this.tender.id + '/measure/material') : this.redirect('/list');
  175. }
  176. this.redirect(this.request.headers.referer);
  177. }
  178. };
  179. };