package com.bonus.common.biz.enums;

import lombok.Getter;

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

/**
 * @author bonus
 */

@Getter
public enum PurchaseTaskStatusEnum {
    TO_NOTICE(1, "待通知", PurchaseTaskStageEnum.MANAGE.getCode()),
    TO_CHECK(2, "待验收", PurchaseTaskStageEnum.CHECK.getCode()),
    TO_BIND(3, "待绑定", PurchaseTaskStageEnum.BIND.getCode()),
    TO_STORE(4, "待入库", PurchaseTaskStageEnum.STORE.getCode()),
    TO_CHECK_AFTER_REJECT(12, "验收(被驳回)", PurchaseTaskStageEnum.CHECK.getCode()),
    TO_BIND_AFTER_REJECT(13, "绑定(被驳回)", PurchaseTaskStageEnum.CHECK.getCode()),
    TO_STORE_AFTER_REJECT(14, "入库(被驳回)", PurchaseTaskStageEnum.BIND.getCode()),
    IN_STORE(19, "已入库", PurchaseTaskStageEnum.STORE.getCode()),

    TASK_IN_PROGRESS(21, "入库进行中", PurchaseTaskStageEnum.STORE.getCode()),
    TASK_FINISHED(22, "入库已完成", PurchaseTaskStageEnum.STORE.getCode());

    private final Integer status;
    private final String statusName;
    // 新购任务阶段
    private final Integer stage;

    PurchaseTaskStatusEnum(Integer status, String statusName, Integer stage) {
        this.status = status;
        this.statusName = statusName;
        this.stage = stage;
    }

    // 判断任务状态是否为此阶段所属状态
    public static boolean isStatusOfStage(Integer status, PurchaseTaskStageEnum stageEnum, Integer manageType) {
        if (null == status || null == stageEnum || null == manageType) {
            return true;
        }

        // 查询出数据是所属哪个状态
        PurchaseTaskStatusEnum findEnum = PurchaseTaskStatusEnum.getByStatus(status);

        if (null == findEnum) {
            return true;
        }

        // 如果是最后的入库完成状态，那么说明流程已走完，不需要再对比状态了
        if (Objects.equals(findEnum.getStatus(), TASK_FINISHED.getStatus()) || Objects.equals(findEnum.getStatus(), IN_STORE.getStatus())) {
            return false;
        }

        // 如果是验收阶段, 并且是数量管理的物资，那么入库被驳回的也要判断为未完成，因为计数管理不需要绑定，直接从验收到入库
        if (PurchaseTaskStageEnum.CHECK == stageEnum) {
            if (PurchaseTaskStatusEnum.TO_STORE_AFTER_REJECT == findEnum && Objects.equals(manageType, 1)) {
                return true;
            }
        }

        // 如果查询的是入库阶段、数据是待绑定，那么直接返回true：未完成
        if (PurchaseTaskStageEnum.STORE == stageEnum && (TO_BIND == findEnum || TO_STORE_AFTER_REJECT == findEnum)) {
            return true;
        }

        return findEnum.getStage().equals(stageEnum.getCode());
    }

    public static PurchaseTaskStatusEnum getByStatus(Integer status) {
        for (PurchaseTaskStatusEnum statusEnum : values()) {
            if (statusEnum.status.equals(status)) {
                return statusEnum;
            }
        }
        return null;
    }

}
