package com.ningdatech.pmapi.projectdeclared.manage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.pmapi.common.helper.UserInfoHelper;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import com.ningdatech.pmapi.projectlib.model.dto.ProjectDTO;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO;
import com.wflow.bean.entity.WflowModels;
import com.wflow.bean.entity.WflowOrgModels;
import com.wflow.enums.OrgTypeEnum;
import com.wflow.enums.ProcessDefTypeEnum;
import com.wflow.service.OrgProcdefService;
import com.wflow.workflow.bean.dto.OrgInfoDTO;
import com.wflow.workflow.bean.dto.ProcessInstanceUserDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname DefaultDeclaredProjectManage
 *
 * @Description
 * @Date 2023/2/14 17:12
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class DefaultDeclaredProjectManage {

    private final IProjectService projectService;

    private final UserInfoHelper userInfoHelper;

    private final IDingOrganizationService dingOrganizationService;
    private final OrgProcdefService orgProcdefService;

    //项目名称去重
    public void checkDuplication(ProjectDTO project){
        VUtils.isTrue(projectService.count(Wrappers.lambdaQuery(Project.class)
                        .eq(Project::getProjectName,project.getProjectName())
                        .ne(Objects.nonNull(project.getId()),Project::getId,project.getId())) > 0)
                .throwMessage(String.format("修改失败 此项目名 【%s】 已存在!",project.getProjectName()));
    }

    public ProcessInstanceUserDto buildUser(Long userId){
        UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId);

        VUtils.isTrue(Objects.isNull(userFullInfo))
                .throwMessage("当前登录用户 信息出错!");

        return ProcessInstanceUserDto.builder()
                .userId(String.valueOf(userFullInfo.getUserId()))
                .userName(userFullInfo.getRealName())
                .orgCode(userFullInfo.getOrganizationCode())
                .orgName(userFullInfo.getOrganizationName())
                .build();
    }

    public Map<String, OrgInfoDTO> getOrgModelInfo(Long userId, WflowModels model) {
        Map<String, OrgInfoDTO> orgMap = new HashMap<>();

        // 查出所有的单位流程配置
        List<WflowOrgModels> orgModelsList = orgProcdefService.list();

        UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId);
        DingOrganization startOrg = dingOrganizationService.getOne(Wrappers.lambdaQuery(DingOrganization.class)
                .eq(DingOrganization::getOrganizationCode, userFullInfo.getOrganizationCode()));
        String startOrgCode = startOrg.getOrganizationCode();
        String startOrgName = startOrg.getOrganizationName();
        String startOrgParentCode = startOrg.getParentCode();
        String startOrgParentName = startOrg.getParentName();
        //查询 当前发起人及主管单位所在区域的 单位流程配置
        OrgInfoDTO startOrgInfoDto = new OrgInfoDTO();
        startOrgInfoDto.setOrganizationCode(startOrgCode);
        startOrgInfoDto.setOrganizationName(startOrgName);

        startOrgInfoDto.setOrgModelMap(orgModelsList.stream()
                .filter(v -> v.getOrgCode().equals(startOrgCode)
                        && Boolean.FALSE.equals(v.getIsDelete())
                        && (ProcessDefTypeEnum.SEAL.name().equals(v.getType()) ||
                        ProcessDefTypeEnum.DEFAULT.name().equals(v.getType())))
                .collect(Collectors.toMap(WflowOrgModels::getType, v -> v)));

        OrgInfoDTO parentOrgInfoDto = new OrgInfoDTO();
        parentOrgInfoDto.setOrganizationCode(startOrgParentCode);
        parentOrgInfoDto.setOrganizationName(startOrgParentName);
        parentOrgInfoDto.setOrgModelMap(orgModelsList.stream()
                .filter(v -> v.getOrgCode().equals(startOrgParentCode)
                        && Boolean.FALSE.equals(v.getIsDelete())
                        && (ProcessDefTypeEnum.SEAL.name().equals(v.getType()) ||
                        ProcessDefTypeEnum.DEFAULT.name().equals(v.getType())))
                .collect(Collectors.toMap(WflowOrgModels::getType, v -> v)));

        orgMap.put(OrgTypeEnum.TARGET_OWNER.name(),startOrgInfoDto);
        orgMap.put(OrgTypeEnum.TARGET_MANAGEMENT.name(),parentOrgInfoDto);

        // 如果是指定单位，直接根据流程定义ID放入map
        OrgInfoDTO orgInfoDTO = new OrgInfoDTO();
        orgInfoDTO.setOrgModelMap(orgModelsList.stream()
                .collect(Collectors.toMap(WflowOrgModels::getProcessDefId, v -> v)));
        orgMap.put(OrgTypeEnum.TARGET_LABEL.name(),orgInfoDTO);
        return orgMap;
    }
}
