| | |
| | | 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.BiChartConfigImportExcelVO; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.config.vo.BiChartConfigImportReqVO; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.config.vo.BiChartConfigImportRespVO; |
| | | import cn.iocoder.yudao.module.bi.dal.dataobject.BiChartConfigDO; |
| | | import cn.iocoder.yudao.module.bi.dal.dataobject.BiDashboardDO; |
| | | import cn.iocoder.yudao.module.bi.dal.dataobject.BiDataSourceConfigDO; |
| | | import cn.iocoder.yudao.module.bi.dal.mysql.BiChartConfigMapper; |
| | | import cn.iocoder.yudao.module.bi.dal.mysql.BiDashboardMapper; |
| | | import cn.iocoder.yudao.module.bi.dal.mysql.BiDataSourceConfigMapper; |
| | | import cn.iocoder.yudao.module.bi.enums.ErrorCodeConstants; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | 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 实现 |
| | |
| | | |
| | | @Resource |
| | | private BiChartConfigMapper mapper; |
| | | |
| | | @Resource |
| | | private BiDashboardMapper dashboardMapper; |
| | | |
| | | @Resource |
| | | private BiDataSourceConfigMapper biDataSourceConfigMapper; |
| | | |
| | | @Override |
| | | public Long create(BiChartConfigDO config) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<BiChartConfigDO> getPage(Long dashboardId, Integer pageNo, Integer pageSize) { |
| | | public PageResult<BiChartConfigDO> getPage(Long dashboardId, String name, Integer pageNo, Integer pageSize) { |
| | | IPage<BiChartConfigDO> page = mapper.selectPage(new Page<>(pageNo, pageSize), |
| | | new LambdaQueryWrapperX<BiChartConfigDO>() |
| | | .eqIfPresent(BiChartConfigDO::getDashboardId, dashboardId) |
| | | .likeIfPresent(BiChartConfigDO::getName, name) |
| | | .orderByAsc(BiChartConfigDO::getSort)); |
| | | return new PageResult<>(page.getRecords(), page.getTotal()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public BiChartConfigImportRespVO importChartConfigList(List<BiChartConfigImportExcelVO> importList, |
| | | BiChartConfigImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importList = filterList(importList, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(ErrorCodeConstants.BI_CHART_CONFIG_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐条处理 |
| | | BiChartConfigImportRespVO respVO = BiChartConfigImportRespVO.builder() |
| | | .createChartConfigNames(new ArrayList<>()) |
| | | .updateChartConfigNames(new ArrayList<>()) |
| | | .failureChartConfigNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importList.forEach(item -> { |
| | | try { |
| | | // 2.1 解析所属仪表盘 |
| | | BiDashboardDO dashboard = StrUtil.isBlank(item.getDashboardName()) ? null |
| | | : dashboardMapper.selectByName(item.getDashboardName()); |
| | | if (dashboard == null) { |
| | | throw exception(ErrorCodeConstants.BI_CHART_CONFIG_IMPORT_DASHBOARD_NOT_EXISTS, item.getDashboardName()); |
| | | } |
| | | // 2.2 解析数据源 |
| | | BiDataSourceConfigDO dataSource = StrUtil.isBlank(item.getDataSourceName()) ? null |
| | | : biDataSourceConfigMapper.selectByName(item.getDataSourceName()); |
| | | if (dataSource == null) { |
| | | throw exception(ErrorCodeConstants.BI_CHART_CONFIG_IMPORT_DATA_SOURCE_NOT_EXISTS, item.getDataSourceName()); |
| | | } |
| | | // 2.3 判断是否存在 |
| | | BiChartConfigDO exist = mapper.selectByNameAndDashboardId(item.getName(), dashboard.getId()); |
| | | if (exist == null) { |
| | | // 2.3.1 新增 |
| | | BiChartConfigDO config = BeanUtils.toBean(item, BiChartConfigDO.class) |
| | | .setDashboardId(dashboard.getId()) |
| | | .setDataSourceId(dataSource != null ? dataSource.getId() : null); |
| | | mapper.insert(config); |
| | | respVO.getCreateChartConfigNames().add(item.getName()); |
| | | return; |
| | | } |
| | | // 2.3.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureChartConfigNames().put(item.getName(), |
| | | StrUtil.format(ErrorCodeConstants.BI_CHART_CONFIG_NAME_EXISTS.getMsg(), item.getName())); |
| | | return; |
| | | } |
| | | BiChartConfigDO updateObj = BeanUtils.toBean(item, BiChartConfigDO.class) |
| | | .setId(exist.getId()) |
| | | .setDashboardId(dashboard.getId()) |
| | | .setDataSourceId(dataSource != null ? dataSource.getId() : null); |
| | | mapper.updateById(updateObj); |
| | | respVO.getUpdateChartConfigNames().add(item.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureChartConfigNames().put(item.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | private void validateExists(Long id) { |
| | | if (mapper.selectById(id) == null) { |
| | | throw exception(ErrorCodeConstants.BI_CHART_CONFIG_NOT_EXISTS); |