|  |  |  | 
|---|
|  |  |  | package com.zy.acs.manager.system.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.zy.acs.common.utils.GsonUtils; | 
|---|
|  |  |  | import com.zy.acs.framework.common.DateUtils; | 
|---|
|  |  |  | import com.zy.acs.manager.system.entity.Config; | 
|---|
|  |  |  | import com.zy.acs.manager.system.enums.ConfigType; | 
|---|
|  |  |  | import com.zy.acs.manager.system.mapper.ConfigMapper; | 
|---|
|  |  |  | import com.zy.acs.manager.system.service.ConfigService; | 
|---|
|  |  |  | import com.zy.acs.framework.exception.CoolException; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 8/30/2024 | 
|---|
|  |  |  | 
|---|
|  |  |  | public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String getVal(String key) { | 
|---|
|  |  |  | throw new CoolException("Config还没做"); | 
|---|
|  |  |  | @SuppressWarnings("unchecked") | 
|---|
|  |  |  | public <T> T getVal(String key, Class<T> clazz) { | 
|---|
|  |  |  | List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key)); | 
|---|
|  |  |  | Config config = list.stream().findFirst().orElse(null); | 
|---|
|  |  |  | if (null == config) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String val = config.getVal(); | 
|---|
|  |  |  | switch (ConfigType.query(config.getType())) { | 
|---|
|  |  |  | case BOOLEAN: | 
|---|
|  |  |  | if (val.equals("1") || val.trim().toUpperCase().equals("TRUE")) { | 
|---|
|  |  |  | return (T) Boolean.TRUE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return (T) Boolean.FALSE; | 
|---|
|  |  |  | case NUMBER: | 
|---|
|  |  |  | if (clazz == Integer.class) { | 
|---|
|  |  |  | return (T) Integer.valueOf(val); | 
|---|
|  |  |  | } else if (clazz == Short.class) { | 
|---|
|  |  |  | return (T) Short.valueOf(val); | 
|---|
|  |  |  | } else if (clazz == Long.class) { | 
|---|
|  |  |  | return (T) Long.valueOf(val); | 
|---|
|  |  |  | } else if (clazz == Double.class) { | 
|---|
|  |  |  | return (T) Double.valueOf(val); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | throw new UnsupportedOperationException("Unsupported type: " + clazz.getName()); | 
|---|
|  |  |  | case STRING: | 
|---|
|  |  |  | return (T) val; | 
|---|
|  |  |  | case JSON: | 
|---|
|  |  |  | return GsonUtils.fromJson(val, clazz); | 
|---|
|  |  |  | case DATE: | 
|---|
|  |  |  | return (T) DateUtils.convert(val); | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|