dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.modbus.controller.admin;
H 2
03e8ac 3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
c7f709 4 import com.iailab.framework.common.pojo.CommonResult;
03e8ac 5 import com.iailab.framework.common.pojo.PageParam;
c7f709 6 import com.iailab.framework.common.pojo.PageResult;
L 7 import com.iailab.framework.common.util.object.BeanUtils;
849c3b 8 import com.iailab.framework.common.util.object.ConvertUtils;
D 9 import com.iailab.framework.excel.core.util.ExcelUtils;
e8ad66 10 import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
c7f709 11 import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity;
a6de49 12 import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity;
H 13 import com.iailab.module.data.channel.modbus.service.ChannelModbusTagService;
03e8ac 14 import com.iailab.module.data.channel.modbus.vo.ModBusTagExportExcelVO;
J 15 import com.iailab.module.data.channel.modbus.vo.ModBusTagImportExcelVO;
c7f709 16 import com.iailab.module.data.channel.modbus.vo.ModBusTagPageReqVO;
L 17 import com.iailab.module.data.channel.modbus.vo.ModBusTagRespVO;
03e8ac 18 import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
J 19 import io.swagger.v3.oas.annotations.Operation;
20 import io.swagger.v3.oas.annotations.Parameter;
21 import io.swagger.v3.oas.annotations.Parameters;
22 import org.springframework.security.access.prepost.PreAuthorize;
23 import org.springframework.validation.annotation.Validated;
a6de49 24 import org.springframework.web.bind.annotation.*;
03e8ac 25 import org.springframework.web.multipart.MultipartFile;
a6de49 26
c7f709 27 import javax.annotation.Resource;
03e8ac 28 import javax.servlet.http.HttpServletResponse;
c7f709 29 import javax.validation.Valid;
e8ad66 30 import java.util.ArrayList;
03e8ac 31 import java.io.IOException;
J 32 import java.util.Collections;
c7f709 33 import java.util.Date;
e8ad66 34 import java.util.List;
a6de49 35 import java.util.UUID;
e8ad66 36 import java.util.stream.Collectors;
a6de49 37
03e8ac 38 import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
c7f709 39 import static com.iailab.framework.common.pojo.CommonResult.success;
L 40
a6de49 41 /**
c7f709 42  * @author lirm
L 43  * @Description
44  * @createTime 2024年08月27日
a6de49 45  */
H 46 @RestController
47 @RequestMapping("/data/channel/modbus/tag")
48 public class ChannelModbusTagController {
49     @Resource
50     private ChannelModbusTagService channelModbusTagService;
51
e8ad66 52     @Resource
D 53     private ModBusCollector modBusCollector;
54
c7f709 55     @GetMapping("/page")
L 56     public CommonResult<PageResult<ModBusTagRespVO>> list(@Valid ModBusTagPageReqVO reqVO) {
57         PageResult<ChannelModBusTagEntity> page = channelModbusTagService.queryPage(reqVO);
e8ad66 58         PageResult<ModBusTagRespVO> pageResultVO = new PageResult<>();
D 59         pageResultVO.setTotal(page.getTotal());
c7f709 60
e8ad66 61         List<ModBusTagRespVO> vos = page.getList().stream().map(entity -> {
D 62
63             ModBusTagRespVO vo = BeanUtils.toBean(entity,ModBusTagRespVO.class);
849c3b 64             vo.setDataValue(modBusCollector.getTagValue(entity.getDevice(),entity.getTagName()));
e8ad66 65             return vo;
D 66         }).collect(Collectors.toList());
67
68         pageResultVO.setList(vos);
69
70         return success(pageResultVO);
a6de49 71     }
H 72     /**
c7f709 73      * 根据id查询设备详情
a6de49 74      *
H 75      * @param id
76      */
77     @GetMapping("/info/{id}")
c7f709 78     public CommonResult<ChannelModBusTagEntity> info(@PathVariable("id") String id) {
L 79         ChannelModBusTagEntity info = channelModbusTagService.info(id);
80         return success(info);
a6de49 81     }
H 82
83     /**
c7f709 84      * 添加设备
a6de49 85      *
H 86      * @param channelModBusTagEntity
87      */
a477ef 88     @PostMapping("/create")
89     public CommonResult<Boolean> create(@RequestBody ChannelModBusTagEntity channelModBusTagEntity) {
c7f709 90         String id = UUID.randomUUID().toString();
L 91         channelModBusTagEntity.setId(id);
92         channelModBusTagEntity.setCreateTime(new Date());
93         channelModbusTagService.add(channelModBusTagEntity);
94         return success(true);
a6de49 95     }
H 96
97     /**
c7f709 98      * 修改设备
a6de49 99      *
c7f709 100      * @param channelModBusTagEntity
a6de49 101      */
c7f709 102     @PutMapping("/update")
L 103     public CommonResult<Boolean> update(@RequestBody ChannelModBusTagEntity channelModBusTagEntity) {
104         channelModBusTagEntity.setUpdateTime(new Date());
105         channelModbusTagService.update(channelModBusTagEntity);
106         return success(true);
a6de49 107     }
H 108
c7f709 109     /**
L 110      * 删除设备
111      *
112      * @param id
113      */
114     @DeleteMapping("/delete")
115     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
116         channelModbusTagService.delete(id);
117         return success(true);
118     }
03e8ac 119
J 120     @GetMapping("/export")
121     @Operation(summary = "导出modbus tag列表")
122     @PreAuthorize("@ss.hasPermission('data:channel-modbus-tag:export')")
123     @ApiAccessLog(operateType = EXPORT)
124     public void exportPointList(@Validated ModBusTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
125         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
126         PageResult<ChannelModBusTagEntity> page = channelModbusTagService.queryPage(reqVO);
127
128         List<ModBusTagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), ModBusTagExportExcelVO.class);
129         ExcelUtils.write(response, "tag列表.xls", "数据", ModBusTagExportExcelVO.class, list, true);
130     }
131
132
133     @GetMapping("/get-import-template")
134     @Operation(summary = "获得tag导入模板")
135     public void importTemplate(HttpServletResponse response) throws IOException {
136         // 手动创建导出 demo
137         List<ModBusTagImportExcelVO> list = Collections.singletonList(
138                 ModBusTagImportExcelVO.builder().tagName("Tag名称").tagDesc("Tag描述").dataType("String").
139                         address("123").format("1").samplingRate(1000).enabled(1)
140                         .build()
141         );
142         // 输出
143         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", ModBusTagImportExcelVO.class, list,true);
144     }
145
146     @PostMapping("/import")
147     @Operation(summary = "导入tag")
148     @Parameters({
149             @Parameter(name = "file", description = "Excel 文件", required = true),
150             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
151     })
152     @PreAuthorize("@ss.hasPermission('data:channel-modbus-tag:import')")
153     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
154                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
155                                                      @RequestParam("device") String device) throws Exception {
156         List<ModBusTagImportExcelVO> list = ExcelUtils.read(file, ModBusTagImportExcelVO.class);
157         return success(channelModbusTagService.importModBusTagList(list, updateSupport,device));
158     }
a6de49 159 }