package com.zt.modules.sys.service; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zt.common.constant.CacheKey; import com.zt.common.constant.Constant; import com.zt.common.db.query.QueryFilter; import com.zt.common.exception.ErrorCode; import com.zt.common.exception.RenException; import com.zt.common.service.BaseService; import com.zt.common.utils.CacheUtils; import com.zt.core.sys.service.ISysParamsService; import com.zt.modules.sys.dao.SysParamsDao; import com.zt.modules.sys.dto.ConfigDto; import com.zt.modules.sys.enums.ParamValueType; import com.zt.modules.sys.model.SysParams; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; /** * 参数管理 * * @author hehz * @since 1.0.0 */ @Service public class SysParamsService extends BaseService implements ISysParamsService { public List page(QueryFilter queryFilter) { return super.query(queryFilter); } public List getConfigs() { List configs = (List) CacheUtils.get(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey()); if (configs == null) { List params = baseDao.selectList(new QueryWrapper() .eq("is_to_web", Constant.Bool.YES).eq(Constant.TableColumn.IS_DELETE, Constant.Bool.NO) .eq(Constant.TableColumn.TENANT_ID, Constant.Sys.PLATFORM_TENANT_ID)); configs = params.stream().map(p -> { ConfigDto dto = new ConfigDto(); dto.setKey(p.getParamCode()); String value = p.getParamValue(); if (ParamValueType.STRING.getValue().equals(p.getParamValueType())) { dto.setValue(value); } else if (ParamValueType.INT.getValue().equals(p.getParamValueType())) { dto.setValue(Integer.parseInt(value)); } else if (ParamValueType.LONG.getValue().equals(p.getParamValueType())) { dto.setValue(Long.parseLong(value)); } else if (ParamValueType.NUMBER.getValue().equals(p.getParamValueType())) { dto.setValue(Double.parseDouble(value)); } else if (ParamValueType.BOOLEAN.getValue().equals(p.getParamValueType())) { dto.setValue(Boolean.parseBoolean(value)); } return dto; }).collect(Collectors.toList()); CacheUtils.put(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey(), configs); } return configs; } public SysParams get(Long id) { return baseDao.selectById(id); } @Transactional(rollbackFor = Exception.class) public void insert(SysParams entity) { super.insert(entity); CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存 } @Transactional(rollbackFor = Exception.class) public void update(SysParams entity) { super.update(entity); CacheUtils.put(Constant.Cache.SYS, entity.getParamCode(), entity.getParamValue()); CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存 } @Transactional(rollbackFor = Exception.class) public void delete(Long[] ids) { super.deleteLogic(ids); CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存 } @Override public String getValue(String paramCode) { String paramValue = (String) CacheUtils.get(Constant.Cache.SYS, paramCode); if (paramValue == null) { paramValue = baseDao.getValueByCode(paramCode); CacheUtils.put(Constant.Cache.SYS, paramCode, paramValue); } return paramValue; } @Override public boolean getBooleanValue(String paramCode, boolean defaultValue) { String value = getValue(paramCode); return StringUtils.isEmpty(value) ? defaultValue : String.valueOf(Constant.Bool.YES).equals(value); } @Override public T getValueObject(String paramCode, Class clazz) { String paramValue = getValue(paramCode); if (StringUtils.isNotBlank(paramValue)) { return JSON.parseObject(paramValue, clazz); } try { return clazz.newInstance(); } catch (Exception e) { throw new RenException(ErrorCode.PARAMS_GET_ERROR); } } @Override @Transactional(rollbackFor = Exception.class) public int updateValueByCode(String paramCode, String paramValue) { int count = baseDao.updateValueByCode(paramCode, paramValue); CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存 return count; } }