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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.model.constant.ProjectDeclareConst;
import com.hz.pm.api.common.enumeration.ProjectProcessStageEnum;
import com.hz.pm.api.common.statemachine.util.StateMachineUtil;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.wflow.bean.entity.WflowModels;
import com.wflow.workflow.bean.vo.ProcessDetailVO;
import com.wflow.workflow.service.ProcessModelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 流程执行链
 *
 * @author CMM
 * @since 2023/02/26 21:36
 */
@Component
@RequiredArgsConstructor
public class ProcessExecuteChainHandle {

    private final IProjectService projectService;
    private final ProcessModelService processModelService;

    /**
     * 具体处理者的集合
     */
    private final List<AbstractProcessBusinessHandle> processBusinessHandles;

    /**
     * 遍历处理链，通过前缀判断能否处理逻辑，如果不能继续遍历
     *
     * @param projectId \
     * @return void
     * @author CMM
     * @since 2023/02/26
     */
    public List<ProcessDetailVO> handle(Long projectId, List<ProcessDetailVO> processSchedule) {
        List<AbstractProcessBusinessHandle> handles = Lists.newArrayList();
        for (AbstractProcessBusinessHandle processBusinessHandle : processBusinessHandles) {
            if (processBusinessHandle instanceof AnnualPlanHandle) {
                processBusinessHandle.setOrder(6);
            } else if (processBusinessHandle instanceof ConstructionPlanReviewHandle) {
                processBusinessHandle.setOrder(7);
            }

            //建设方案
            if (processBusinessHandle instanceof ConstructionPlanReviewHandle) {
                WflowModels model = processModelService.getOne(Wrappers.lambdaQuery(WflowModels.class)
                        .eq(WflowModels::getProcessType, ProjectProcessStageEnum.CONSTRUCTION_PROJECT_APPROVAL_PROCESS.getCode())
                        .last(BizConst.LIMIT_1));
                if (Objects.nonNull(model) && (Boolean.FALSE.equals(model.getIsStop()))) {
                        handles.add(processBusinessHandle);
                }
            } else if (processBusinessHandle instanceof ProvinceUnitedReviewHandle) {
                //省级联审
                // TODO
            } else {
                handles.add(processBusinessHandle);
            }
        }

        if (!handles.isEmpty()) {
            handles.sort(Comparator.comparing(AbstractProcessBusinessHandle::getOrder));
            for (AbstractProcessBusinessHandle processBusinessHandle : handles) {
                processBusinessHandle.businessHandle(projectId, processSchedule);
            }
            return processSchedule;
        }
        return Collections.emptyList();
    }
}
