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