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


import java.util.List;
import java.util.Objects;

import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.wflow.workflow.bean.vo.ProcessDetailVO;
import com.wflow.workflow.enums.StepStatusEnum;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.CommonConst;
import com.hz.pm.api.common.statemachine.event.ProjectStatusChangeEvent;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;

/**
 * 归档处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(12)
@Component
public class ArchivedHandle extends AbstractProcessBusinessHandle {
    private final IProjectService projectService;
    private final INdProjectStatusChangeService projectStatusChangeService;

    private Integer order = 12;

    public ArchivedHandle( IProjectService projectService, INdProjectStatusChangeService projectStatusChangeService){
        this.projectService = projectService;
        this.projectStatusChangeService = projectStatusChangeService;
    }

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        Project project = projectService.getById(projectId);
        Integer status = project.getStage();
        //查出历史版本的 所有项目ID
        List<Long> allVersionProjectIds = projectService.getAllVersionProjectId(project);
        // 根据项目阶段判断是否已归档
        // 项目阶段不为已归档
        if (!ProjectStatusEnum.ARCHIVED.getCode().equals(status)){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.ARCHIVED);
            processSchedule.add(processDetailVO);
            return;
        }
        // 项目阶段为已归档
        processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
        // 根据项目终验获取归档时间
        ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                .in(ProjectStatusChange::getProjectId, allVersionProjectIds)
                .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.FINAL_ACCEPTANCE_PASS.name())
                .orderByDesc(ProjectStatusChange::getCreateOn)
                .last(BizConst.LIMIT_1));
        if(Objects.nonNull(projectStatusChange)){
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setProcessName(CommonConst.ARCHIVED);
        }
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

    @Override
    void setOrder(Integer order) {
        this.order = order;
    }
}
