package com.sunxung.factoring.service.businessmanagement.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.dict.impl.BusinessStageDict;
import com.sunxung.factoring.entity.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.businessmanagement.BusinessTemplateReview;
import com.sunxung.factoring.mapper.businessManagement.BusinessTemplateReviewMapper;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.businessmanagement.IBusinessTemplateReviewService;
import com.sunxung.factoring.service.project.BusinessAssigneeService;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 业务管理模板审核信息 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-02-27
 */
@Service
public class BusinessTemplateReviewServiceImpl extends ServiceImpl<BusinessTemplateReviewMapper, BusinessTemplateReview> implements IBusinessTemplateReviewService {

    @Autowired
    private BusinessBasicInfoService businessBasicInfoService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private BusinessAssigneeService assigneeService;

    @Autowired
    private DictionaryService dictionaryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BusinessTemplateReview businessTemplateReview) {
        //保存审核信息
        businessTemplateReview.setBusinessKey(businessTemplateReview.getBusinessBasicInfoId());
        save(businessTemplateReview);
        BusinessBasicInfo businessBasicInfo = businessBasicInfoService.getById(businessTemplateReview.getBusinessKey());
        Map<String, Object> procVariables = new HashMap<>();
        switch (businessTemplateReview.getConclusion()) {
            case 1:
                procVariables.put("result",ConstantUtil.APPROVAL);
                businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.GUARANTEE_MODE.getCode()).getId());
                businessBasicInfoService.updateById(businessBasicInfo);
                break;
            case 0:
                procVariables.put("result",ConstantUtil.OVERRULE);
                businessBasicInfo.setStage(dictionaryService.getByCode(BusinessStageDict.ChildEnum.CREATE.getCode()).getId());
                businessBasicInfoService.updateById(businessBasicInfo);
                break;
            default:
                throw new ValidatorException(CodeUtil.FAIL,"结论错误！");

        }
        //3.开启流程实例   保证方式确认操作岗  业务经理
        procVariables.put(ConstantUtil.GUARANTEE_METHOD_CONFIRM_ASSIGNEE,
                assigneeService.getIdByBusiness(businessBasicInfo.getId(), ConstantUtil.BUSINESS_MANAGEMENT, ConstantUtil.ASSIGNEE_PROJECT_MANAGER));
        //完成审核流程流转到下一流程或者完成流程
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + businessBasicInfo.getBusinessNumber());
        if (businessTemplateReview.getConclusion().equals(0)) {
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务模板审核已驳回，进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BUSINESS_MANAGEMENT) + "—退回修改”阶段，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        } else if (businessTemplateReview.getConclusion().equals(1)) {
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "业务编号为：" + businessBasicInfo.getBusinessNumber() + "”的业务模板审核已通过,进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.BUSINESS_MANAGEMENT) + "—保证方式确认”阶段，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        }
        flowableService.completeMyTask(businessTemplateReview.getTaskId(), businessBasicInfo, procVariables, null, businessTemplateReview.getId(), msgVariables, ConstantUtil.BUSINESS_MANAGEMENT);
    }

    @Override
    public BusinessTemplateReview getView(Long formBusinessId) {
        BusinessTemplateReview businessTemplateReview = getById(formBusinessId);
        return businessTemplateReview;
    }
}
