package com.fs.erp.constant;

/**
 * 交易状态枚举
 * 用于表示交易过程中的各种状态
 */
public enum TradeStatus {
    /**
     * 买家未收到货
     */
    BUYER_NOT_RECEIVED("BUYER_NOT_RECEIVED", "买家未收到货"),

    /**
     * 买家已收到货
     */
    BUYER_RECEIVED("BUYER_RECEIVED", "买家已收到货"),

    /**
     * 买家已退货
     */
    BUYER_RETURNED_GOODS("BUYER_RETURNED_GOODS", "买家已退货"),

    /**
     * 卖家已收到退货
     */
    SELLER_RECEIVED("SELLER_RECEIVED", "卖家已收到退货");

    /**
     * 状态编码
     */
    private final String code;

    /**
     * 状态描述
     */
    private final String description;

    TradeStatus(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据状态编码获取对应的枚举实例
     *
     * @param code 状态编码
     * @return 对应的枚举实例，如果未找到则返回null
     */
    public static TradeStatus getByCode(String code) {
        if (code == null) {
            return null;
        }

        for (TradeStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 判断当前状态是否可以更新为目标状态
     *
     * @param target 目标状态
     * @return 是否可以更新
     */
    public boolean canUpdateTo(TradeStatus target) {
        if (target == null) {
            return false;
        }

        switch (this) {
            case BUYER_NOT_RECEIVED:
                // 未收到货可以更新为已收到货或买家退货
                return target == BUYER_RECEIVED || target == BUYER_RETURNED_GOODS;
            case BUYER_RECEIVED:
                // 已收到货可以更新为买家退货
                return target == BUYER_RETURNED_GOODS;
            case BUYER_RETURNED_GOODS:
                // 买家已退货可以更新为卖家已收到退货
                return target == SELLER_RECEIVED;
            case SELLER_RECEIVED:
                // 卖家已收到退货不可再更新
                return false;
            default:
                return false;
        }
    }
}
