潘志宝
2024-11-15 4be7d863a161b64f8592a789d699e807545e7dc6
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.kio.controller.admin;
H 2
03e8ac 3 import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
a6de49 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;
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.kio.collector.KingIOCollector;
aecc49 11 import com.iailab.framework.common.util.object.ConvertUtils;
L 12 import com.iailab.framework.excel.core.util.ExcelUtils;
13 import com.iailab.module.data.channel.kio.entity.ChannelKioTagEntity;
a6de49 14 import com.iailab.module.data.channel.kio.service.ChannelKioTagService;
aecc49 15 import com.iailab.module.data.channel.kio.vo.KioTagPageReqVO;
L 16 import com.iailab.module.data.channel.kio.vo.KioTagRespVO;
03e8ac 17 import com.iailab.module.data.channel.tag.vo.TagExportExcelVO;
J 18 import com.iailab.module.data.channel.tag.vo.TagImportExcelVO;
19 import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
f21253 20 import com.iailab.module.data.common.enums.IsEnableEnum;
a6de49 21 import io.swagger.v3.oas.annotations.Operation;
03e8ac 22 import io.swagger.v3.oas.annotations.Parameter;
J 23 import io.swagger.v3.oas.annotations.Parameters;
a477ef 24 import org.springframework.security.access.prepost.PreAuthorize;
03e8ac 25 import org.springframework.validation.annotation.Validated;
a6de49 26 import org.springframework.web.bind.annotation.*;
03e8ac 27 import org.springframework.web.multipart.MultipartFile;
a6de49 28
aecc49 29 import javax.annotation.Resource;
03e8ac 30 import javax.servlet.http.HttpServletResponse;
aecc49 31 import javax.validation.Valid;
03e8ac 32 import java.io.IOException;
J 33 import java.util.Collections;
aecc49 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;
aecc49 40 import static com.iailab.framework.common.pojo.CommonResult.success;
L 41
a6de49 42 /**
c7f709 43
a6de49 44  */
H 45 @RestController
46 @RequestMapping("/data/channel/kio/tag")
47 public class ChannelKioTagController {
48     @Resource
49     private ChannelKioTagService channelKioTagService;
50
e8ad66 51     @Resource
D 52     private KingIOCollector kingIOCollector;
53
a6de49 54     /**
H 55      * 分页查询tag
56      * */
a477ef 57     @PreAuthorize("@ss.hasPermission('data:channel-kio:query')")
a6de49 58     @GetMapping("page")
aecc49 59     public CommonResult<PageResult<KioTagRespVO>> page(@Valid KioTagPageReqVO reqVO){
e8ad66 60
aecc49 61         PageResult<ChannelKioTagEntity> page = channelKioTagService.queryPage(reqVO);
e8ad66 62         PageResult<KioTagRespVO> pageResultVO = new PageResult<>();
D 63         pageResultVO.setTotal(page.getTotal());
64
65         List<KioTagRespVO> vos = page.getList().stream().map(entity -> {
66
67             KioTagRespVO vo = BeanUtils.toBean(entity,KioTagRespVO.class);
68             try {
69                 vo.setDataValue(Double.parseDouble(kingIOCollector.getTagValue(reqVO.getDeviceId(), entity.getTagName())));
70             }catch (Exception e){
71                 e.printStackTrace();
72             }
73             return vo;
74         }).collect(Collectors.toList());
75
76         pageResultVO.setList(vos);
77
78         return success(pageResultVO);
a6de49 79     }
H 80
a477ef 81     @PreAuthorize("@ss.hasPermission('data:channel-kio:query')")
aecc49 82     @GetMapping("/info/{id}")
a6de49 83     @Operation(summary = "信息")
aecc49 84     public CommonResult<ChannelKioTagEntity> info(@PathVariable("id") String id) {
L 85         ChannelKioTagEntity info = channelKioTagService.info(id);
86         return success(info);
a6de49 87     }
H 88
a477ef 89     @PreAuthorize("@ss.hasPermission('data:channel-kio:create')")
90     @PostMapping("/create")
91     public CommonResult<Boolean> create(@RequestBody ChannelKioTagEntity channelKioTagEntity) {
aecc49 92         String id = UUID.randomUUID().toString();
L 93         channelKioTagEntity.setId(id);
94         channelKioTagEntity.setCreateTime(new Date());
95         channelKioTagService.add(channelKioTagEntity);
96         return success(true);
a6de49 97     }
H 98
a477ef 99     @PreAuthorize("@ss.hasPermission('data:channel-kio:update')")
aecc49 100     @PutMapping("/update")
L 101     public CommonResult<Boolean> update(@RequestBody ChannelKioTagEntity channelKioTagEntity) {
102         channelKioTagEntity.setUpdateTime(new Date());
103         channelKioTagService.update(channelKioTagEntity);
104         return success(true);
a6de49 105     }
H 106
a477ef 107     @PreAuthorize("@ss.hasPermission('data:channel-kio:delete')")
aecc49 108     @DeleteMapping("/delete")
L 109     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
110         channelKioTagService.delete(id);
111         return success(true);
a6de49 112     }
H 113
03e8ac 114     @GetMapping("/export")
J 115     @Operation(summary = "导出modbus tag列表")
116     @PreAuthorize("@ss.hasPermission('data:channel-kio-tag:export')")
117     @ApiAccessLog(operateType = EXPORT)
118     public void exportPointList(@Validated KioTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
119         reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
120         PageResult<ChannelKioTagEntity> page = channelKioTagService.queryPage(reqVO);
121         List<TagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), TagExportExcelVO.class);
122         ExcelUtils.write(response, "tag列表.xls", "数据", TagExportExcelVO.class, list, true);
123     }
5b634f 124
03e8ac 125     @GetMapping("/get-import-template")
J 126     @Operation(summary = "获得tag导入模板")
127     public void importTemplate(HttpServletResponse response) throws IOException {
128         // 手动创建导出 demo
129         List<TagImportExcelVO> list = Collections.singletonList(
f21253 130                 TagImportExcelVO.builder().tagName("Tag名称").tagDesc("Tag描述").dataType("String").enabled(IsEnableEnum.ENABLE.getCode())
03e8ac 131                         .build()
J 132         );
133         // 输出
134         ExcelUtils.write(response, "tag导入模板.xls", "tag列表", TagImportExcelVO.class, list,true);
135     }
136
137     @PostMapping("/import")
138     @Operation(summary = "导入tag")
139     @Parameters({
140             @Parameter(name = "file", description = "Excel 文件", required = true),
141             @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
142     })
143     @PreAuthorize("@ss.hasPermission('data:channel-kio-tag:import')")
144     public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
145                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
146                                                      @RequestParam("device") String device) throws Exception {
147         List<TagImportExcelVO> list = ExcelUtils.read(file, TagImportExcelVO.class);
148         return success(channelKioTagService.importKioTagList(list, updateSupport, device));
149     }
a6de49 150 }