| | |
| | | package cn.iocoder.yudao.module.bi.service.config; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.config.vo.BiDataSourceConfigImportExcelVO; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.config.vo.BiDataSourceConfigImportReqVO; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.config.vo.BiDataSourceConfigImportRespVO; |
| | | import cn.iocoder.yudao.module.bi.dal.dataobject.BiDataSourceConfigDO; |
| | | import cn.iocoder.yudao.module.bi.dal.mysql.BiDataSourceConfigMapper; |
| | | import cn.iocoder.yudao.module.bi.enums.ErrorCodeConstants; |
| | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | |
| | | /** |
| | | * 数据源配置 Service 实现 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | public class BiDataSourceConfigServiceImpl implements BiDataSourceConfigService { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<BiDataSourceConfigDO> getPage(Integer pageNo, Integer pageSize) { |
| | | public PageResult<BiDataSourceConfigDO> getPage(String name, String type, Integer pageNo, Integer pageSize) { |
| | | IPage<BiDataSourceConfigDO> page = mapper.selectPage(new Page<>(pageNo, pageSize), |
| | | new LambdaQueryWrapperX<BiDataSourceConfigDO>() |
| | | .likeIfPresent(BiDataSourceConfigDO::getName, name) |
| | | .eqIfPresent(BiDataSourceConfigDO::getType, type) |
| | | .orderByDesc(BiDataSourceConfigDO::getCreateTime)); |
| | | return new PageResult<>(page.getRecords(), page.getTotal()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public BiDataSourceConfigImportRespVO importDataSourceConfigList(List<BiDataSourceConfigImportExcelVO> importList, |
| | | BiDataSourceConfigImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importList = filterList(importList, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(ErrorCodeConstants.BI_DATA_SOURCE_CONFIG_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐条处理 |
| | | BiDataSourceConfigImportRespVO respVO = BiDataSourceConfigImportRespVO.builder() |
| | | .createDataSourceConfigNames(new ArrayList<>()) |
| | | .updateDataSourceConfigNames(new ArrayList<>()) |
| | | .failureDataSourceConfigNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importList.forEach(item -> { |
| | | try { |
| | | BiDataSourceConfigDO exist = mapper.selectByName(item.getName()); |
| | | if (exist == null) { |
| | | // 2.1 新增 |
| | | BiDataSourceConfigDO config = BeanUtils.toBean(item, BiDataSourceConfigDO.class); |
| | | mapper.insert(config); |
| | | respVO.getCreateDataSourceConfigNames().add(item.getName()); |
| | | return; |
| | | } |
| | | // 2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureDataSourceConfigNames().put(item.getName(), |
| | | StrUtil.format(ErrorCodeConstants.BI_DATA_SOURCE_CONFIG_NAME_EXISTS.getMsg(), item.getName())); |
| | | return; |
| | | } |
| | | BiDataSourceConfigDO updateObj = BeanUtils.toBean(item, BiDataSourceConfigDO.class) |
| | | .setId(exist.getId()); |
| | | mapper.updateById(updateObj); |
| | | respVO.getUpdateDataSourceConfigNames().add(item.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureDataSourceConfigNames().put(item.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | private void validateExists(Long id) { |
| | | if (mapper.selectById(id) == null) { |
| | | throw exception(ErrorCodeConstants.BI_DATA_SOURCE_CONFIG_NOT_EXISTS); |