'use strict'; const assert = require('assert'); const _ = require('lodash'); const {ObjectId} = require('mongoose').Types; const {CrudService} = require('naf-framework-mongoose/lib/service'); const {BusinessError, ErrorCode} = require('naf-core').Error; class TDeclarationApprovalService extends CrudService { constructor(ctx) { super(ctx, 't_declaration_approval'); this.model = this.ctx.model.TDeclarationApproval; this.cModel = this.ctx.model.Companyuser; this.pModel = this.ctx.model.TPolicyDeclaration; this.oModel = this.ctx.model.Otheruser; } // 检查方法 async check(data) { const {policy_declaration_id, uid} = data; let temp = await this.model.findOne({ policy_declaration_id: policy_declaration_id, uid: uid, current_state: {$nin: ['2', '3']} }); if (temp) { throw new BusinessError(ErrorCode.DATA_EXISTED, '您已申请过该政策'); } return 'ok'; } // 重写创建方法 async create(data) { const {policy_declaration_id, uid, phone} = data; assert(/^\d{11}$/i.test(phone), 'phone无效'); let temp = await this.model.findOne({ policy_declaration_id: policy_declaration_id, uid: uid, current_state: {$nin: ['2', '3']} }); if (temp) { throw new BusinessError(ErrorCode.DATA_EXISTED, '您已申请过该政策'); } let companyUser = await this.cModel.findById({_id: ObjectId(uid)}); let policyDeclaration = await this.pModel.findById({_id: ObjectId(policy_declaration_id)}); data.company_name = companyUser.company_name; data.policy_declaration_title = policyDeclaration.title; let approvalInfo = []; let time = new Date().getTime(); approvalInfo.push({ state: '0', state_description: '审核中', result_description: '', state_time: time, approval_id: uid, approval: companyUser.company_name }); data.approval_info = approvalInfo; /*data.current_state_time = time;*/ const res = await this.model.create(data); let msg = await this.ctx.service.viewnews.insertViewNews('政策申报',`您申请的政策 ${policyDeclaration.title} 已提交,请等待审核。`,uid); return res; } // 修改状态方法 async state(data) { let approval = await this.oModel.findById({_id: ObjectId(data.current_approval_id)}); let declarationApproval = await this.model.findById({_id: ObjectId(data.id)}); let time = new Date().getTime(); declarationApproval.current_state = data.state; declarationApproval.current_state_description = data.state_description; declarationApproval.current_result_description = data.result_description; declarationApproval.current_state_time = time; declarationApproval.current_approval_id = data.current_approval_id; declarationApproval.current_approval = approval.name; declarationApproval.approval_info.push({ state: data.state, state_description: data.state_description, result_description: data.result_description, state_time: time, approval_id: data.current_approval_id, approval: approval.name }); let tempMsg = ''; switch(data.state) { case '1': tempMsg = '审核通过'; break; case '2': tempMsg = '审核拒绝'; break; case '3': tempMsg = '已停用'; break; case '9': tempMsg = '已完成'; break; } let msg = await this.ctx.service.viewnews.insertViewNews('政策申报',`您申请的政策 ${declarationApproval.policy_declaration_title} ${tempMsg}。`,declarationApproval.uid); return await declarationApproval.save(); } } module.exports = TDeclarationApprovalService;