package com.ningdatech.pmapi.projectlib.handle;

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

import java.util.*;

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(8)
@Component
public class TenderPurchaseHandle extends AbstractProcessBusinessHandle {
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;

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

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

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        // 根据项目是否通过立项批复(立项批复时间)来判断招标采购是否开始
        Project project = projectService.getById(projectId);
        Integer status = project.getStatus();
        // 未进行立项批复
        if (Objects.isNull(project.getApprovalDate())){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.TENDER_PURCHASE);
            processSchedule.add(processDetailVO);
            return;
        }
        List<Integer> fieldList = CollUtils.fieldList(TENDER_PURCHASE_LIST_STATUS, ProjectStatusEnum::getCode);
        // 进行了立项批复且项目状态是待采购
        if (ProjectStatusEnum.TO_BE_PURCHASED.getCode().equals(project.getStatus())){
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);
        }else if (fieldList.contains(status)){
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
            // 根据采购备案的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PURCHASE_PUT_ON_RECORD.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
        }
        processDetailVO.setProcessName(CommonConst.TENDER_PURCHASE);
        processSchedule.add(processDetailVO);
    }
}
