Jelajahi Sumber

feat(adm-diagram): 图标相关接口逻辑变更

liyang 3 tahun lalu
induk
melakukan
30f38c948d

+ 47 - 4
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/controller/DiagramController.java

@@ -1,11 +1,14 @@
 package com.persagy.adm.diagram.controller;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.persagy.adm.diagram.core.ContentParser;
 import com.persagy.adm.diagram.core.DataStrategy;
 import com.persagy.adm.diagram.core.model.Diagram;
 import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
 import com.persagy.adm.diagram.frame.BdtpRequest;
 import com.persagy.adm.diagram.manage.DemoDiagramManager;
+import com.persagy.dmp.common.constant.ResponseCode;
+import com.persagy.dmp.common.exception.BusinessException;
 import com.persagy.dmp.common.model.response.CommonResult;
 import com.persagy.dmp.common.utils.ResultHelper;
 import io.swagger.annotations.ApiOperation;
@@ -16,9 +19,12 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * 系统图相关接口
@@ -36,6 +42,8 @@ public class DiagramController {
     private DataStrategy dataStrategy;
     @Autowired
     private DemoDiagramManager diagramManager;
+    @Autowired
+    private ContentParser parser;
 
     /**
      * 系统图列表接口
@@ -57,14 +65,49 @@ public class DiagramController {
     }
 
     /**
-     * 新增系统图
+     * 新增或修改系统图
      *
-     * @param diagram 系统图对象信息
-     * @return 是否成功
+     * @param params    参数集合
+     * @param projectId 项目id
+     * @param systemId  系统实例id
+     * @param groupCode 集团code
+     * @return 系统图信息
      */
     @ApiOperation("新增系统图")
     @PostMapping("/newDiagram")
-    public CommonResult<Diagram> newDiagram(@RequestBody Diagram diagram) {
+    public CommonResult<Diagram> newDiagram(@RequestBody Map<String, Object> params,
+                                            @RequestParam String projectId,
+                                            @RequestParam(required = false) String systemId,
+                                            @RequestParam String groupCode) {
+        Diagram diagram = new Diagram();
+        diagram.setGroupCode(groupCode);
+        diagram.setProjectId(projectId);
+        diagram.setSystemId(systemId);
+
+        Optional.ofNullable(params).ifPresent(map -> {
+            Optional.ofNullable(map.get("id")).ifPresent(o -> diagram.setId(String.valueOf(o)));
+            Optional.ofNullable(map.get("type")).ifPresent(o -> {
+                if (String.valueOf(o).length() < 4) {
+                    throw new BusinessException(ResponseCode.A0400.getCode(), "type参数非法");
+                }
+                diagram.setType(String.valueOf(o));
+                diagram.setSystem(String.valueOf(o).substring(0, 4));
+            });
+            Optional.ofNullable(map.get("name")).ifPresent(o -> diagram.setName(String.valueOf(o)));
+            Optional.ofNullable(map.get("remark")).ifPresent(o -> diagram.setRemark(String.valueOf(o)));
+            Optional.ofNullable(map.get("templateId")).ifPresent(o -> diagram.setTemplateId(String.valueOf(o)));
+            Optional.ofNullable(map.get("extraProps")).ifPresent(s -> {
+                Map<String, Object> m = parser.parseContent(parser.toJson(s), Map.class);
+                diagram.setExtraProp("state", m.get("state"));
+            });
+            Optional.ofNullable(map.get("lines")).ifPresent(lines ->
+                    diagram.setLines(parser.parseContent(parser.toJson(lines), List.class)));
+            Optional.ofNullable(map.get("nodes")).ifPresent(nodes ->
+                    diagram.setNodes(parser.parseContent(parser.toJson(nodes), List.class)));
+            Optional.ofNullable(map.get("template")).ifPresent(s ->
+                    diagram.setTemplate(parser.parseContent(parser.toJson(s), DiagramTemplate.class)));
+        });
+
         return ResultHelper.single(dataStrategy.saveDiagram(diagram));
     }
 

+ 25 - 18
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/controller/IconController.java

@@ -1,5 +1,6 @@
 package com.persagy.adm.diagram.controller;
 
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpUtil;
 import com.persagy.adm.diagram.entity.IconEntity;
 import com.persagy.adm.diagram.frame.BdtpRequest;
@@ -21,11 +22,9 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.imageio.ImageIO;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.awt.*;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
@@ -76,30 +75,32 @@ public class IconController {
             entity.setIconName(name);
             entity.setIconType(fileType);
         });
-
         InputStream inputStream = null;
-        Image img = null;
         try {
             inputStream = file.getInputStream();
-            img = ImageIO.read(file.getInputStream());
         } catch (IOException e) {
             e.printStackTrace();
         }
-        boolean isImage = img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0;
-        if (null == inputStream || isImage) {
+        if (null == inputStream) {
             throw new BusinessException(ResponseCode.A0400.getCode(), "文件不能为空,并且必须是图片!");
         }
-
-        FileInfo fileInfo = FileInfoCreator.of(BdtpRequest.getCurrent().getGroupCode(), null,
-                null, entity.getIconName());
-        String fileId = FileStorageHelper.uploadFile(fileInfo, inputStream);
+        //如果图例存在(已上传),那么需要替换
+        IconEntity icon = iconService.getIconsByLegendId(legendId, skin);
+        if (null != icon) {
+            String s = FileStorageHelper.replaceFile(icon.getIconFileId(), entity.getIconName(), inputStream);
+            entity.setId(icon.getId());
+            entity.setIconFileId(s);
+        } else {
+            FileInfo fileInfo = FileInfoCreator.of(BdtpRequest.getCurrent().getGroupCode(), null,
+                    null, entity.getIconName());
+            String fileId = FileStorageHelper.uploadFile(fileInfo, inputStream);
+            entity.setIconFileId(fileId);
+        }
         try {
             inputStream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
-        entity.setIconFileId(fileId);
-
         return ResultHelper.single(iconService.saveIcon(entity));
 
     }
@@ -124,7 +125,13 @@ public class IconController {
         byte[] bytes = HttpUtil.downloadBytes(fileInfo.getFileDownloadUrl());
         ServletOutputStream out = null;
         try {
-            response.setHeader("Content-Type", "image/" + info.getIconType());
+            //svg格式文件的Content-Type:svg+xml;charset=utf-8   text/xml ,都可以
+            if (StrUtil.equalsIgnoreCase(info.getIconType(), "svg")) {
+                //response.setHeader("Content-Type", "image/svg+xml;charset=utf-8");
+                response.setHeader("Content-Type", "text/xml");
+            } else {
+                response.setHeader("Content-Type", "image/" + info.getIconType());
+            }
             out = response.getOutputStream();
             out.write(bytes);
             out.flush();
@@ -142,15 +149,15 @@ public class IconController {
     }
 
     /**
-     * 根据图例id获取图标列表
+     * 根据图例id获取图标
      *
      * @param legendId 图例id
      * @return
      */
-    @ApiOperation("根据图例id获取图标列表")
+    @ApiOperation("根据图例id获取图")
     @GetMapping("/icons/legend")
-    public CommonResult<List<IconEntity>> getIconsByLegendId(String legendId) {
-        return ResultHelper.multi(iconService.getIconsByLegendId(legendId));
+    public CommonResult<IconEntity> getIconsByLegendId(String legendId) {
+        return ResultHelper.single(iconService.getIconsByLegendId(legendId, null));
     }
 
     /**

+ 2 - 1
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/dao/IconMapper.java

@@ -18,9 +18,10 @@ public interface IconMapper extends BaseMapper<IconEntity> {
      * 获取某个图例下的所有图标
      *
      * @param legendId 图例id
+     * @param skin     图例皮肤
      * @return
      */
-    List<IconEntity> getIconsByLegendId(String legendId);
+    IconEntity getIconsByLegendId(String legendId, String skin);
 
     /**
      * 获取所有图标

+ 80 - 70
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/entity/ModelAdapter.java

@@ -1,93 +1,103 @@
 package com.persagy.adm.diagram.entity;
 
-import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
 import com.persagy.adm.diagram.core.ContentParser;
 import com.persagy.adm.diagram.core.model.Diagram;
 import com.persagy.adm.diagram.core.model.legend.Legend;
+import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.Map;
+
 /**
  * 运行时模型-实体 类型转换适配器
+ *
  * @author zhaoyk
  */
 @Service
 public class ModelAdapter {
 
-	@Autowired
-	private ContentParser parser;
+    @Autowired
+    private ContentParser parser;
+
+    public DiagramTemplate toTemplate(TemplateEntity templateEntity) {
+        DiagramTemplate template = parser.parseContent(templateEntity.getTemplateContent(), DiagramTemplate.class);
+        template.setId(templateEntity.getId());
+        template.setName(templateEntity.getName());
+        template.setCode(templateEntity.getCode());
+        template.setRemark(templateEntity.getRemark());
+        template.setDiagramType(templateEntity.getDiagramType());
+        return template;
+    }
 
-	public DiagramTemplate toTemplate(TemplateEntity templateEntity){
-		DiagramTemplate template = parser.parseContent(templateEntity.getTemplateContent(), DiagramTemplate.class);
-		template.setId(templateEntity.getId());
-		template.setName(templateEntity.getName());
-		template.setCode(templateEntity.getCode());
-		template.setRemark(templateEntity.getRemark());
-		template.setDiagramType(templateEntity.getDiagramType());
-		return template;
-	}
+    public TemplateEntity toTemplateEntity(DiagramTemplate template) {
+        TemplateEntity entity = new TemplateEntity();
+        entity.setId(template.getId());
+        entity.setName(template.getName());
+        entity.setCode(template.getCode());
+        entity.setRemark(template.getRemark());
+        entity.setDiagramType(template.getDiagramType());
+        entity.setTemplateContent(parser.toJson(template));
+        return entity;
+    }
 
-	public TemplateEntity toTemplateEntity(DiagramTemplate template){
-		TemplateEntity entity = new TemplateEntity();
-		entity.setId(template.getId());
-		entity.setName(template.getName());
-		entity.setCode(template.getCode());
-		entity.setRemark(template.getRemark());
-		entity.setDiagramType(template.getDiagramType());
-		entity.setTemplateContent(parser.toJson(template));
-		return entity;
-	}
+    public Legend toLegend(LegendEntity legendEntity) {
+        Legend legend = parser.parseContent(legendEntity.getLegendContent(), Legend.class);
+        legend.setId(legendEntity.getId());
+        legend.setName(legendEntity.getName());
+        legend.setCode(legendEntity.getCode());
+        legend.setRemark(legendEntity.getRemark());
+        legend.setSystem(legendEntity.getBtSystem());
+        return legend;
+    }
 
-	public Legend toLegend(LegendEntity legendEntity){
-		Legend legend = parser.parseContent(legendEntity.getLegendContent(), Legend.class);
-		legend.setId(legendEntity.getId());
-		legend.setName(legendEntity.getName());
-		legend.setCode(legendEntity.getCode());
-		legend.setRemark(legendEntity.getRemark());
-		legend.setSystem(legendEntity.getBtSystem());
-		return legend;
-	}
+    public LegendEntity toLegendEntity(Legend legend) {
+        LegendEntity entity = new LegendEntity();
+        entity.setId(legend.getId());
+        entity.setName(legend.getName());
+        entity.setCode(legend.getCode());
+        entity.setRemark(legend.getRemark());
+        entity.setBtSystem(legend.getSystem());
+        entity.setLegendContent(parser.toJson(legend));
+        return entity;
+    }
 
-	public LegendEntity toLegendEntity(Legend legend) {
-		LegendEntity entity = new LegendEntity();
-		entity.setId(legend.getId());
-		entity.setName(legend.getName());
-		entity.setCode(legend.getCode());
-		entity.setRemark(legend.getRemark());
-		entity.setBtSystem(legend.getSystem());
-		entity.setLegendContent(parser.toJson(legend));
-		return entity;
-	}
+    public Diagram toDiagram(DiagramEntity diagramEntity) {
+        //Diagram diagram = parser.parseContent(diagramEntity.getDiagramContent(), Diagram.class);
+        Diagram diagram = new Diagram();
+        Map map = parser.parseContent(diagramEntity.getDiagramContent(), Map.class);
+        diagram.setLines(parser.parseContent(parser.toJson(map.get("lines")), List.class));
+        diagram.setNodes(parser.parseContent(parser.toJson(map.get("nodes")), List.class));
+        diagram.setTemplate(parser.parseContent(parser.toJson(map.get("template")), DiagramTemplate.class));
 
-	public Diagram toDiagram(DiagramEntity diagramEntity) {
-		Diagram diagram = parser.parseContent(diagramEntity.getDiagramContent(), Diagram.class);
-		diagram.setId(diagramEntity.getId());
-		diagram.setName(diagramEntity.getName());
-		diagram.setRemark(diagramEntity.getRemark());
-		diagram.setType(diagramEntity.getType());
-		diagram.setTemplateId(diagramEntity.getTemplateId());
-		diagram.setSystem(diagramEntity.getBtSystem());
-		diagram.setProjectId(diagramEntity.getProjectId());
-		diagram.setSystemId(diagramEntity.getSystemId());
-		diagram.setGroupCode(diagramEntity.getGroupCode());
-		diagram.setExtraProp("state", diagramEntity.getState());
-		return diagram;
-	}
+        diagram.setId(diagramEntity.getId());
+        diagram.setName(diagramEntity.getName());
+        diagram.setRemark(diagramEntity.getRemark());
+        diagram.setType(diagramEntity.getType());
+        diagram.setTemplateId(diagramEntity.getTemplateId());
+        diagram.setSystem(diagramEntity.getBtSystem());
+        diagram.setProjectId(diagramEntity.getProjectId());
+        diagram.setSystemId(diagramEntity.getSystemId());
+        diagram.setGroupCode(diagramEntity.getGroupCode());
+        diagram.setExtraProp("state", diagramEntity.getState());
+        return diagram;
+    }
 
-	public DiagramEntity toDiagramEntity(Diagram diagram) {
-		DiagramEntity entity = new DiagramEntity();
-		entity.setId(diagram.getId());
-		entity.setName(diagram.getName());
-		entity.setRemark(diagram.getRemark());
-		entity.setType(diagram.getType());
-		entity.setTemplateId(diagram.getTemplateId());
-		entity.setBtSystem(diagram.getSystem());
-		entity.setProjectId(diagram.getProjectId());
-		entity.setSystemId(diagram.getSystemId());
-		entity.setGroupCode(diagram.getGroupCode());
-		entity.setState((String) diagram.getExtraProp("state"));
-		entity.setDiagramContent(parser.toJson(diagram));
-		return entity;
-	}
+    public DiagramEntity toDiagramEntity(Diagram diagram) {
+        DiagramEntity entity = new DiagramEntity();
+        entity.setId(diagram.getId());
+        entity.setName(diagram.getName());
+        entity.setRemark(diagram.getRemark());
+        entity.setType(diagram.getType());
+        entity.setTemplateId(diagram.getTemplateId());
+        entity.setBtSystem(diagram.getSystem());
+        entity.setProjectId(diagram.getProjectId());
+        entity.setSystemId(diagram.getSystemId());
+        entity.setGroupCode(diagram.getGroupCode());
+        entity.setState((String) diagram.getExtraProp("state"));
+        entity.setDiagramContent(parser.toJson(diagram));
+        return entity;
+    }
 
 }

+ 2 - 1
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/service/IconService.java

@@ -32,9 +32,10 @@ public interface IconService {
      * 根据图例id获取所有图标
      *
      * @param legendId 图例id
+     * @param skin     图例皮肤
      * @return
      */
-    List<IconEntity> getIconsByLegendId(String legendId);
+    IconEntity getIconsByLegendId(String legendId, String skin);
 
     /**
      * 获取所有图标

+ 11 - 5
adm-business/adm-diagram/src/main/java/com/persagy/adm/diagram/service/impl/IconServiceImpl.java

@@ -1,9 +1,9 @@
 package com.persagy.adm.diagram.service.impl;
 
 import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.StrUtil;
 import com.persagy.adm.diagram.dao.IconMapper;
 import com.persagy.adm.diagram.entity.IconEntity;
-import com.persagy.adm.diagram.frame.BdtpRequest;
 import com.persagy.adm.diagram.service.IconService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -24,10 +24,15 @@ public class IconServiceImpl implements IconService {
      */
     @Override
     public IconEntity saveIcon(IconEntity entity) {
-
+        if (StrUtil.isNotBlank(entity.getId())) {
+            IconEntity icon = iconMapper.selectById(entity.getId());
+            if (null != icon) {
+                iconMapper.updateById(entity);
+                return entity;
+            }
+        }
         entity.setId(IdUtil.simpleUUID());
         iconMapper.insert(entity);
-
         return entity;
     }
 
@@ -37,14 +42,15 @@ public class IconServiceImpl implements IconService {
     }
 
     @Override
-    public List<IconEntity> getIconsByLegendId(String legendId) {
-        return iconMapper.getIconsByLegendId(legendId);
+    public IconEntity getIconsByLegendId(String legendId, String skin) {
+        return iconMapper.getIconsByLegendId(legendId, skin);
     }
 
     @Override
     public List<IconEntity> getIcons() {
         return iconMapper.getIcons();
     }
+
     /**
      * 根据系统类型获取所有图标
      *

+ 3 - 2
adm-business/adm-diagram/src/main/resources/db.init/schema.sql

@@ -86,12 +86,13 @@ CREATE TABLE IF NOT EXISTS `diagram_icon` (
   `system_type` varchar(40) DEFAULT NULL COMMENT '图标所属系统类型',
   `icon_size` varchar(255) DEFAULT NULL COMMENT '图标大小',
   `icon_file_id` varchar(255) NOT NULL COMMENT '图标对应的文件id',
-  `icon_skin` varchar(255) DEFAULT NULL COMMENT '图标皮肤',
+  `icon_skin` varchar(255) DEFAULT null COMMENT '图标皮肤',
   `creator` varchar(32) DEFAULT NULL COMMENT '创建人',
   `creation_time` timestamp NULL DEFAULT NULL COMMENT '创建时间',
   `modifier` varchar(32) DEFAULT NULL COMMENT '最后修改人',
   `modified_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
   `valid` tinyint DEFAULT 1 NOT NULL COMMENT '合法标识',
   `ts` timestamp default current_timestamp on update current_timestamp NOT NULL COMMENT '乐观锁',
-  PRIMARY KEY (`id`) USING BTREE
+  PRIMARY KEY (`id`) USING BTREE,
+  UNIQUE KEY `idx_legendId_skin`(`legend_id`, `icon_skin`) USING BTREE
 ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC COMMENT='图标表结构';

+ 3 - 0
adm-business/adm-diagram/src/main/resources/mapper/DiagramIcon.xml

@@ -29,6 +29,9 @@
         <include refid="baseSql"/>
         FROM diagram_icon
         WHERE valid = 1 AND legend_id=#{legendId}
+        <if test="null != skin and skin.length > 0">
+            AND icon_skin=#{skin}
+        </if>
     </select>
 
     <select id="getIcons" resultMap="iconBaseMap">