From b333dc0260b7251b9bf2a2837378c2e199ad4d2d Mon Sep 17 00:00:00 2001 From: jay <75509151@qq.com> Date: Thu, 1 Jun 2023 20:12:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20oss=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iotkit/data/system/ISysOssConfigData.java | 6 +- .../data/service/SysOssConfigDataImpl.java | 52 +++ .../service/impl/SysOssConfigServiceImpl.java | 332 +++++++++--------- 3 files changed, 216 insertions(+), 174 deletions(-) create mode 100644 iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysOssConfigDataImpl.java diff --git a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysOssConfigData.java b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysOssConfigData.java index c9a9010b..15779b87 100644 --- a/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysOssConfigData.java +++ b/iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/system/ISysOssConfigData.java @@ -1,9 +1,13 @@ package cc.iotkit.data.system; +import cc.iotkit.data.ICommonData; +import cc.iotkit.model.system.SysOperLog; +import cc.iotkit.model.system.SysOssConfig; + /** * 操作日志数据接口 * * @author sjg */ -public interface ISysOssConfigData { +public interface ISysOssConfigData extends ICommonData { } diff --git a/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysOssConfigDataImpl.java b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysOssConfigDataImpl.java new file mode 100644 index 00000000..1d82fd87 --- /dev/null +++ b/iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/SysOssConfigDataImpl.java @@ -0,0 +1,52 @@ +package cc.iotkit.data.service; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; +import cc.iotkit.common.utils.MapstructUtils; +import cc.iotkit.common.utils.StringUtils; +import cc.iotkit.data.dao.IJPACommData; +import cc.iotkit.data.dao.SysOssConfigRepository; +import cc.iotkit.data.model.TbSysOssConfig; +import cc.iotkit.data.system.ISysOssConfigData; +import cc.iotkit.data.util.PageBuilder; +import cc.iotkit.data.util.PredicateBuilder; +import cc.iotkit.model.system.SysOssConfig; +import cn.hutool.core.util.ArrayUtil; +import com.querydsl.core.types.Predicate; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Primary; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; + +import static cc.iotkit.data.model.QTbSysOssConfig.tbSysOssConfig; + +/** + * @Author:tfd + * @Date:2023/5/31 15:24 + */ +@Primary +@Service +@RequiredArgsConstructor +public class SysOssConfigDataImpl implements ISysOssConfigData, IJPACommData { + + private SysOssConfigRepository operLogRepository; + + + private final JPAQueryFactory jpaQueryFactory; + + @Override + public JpaRepository getBaseRepository() { + return operLogRepository; + } + + @Override + public Class getJpaRepositoryClass() { + return TbSysOssConfig.class; + } + + +} diff --git a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssConfigServiceImpl.java b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssConfigServiceImpl.java index 91ca8d18..8962abcf 100644 --- a/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssConfigServiceImpl.java +++ b/iot-module/iot-system/src/main/java/cc/iotkit/system/service/impl/SysOssConfigServiceImpl.java @@ -1,180 +1,166 @@ -//package cc.iotkit.system.service.impl; -// -//import cc.iotkit.common.api.PageRequest; -//import cc.iotkit.common.constant.CacheNames; -//import cc.iotkit.common.undefined.PagedDataVo; -//import cc.iotkit.common.exception.BizException; -//import cc.iotkit.common.redis.utils.CacheUtils; -//import cc.iotkit.common.redis.utils.RedisUtils; -//import cc.iotkit.common.tenant.core.TenantDto; -//import cc.iotkit.common.tenant.helper.TenantHelper; -//import cc.iotkit.common.utils.JsonUtils; -//import cc.iotkit.common.utils.MapstructUtils; -//import cc.iotkit.common.utils.StreamUtils; -//import cc.iotkit.common.utils.StringUtils; -//import cc.iotkit.model.system.SysOssConfig; -//import cc.iotkit.system.dto.bo.SysOssConfigBo; -//import cc.iotkit.system.dto.vo.SysOssConfigVo; -//import cn.hutool.core.collection.CollUtil; -//import cn.hutool.core.util.ObjectUtil; -//import lombok.RequiredArgsConstructor; -//import lombok.extern.slf4j.Slf4j; -//import cc.iotkit.system.service.ISysOssConfigService; -//import org.springframework.stereotype.Service; -//import org.springframework.transaction.annotation.Transactional; -// -//import java.util.Collection; -//import java.util.List; -//import java.util.Map; -// -///** -// * 对象存储配置Service业务层处理 -// * -// * @author Lion Li -// * @author 孤舟烟雨 -// * @date 2021-08-13 -// */ -//@Slf4j -//@RequiredArgsConstructor -//@Service -//public class SysOssConfigServiceImpl implements ISysOssConfigService { -// -// private final SysOssConfigMapper baseMapper; -// -// /** -// * 项目启动时,初始化参数到缓存,加载配置类 -// */ -// @Override -// public void init() { -// List list = TenantHelper.ignore(() -> -// baseMapper.selectList( -// new LambdaQueryWrapper().orderByAsc(TenantDto::getTenantId)) -// ); -// Map> map = StreamUtils.groupByKey(list, SysOssConfig::getTenantId); -// try { -// for (String tenantId : map.keySet()) { -// TenantHelper.setDynamic(tenantId); -// // 加载OSS初始化配置 -// for (SysOssConfig config : map.get(tenantId)) { -// String configKey = config.getConfigKey(); -// if ("0".equals(config.getStatus())) { -// RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey); -// } -// CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); -// } -// } -// } finally { -// TenantHelper.clearDynamic(); -// } -// } -// -// @Override -// public SysOssConfigVo queryById(Long ossConfigId) { -// return baseMapper.selectVoById(ossConfigId); -// } -// -// @Override -// public PagedDataVo queryPageList(SysOssConfigBo bo, PageRequest query) { -// LambdaQueryWrapper lqw = buildQueryWrapper(bo); -// Page result = baseMapper.selectVoPage(query.build(), lqw); -// return TableDataInfo.build(result); -// } -// -// -// private LambdaQueryWrapper buildQueryWrapper(SysOssConfigBo bo) { -// LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); -// lqw.eq(StringUtils.isNotBlank(bo.getConfigKey()), SysOssConfig::getConfigKey, bo.getConfigKey()); -// lqw.like(StringUtils.isNotBlank(bo.getBucketName()), SysOssConfig::getBucketName, bo.getBucketName()); -// lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysOssConfig::getStatus, bo.getStatus()); -// return lqw; -// } -// -// @Override -// public Boolean insertByBo(SysOssConfigBo bo) { -// SysOssConfig config = MapstructUtils.convert(bo, SysOssConfig.class); -// validEntityBeforeSave(config); -// boolean flag = baseMapper.insert(config) > 0; -// if (flag) { -// CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); -// } -// return flag; -// } -// -// @Override -// public Boolean updateByBo(SysOssConfigBo bo) { -// SysOssConfig config = MapstructUtils.convert(bo, SysOssConfig.class); -// validEntityBeforeSave(config); -// LambdaUpdateWrapper luw = new LambdaUpdateWrapper<>(); -// luw.set(ObjectUtil.isNull(config.getPrefix()), SysOssConfig::getPrefix, ""); -// luw.set(ObjectUtil.isNull(config.getRegion()), SysOssConfig::getRegion, ""); -// luw.set(ObjectUtil.isNull(config.getExt1()), SysOssConfig::getExt1, ""); -// luw.set(ObjectUtil.isNull(config.getRemark()), SysOssConfig::getRemark, ""); -// luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId()); -// boolean flag = baseMapper.update(config, luw) > 0; -// if (flag) { -// CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); -// } -// return flag; -// } -// -// /** -// * 保存前的数据校验 -// */ -// private void validEntityBeforeSave(SysOssConfig entity) { -// if (StringUtils.isNotEmpty(entity.getConfigKey()) -// && !checkConfigKeyUnique(entity)) { -// throw new BizException("操作配置'" + entity.getConfigKey() + "'失败, 配置key已存在!"); -// } -// } -// -// @Override -// public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { -// if (isValid) { -// if (CollUtil.containsAny(ids, OssConstant.SYSTEM_DATA_IDS)) { -// throw new BizException("系统内置, 不可删除!"); -// } -// } -// List list = CollUtil.newArrayList(); -// for (Long configId : ids) { -// SysOssConfig config = baseMapper.selectById(configId); -// list.add(config); -// } -// boolean flag = baseMapper.deleteBatchIds(ids) > 0; -// if (flag) { -// list.forEach(sysOssConfig -> -// CacheUtils.evict(CacheNames.SYS_OSS_CONFIG, sysOssConfig.getConfigKey())); -// } -// return flag; -// } -// -// /** -// * 判断configKey是否唯一 -// */ -// private boolean checkConfigKeyUnique(SysOssConfig sysOssConfig) { -// long ossConfigId = ObjectUtil.isNull(sysOssConfig.getOssConfigId()) ? -1L : sysOssConfig.getOssConfigId(); -// SysOssConfig info = baseMapper.selectOne(new LambdaQueryWrapper() -// .select(SysOssConfig::getOssConfigId, SysOssConfig::getConfigKey) -// .eq(SysOssConfig::getConfigKey, sysOssConfig.getConfigKey())); -// if (ObjectUtil.isNotNull(info) && info.getOssConfigId() != ossConfigId) { -// return false; -// } -// return true; -// } -// -// /** -// * 启用禁用状态 -// */ -// @Override -// @Transactional(rollbackFor = Exception.class) -// public int updateOssConfigStatus(SysOssConfigBo bo) { +package cc.iotkit.system.service.impl; + +import cc.iotkit.common.api.PageRequest; +import cc.iotkit.common.api.Paging; +import cc.iotkit.common.constant.CacheNames; +import cc.iotkit.common.oss.constant.OssConstant; + +import cc.iotkit.common.exception.BizException; +import cc.iotkit.common.redis.utils.CacheUtils; +import cc.iotkit.common.redis.utils.RedisUtils; +import cc.iotkit.common.tenant.core.TenantDto; +import cc.iotkit.common.tenant.helper.TenantHelper; +import cc.iotkit.common.utils.JsonUtils; +import cc.iotkit.common.utils.MapstructUtils; +import cc.iotkit.common.utils.StreamUtils; +import cc.iotkit.common.utils.StringUtils; +import cc.iotkit.data.system.ISysOssConfigData; +import cc.iotkit.model.system.SysOssConfig; +import cc.iotkit.system.dto.bo.SysOssConfigBo; +import cc.iotkit.system.dto.vo.SysOssConfigVo; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import cc.iotkit.system.service.ISysOssConfigService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * 对象存储配置Service业务层处理 + * + * @author Lion Li + * @author 孤舟烟雨 + * @date 2021-08-13 + */ +@Slf4j +@RequiredArgsConstructor +@Service +public class SysOssConfigServiceImpl implements ISysOssConfigService { + + private final ISysOssConfigData baseData; + + /** + * 项目启动时,初始化参数到缓存,加载配置类 + */ + @Override + public void init() { + List list = baseData.findAll(); + Map> map = StreamUtils.groupByKey(list, SysOssConfig::getTenantId); + try { + for (String tenantId : map.keySet()) { + TenantHelper.setDynamic(tenantId); + // 加载OSS初始化配置 + for (SysOssConfig config : map.get(tenantId)) { + String configKey = config.getConfigKey(); + if ("0".equals(config.getStatus())) { + RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey); + } + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + } + } + } finally { + TenantHelper.clearDynamic(); + } + } + + @Override + public SysOssConfigVo queryById(Long ossConfigId) { + return MapstructUtils.convert(baseData.findById(ossConfigId), SysOssConfigVo.class); + } + + @Override + public Paging queryPageList(SysOssConfigBo bo, PageRequest query) { + return null; + } + + + + + @Override + public Boolean insertByBo(SysOssConfigBo bo) { + SysOssConfig config = MapstructUtils.convert(bo, SysOssConfig.class); + validEntityBeforeSave(config); + SysOssConfig save = baseData.save(config); + if (ObjectUtil.isNotNull(save)) { + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + } + return true; + } + + @Override + public Boolean updateByBo(SysOssConfigBo bo) { + SysOssConfig config = MapstructUtils.convert(bo, SysOssConfig.class); + validEntityBeforeSave(config); + SysOssConfig save = baseData.save(config); + return true; + } + + /** + * 保存前的数据校验 + */ + private void validEntityBeforeSave(SysOssConfig entity) { + if (StringUtils.isNotEmpty(entity.getConfigKey()) + && !checkConfigKeyUnique(entity)) { + throw new BizException("操作配置'" + entity.getConfigKey() + "'失败, 配置key已存在!"); + } + } + + @Override + public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { + if (isValid) { + if (CollUtil.containsAny(ids, OssConstant.SYSTEM_DATA_IDS)) { + throw new BizException("系统内置, 不可删除!"); + } + } + List list = CollUtil.newArrayList(); + for (Long configId : ids) { + SysOssConfig config = baseData.findById(configId); + list.add(config); + } + // TODO: 2021/8/13 删除数据校验 + baseData.deleteByIds(ids); + boolean flag = true; + if (flag) { + list.forEach(sysOssConfig -> + CacheUtils.evict(CacheNames.SYS_OSS_CONFIG, sysOssConfig.getConfigKey())); + } + return flag; + } + + /** + * 判断configKey是否唯一 + */ + private boolean checkConfigKeyUnique(SysOssConfig sysOssConfig) { + long ossConfigId = ObjectUtil.isNull(sysOssConfig.getId()) ? -1L : sysOssConfig.getId(); + SysOssConfig q = new SysOssConfig(); + q.setConfigKey(sysOssConfig.getConfigKey()); + SysOssConfig info = baseData.findOneByCondition(q); + if (ObjectUtil.isNotNull(info) && info.getId() != ossConfigId) { + return false; + } + return true; + } + + /** + * 启用禁用状态 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public int updateOssConfigStatus(SysOssConfigBo bo) { // SysOssConfig sysOssConfig = MapstructUtils.convert(bo, SysOssConfig.class); -// int row = baseMapper.update(null, new LambdaUpdateWrapper() +// int row = baseData.save(null, new LambdaUpdateWrapper() // .set(SysOssConfig::getStatus, "1")); // row += baseMapper.updateById(sysOssConfig); // if (row > 0) { // RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, sysOssConfig.getConfigKey()); // } // return row; -// } -// -//} + return 0; + } + +}