package com.fs.erp.constant;

/**
 * 平台单据状态枚举
 *
 * 定义售后单据在不同阶段的状态常量
 */
public enum AfterSalesOrderStatusEnum {

    /**
     * 买家已经申请，等待卖家同意
     */
    WAIT_SELLER_AGREE(0, "WAIT_SELLER_AGREE", "买家已经申请，等待卖家同意"),

    /**
     * 卖家已经同意，等待买家退货
     */
    WAIT_BUYER_RETURN_GOODS(1, "WAIT_BUYER_RETURN_GOODS", "卖家已经同意，等待买家退货"),

    /**
     * 买家已经退货，等待卖家确认收货
     */
    WAIT_SELLER_CONFIRM_GOODS(2, "WAIT_SELLER_CONFIRM_GOODS", "买家已经退货，等待卖家确认收货"),

    /**
     * 卖家拒绝售后
     */
    SELLER_REFUSE_BUYER(3, "SELLER_REFUSE_BUYER", "卖家拒绝售后"),

    /**
     * 等待卖家发货
     */
    WAIT_SELLER_DELIVER_GOODS(4, "WAIT_SELLER_DELIVER_GOODS", "等待卖家发货"),

    /**
     * 补发卖家发货
     */
    REISSUE_SELLER_DELIVERY(5, "REISSUE_SELLER_DELIVERY", "补发卖家发货"),

    /**
     * 售后关闭(售后单未确认前填写该状态erp的售后单自动作废)
     */
    CLOSED(6, "CLOSED", "售后关闭"),

    /**
     * 退款成功
     * 补发、换货售后单确认时需在系统中手动确认
     * 注意：抖音、拼多多、淘系等线上平台换货单据，通过接口修改状态为SUCCESS且发出的快递单号是空，
     * 平台会判定为换货单超时未处理，会被平台强制退款，线上售后类型会变为普通退货
     */
    SUCCESS(7, "SUCCESS", "退款成功"),
    CONFIRM(10, "CONFIRM", "确认");

    private final Integer index;
    private final String code;
    private final String description;

    AfterSalesOrderStatusEnum(Integer index, String code, String description) {
        this.index = index;
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public Integer getIndex() {
        return index;
    }

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

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

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

        for (AfterSalesOrderStatusEnum status : AfterSalesOrderStatusEnum.values()) {
            if (status.getIndex().equals(index)) {
                return status;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.code;
    }
}
