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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.BusinessException;
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.component.util.ThreadLocalUtil;
import com.sunxung.factoring.dict.impl.FinancialCustomerStatusDict;
import com.sunxung.factoring.entity.fundmanagement.FinancialCustomerApprovalInfo;
import com.sunxung.factoring.entity.fundmanagement.FinancialCustomerInfo;
import com.sunxung.factoring.entity.fundmanagement.vo.FinancialCustomerVo;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.mapper.fundmanagement.FinancialCustomerApprovalInfoMapper;
import com.sunxung.factoring.service.fundmanagement.FinancialCustomerApprovalInfoService;
import com.sunxung.factoring.service.fundmanagement.FinancialCustomerInfoService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 客户信息-审核service实现类
 * @Author Yafan Wang
 * @Date 2023/2/11 16:23
 */
@Service(value = "financialCustomerApprovalService")
public class FinancialCustomerApprovalInfoServiceImpl extends ServiceImpl<FinancialCustomerApprovalInfoMapper, FinancialCustomerApprovalInfo> implements FinancialCustomerApprovalInfoService {

    @Autowired
    private FinancialCustomerApprovalInfoMapper financialCustomerApprovalInfoMapper;

    @Autowired
    private FinancialCustomerInfoService financialCustomerInfoService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private BusinessAssigneeService assigneeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doEdit(FinancialCustomerApprovalInfo customerApprovalInfo) {
        if (customerApprovalInfo.getApprovalConclusion() != null) {
            customerApprovalInfo.setAssigneeId(ThreadLocalUtil.getUserId());
            financialCustomerApprovalInfoMapper.insert(customerApprovalInfo);
            FinancialCustomerVo customerVo = financialCustomerInfoService.findVoById(customerApprovalInfo.getFinancialCustomerInfoId());
            FinancialCustomerInfo customerInfo = financialCustomerInfoService.get(customerApprovalInfo.getFinancialCustomerInfoId());
            //退回时流程处理人
            String candidate = assigneeService.getCandidateByBusiness(customerApprovalInfo.getFinancialCustomerInfoId(),
                    ConstantUtil.FINANCIAL_CUSTOMER_PROCESS, ConstantUtil.ASSIGNEE_REFINANCIAL_OPERATOR);
            String[] split = candidate.split(",");
            List<Long> users = new ArrayList<>();
            for (String s : split) {
                users.add(Long.valueOf(s));
            }
            //流程处理
            Map<String, Object> msgVariables = new HashMap<>();
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "客户名称：" + customerVo.getCustomerName());
            if (customerApprovalInfo.getApprovalConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_PASS)){
                msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                        "客户名称为“" + customerVo.getCustomerName() + "的基础资料审核已通过！");
            }else if (customerApprovalInfo.getApprovalConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_BACK)){
                msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                        "客户名称为“" + customerVo.getCustomerName() + "的基础资料审核已驳回，进入"
                                + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.FINANCIAL_CUSTOMER_PROCESS) + "—formBusiness”阶段，"
                                + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            }
            Boolean result = flowableService.completeMyTask(customerApprovalInfo.getTaskId(), customerInfo, null,
                    ConstantConversionUtil.processApprovalResult.get(customerApprovalInfo.getApprovalConclusion()), customerApprovalInfo.getId(), msgVariables, ConstantUtil.FINANCIAL_CUSTOMER_PROCESS, users);
            if (result == null || !result) {
                throw new ValidatorException(CodeUtil.FAIL, "基础资料审核时出错啦！");
            }
            //如果审核结论为通过、修改状态为通过
            if (customerApprovalInfo.getApprovalConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_PASS)) {
                Dictionary passDic = dictionaryService.getByCode(FinancialCustomerStatusDict.ChildEnum.FINANCIAL_CUSTOMER_PASS.getCode());
                customerInfo.setCustomerStatus(passDic.getId());
            } else if (customerApprovalInfo.getApprovalConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_VETO)) {
                //如果审核结论为拒绝、修改状态为拒绝
                Dictionary refuseDic = dictionaryService.getByCode(FinancialCustomerStatusDict.ChildEnum.FINANCIAL_CUSTOMER_REFUSE.getCode());
                customerInfo.setCustomerStatus(refuseDic.getId());
            } else if (customerApprovalInfo.getApprovalConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_BACK)) {
                //如果审核结论为退回、修改状态为待编辑
                Dictionary editDic = dictionaryService.getByCode(FinancialCustomerStatusDict.ChildEnum.FINANCIAL_CUSTOMER_TO_BE_EDIT.getCode());
                customerInfo.setCustomerStatus(editDic.getId());
            }
            financialCustomerInfoService.updateById(customerInfo);
        } else {
            throw new BusinessException(CodeUtil.FAIL, "审批结论不能为空！");

        }
    }

    @Override
    public FinancialCustomerApprovalInfo getView(Long id) {
        return financialCustomerApprovalInfoMapper.selectById(id);
    }
}
