package com.cpop.mall.framework.enums;

import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.framework.strategy.buyRestrict.BuyRestrictStrategy;
import com.cpop.mall.framework.strategy.buyRestrict.MemberRestrictStrategy;
import com.cpop.mall.framework.strategy.buyRestrict.NewCustomerRestrictStrategy;
import com.cpop.mall.framework.strategy.buyRestrict.UserRestrictStrategy;
import lombok.Getter;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DB
 * @createTime 2023/11/06 15:21
 * @description 购买限制枚举
 */
@Getter
public enum BuyRestrictEnums {
    /**
     * 会员限制
     */
    MEMBER_RESTRICT(0, "会员限制", SpringUtils.getBean(MemberRestrictStrategy.class)),
    /**
     * 新客限定
     */
    NEW_CUSTOMER_RESTRICT(1, "新客限定", SpringUtils.getBean(NewCustomerRestrictStrategy.class)),
    /**
     * 用户限购
     */
    USER_RESTRICT(2, "用户限购", SpringUtils.getBean(UserRestrictStrategy.class))
    ;

    private Integer code;

    private String name;

    private BuyRestrictStrategy buyRestrictStrategy;

    BuyRestrictEnums(Integer code, String name, BuyRestrictStrategy buyRestrictStrategy) {
        this.code = code;
        this.name = name;
        this.buyRestrictStrategy = buyRestrictStrategy;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 放入map中，通过键取值
     */
    private static Map<Integer, BuyRestrictEnums> buyRestrictMap = new HashMap<>();

    static {
        for (BuyRestrictEnums value : BuyRestrictEnums.values()) {
            buyRestrictMap.put(value.getCode(), value);
        }
    }

    /**
     * 根据code获取枚举
     * @param code
     * @return
     */
    public static BuyRestrictEnums getByCode(Integer code) {
        return buyRestrictMap.get(code);
    }

}
