tDeclarationApproval.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const {ObjectId} = require('mongoose').Types;
  5. const {CrudService} = require('naf-framework-mongoose/lib/service');
  6. const {BusinessError, ErrorCode} = require('naf-core').Error;
  7. class TDeclarationApprovalService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 't_declaration_approval');
  10. this.model = this.ctx.model.TDeclarationApproval;
  11. this.cModel = this.ctx.model.Companyuser;
  12. this.pModel = this.ctx.model.TPolicyDeclaration;
  13. this.oModel = this.ctx.model.Otheruser;
  14. }
  15. // 检查方法
  16. async check(data) {
  17. const {policy_declaration_id, uid} = data;
  18. let temp = await this.model.findOne({
  19. policy_declaration_id: policy_declaration_id,
  20. uid: uid,
  21. current_state: {$nin: ['2', '3']}
  22. });
  23. if (temp) {
  24. throw new BusinessError(ErrorCode.DATA_EXISTED, '您已申请过该政策');
  25. }
  26. return 'ok';
  27. }
  28. // 重写创建方法
  29. async create(data) {
  30. const {policy_declaration_id, uid, phone} = data;
  31. assert(/^\d{11}$/i.test(phone), 'phone无效');
  32. let temp = await this.model.findOne({
  33. policy_declaration_id: policy_declaration_id,
  34. uid: uid,
  35. current_state: {$nin: ['2', '3']}
  36. });
  37. if (temp) {
  38. throw new BusinessError(ErrorCode.DATA_EXISTED, '您已申请过该政策');
  39. }
  40. let companyUser = await this.cModel.findById({_id: ObjectId(uid)});
  41. let policyDeclaration = await this.pModel.findById({_id: ObjectId(policy_declaration_id)});
  42. data.company_name = companyUser.company_name;
  43. data.policy_declaration_title = policyDeclaration.title;
  44. let approvalInfo = [];
  45. let time = new Date().getTime();
  46. approvalInfo.push({
  47. state: '0',
  48. state_description: '审核中',
  49. result_description: '',
  50. state_time: time,
  51. approval_id: uid,
  52. approval: companyUser.company_name
  53. });
  54. data.approval_info = approvalInfo;
  55. /*data.current_state_time = time;*/
  56. const res = await this.model.create(data);
  57. let msg = await this.ctx.service.viewnews.insertViewNews('政策申报',`您申请的政策 ${policyDeclaration.title} 已提交,请等待审核。`,uid);
  58. return res;
  59. }
  60. // 修改状态方法
  61. async state(data) {
  62. let approval = await this.oModel.findById({_id: ObjectId(data.current_approval_id)});
  63. let declarationApproval = await this.model.findById({_id: ObjectId(data.id)});
  64. let time = new Date().getTime();
  65. declarationApproval.current_state = data.state;
  66. declarationApproval.current_state_description = data.state_description;
  67. declarationApproval.current_result_description = data.result_description;
  68. declarationApproval.current_state_time = time;
  69. declarationApproval.current_approval_id = data.current_approval_id;
  70. declarationApproval.current_approval = approval.name;
  71. declarationApproval.approval_info.push({
  72. state: data.state,
  73. state_description: data.state_description,
  74. result_description: data.result_description,
  75. state_time: time,
  76. approval_id: data.current_approval_id,
  77. approval: approval.name
  78. });
  79. let tempMsg = '';
  80. switch(data.state)
  81. {
  82. case '1':
  83. tempMsg = '审核通过';
  84. break;
  85. case '2':
  86. tempMsg = '审核拒绝';
  87. break;
  88. case '3':
  89. tempMsg = '已停用';
  90. break;
  91. case '9':
  92. tempMsg = '已完成';
  93. break;
  94. }
  95. let msg = await this.ctx.service.viewnews.insertViewNews('政策申报',`您申请的政策 ${declarationApproval.policy_declaration_title} ${tempMsg}。`,declarationApproval.uid);
  96. return await declarationApproval.save();
  97. }
  98. }
  99. module.exports = TDeclarationApprovalService;