dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
提交 | 用户 | 时间
a477ef 1 package com.iailab.module.data.channel.opcda.controller.admin;
9d7e02 2
03e8ac 3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
aecc49 4 import com.iailab.framework.common.pojo.CommonResult;
03e8ac 5 import com.iailab.framework.common.pojo.PageParam;
aecc49 6 import com.iailab.framework.common.pojo.PageResult;
L 7 import com.iailab.framework.common.util.object.BeanUtils;
e8ad66 8 import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
D 9 import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity;
03e8ac 10 import com.iailab.framework.common.util.object.ConvertUtils;
J 11 import com.iailab.framework.excel.core.util.ExcelUtils;
9d7e02 12 import com.iailab.module.data.channel.opcda.entity.ChannelOPCDATagEntity;
e8ad66 13 import com.iailab.module.data.channel.opcda.service.ChannelOPCDADeviceService;
9d7e02 14 import com.iailab.module.data.channel.opcda.service.ChannelOPCDATagService;
03e8ac 15 import com.iailab.module.data.channel.opcda.vo.OpcDaTagExportExcelVO;
J 16 import com.iailab.module.data.channel.opcda.vo.OpcDaTagImportExcelVO;
aecc49 17 import com.iailab.module.data.channel.opcda.vo.OpcDaTagPageReqVO;
L 18 import com.iailab.module.data.channel.opcda.vo.OpcDaTagRespVO;
e8ad66 19 import com.iailab.module.data.common.enums.DataSourceType;
03e8ac 20 import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
9d7e02 21 import com.iailab.module.data.common.exception.RRException;
e8ad66 22 import com.iailab.module.data.common.utils.TagUtils;
03e8ac 23 import io.swagger.v3.oas.annotations.Operation;
J 24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.Parameters;
9d7e02 26 import org.springframework.beans.factory.annotation.Autowired;
a477ef 27 import org.springframework.security.access.prepost.PreAuthorize;
03e8ac 28 import org.springframework.validation.annotation.Validated;
9d7e02 29 import org.springframework.web.bind.annotation.*;
30 import org.springframework.web.multipart.MultipartFile;
31
03e8ac 32 import javax.servlet.http.HttpServletResponse;
aecc49 33 import javax.validation.Valid;
e8ad66 34 import java.util.ArrayList;
03e8ac 35 import java.io.IOException;
J 36 import java.util.Collections;
aecc49 37 import java.util.Date;
e8ad66 38 import java.util.List;
9d7e02 39 import java.util.UUID;
e8ad66 40 import java.util.stream.Collectors;
aecc49 41
03e8ac 42 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
aecc49 43 import static com.iailab.framework.common.pojo.CommonResult.success;
9d7e02 44
45 /**
46  * 操作OPCDA tag配置
aecc49 47  * @author lirm
L 48  * @Description
49  * @createTime 2024年08月26日
9d7e02 50  */
51 @RestController
aecc49 52 @RequestMapping("/data/channel/opcda/tag")
9d7e02 53 public class ChannelOPCDATagController {
54     @Autowired
55     private ChannelOPCDATagService channelOPCDATagService;
a477ef 56
e8ad66 57     @Autowired
D 58     private OpcDACollector opcDACollector;
59
60     @Autowired
61     private ChannelOPCDADeviceService channelOPCDADeviceService;
62
03e8ac 63     @PreAuthorize("@ss.hasPermission('data:channel-opcda:query')")
aecc49 64     @GetMapping("page")
L 65     public CommonResult<PageResult<OpcDaTagRespVO>> list(@Valid OpcDaTagPageReqVO reqVO) {
e8ad66 66
aecc49 67         PageResult<ChannelOPCDATagEntity> page = channelOPCDATagService.queryPage(reqVO);
e8ad66 68         PageResult<OpcDaTagRespVO> pageResultVO = new PageResult<>();
D 69         pageResultVO.setTotal(page.getTotal());
70
71         List<OpcDaTagRespVO> vos = page.getList().stream().map(entity -> {
72
73             OpcDaTagRespVO vo = BeanUtils.toBean(entity,OpcDaTagRespVO.class);
74             List<String[]> tags = new ArrayList<>();
75             String[] array  = {reqVO.getServerId(),entity.getTagName()};
76             tags.add(array);
77             ChannelOPCDADeviceEntity OPCDADevice = channelOPCDADeviceService.info(reqVO.getServerId());
78             vo.setDataValue(Double.parseDouble(opcDACollector.getTagValues(tags).get(TagUtils.genTagId(DataSourceType.OPCDA.getCode(), OPCDADevice.getServerName(),entity.getTagName())).toString()));
79             return vo;
80         }).collect(Collectors.toList());
81
82         pageResultVO.setList(vos);
83
84         return success(pageResultVO);
9d7e02 85     }
aecc49 86
03e8ac 87     @PreAuthorize("@ss.hasPermission('data:channel-opcda:query')")
9d7e02 88     @GetMapping("/info/{id}")
aecc49 89     public CommonResult<ChannelOPCDATagEntity> info(@PathVariable("id") String id) {
L 90         ChannelOPCDATagEntity info = channelOPCDATagService.info(id);
91         return success(info);
9d7e02 92     }
aecc49 93
03e8ac 94     @PreAuthorize("@ss.hasPermission('data:channel-opcda:create')")
a477ef 95     @PostMapping("/create")
96     public CommonResult<Boolean> create(@RequestBody ChannelOPCDATagEntity channelOPCDATagEntity) {
aecc49 97         String id = UUID.randomUUID().toString();
L 98         channelOPCDATagEntity.setId(id);
99         channelOPCDATagEntity.setCreateTime(new Date());
100         channelOPCDATagService.add(channelOPCDATagEntity);
101         return success(true);
9d7e02 102     }
103
03e8ac 104     @PreAuthorize("@ss.hasPermission('data:channel-opcda:update')")
aecc49 105     @PutMapping("/update")
L 106     public CommonResult<Boolean> update(@RequestBody ChannelOPCDATagEntity channelOPCDATagEntity) {
107         channelOPCDATagEntity.setUpdateTime(new Date());
9d7e02 108         channelOPCDATagService.update(channelOPCDATagEntity);
aecc49 109         return success(true);
9d7e02 110     }
111
03e8ac 112     @PreAuthorize("@ss.hasPermission('data:channel-opcda:delete')")
aecc49 113     @DeleteMapping("/delete")
L 114     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
9d7e02 115         channelOPCDATagService.delete(id);
aecc49 116         return success(true);
9d7e02 117     }
a477ef 118
9d7e02 119     @PostMapping("/import/{serverId}")
aecc49 120     public CommonResult<String> importTag(@PathVariable("serverId") String serverId, @RequestParam("file") MultipartFile file) {
9d7e02 121         try {
122             if (file.isEmpty()) {
123                 throw new RRException("上传文件不能为空");
124             }
125             channelOPCDATagService.importTag(serverId, file);
126         } catch (Exception ex) {
aecc49 127             ex.getMessage();
9d7e02 128         }
aecc49 129         return success("上传成功");
9d7e02 130     }
03e8ac 131
J 132     @GetMapping("/export")
133     @Operation(summary = "导出modbus tag列表")
134     @PreAuthorize("@ss.hasPermission('data:channel-opcda-tag:export')")
135     @ApiAccessLog(operateType = EXPORT)
136     public void exportPointList(@Validated OpcDaTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
137         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
138         PageResult<ChannelOPCDATagEntity> page = channelOPCDATagService.queryPage(reqVO);
139         List<OpcDaTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), OpcDaTagExportExcelVO.class);
140         ExcelUtils.write(response, "tag列表.xls", "数据", OpcDaTagExportExcelVO.class, list, true);
141     }
142
143
144     @GetMapping("/get-import-template")
145     @Operation(summary = "获得tag导入模板")
146     public void importTemplate(HttpServletResponse response) throws IOException {
147         // 手动创建导出 demo
148         List<OpcDaTagImportExcelVO> list = Collections.singletonList(
149                 OpcDaTagImportExcelVO.builder().tagName("Tag名称").dataType("String").enabled(1)
150                         .build()
151         );
152         // 输出
153         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", OpcDaTagImportExcelVO.class, list,true);
154     }
155
156     @PostMapping("/import")
157     @Operation(summary = "导入tag")
158     @Parameters({
159             @Parameter(name = "file", description = "Excel 文件", required = true),
160             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
161     })
162     @PreAuthorize("@ss.hasPermission('data:channel-opcda-tag:import')")
163     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
164                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
165                                                      @RequestParam("device") String device) throws Exception {
166         List<OpcDaTagImportExcelVO> list = ExcelUtils.read(file, OpcDaTagImportExcelVO.class);
167         return success(channelOPCDATagService.importOpcDaTagList(list, updateSupport,device));
168     }
9d7e02 169 }