package com.hz.pm.api.projectlib.utils;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.CommonConst;
import com.hz.pm.api.projectlib.model.enumeration.InstTypeEnum;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectInst;
import com.hz.pm.api.projectlib.service.IProjectInstService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.ningdatech.basic.exception.BizException;
import com.wflow.workflow.bean.vo.ProcessProgressVo;
import com.wflow.workflow.enums.ProcessStatusEnum;
import com.wflow.workflow.service.ProcessInstanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * 根据项目版本获取流程详情工具类
 *
 * @author CMM
 * @since 2023/04/20 09:57
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ProjectVersionUtil {

    private final IProjectService projectService;
    private final ProcessInstanceService processInstanceService;
    private final IProjectInstService projectInstService;

    /**
     * 根据最新的项目ID获取临近项目版本的审核记录信息
     *
     * @param projectId
     * @return
     */
    public ProcessProgressVo getPreVerProcessInfo(Long projectId, InstTypeEnum instTypeEnum) {
        Project project = projectService.getById(projectId);
        if (Objects.isNull(project)) {
            throw new BizException("当前项目不存在!");
        }
        String projectCode = project.getProjectCode();
        if (StringUtils.isEmpty(projectCode)) {
            throw new BizException("当前项目编号为空!");
        }
        // 获取相同项目编号的项目
        List<Project> projectList = projectService.list(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getProjectCode, projectCode));
        if (projectList.size() < CommonConst.VERSION_SIZE) {
            throw new BizException("未发现该项目的历史版本!");
        }

        // 获取之前版本的项目
        for (Project preProject : projectList) {
            // 从项目流程实例关联表中查出实例详情
            List<ProjectInst> projectInstList = projectInstService
                    .list(Wrappers.lambdaQuery(ProjectInst.class).eq(ProjectInst::getProjectId, preProject.getId())
                            .eq(ProjectInst::getInstType, instTypeEnum.getCode()));
            if (CollUtil.isNotEmpty(projectInstList)) {
                List<ProcessProgressVo> processProgressVoList = projectInstList.stream()
                        .map(p -> processInstanceService.getProgressInstanceDetail(null, p.getInstCode()))
                        .collect(Collectors.toList());
                // 筛选出上个版本该类型审核流程通过的流程详情
                List<ProcessProgressVo> instanceDetailList = processProgressVoList.stream()
                        .filter(p -> ProcessStatusEnum.APPROVED.getDesc().equals(p.getStatus()))
                        .collect(Collectors.toList());
                if (CollUtil.isNotEmpty(instanceDetailList)) {
                    // 获取最后一个流程详情
                    return instanceDetailList.get(instanceDetailList.size() - 1);
                }
            }
        }
        return null;
    }
}
