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