|
|
@@ -0,0 +1,127 @@
|
|
|
+package cn.tr.module.sys.storage.service.impl;
|
|
|
+
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.core.utils.JsonUtils;
|
|
|
+import cn.tr.core.utils.ValidationUtils;
|
|
|
+import cn.tr.plugin.file.config.FileClientConfig;
|
|
|
+import cn.tr.plugin.file.enums.FileStorageEnum;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import cn.tr.module.sys.storage.repository.SysStorageConfigRepository;
|
|
|
+import cn.tr.module.sys.storage.po.SysStorageConfigPO;
|
|
|
+import cn.tr.module.sys.storage.dto.SysStorageConfigDTO;
|
|
|
+import cn.tr.module.sys.storage.dto.SysStorageConfigQueryDTO;
|
|
|
+import java.util.*;
|
|
|
+import cn.tr.module.sys.storage.service.ISysStorageConfigService;
|
|
|
+import cn.tr.module.sys.storage.mapper.SysStorageConfigMapper;
|
|
|
+
|
|
|
+import javax.validation.Validator;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件存储配置Service接口实现类
|
|
|
+ *
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class SysStorageConfigServiceImpl implements ISysStorageConfigService {
|
|
|
+
|
|
|
+ private final SysStorageConfigRepository baseRepository;
|
|
|
+
|
|
|
+ private final Validator validator;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据条件查询文件存储配置
|
|
|
+ * @param query 查询参数
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<SysStorageConfigDTO> selectSysStorageConfigList(SysStorageConfigQueryDTO query){
|
|
|
+ return SysStorageConfigMapper.INSTANCE.convertDtoList(
|
|
|
+ baseRepository.selectList(new LambdaQueryWrapper<SysStorageConfigPO>()
|
|
|
+ .like(Objects.nonNull(query.getName()),SysStorageConfigPO::getName,
|
|
|
+ query.getName())
|
|
|
+ )
|
|
|
+ );
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据id查询文件存储配置
|
|
|
+ * @param id 主键id
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SysStorageConfigDTO selectSysStorageConfigById(String id){
|
|
|
+ return SysStorageConfigMapper.INSTANCE.convertDto(baseRepository.selectById(id));
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑文件存储配置
|
|
|
+ * @param source 编辑实体类
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean updateSysStorageConfigById(SysStorageConfigDTO source){
|
|
|
+ SysStorageConfigPO config = SysStorageConfigMapper.INSTANCE.convertPO(source);
|
|
|
+ config
|
|
|
+ .setConfig(parseClientConfig(config.getType(), source.getConfig()));
|
|
|
+ return baseRepository.updateById(config)!=0;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增文件存储配置
|
|
|
+ * @param source 新增实体类
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean insertSysStorageConfig(SysStorageConfigDTO source){
|
|
|
+ SysStorageConfigPO config = SysStorageConfigMapper.INSTANCE.convertPO(source);
|
|
|
+ config
|
|
|
+ .setConfig(parseClientConfig(config.getType(), source.getConfig()))
|
|
|
+ // 默认非 master
|
|
|
+ .setMaster(false);
|
|
|
+ return baseRepository.insert(config)!=0;
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件存储配置详情
|
|
|
+ * @param ids 删除主键集合
|
|
|
+ * @author lf
|
|
|
+ * @date 2023/05/09 14:29
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int removeSysStorageConfigByIds(Collection<String> ids){
|
|
|
+ Long count = baseRepository.selectCount(new LambdaQueryWrapper<SysStorageConfigPO>()
|
|
|
+ .eq(SysStorageConfigPO::getMaster, true)
|
|
|
+ .in(SysStorageConfigPO::getId, ids));
|
|
|
+ if(count>0){
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"不可删除主配置");
|
|
|
+ }
|
|
|
+ return baseRepository.deleteBatchIds(ids);
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ private FileClientConfig parseClientConfig(String storage, Map<String, Object> config) {
|
|
|
+ // 获取配置类
|
|
|
+ Class<? extends FileClientConfig> configClass = FileStorageEnum.getByStorage(storage)
|
|
|
+ .getConfigClass();
|
|
|
+ FileClientConfig clientConfig = JsonUtils.parseObject(JsonUtils.toJsonString(config), configClass);
|
|
|
+ // 参数校验
|
|
|
+ ValidationUtils.validate(validator, clientConfig);
|
|
|
+ // 设置参数
|
|
|
+ return clientConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|