dongyukun
2024-10-28 139c6a60f3d7a3e0a190a80d132c5e5cb5d328c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.iailab.module.data.point.controller.admin;
 
import com.fasterxml.jackson.annotation.JsonFormat;
import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.pojo.PageParam;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.framework.excel.core.util.ExcelUtils;
import com.iailab.module.data.api.dto.ApiPointValueQueryDTO;
import com.iailab.module.data.influxdb.service.InfluxDBService;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.point.entity.DaPointEntity;
import com.iailab.module.data.point.service.DaPointService;
import com.iailab.module.data.point.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.iailab.framework.common.pojo.CommonResult.success;
import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
 
 
/**
 * @author lirm
 * @Description
 * @createTime 2024年09月2日
 */
@Tag(name = "数据采集,测点")
@RestController
@RequestMapping("/data/da/point")
@Validated
public class DaPointController {
 
    @Resource
    private DaPointService daPointService;
 
    @Resource
    private InfluxDBService influxDBService;
 
    @PreAuthorize("@ss.hasPermission('data:point:query')")
    @GetMapping("page")
    public CommonResult<PageResult<DaPointDTO>> page(@Valid DaPointPageReqVO reqVO){
        PageResult<DaPointDTO> page = daPointService.queryPage(reqVO);
        return success(page);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:query')")
    @GetMapping("/list")
    @Operation(summary = "列表")
    public CommonResult<List<DaPointDTO>> list(@Valid @RequestParam Map<String, Object> params) {
        List<DaPointDTO> list = daPointService.list(params);
        return success(list);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:query')")
    @GetMapping("/info/{id}")
    public CommonResult<DaPointDTO> info(@PathVariable("id") String id){
        DaPointDTO info= daPointService.info(id);
        return success(info);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:create')")
    @PostMapping("create")
    public CommonResult<Boolean> create(@RequestBody DaPointDTO daPointDTO){
        String id = UUID.randomUUID().toString();
        daPointDTO.setId(id);
        daPointService.add(daPointDTO);
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:update')")
    @PutMapping("/update")
    public CommonResult<Boolean> update(@RequestBody DaPointDTO daPointDTO) {
        daPointService.update(daPointDTO);
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:delete')")
    @DeleteMapping("/delete")
    public CommonResult<Boolean> delete(String id) {
        daPointService.delete(new String[]{id});
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:query')")
    @GetMapping("pointNo")
    public CommonResult<List<DaPointDTO>> getpoint(@RequestParam Map<String, Object> params){
        List<DaPointDTO> list = daPointService.list(params);
 
        return new CommonResult<List<DaPointDTO>>().setData(list);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:update')")
    @PutMapping("/enable")
    @Operation(summary = "启用")
    @Transactional
    public CommonResult<Boolean> enable(@RequestBody String[] ids) {
        daPointService.enableByIds(ids);
        return success(true);
    }
 
    @PreAuthorize("@ss.hasPermission('data:point:update')")
    @PutMapping("/disable")
    @Operation(summary = "禁用")
    @Transactional
    public CommonResult<Boolean> disable(@RequestBody String[] ids) {
        daPointService.disableByIds(ids);
        return success(true);
    }
 
    @GetMapping("/export")
    @Operation(summary = "导出测点列表")
    @PreAuthorize("@ss.hasPermission('data:point:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPointList(@Validated DaPointPageReqVO exportReqVO,
                               HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DaPointEntity> list = daPointService.queryExcelList(exportReqVO).getList();
        List<DaPointExcelVO> daPointExcelVOlist = ConvertUtils.sourceToTarget(list,DaPointExcelVO.class);
        ExcelUtils.write(response, "测点列表.xls", "数据", DaPointExcelVO.class, daPointExcelVOlist);
    }
 
    @GetMapping("/exportValue")
    @Operation(summary = "导出测点数据")
    @PreAuthorize("@ss.hasPermission('data:point:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPointValue(@RequestParam("pointNo") String pointNo,
                                 @RequestParam("start") String start,
                                 @RequestParam("end") String end,
                                 HttpServletResponse response) throws IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
        List<String> pointNos = new ArrayList<>();
        pointNos.add(pointNo);
        queryDto.setPointNos(pointNos);
        try{
            if (start == null) {
                queryDto.setStart(new Date());
            }else{
                queryDto.setStart(formatter.parse(start));
 
            }
            if (end == null) {
                queryDto.setEnd(new Date());
            }else{
                queryDto.setStart(formatter.parse(end));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<PointValueExportVO> dointValueExportlist = influxDBService.exportPointValue(queryDto);
        ExcelUtils.write(response, "测点数据.xls", "数据", PointValueExportVO.class, dointValueExportlist);
    }
 
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入测点模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<PointImportExcelVO> list = Arrays.asList(
                PointImportExcelVO.builder().pointName("测点1").pointType("MEASURE").dataType("float").valueType("SIMULATE")
                        .storeType(null).unit("t(非必填)").unittransfactor(new BigDecimal(1)).defaultValue(new BigDecimal(0)).maxValue(new BigDecimal(10000000.000000)).minValue(new BigDecimal(0))
                        .minfreqid("NET10").remark("备注(非必填)").sourceType("HTTP").sourceId("b9d0f670-2135-458f-9494-0cda4f35ec09").tagNo("SARD1200G00102RC001")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "测点导入模板.xls", "测点列表", PointImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入测点")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
    })
//    @PreAuthorize("@ss.hasPermission('data:point:import')")
    public CommonResult<PointImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                       @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<PointImportExcelVO> list = ExcelUtils.read(file, PointImportExcelVO.class);
        return success(daPointService.importPointList(list, updateSupport));
    }
 
}