From ee9f604388a3e77d3f4654e326f3976552e7f532 Mon Sep 17 00:00:00 2001
From: Jay <csj123456>
Date: 星期一, 25 十一月 2024 13:40:07 +0800
Subject: [PATCH] 测点启用和禁用

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/controller/admin/ChannelOPCUATagController.java |   80 +++++++++++++++++++++++++++++++++------
 1 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/controller/admin/ChannelOPCUATagController.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/controller/admin/ChannelOPCUATagController.java
index 981ef30..8c85b89 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/controller/admin/ChannelOPCUATagController.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/opcua/controller/admin/ChannelOPCUATagController.java
@@ -1,24 +1,45 @@
 package com.iailab.module.data.channel.opcua.controller.admin;
 
+import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
 import com.iailab.framework.common.pojo.CommonResult;
+import com.iailab.framework.common.pojo.PageParam;
 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.framework.excel.core.util.ExcelUtils;
+import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
 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.OpcUaTagExportExcelVO;
+import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO;
 import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
 import com.iailab.module.data.channel.opcua.vo.OpcUaTagRespVO;
+import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
+import com.iailab.module.data.common.enums.DataQualityEnum;
+import com.iailab.module.data.common.enums.IsEnableEnum;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.Parameters;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.IOException;
+import java.util.Collections;
 import java.util.Date;
+import java.util.List;
 import java.util.UUID;
 
+import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
 import static com.iailab.framework.common.pojo.CommonResult.success;
 
 /**
  * 操作opcua tag配置
+ *
  * @author lirm
  * @Description
  * @createTime 2024年08月26日
@@ -29,11 +50,20 @@
     @Resource
     private ChannelOPCUATagService channelOpcuaTagService;
 
+    @Resource
+    private OpcUaCollector opcUaCollector;
+
     @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
     @GetMapping("page")
     public CommonResult<PageResult<OpcUaTagRespVO>> list(@Valid OpcUaTagPageReqVO reqVO) {
         PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
-        return success(BeanUtils.toBean(page, OpcUaTagRespVO.class));
+        PageResult<OpcUaTagRespVO> pageResult = BeanUtils.toBean(page, OpcUaTagRespVO.class);
+        pageResult.getList().forEach(item -> {
+            item.setDataValue(opcUaCollector.getTagValue(reqVO.getDeviceId(), item.getTagName()));
+            item.setDataTime(new Date());
+            item.setDataQuality(DataQualityEnum.getEumByValue(item.getDataValue()).getDesc());
+        });
+        return success(pageResult);
     }
 
     @PreAuthorize("@ss.hasPermission('data:channel-opcua:query')")
@@ -68,16 +98,40 @@
         return success(true);
     }
 
-//    @PostMapping("/import/{device}")
-//    public R importTag(@PathVariable("device") String device, @RequestParam("file") MultipartFile file) {
-//        try {
-//            if (file.isEmpty()) {
-//                throw new RRException("上传文件不能为空");
-//            }
-//            channelOpcuaTagService.importTag(device, file);
-//        } catch (Exception ex) {
-//            return R.error(ex.getMessage());
-//        }
-//        return R.ok();
-//    }
+    @GetMapping("/export")
+    @Operation(summary = "导出modbus tag列表")
+    @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:export')")
+    @ApiAccessLog(operateType = EXPORT)
+    public void exportPointList(@Validated OpcUaTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
+        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
+        PageResult<ChannelOPCUATagEntity> page = channelOpcuaTagService.queryPage(reqVO);
+        List<OpcUaTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), OpcUaTagExportExcelVO.class);
+        ExcelUtils.write(response, "tag列表.xls", "数据", OpcUaTagExportExcelVO.class, list, true);
+    }
+
+    @GetMapping("/get-import-template")
+    @Operation(summary = "获得tag导入模板")
+    public void importTemplate(HttpServletResponse response) throws IOException {
+        // 手动创建导出 demo
+        List<OpcUaTagImportExcelVO> list = Collections.singletonList(
+                OpcUaTagImportExcelVO.builder().tagName("Tag名称").dataType("String").address("123").samplingRate(1000).enabled(IsEnableEnum.ENABLE.getCode())
+                        .build()
+        );
+        // 输出
+        ExcelUtils.write(response, "tag导入模板.xls", "tag列表", OpcUaTagImportExcelVO.class, list, true);
+    }
+
+    @PostMapping("/import")
+    @Operation(summary = "导入tag")
+    @Parameters({
+            @Parameter(name = "file", description = "Excel 文件", required = true),
+            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
+    })
+    @PreAuthorize("@ss.hasPermission('data:channel-opcua-tag:import')")
+    public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
+                                                     @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
+                                                     @RequestParam("device") String device) throws Exception {
+        List<OpcUaTagImportExcelVO> list = ExcelUtils.read(file, OpcUaTagImportExcelVO.class);
+        return success(channelOpcuaTagService.importOpcUaTagList(list, updateSupport, device));
+    }
 }

--
Gitblit v1.9.3