From db184afd0c5bf3359b44eb0251fa5b07386eb3ff Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期三, 06 十一月 2024 09:48:13 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/service/impl/ChannelOPCUATagServiceImpl.java |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/service/impl/ChannelOPCUATagServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/service/impl/ChannelOPCUATagServiceImpl.java
index 0e0f9f0..074aae8 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/service/impl/ChannelOPCUATagServiceImpl.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/service/impl/ChannelOPCUATagServiceImpl.java
@@ -1,15 +1,21 @@
 package com.iailab.module.data.channel.opcua.service.impl;
 
+import cn.hutool.core.collection.CollUtil;
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.iailab.framework.common.pojo.PageResult;
+import com.iailab.framework.common.util.object.BeanUtils;
 import com.iailab.framework.common.util.object.ConvertUtils;
 import com.iailab.module.data.channel.opcua.dao.ChannelOPCUATagDao;
 import com.iailab.module.data.channel.opcua.dto.ChannelOPCUATagDTO;
 import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
 import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
+import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO;
 import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
+import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
+import com.iailab.module.data.common.enums.CommonConstant;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -17,15 +23,15 @@
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.FileInputStream;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
+
+import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
+import static com.iailab.module.data.enums.ErrorCodeConstants.*;
 
 /**
  * @author lirm
@@ -93,7 +99,7 @@
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
+    @DSTransactional(rollbackFor = Exception.class)
     public void importTag(String device, MultipartFile file) throws Exception {
         try {
             String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
@@ -122,7 +128,7 @@
                 }else if(row.getCell(3).getStringCellValue().equals("3")){
                     tagEntity.setAddress(String.format("4%04d",Integer.parseInt(row.getCell(4).getStringCellValue())));
                 }
-                tagEntity.setEnabled(true);
+                tagEntity.setEnabled(1);
                 tagEntity.setDevice(device);
                 tagEntity.setSamplingRate(1);
                 dangerList.add(tagEntity);
@@ -144,4 +150,44 @@
             throw ex;
         }
     }
+
+    @Override
+    public TagImportRespVO importOpcUaTagList(List<OpcUaTagImportExcelVO> importTags, boolean isUpdateSupport, String device) {
+        // 1.1 参数校验
+        if (CollUtil.isEmpty(importTags)) {
+            throw exception(TAG_IMPORT_LIST_IS_EMPTY);
+        }
+        // 2. 遍历,逐个创建 or 更新
+        TagImportRespVO respVO = TagImportRespVO.builder().createTagNames(new ArrayList<>())
+                .updateTagNames(new ArrayList<>()).failureTagNames(new LinkedHashMap<>()).build();
+        importTags.forEach(importTag -> {
+            // 判断如果不存在,再进行插入
+            ChannelOPCUATagEntity existTag = channelOPCUATagDao.selectOne(new QueryWrapper<ChannelOPCUATagEntity>()
+                    .eq("device", device)
+                    .eq("tag_name",importTag.getTagName()));
+            if (existTag == null) {
+                ChannelOPCUATagEntity channelOpCuaTagEntity = ConvertUtils.sourceToTarget(importTag, ChannelOPCUATagEntity.class);
+                channelOpCuaTagEntity.setId(UUID.randomUUID().toString());
+                channelOpCuaTagEntity.setEnabled(CommonConstant.IS_ENABLE);
+                channelOpCuaTagEntity.setDevice(device);
+                channelOpCuaTagEntity.setCreateTime(new Date());
+                channelOPCUATagDao.insert(channelOpCuaTagEntity);
+
+                respVO.getCreateTagNames().add(channelOpCuaTagEntity.getTagName());
+                return;
+            }
+
+            // 如果存在,判断是否允许更新
+            if (!isUpdateSupport) {
+                respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg());
+                return;
+            }
+
+            ChannelOPCUATagEntity updateTag = BeanUtils.toBean(importTag, ChannelOPCUATagEntity.class);
+            updateTag.setId(existTag.getId());
+            baseMapper.updateById(updateTag);
+            respVO.getUpdateTagNames().add(importTag.getTagName());
+        });
+        return respVO;
+    }
 }

--
Gitblit v1.9.3