package com.dayou.workflow.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.dto.HandlerPermissionDTO;
import com.dayou.dto.WorkNodeCommit;
import com.dayou.dto.WorkNodeDTO;
import com.dayou.entity.Department;
import com.dayou.entity.Personal;
import com.dayou.entity.WorkFlowNodeInstance;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.enums.ProductionEnum;
import com.dayou.enums.workflow.CallbackEnum;
import com.dayou.enums.workflow.HandlerPermissionEnum;
import com.dayou.enums.workflow.WorkflowNodeEnum;
import com.dayou.mapper.DepartmentMapper;
import com.dayou.mapper.PersonalMapper;
import com.dayou.mapper.WorkFlowNodeInstanceMapper;
import com.dayou.service.ICustomerCompanyService;
import com.dayou.service.IUserPostService;
import com.dayou.service.workflow.IWorkFlowNodeInstanceService;
import com.dayou.service.workflow.IWorkNodeService;
import com.dayou.workflow.config.WorkNodeProcessable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static com.dayou.common.Constants.PERSONAL_DEPARTMENT;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2024/3/4
 * created with IntelliJ IDEA.
 */
@Component
public class PersonalNodeHandler extends WorkNodeProcessable {

    @Autowired
    private PersonalMapper personalMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private IUserPostService userPostService;

    @Autowired
    private IWorkNodeService workNodeService;

    @Autowired
    private ICustomerCompanyService customerCompanyService;

    @Autowired
    private WorkFlowNodeInstanceMapper workFlowNodeInstanceMapper;


    @Override
    public boolean forward(WorkNodeCommit workNodeCommit) {
        WorkFlowNodeInstance currentInstanceNode = workFlowNodeInstanceMapper.getById(workNodeCommit.getInstanceNodeId());
        WorkNodeDTO nextWorkNode = beforeForward(workNodeCommit,currentInstanceNode);
        if (nextWorkNode!=null){
            String ifVerify = currentInstanceNode.getVerify();
            if (StrUtil.isNotEmpty(ifVerify)) {
                CallbackEnum callback = CallbackEnum.valueOf(ifVerify);
                if (callback != null) {
                    //根据callback 确定具体的业务数据
                    //获取产品类型
                    List<ProductionEnum> production = workNodeCommit.getProduction();
                    switch (callback) {
                        case CHECK_PERSONAL_PRODUCTION_TYPE:
                            //1. 获取业务上出具的产品类型。
                            //2.根据出具的产品类型 确定需要哪些节点
                            Map<String, List<WorkflowNodeEnum>> personalNecessaryNodes = CallbackEnum.NecessaryNode.getByProduction(MainBusinessEnum.PERSONAL_BUSINESS.name(),
                                    production);
                            nextWorkNode = workNodeService.getNextWorkNodeByBusiness(currentInstanceNode.getFlowId(),MainBusinessEnum.PERSONAL_BUSINESS, currentInstanceNode.getNodeCode(), personalNecessaryNodes);
                            break;
                        case CHECK_FEEDBACK:
                            if (workNodeCommit.getIfFeedback()!=null && !workNodeCommit.getIfFeedback()) {
                                nextWorkNode = workNodeService.getNextWorkNode(nextWorkNode.getFlowId(), nextWorkNode.getId());
                            }
                            break;
                        case CHECK_PERSONAL_CLIENT_TYPE:
                            Long ccId = workNodeCommit.getCcId();
                            if (ccId !=null){
                                if (customerCompanyService.avoidWareHouseApply(ccId)){
                                    for (int i=0; i<2; i++){
                                        nextWorkNode = workNodeService.getNextWorkNode(nextWorkNode.getFlowId(), nextWorkNode.getId());
                                    }

                                }
                            }
                    }
                }
            }
            addNextInstanceNode(currentInstanceNode,nextWorkNode, workNodeCommit.getNextHandlerId(),workNodeCommit.getBusinessSubId(),workNodeCommit.getBusinessMinId());
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Long searchHandlerId(WorkFlowNodeInstance instanceNode, HandlerPermissionDTO handlerPermissionDTO){
        HandlerPermissionEnum handlerPermissionEnum = handlerPermissionDTO.getName();
        Personal personal = personalMapper.selectOne(new LambdaQueryWrapper<Personal>().select(Personal::getClientManagerId, Personal::getInwardStaff, Personal::getOutwardStaff, Personal::getPricingStaff)
                .eq(BaseEntity::getId, instanceNode.getBusinessId()));
        switch (handlerPermissionEnum){
            case EMPLOYEE:
                return handlerPermissionDTO.getPowerId();
            case POST:
                Long handlerId = userPostService.getUniqueHandlerByPostForWorkNode(handlerPermissionDTO.getPowerId());
                return handlerId==null?
                        departmentMapper.selectOne(new LambdaQueryWrapper<Department>().select(Department::getLeaderId).eq(Department::getName,PERSONAL_DEPARTMENT)).getLeaderId()
                        :handlerId;
            case DEPARTMENT_LEADER:
                return departmentMapper.selectOne(new LambdaQueryWrapper<Department>().select(Department::getLeaderId).eq(Department::getName,PERSONAL_DEPARTMENT)).getLeaderId();
            case CLIENT_MANAGER:
                return personal.getClientManagerId();
            case OUTWARD_STAFF:
                return personal.getOutwardStaff();
            case INWARD_STAFF:
                return personal.getInwardStaff();
            case PRICING_STAFF:
                return personal.getPricingStaff();
            case SYSTEM:
                //暂无程序处理逻辑
                return 0L;
            default:
                //实在找不到审批人 交由部门领导处理
                return departmentMapper.selectOne(new LambdaQueryWrapper<Department>().select(Department::getLeaderId).eq(Department::getName,PERSONAL_DEPARTMENT)).getLeaderId();
        }
    }
}
