package cn.freemud.enums;

import java.util.Objects;

/**
 * @author : xh.Z
 * @email : fisherman0510@163.com
 * @Date : 2021/12/1 下午3:32
 * @description :
 * @warning 这里的订单状态是依据 基础服务给出的原始库订单状态,和很老的订单状态有冲突
 * 在使用的过程中注意识别冲突
 * * 1待支付
 * * 2下单
 * * 3已接单
 * * 4制作完成 5配送中   4/5的状态根据订单类型(自提/外卖)二选一不会同时出现
 * * 6已完成
 * * 7已关闭(退款完成)
 * <p>
 * ********** 老的订单状态 如下
 * @see OrderStatus
 */
public enum BaseOrderStatus {
    WAIT_PAY(1, "待支付", "To be paid", 2),
    TAKE_ORDER(2, "下单", "place an order", 1),
    RECEIPT(3, "接单", "Receiving orders", 3),
    COMPLETE_MAKE(4, "制作完成", "Complete Make", 31),
    DISTRIBUTION(5, "配送中", "In delivery", 4),
    COMPLETE(6, "已完成", "Complete", 5),
    CALCEL(7, "已关闭(退款完成)", "Cancel", 6);

    // 订单数据库记录的状态
    private Integer baseStatus;
    private String desc;
    private String descEn;
    // 老业务订单的状态
    private Integer oldStatus;

    BaseOrderStatus(Integer code, String desc, String descEn, Integer oldStatus) {
        this.baseStatus = code;
        this.desc = desc;
        this.descEn = descEn;
        this.oldStatus = oldStatus;
    }

    public static Integer getOldStatus(Integer baseStatus) {
        BaseOrderStatus byCode = getByCode(baseStatus);
        Objects.requireNonNull(byCode, "新老订单类型转化错误,入参:" + baseStatus);
        return byCode.getOldStatus();
    }

    public static Integer[] getAllCodes() {
        Integer[] codeArray = new Integer[values().length];
        int i = 0;
        for (BaseOrderStatus orderStatus : values()) {
            codeArray[i] = orderStatus.getBaseStatus();
            i++;
        }
        return codeArray;
    }

    public static BaseOrderStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (BaseOrderStatus orderStatus : values()) {
            if (orderStatus.getBaseStatus().equals(code)) {
                return orderStatus;
            }
        }
        return null;
    }

    public String getDescEn() {
        return descEn;
    }

    public void setDescEn(String descEn) {
        this.descEn = descEn;
    }

    public Integer getBaseStatus() {
        return baseStatus;
    }

    public void setBaseStatus(Integer baseStatus) {
        this.baseStatus = baseStatus;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getOldStatus() {
        return oldStatus;
    }

    public void setOldStatus(Integer oldStatus) {
        this.oldStatus = oldStatus;
    }
}
