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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.BizConst;
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.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.ningdatech.basic.util.CollUtils;
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 java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum.*;

/**
 * 招标采购处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(9)
@Component
public class TenderPurchaseHandle extends AbstractProcessBusinessHandle {
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;

    private Integer order = 9;

    /**
     * 项目状态为：建设中及之后的状态
     */
    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,
            ACCEPTED
    );

    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.getNewProject(projectId);
        List<Long> allVersionProjectIds = projectService.getAllVersionProjectId(project);
        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)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectIds)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PURCHASE_PUT_ON_RECORD.name())
                    .orderByDesc(ProjectStatusChange::getCreateOn)
                    .last(BizConst.LIMIT_1));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
        }
        processDetailVO.setProcessName(CommonConst.TENDER_PURCHASE);
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

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