package com.cf.imes.module.system.service.systemconfig.factory;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.module.system.enums.ErrorCodeConstants;
import com.cf.imes.module.system.enums.config.SystemConfigTypeEnum;
import com.cf.imes.module.system.service.systemconfig.factory.service.AbstractSystemConfigServiceHandler;
import com.cf.imes.module.system.service.systemconfig.factory.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 系统配置服务分配工厂类
 *
 * @author Gqr
 * @since 2024/11/8 16:40
 */
@Component
public class SystemConfigServiceFactory {
    private final ApplicationContext applicationContext;

    @Autowired
    public SystemConfigServiceFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 根据类型获取对应的服务实现
     *
     * @param type
     * @return
     */
    public AbstractSystemConfigServiceHandler getConfigServiceHandler(Integer type) {
        SystemConfigTypeEnum systemConfigTypeEnum = SystemConfigTypeEnum.fromType(type);
        if (ObjectUtil.isNull(systemConfigTypeEnum)) {
            return applicationContext.getBean(DefaultSystemConfigServiceHandler.class);
        }
        switch (systemConfigTypeEnum) {

            // 自定义板编号
            case CUSTOM_PLATE_NO:
                return applicationContext.getBean(CustomPlateNoServiceHandler.class);
            // 板件分线
            case INTELLIGENT_BRANCH:
                return applicationContext.getBean(IntellectBranchingService.class);
            // 加工筛选
            case PROCESSING_SCREENING:
                return applicationContext.getBean(ProcessScreeningServiceHandler.class);
            // 数据处理
            case DATA_PROCESSING:
                return applicationContext.getBean(DataProcessServiceHandler.class);
            // 加工方案
            case PROCESS_SCHEME:
                return applicationContext.getBean(ProcessSchemeServiceHandler.class);
            // 封边条对应
            case SEALEDGE:
                return applicationContext.getBean(SealedgeServiceHandler.class);

            default:
                throw new ServiceException(ErrorCodeConstants.SYSTEM_CONFIG_TYPE_NOT_SUPPORT);
        }
    }
}
