潘志宝
2024-10-29 d41f14d2986b46da9dd7742f6df63d9725cd29f3
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.api.controller;
H 2
3 import com.iailab.framework.common.pojo.CommonResult;
4 import com.iailab.framework.common.util.date.DateUtils;
585be5 5 import com.iailab.module.data.api.dto.IndexQueryDTO;
6 import com.iailab.module.data.api.dto.echarts.BarLineDTO;
7 import com.iailab.module.data.api.dto.echarts.SeriesItem;
d41f14 8 import com.iailab.module.data.api.point.DataPointApi;
9 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
10 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
11 import com.iailab.module.data.api.point.dto.ApiPointsValueQueryDTO;
a6de49 12 import com.iailab.module.data.point.common.PointDataTypeEnum;
H 13 import com.iailab.module.data.point.dto.DaPointDTO;
14 import com.iailab.module.data.point.service.DaPointService;
15 import com.iailab.module.data.api.dto.DeviceValueDTO;
16 import com.iailab.module.data.api.utils.ApiSecurityUtils;
17 import io.swagger.v3.oas.annotations.Operation;
18 import io.swagger.v3.oas.annotations.tags.Tag;
19 import lombok.extern.slf4j.Slf4j;
d41f14 20
a6de49 21 import javax.annotation.Resource;
d41f14 22
23 import org.springframework.beans.factory.annotation.Autowired;
a6de49 24 import org.springframework.util.CollectionUtils;
H 25 import org.springframework.web.bind.annotation.*;
26
9100b0 27 import javax.annotation.security.PermitAll;
a6de49 28 import javax.servlet.http.HttpServletRequest;
H 29 import javax.servlet.http.HttpServletResponse;
30 import java.math.BigDecimal;
31 import java.util.*;
32 import java.util.stream.Collectors;
c7f709 33
L 34 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 35
H 36 /**
37  * @author PanZhibao
38  * @Description
39  * @createTime 2023年05月02日 10:58:00
40  */
41 @Slf4j
42 @RestController
d41f14 43 @RequestMapping("/admin-api/data/api")
a6de49 44 @Tag(name = "数据")
9961c7 45 public class ApiDataController {
a6de49 46
H 47     @Resource
48     private DaPointService daPointService;
49
50     @Resource
51     private ApiSecurityUtils apiSecurityUtils;
52
d41f14 53     @Autowired
54     private DataPointApi dataPointApi;
a6de49 55
9100b0 56     @PermitAll
d41f14 57     @PostMapping("/query-points/history-value")
58     @Operation(summary = "查询多个测点历史值")
59     public CommonResult<Map<String, List<Map<String, Object>>>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest
60             request, @RequestBody ApiPointsValueQueryDTO queryDto) {
61         Map<String, List<Map<String, Object>>> data = new HashMap<>();
a6de49 62         try {
H 63             apiSecurityUtils.validate(request);
d41f14 64             data = dataPointApi.queryPointsHistoryValue(queryDto);
c7f709 65             return success(data);
a6de49 66
H 67         } catch (Exception ex) {
c7f709 68             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 69         }
H 70     }
71
d41f14 72     @PermitAll
73     @PostMapping("/query-point/history-value")
74     @Operation(summary = "查询单个测点历史值")
75     public CommonResult<List<ApiPointValueDTO>> queryPointHistoryValue(HttpServletResponse response, HttpServletRequest
76             request, @RequestBody ApiPointValueQueryDTO queryDto) {
77         List<ApiPointValueDTO> pointValueList = new ArrayList<>();
a6de49 78         try {
d41f14 79             apiSecurityUtils.validate(request);
80             pointValueList = dataPointApi.queryPointHistoryValue(queryDto);
81             return success(pointValueList);
82
83         } catch (Exception ex) {
84             return new CommonResult<List<ApiPointValueDTO>>().setMsg(ex.getMessage());
85         }
86     }
87
88     @PostMapping("/query-points/real-value")
89     @Operation(summary = "查询多个测点当前值")
90     public CommonResult<Map<String, Object>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest
91             request, @RequestBody List<String> pointNos) {
92         Map<String, Object> data = new HashMap<>();
93         try {
94             apiSecurityUtils.validate(request);
95             data = dataPointApi.queryPointsRealValue(pointNos);
c7f709 96             return success(data);
a6de49 97         } catch (Exception ex) {
c7f709 98             return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage());
a6de49 99         }
H 100     }
101
d41f14 102     @PostMapping("/query-points/chart")
103     public CommonResult<BarLineDTO> queryPointsChart(HttpServletResponse response, HttpServletRequest
104             request, @RequestBody IndexQueryDTO dto) {
a6de49 105         BarLineDTO CommonResult = new BarLineDTO();
H 106         try {
d41f14 107             apiSecurityUtils.validate(request);
a6de49 108             List<String> legend = new ArrayList<>();
H 109             List<SeriesItem> series = new ArrayList<>();
110             String endDateStr = dto.getEndDate() == null ? DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") : DateUtils.format(dto.getEndDate(), "yyyy-MM-dd HH:mm:ss");
111             Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm");
112             Date startDate = dto.getStartDate() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStartDate();
d41f14 113             List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 60 : dto.getGranularity());
a6de49 114             if (CollectionUtils.isEmpty(dto.getCodes())) {
H 115                 return new CommonResult<BarLineDTO>().setData(CommonResult);
116             }
117             List<DaPointDTO> pointList = new ArrayList<>();
118             dto.getCodes().forEach(item -> {
119                 pointList.add(daPointService.getByNo(item));
120             });
121             pointList.forEach(item -> {
122                 legend.add(item.getPointName());
123                 SeriesItem seriesItem = new SeriesItem();
124                 seriesItem.setName(item.getPointName());
d41f14 125                 ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
126                 queryDto.setStart(startDate);
127                 queryDto.setEnd(endDate);
128                 queryDto.setPointNo(item.getPointNo());
129                 List<ApiPointValueDTO> list = dataPointApi.queryPointHistoryValue(queryDto);
a6de49 130                 List<Object[]> sData = list.stream().map(dataItem -> {
d41f14 131                     Object[] valueArray = new Object[]{DateUtils.format(dataItem.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND),
132                             getFormatValue(item.getDataType(), dataItem.getDataValue())};
a6de49 133                     return valueArray;
H 134                 }).collect(Collectors.toList());
135                 seriesItem.setData(sData);
136                 series.add(seriesItem);
137             });
138             CommonResult.setLegend(legend);
139             CommonResult.setCategories(categories);
140             CommonResult.setSeries(series);
141         } catch (Exception ex) {
142             ex.printStackTrace();
143         }
144         return new CommonResult<BarLineDTO>().setData(CommonResult);
145     }
146
147     private Object getFormatValue(String dataType, Object value) {
148         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(dataType)) {
149             BigDecimal decValue = new BigDecimal(value.toString());
150             if (PointDataTypeEnum.FLOAT.getCode().equals(dataType)) {
151                 return decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
152             } else if (PointDataTypeEnum.INT.getCode().equals(dataType)) {
153                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
154             }
155         }
156         return value;
157     }
158
159
160     @GetMapping("/device-value")
161     public List<DeviceValueDTO> getDeviceValue(@RequestParam Map<String, Object> params) {
162         List<DeviceValueDTO> CommonResult = new ArrayList<>();
163         if (params.get("pointNos") == null) {
164             return CommonResult;
165         }
166         List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(","));
d41f14 167         Map<String, Object> data = dataPointApi.queryPointsRealValue(pointNos);
a6de49 168         if (!CollectionUtils.isEmpty(data)) {
H 169             data.forEach((k, v) -> {
170                 DeviceValueDTO dto = new DeviceValueDTO();
171                 dto.setDataId(k);
172                 dto.setValue(new BigDecimal(v.toString()));
173                 CommonResult.add(dto);
174             });
175         }
176         return CommonResult;
177     }
178 }