package com.ningdatech.pmapi.projectlib.handle;

import static com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum.*;

import java.util.Arrays;
import java.util.List;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.CommonConst;
import com.ningdatech.pmapi.common.statemachine.event.ProjectStatusChangeEvent;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.enumeration.StepStatusEnum;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectStatusChange;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.model.vo.ProcessDetailVO;
import com.ningdatech.pmapi.projectlib.service.INdProjectStatusChangeService;
import com.ningdatech.pmapi.projectlib.service.IProjectService;

/**
 * 项目初验处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(9)
@Component
public class ProjectPreliminaryInspectionHandle extends AbstractProcessBusinessHandle {
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;

    /**
     * 项目状态为：建设中及之后的状态
     */
    private static final List<ProjectStatusEnum> PROJECT_PRELIMINARY_INSPECTION_LIST_STATUS = Arrays.asList(
            UNDER_CONSTRUCTION,
            TO_BE_FINALLY_INSPECTED,
            FINAL_ACCEPTANCE_IS_UNDER_REVIEW,
            FINAL_ACCEPTANCE_REVIEW_FAILED,
            ARCHIVED
    );

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

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        // 根据项目状态是否在建设中及之后的状态判断项目初验是否开始
        List<Integer> fieldList = CollUtils.fieldList(PROJECT_PRELIMINARY_INSPECTION_LIST_STATUS, ProjectStatusEnum::getCode);
        Project project = projectService.getById(projectId);
        Integer status = project.getStatus();
        // 项目状态不在建设中及之后的状态
        if (!fieldList.contains(status)){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.PROJECT_PRELIMINARY_INSPECTION);
            processSchedule.add(processDetailVO);
            return;
        }
        // 项目状态为建设中或之后的状态
        if (ProjectStatusEnum.UNDER_CONSTRUCTION.getCode().equals(project.getStatus())){
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);
        }else {
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
            // 根据初验备案的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PRELIMINARY_ACCEPTANCE_PUT_ON_RECORD.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
        }
        processDetailVO.setProcessName(CommonConst.TENDER_PURCHASE);
        processSchedule.add(processDetailVO);
    }
}
