package com.fjhx.common.entity.documentary.eums;

import cn.hutool.extra.spring.SpringUtil;
import com.fjhx.common.service.documentary.GetDocumentaryBusinessTemplate;
import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum DocumentaryTypeEnum {

    sales(1, "销售跟单", "contractServiceImpl"),

    purchase(2, "采购跟单", "purchaseServiceImpl");

    /**
     * 跟单类型
     */
    private final Integer key;

    /**
     * 类型名称
     */
    private final String name;

    /**
     * 实现bean名称
     */
    private final String beanName;

    private final static Map<Integer, DocumentaryTypeEnum> map = new HashMap<>();

    static {
        for (DocumentaryTypeEnum value : DocumentaryTypeEnum.values()) {
            map.put(value.key, value);
        }
    }

    public static DocumentaryTypeEnum getEnum(Integer key) {
        return map.computeIfAbsent(key, item -> {
            throw new ServiceException("未知跟单类型");
        });
    }

    public GetDocumentaryBusinessTemplate getBusinessBean() {
        GetDocumentaryBusinessTemplate bean = SpringUtil.getBean(beanName, GetDocumentaryBusinessTemplate.class);
        if (bean == null) {
            throw new ServiceException("未配置 " + name + " 跟单业务bean");
        }
        return bean;
    }

}
