From 08d1d80e51519daba0f5bc27e896a4e0a1dc121e Mon Sep 17 00:00:00 2001 From: wangjiahao <1522128093@qq.com> Date: Wed, 20 Apr 2022 17:25:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BB=AA=E8=A1=A8=E6=9D=BF?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E7=BC=93=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/PanelConstants.java | 26 +++- .../controller/chart/ChartViewController.java | 15 ++- .../request/panel/PanelGroupQueryRequest.java | 19 +++ .../io/dataease/ext/ExtChartViewMapper.java | 6 +- .../io/dataease/ext/ExtChartViewMapper.xml | 119 ++++++++++++++---- .../ext/ExtPanelViewLinkageMapper.xml | 12 +- .../service/chart/ChartViewCacheService.java | 4 + .../service/chart/ChartViewService.java | 77 ++++++------ .../service/panel/PanelGroupService.java | 9 +- .../service/panel/PanelViewService.java | 2 +- frontend/src/api/chart/chart.js | 4 +- frontend/src/views/chart/view/ChartEdit.vue | 12 +- 12 files changed, 213 insertions(+), 92 deletions(-) create mode 100644 backend/src/main/java/io/dataease/controller/request/panel/PanelGroupQueryRequest.java diff --git a/backend/src/main/java/io/dataease/commons/constants/PanelConstants.java b/backend/src/main/java/io/dataease/commons/constants/PanelConstants.java index 9e099c67db..a412b9bddf 100644 --- a/backend/src/main/java/io/dataease/commons/constants/PanelConstants.java +++ b/backend/src/main/java/io/dataease/commons/constants/PanelConstants.java @@ -11,14 +11,10 @@ public class PanelConstants { public final static String COMPONENT_TYPE_PUBLIC = "public"; - public final static String TEMPLATE_TYPE_SYSTEM = "system"; public final static String TEMPLATE_TYPE_SELF = "self"; - public final static String PANEL_TYPE_SYSTEM = "system"; - - public final static String PANEL_TYPE_SELF = "self"; public final static String PANEL_NODE_TYPE_FOlDER = "folder"; @@ -47,4 +43,26 @@ public class PanelConstants { } + //仪表板类型 + public static final class PANEL_TYPE { + + // 普通仪表板 + public static final String SELF = "self"; + + // 默认仪表板 + public static final String SYSTEM = "system"; + + } + + //仪表板查询来源 + public static final class QUERY_FROM { + + // 普通查询 + public static final String NORMAL = "normal"; + + // 编辑查询 + public static final String EDIT = "edit"; + + } + } diff --git a/backend/src/main/java/io/dataease/controller/chart/ChartViewController.java b/backend/src/main/java/io/dataease/controller/chart/ChartViewController.java index 1e69501b6d..8f4604dd22 100644 --- a/backend/src/main/java/io/dataease/controller/chart/ChartViewController.java +++ b/backend/src/main/java/io/dataease/controller/chart/ChartViewController.java @@ -42,7 +42,7 @@ public class ChartViewController { @DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_MANAGE) @ApiOperation("保存") @PostMapping("/save/{panelId}") - public ChartViewDTO save(@PathVariable String panelId, @RequestBody ChartViewCacheRequest request) { + public ChartViewDTO save(@PathVariable String panelId, @RequestBody ChartViewRequest request) { return chartViewService.save(request); } @@ -54,10 +54,10 @@ public class ChartViewController { } @DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_MANAGE) - @ApiOperation("保存到缓存表") - @PostMapping("/save2Cache/{panelId}") - public void save2Cache(@PathVariable String panelId, @RequestBody ChartViewCacheWithBLOBs chartViewWithBLOBs) { - chartViewService.save2Cache(chartViewWithBLOBs); + @ApiOperation("保存编辑的视图信息") + @PostMapping("/viewEditSave/{panelId}") + public void viewEditSave(@PathVariable String panelId, @RequestBody ChartViewWithBLOBs chartViewWithBLOBs) { + chartViewService.viewEditSave(chartViewWithBLOBs); } @ApiIgnore @@ -147,12 +147,11 @@ public class ChartViewController { chartViewService.initViewCache(panelId); } - @DePermission(type = DePermissionType.PANEL, level = ResourceAuthLevel.PANNEL_LEVEL_VIEW, paramIndex = 1) - @ApiOperation("重置视图缓存") + @ApiOperation("重置视图") @PostMapping("/resetViewCache/{id}/{panelId}") public void resetViewCache(@PathVariable String id, @PathVariable String panelId) { - chartViewCacheService.refreshCache(id); + chartViewCacheService.resetView(id); } @ApiOperation("校验视图Title") diff --git a/backend/src/main/java/io/dataease/controller/request/panel/PanelGroupQueryRequest.java b/backend/src/main/java/io/dataease/controller/request/panel/PanelGroupQueryRequest.java new file mode 100644 index 0000000000..d057b5afbe --- /dev/null +++ b/backend/src/main/java/io/dataease/controller/request/panel/PanelGroupQueryRequest.java @@ -0,0 +1,19 @@ +package io.dataease.controller.request.panel; + +import lombok.Data; + +/** + * Author: wangjiahao + * Date: 2022/4/20 + * Description: + */ +@Data +public class PanelGroupQueryRequest { + + private String panelId; + + private String panelType; //仪表板类型 self 普通仪表板 or system默认仪表板 + + private String queryFrom; //查询来源 edit 编辑初始化查询 + +} diff --git a/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.java b/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.java index 62e9a6a54d..b3e5093a27 100644 --- a/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.java +++ b/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.java @@ -35,7 +35,7 @@ public interface ExtChartViewMapper { List searchViewsWithPanelId(@Param("panelId") String panelId); - ChartViewDTO searchOneFromCache(@Param("id") String id ); +// ChartViewDTO searchOneFromCache(@Param("id") String id ); void copyToCache(@Param("id") String id ); @@ -47,7 +47,11 @@ public interface ExtChartViewMapper { int updateToCache(@Param("viewId") String viewId ); + int updateToViewFromCache(@Param("viewId") String viewId ); + void copyCache(@Param("sourceViewId") String sourceViewId,@Param("newViewId") String newViewId); void deleteNoUseView(@Param("viewIds") List viewIds,@Param("panelId") String panelId ); + + void initPanelChartViewCache(@Param("panelId") String panelId); } diff --git a/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.xml b/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.xml index 0c21b19fd7..8b7a937419 100644 --- a/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.xml +++ b/backend/src/main/java/io/dataease/ext/ExtChartViewMapper.xml @@ -20,12 +20,6 @@ from chart_view where id = #{id} - - INSERT INTO chart_view_cache ( id, @@ -152,23 +146,68 @@ chart_view_cache.id = #{sourceViewId} - - - - - - - - - - - - - - - - - + + INSERT INTO chart_view_cache ( + id, + `name`, + title, + scene_id, + table_id, + `type`, + render, + result_count, + result_mode, + create_by, + create_time, + update_time, + style_priority, + chart_type, + is_plugin, + x_axis, + x_axis_ext, + y_axis, + y_axis_ext, + ext_stack, + ext_bubble, + custom_attr, + custom_style, + custom_filter, + drill_fields, + senior, + SNAPSHOT, + data_from + ) SELECT + id, + `name`, + title, + scene_id, + table_id, + `type`, + render, + result_count, + result_mode, + create_by, + create_time, + update_time, + style_priority, + chart_type, + is_plugin, + x_axis, + x_axis_ext, + y_axis, + y_axis_ext, + ext_stack, + ext_bubble, + custom_attr, + custom_style, + custom_filter, + drill_fields, + senior, + SNAPSHOT, + data_from from chart_view + WHERE + chart_view.scene_id = #{panelId} + SELECT - chart_view_cache.`name` as 'targetViewName', - chart_view_cache.id as 'target_view_id', - chart_view_cache.table_id, + chart_view.`name` as 'targetViewName', + chart_view.id as 'target_view_id', + chart_view.table_id, (case when panel_view_linkage.target_view_id is null then 0 else 1 end) as 'linkageActive', panel_view_linkage_field.source_field, panel_view_linkage_field.target_field FROM - chart_view_cache - LEFT JOIN panel_view_linkage ON chart_view_cache.id = panel_view_linkage.target_view_id + chart_view + LEFT JOIN panel_view_linkage ON chart_view.id = panel_view_linkage.target_view_id AND panel_view_linkage.panel_id = #{panelId} AND panel_view_linkage.source_view_id = #{sourceViewId} LEFT JOIN panel_view_linkage_field ON panel_view_linkage.id = panel_view_linkage_field.linkage_id - where chart_view_cache.id in + where chart_view.id in #{targetViewId} diff --git a/backend/src/main/java/io/dataease/service/chart/ChartViewCacheService.java b/backend/src/main/java/io/dataease/service/chart/ChartViewCacheService.java index 866fa92569..a8d0207442 100644 --- a/backend/src/main/java/io/dataease/service/chart/ChartViewCacheService.java +++ b/backend/src/main/java/io/dataease/service/chart/ChartViewCacheService.java @@ -24,4 +24,8 @@ public class ChartViewCacheService { } } + public void resetView(String viewId){ + extChartViewMapper.updateToViewFromCache(viewId); + } + } diff --git a/backend/src/main/java/io/dataease/service/chart/ChartViewService.java b/backend/src/main/java/io/dataease/service/chart/ChartViewService.java index 4d36165f5e..c4ae8443aa 100644 --- a/backend/src/main/java/io/dataease/service/chart/ChartViewService.java +++ b/backend/src/main/java/io/dataease/service/chart/ChartViewService.java @@ -49,6 +49,7 @@ import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.pentaho.di.core.util.UUIDUtil; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -104,23 +105,23 @@ public class ChartViewService { private ReentrantLock lock = new ReentrantLock(); // 直接保存统一到缓存表 - public ChartViewDTO save(ChartViewCacheRequest chartView) { + public ChartViewDTO save(ChartViewRequest chartView) { long timestamp = System.currentTimeMillis(); chartView.setUpdateTime(timestamp); - chartViewCacheMapper.updateByPrimaryKeySelective(chartView); + chartViewMapper.updateByPrimaryKeySelective(chartView); Optional.ofNullable(chartView.getId()).ifPresent(id -> { CacheUtils.remove(JdbcConstants.VIEW_CACHE_KEY, id); }); return getOne(chartView.getId(), "panel_edit"); } - public String checkTitle(ChartViewCacheRequest chartView){ + public String checkTitle(ChartViewCacheRequest chartView) { ChartViewCacheExample example = new ChartViewCacheExample(); example.createCriteria().andTitleEqualTo(chartView.getTitle()).andSceneIdEqualTo(chartView.getSceneId()).andIdNotEqualTo(chartView.getId()); - List result = chartViewCacheMapper.selectByExample(example); - if(CollectionUtils.isNotEmpty(result)){ - return "fail"; - }else{ + List result = chartViewCacheMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(result)) { + return "fail"; + } else { return "success"; } } @@ -130,8 +131,8 @@ public class ChartViewService { // 校验名称 ChartViewExample queryExample = new ChartViewExample(); queryExample.createCriteria().andSceneIdEqualTo(chartView.getSceneId()).andNameEqualTo(chartView.getName()); - List result = chartViewMapper.selectByExample(queryExample); - if(CollectionUtils.isNotEmpty(result)){ + List result = chartViewMapper.selectByExample(queryExample); + if (CollectionUtils.isNotEmpty(result)) { DEException.throwException(Translator.get("theme_name_repeat")); } chartView.setUpdateTime(timestamp); @@ -155,11 +156,14 @@ public class ChartViewService { } - // 直接保存统一到缓存表 - public void save2Cache(ChartViewCacheWithBLOBs chartView) { + /** + * @Description 保存编辑的视图信息 + * @param chartView + */ + public void viewEditSave(ChartViewWithBLOBs chartView) { long timestamp = System.currentTimeMillis(); chartView.setUpdateTime(timestamp); - chartViewCacheMapper.updateByPrimaryKeySelective(chartView); + chartViewMapper.updateByPrimaryKeySelective(chartView); Optional.ofNullable(chartView.getId()).ifPresent(id -> { CacheUtils.remove(JdbcConstants.VIEW_CACHE_KEY, id); }); @@ -250,25 +254,14 @@ public class ChartViewService { } - @Transactional public ChartViewDTO getOne(String id, String queryFrom) { - try{ - ChartViewDTO result; - if(CommonConstants.VIEW_QUERY_FROM.PANEL_EDIT.equals(queryFrom)) { - //仪表板编辑页面 从缓存表中取数据 缓存表中没有数据则进行插入 - result = extChartViewMapper.searchOneFromCache(id); - if (result == null) { - chartViewCacheService.refreshCache(id); - result = extChartViewMapper.searchOneFromCache(id); - } - } else { - result = extChartViewMapper.searchOne(id); - } - if(result==null){ + try { + ChartViewDTO result = extChartViewMapper.searchOne(id); + if (result == null) { DataEaseException.throwException(Translator.get("i18n_chart_delete")); } return result; - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); DataEaseException.throwException(e); } @@ -437,12 +430,12 @@ public class ChartViewService { List filters = new ArrayList<>(); // 联动条件 - if(ObjectUtils.isNotEmpty(requestList.getLinkageFilters())){ + if (ObjectUtils.isNotEmpty(requestList.getLinkageFilters())) { filters.addAll(requestList.getLinkageFilters()); } // 外部参数条件 - if(ObjectUtils.isNotEmpty(requestList.getOuterParamsFilters())){ + if (ObjectUtils.isNotEmpty(requestList.getOuterParamsFilters())) { filters.addAll(requestList.getOuterParamsFilters()); } @@ -517,10 +510,10 @@ public class ChartViewService { // 如果是插件视图 走插件内部的逻辑 if (ObjectUtils.isNotEmpty(view.getIsPlugin()) && view.getIsPlugin()) { Map> fieldMap = new HashMap<>(); - fieldMap.put("xAxis",xAxis); - fieldMap.put("yAxis",yAxis); - fieldMap.put("extStack",extStack); - fieldMap.put("extBubble",extBubble); + fieldMap.put("xAxis", xAxis); + fieldMap.put("yAxis", yAxis); + fieldMap.put("extStack", extStack); + fieldMap.put("extBubble", extBubble); PluginViewParam pluginViewParam = buildPluginParam(fieldMap, fieldCustomFilter, extFilterList, ds, table, view); String sql = pluginViewSql(pluginViewParam, view); if (StringUtils.isBlank(sql)) { @@ -532,7 +525,7 @@ public class ChartViewService { Map mapChart = pluginViewResult(pluginViewParam, view, data, isDrill); Map mapTableNormal = ChartDataBuild.transTableNormal(xAxis, yAxis, view, data, extStack, desensitizationList); - return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal,view, isDrill, drillFilters); + return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal, view, isDrill, drillFilters); // 如果是插件到此结束 } @@ -771,7 +764,7 @@ public class ChartViewService { } // table组件,明细表,也用于导出数据 Map mapTableNormal = ChartDataBuild.transTableNormal(xAxis, yAxis, view, data, extStack, desensitizationList); - return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal,view, isDrill, drillFilters); + return uniteViewResult(datasourceRequest.getQuery(), mapChart, mapTableNormal, view, isDrill, drillFilters); } public ChartViewDTO uniteViewResult(String sql, Map chartData, Map tabelData, ChartViewDTO view, Boolean isDrill, List drillFilters) { @@ -800,8 +793,6 @@ public class ChartViewService { PluginViewLimit pluginViewLimit = BeanUtils.copyBean(new PluginViewLimit(), view); - - List fieldFilters = customFilters.stream().map(filter -> gson.fromJson(gson.toJson(filter), PluginChartFieldCustomFilter.class)).collect(Collectors.toList()); List panelFilters = extFilters.stream().map(filter -> gson.fromJson(gson.toJson(filter), PluginChartExtFilter.class)).collect(Collectors.toList()); @@ -1040,8 +1031,8 @@ public class ChartViewService { public String chartCopy(String id, String panelId) { String newChartId = UUID.randomUUID().toString(); extChartViewMapper.chartCopy(newChartId, id, panelId); - extChartViewMapper.copyCache(id,newChartId); - extPanelGroupExtendDataMapper.copyExtendData(id,newChartId,panelId); + extChartViewMapper.copyCache(id, newChartId); + extPanelGroupExtendDataMapper.copyExtendData(id, newChartId, panelId); chartViewCacheService.refreshCache(newChartId); return newChartId; } @@ -1072,8 +1063,14 @@ public class ChartViewService { } } + /** + * @param panelId + * @Description 初始化仪表板内部视图的cache表 + */ + @Transactional(propagation = Propagation.REQUIRES_NEW) public void initViewCache(String panelId) { - extChartViewMapper.deleteCacheWithPanel(null,panelId); + extChartViewMapper.deleteCacheWithPanel(null, panelId); + extChartViewMapper.initPanelChartViewCache(panelId); } } diff --git a/backend/src/main/java/io/dataease/service/panel/PanelGroupService.java b/backend/src/main/java/io/dataease/service/panel/PanelGroupService.java index f01b5a9631..8022c7749f 100644 --- a/backend/src/main/java/io/dataease/service/panel/PanelGroupService.java +++ b/backend/src/main/java/io/dataease/service/panel/PanelGroupService.java @@ -8,6 +8,7 @@ import io.dataease.commons.utils.LogUtil; import io.dataease.commons.utils.TreeUtils; import io.dataease.controller.request.authModel.VAuthModelRequest; import io.dataease.controller.request.dataset.DataSetTableRequest; +import io.dataease.controller.request.panel.PanelGroupQueryRequest; import io.dataease.controller.request.panel.PanelGroupRequest; import io.dataease.controller.request.panel.PanelViewDetailsRequest; import io.dataease.dto.PanelGroupExtendDataDTO; @@ -131,7 +132,7 @@ public class PanelGroupService { } else if ("toDefaultPanel".equals(request.getOptType())) { // 转存为默认仪表板 panelId = UUID.randomUUID().toString(); PanelGroupWithBLOBs newDefaultPanel = panelGroupMapper.selectByPrimaryKey(request.getId()); - newDefaultPanel.setPanelType(PanelConstants.PANEL_TYPE_SYSTEM); + newDefaultPanel.setPanelType(PanelConstants.PANEL_TYPE.SYSTEM); newDefaultPanel.setNodeType(PanelConstants.PANEL_NODE_TYPE_PANEL); newDefaultPanel.setName(request.getName()); newDefaultPanel.setId(panelId); @@ -221,7 +222,13 @@ public class PanelGroupService { } + /** + * @Description 查询仪表板信息 + * @param panelId + * @return + */ public PanelGroupDTO findOne(String panelId) { + Assert.notNull(panelId,"Method findOne panelId can not be null"); PanelGroupDTO panelGroup = extPanelGroupMapper.findOneWithPrivileges(panelId, String.valueOf(AuthUtils.getUser().getUserId())); // 默认仪表板取源仪表板样式 if (panelGroup != null && StringUtils.isNotEmpty(panelGroup.getSource())) { diff --git a/backend/src/main/java/io/dataease/service/panel/PanelViewService.java b/backend/src/main/java/io/dataease/service/panel/PanelViewService.java index e3edc748ad..2baf1e14f5 100644 --- a/backend/src/main/java/io/dataease/service/panel/PanelViewService.java +++ b/backend/src/main/java/io/dataease/service/panel/PanelViewService.java @@ -125,7 +125,7 @@ public class PanelViewService { extPanelViewMapper.savePanelView(panelViewInsertDTOList); //将视图从cache表中更新到正式表中 viewIds = panelViewInsertDTOList.stream().map(panelView ->panelView.getChartViewId()).collect(Collectors.toList()); - extChartViewMapper.copyCacheToView(viewIds); +// extChartViewMapper.copyCacheToView(viewIds); } extChartViewMapper.deleteCacheWithPanel(viewIds,panelId); extChartViewMapper.deleteNoUseView(viewIds,panelId); diff --git a/frontend/src/api/chart/chart.js b/frontend/src/api/chart/chart.js index aa379278ae..a3de990024 100644 --- a/frontend/src/api/chart/chart.js +++ b/frontend/src/api/chart/chart.js @@ -86,9 +86,9 @@ export function getChartDetails(id, panelId, data) { }) } -export function save2Cache(panelId, data) { +export function viewEditSave(panelId, data) { return request({ - url: '/chart/view/save2Cache/' + panelId, + url: '/chart/view/viewEditSave/' + panelId, method: 'post', loading: false, data diff --git a/frontend/src/views/chart/view/ChartEdit.vue b/frontend/src/views/chart/view/ChartEdit.vue index 7305c32c70..6ef0cf3eac 100644 --- a/frontend/src/views/chart/view/ChartEdit.vue +++ b/frontend/src/views/chart/view/ChartEdit.vue @@ -1160,7 +1160,7 @@ import { ajaxGetDataOnly, post, getChartDetails, - save2Cache, + viewEditSave, resetViewCacheCallBack } from '@/api/chart/chart' import DimensionItem from '../components/drag-item/DimensionItem' @@ -1792,7 +1792,7 @@ export default { this.changeEditStatus(true) const view = this.buildParam(true, 'chart', false, switchType) if (!view) return - save2Cache(this.panelInfo.id, view).then(() => { + viewEditSave(this.panelInfo.id, view).then(() => { bus.$emit('view-in-cache', { type: 'propChange', viewId: this.param.id }) }) }, @@ -1819,7 +1819,7 @@ export default { // 保存到缓存表 const viewSave = this.buildParam(true, 'chart', false, false) if (!viewSave) return - save2Cache(this.panelInfo.id, viewSave) + viewEditSave(this.panelInfo.id, viewSave) bus.$emit('view-in-cache', { type: 'styleChange', viewId: this.param.id, viewInfo: view }) }, @@ -2565,10 +2565,10 @@ export default { }, reset() { - this.changeEditStatus(false) - this.getChart(this.param.id, 'panel') const _this = this - resetViewCacheCallBack(this.param.id, this.panelInfo.id, function(rsp) { + resetViewCacheCallBack(_this.param.id, _this.panelInfo.id, function(rsp) { + _this.changeEditStatus(false) + _this.getChart(_this.param.id, 'panel') bus.$emit('view-in-cache', { type: 'propChange', viewId: _this.param.id }) }) },