Jay
2025-01-17 570ca8e69bf33cff32c3ae1c5d54015de6804c95
提交 | 用户 | 时间
10e4c0 1 package com.iailab.module.data.api.controller.admin;
a6de49 2
H 3 import com.iailab.framework.common.pojo.CommonResult;
e255ba 4 import com.iailab.framework.common.pojo.PageResult;
a6de49 5 import com.iailab.framework.common.util.date.DateUtils;
ed4f78 6 import com.iailab.framework.common.util.object.ConvertUtils;
7 import com.iailab.framework.excel.core.util.ExcelUtils;
df4f01 8 import com.iailab.module.data.api.dto.DeviceValueDTO;
585be5 9 import com.iailab.module.data.api.dto.IndexQueryDTO;
10 import com.iailab.module.data.api.dto.echarts.BarLineDTO;
11 import com.iailab.module.data.api.dto.echarts.SeriesItem;
ed4f78 12 import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO;
13 import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO;
14 import com.iailab.module.data.api.plan.PlanItemApi;
a3a072 15 import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO;
d41f14 16 import com.iailab.module.data.api.point.DataPointApi;
116db5 17 import com.iailab.module.data.api.point.dto.*;
ed4f78 18 import com.iailab.module.data.common.ApiDataQueryDTO;
19 import com.iailab.module.data.common.ApiDataValueDTO;
df4f01 20 import com.iailab.module.data.common.utils.ApiSecurityUtils;
ed4f78 21 import com.iailab.module.data.ind.collection.IndItemCollector;
22 import com.iailab.module.data.ind.item.vo.IndItemValueVO;
23 import com.iailab.module.data.plan.item.entity.PlanItemEntity;
df4f01 24 import com.iailab.module.data.plan.item.service.PlanItemService;
ed4f78 25 import com.iailab.module.data.plan.item.vo.PlanItemValueExportVO;
a6de49 26 import com.iailab.module.data.point.common.PointDataTypeEnum;
H 27 import com.iailab.module.data.point.dto.DaPointDTO;
28 import com.iailab.module.data.point.service.DaPointService;
29 import io.swagger.v3.oas.annotations.Operation;
30 import io.swagger.v3.oas.annotations.tags.Tag;
31 import lombok.extern.slf4j.Slf4j;
d41f14 32 import org.springframework.beans.factory.annotation.Autowired;
a6de49 33 import org.springframework.util.CollectionUtils;
H 34 import org.springframework.web.bind.annotation.*;
35
df4f01 36 import javax.annotation.Resource;
9100b0 37 import javax.annotation.security.PermitAll;
a6de49 38 import javax.servlet.http.HttpServletRequest;
H 39 import javax.servlet.http.HttpServletResponse;
40 import java.math.BigDecimal;
df4f01 41 import java.math.RoundingMode;
a6de49 42 import java.util.*;
H 43 import java.util.stream.Collectors;
c7f709 44
L 45 import static com.iailab.framework.common.pojo.CommonResult.success;
a6de49 46
H 47 /**
48  * @author PanZhibao
49  * @Description
50  * @createTime 2023年05月02日 10:58:00
51  */
52 @Slf4j
53 @RestController
10e4c0 54 @RequestMapping("/data/api")
a6de49 55 @Tag(name = "数据")
9961c7 56 public class ApiDataController {
a6de49 57
H 58     @Resource
59     private DaPointService daPointService;
60
61     @Resource
62     private ApiSecurityUtils apiSecurityUtils;
63
d41f14 64     @Autowired
65     private DataPointApi dataPointApi;
ed4f78 66
67     @Autowired
68     private PlanItemApi planItemApi;
69
70     @Autowired
71     private IndItemCollector indItemCollector;
72
73     @Autowired
74     private PlanItemService planItemService;
a6de49 75
9100b0 76     @PermitAll
d41f14 77     @PostMapping("/query-points/history-value")
78     @Operation(summary = "查询多个测点历史值")
79     public CommonResult<Map<String, List<Map<String, Object>>>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest
80             request, @RequestBody ApiPointsValueQueryDTO queryDto) {
81         Map<String, List<Map<String, Object>>> data = new HashMap<>();
a6de49 82         try {
H 83             apiSecurityUtils.validate(request);
d41f14 84             data = dataPointApi.queryPointsHistoryValue(queryDto);
c7f709 85             return success(data);
a6de49 86
H 87         } catch (Exception ex) {
c7f709 88             return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
a6de49 89         }
H 90     }
91
d41f14 92     @PermitAll
93     @PostMapping("/query-point/history-value")
94     @Operation(summary = "查询单个测点历史值")
95     public CommonResult<List<ApiPointValueDTO>> queryPointHistoryValue(HttpServletResponse response, HttpServletRequest
96             request, @RequestBody ApiPointValueQueryDTO queryDto) {
97         List<ApiPointValueDTO> pointValueList = new ArrayList<>();
a6de49 98         try {
d41f14 99             apiSecurityUtils.validate(request);
100             pointValueList = dataPointApi.queryPointHistoryValue(queryDto);
101             return success(pointValueList);
102
103         } catch (Exception ex) {
104             return new CommonResult<List<ApiPointValueDTO>>().setMsg(ex.getMessage());
105         }
106     }
107
108     @PostMapping("/query-points/real-value")
109     @Operation(summary = "查询多个测点当前值")
110     public CommonResult<Map<String, Object>> queryPointsRealValue(HttpServletResponse response, HttpServletRequest
111             request, @RequestBody List<String> pointNos) {
112         Map<String, Object> data = new HashMap<>();
113         try {
114             apiSecurityUtils.validate(request);
115             data = dataPointApi.queryPointsRealValue(pointNos);
c7f709 116             return success(data);
a6de49 117         } catch (Exception ex) {
c7f709 118             return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage());
a6de49 119         }
H 120     }
121
d41f14 122     @PostMapping("/query-points/chart")
123     public CommonResult<BarLineDTO> queryPointsChart(HttpServletResponse response, HttpServletRequest
124             request, @RequestBody IndexQueryDTO dto) {
a6de49 125         BarLineDTO CommonResult = new BarLineDTO();
H 126         try {
d41f14 127             apiSecurityUtils.validate(request);
a6de49 128             List<String> legend = new ArrayList<>();
H 129             List<SeriesItem> series = new ArrayList<>();
130             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");
131             Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm");
132             Date startDate = dto.getStartDate() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStartDate();
d41f14 133             List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 60 : dto.getGranularity());
a6de49 134             if (CollectionUtils.isEmpty(dto.getCodes())) {
H 135                 return new CommonResult<BarLineDTO>().setData(CommonResult);
136             }
137             List<DaPointDTO> pointList = new ArrayList<>();
138             dto.getCodes().forEach(item -> {
139                 pointList.add(daPointService.getByNo(item));
140             });
141             pointList.forEach(item -> {
142                 legend.add(item.getPointName());
143                 SeriesItem seriesItem = new SeriesItem();
144                 seriesItem.setName(item.getPointName());
d41f14 145                 ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
146                 queryDto.setStart(startDate);
147                 queryDto.setEnd(endDate);
148                 queryDto.setPointNo(item.getPointNo());
149                 List<ApiPointValueDTO> list = dataPointApi.queryPointHistoryValue(queryDto);
a6de49 150                 List<Object[]> sData = list.stream().map(dataItem -> {
9df837 151                     Object[] valueArray = new Object[]{DateUtils.format(dataItem.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND),
152                             getFormatValue(item.getDataType(), dataItem.getV())};
a6de49 153                     return valueArray;
H 154                 }).collect(Collectors.toList());
155                 seriesItem.setData(sData);
156                 series.add(seriesItem);
157             });
158             CommonResult.setLegend(legend);
159             CommonResult.setCategories(categories);
160             CommonResult.setSeries(series);
161         } catch (Exception ex) {
162             ex.printStackTrace();
163         }
164         return new CommonResult<BarLineDTO>().setData(CommonResult);
165     }
166
167     private Object getFormatValue(String dataType, Object value) {
168         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(dataType)) {
169             BigDecimal decValue = new BigDecimal(value.toString());
170             if (PointDataTypeEnum.FLOAT.getCode().equals(dataType)) {
171                 return decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
172             } else if (PointDataTypeEnum.INT.getCode().equals(dataType)) {
173                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
174             }
175         }
176         return value;
177     }
178
179
180     @GetMapping("/device-value")
181     public List<DeviceValueDTO> getDeviceValue(@RequestParam Map<String, Object> params) {
182         List<DeviceValueDTO> CommonResult = new ArrayList<>();
183         if (params.get("pointNos") == null) {
184             return CommonResult;
185         }
186         List<String> pointNos = Arrays.asList(params.get("pointNos").toString().split(","));
d41f14 187         Map<String, Object> data = dataPointApi.queryPointsRealValue(pointNos);
a6de49 188         if (!CollectionUtils.isEmpty(data)) {
H 189             data.forEach((k, v) -> {
190                 DeviceValueDTO dto = new DeviceValueDTO();
191                 dto.setDataId(k);
192                 dto.setValue(new BigDecimal(v.toString()));
193                 CommonResult.add(dto);
194             });
195         }
196         return CommonResult;
197     }
ed4f78 198
199     @PermitAll
200     @PostMapping("/query-plan/history-value")
201     @Operation(summary = "查询单个计划历史值")
202     public CommonResult<List<ApiDataValueDTO>> queryPlanItemHistoryValue(HttpServletResponse response, HttpServletRequest
203             request, @RequestBody ApiDataQueryDTO dto) {
204         List<ApiDataValueDTO> result = new ArrayList<>();
205         try {
206             apiSecurityUtils.validate(request);
207             result = planItemApi.queryPlanItemHistoryValue(dto);
208             return new CommonResult<List<ApiDataValueDTO>>().setData(result);
a3a072 209         } catch (Exception ex) {
ed4f78 210             return new CommonResult<List<ApiDataValueDTO>>().setMsg(ex.getMessage());
a3a072 211         }
212     }
213
214     @PermitAll
215     @PostMapping("/query-plan/record-value")
216     @Operation(summary = "查询单个计划历史值")
217     public CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>> queryPlanItemRecordValue(HttpServletResponse response, HttpServletRequest
218             request, @RequestBody ApiDataQueryDTO dto) {
219         LinkedHashMap<String, List<ApiPlanDataDTO>> result = new LinkedHashMap<>();
220         try {
221             apiSecurityUtils.validate(request);
222             result = planItemApi.queryPlanItemRecordValue(dto);
223             return new CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>>().setData(result);
224         } catch (Exception ex) {
225             return new CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>>().setMsg(ex.getMessage());
ed4f78 226         }
227     }
228
229     @PostMapping("/query-plans/chart")
230     public CommonResult<BarLineDTO> queryPlansChart(HttpServletResponse response, HttpServletRequest
231             request, @RequestBody ApiDataQueryDTO dto) {
232
233         BarLineDTO CommonResult = new BarLineDTO();
a3a072 234
ed4f78 235         try {
236             apiSecurityUtils.validate(request);
237             List<String> legend = new ArrayList<>();
238             List<SeriesItem> series = new ArrayList<>();
a3a072 239             Calendar calendar = Calendar.getInstance();
240             calendar.set(Calendar.MILLISECOND, 0);
241             calendar.set(Calendar.SECOND, 0);
242             Date endDate = dto.getEnd() == null ? DateUtils.addDateHours(calendar.getTime(), 2) : dto.getEnd();
243             Date startDate = dto.getStart() == null ? calendar.getTime() : dto.getStart();
ed4f78 244             List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 60 : dto.getGranularity());
245             if (CollectionUtils.isEmpty(dto.getItemNos())) {
246                 return new CommonResult<BarLineDTO>().setData(CommonResult);
247             }
248             List<PlanItemEntity> planItemList = new ArrayList<>();
249             dto.getItemNos().forEach(item -> {
250                 planItemList.add(planItemService.getInfoByNo(item));
251             });
252             planItemList.forEach(item -> {
253                 legend.add(item.getItemName());
254                 SeriesItem seriesItem = new SeriesItem();
255                 seriesItem.setName(item.getItemName());
256                 ApiDataQueryDTO queryDto = new ApiDataQueryDTO();
257                 queryDto.setItemNo(item.getItemNo());
258                 queryDto.setStart(startDate);
259                 queryDto.setEnd(endDate);
260                 List<ApiDataValueDTO> list = planItemApi.queryPlanItemHistoryValue(queryDto);
261
262                 List<Object[]> sData = list.stream().map(dataItem -> {
263                     Object[] valueArray = new Object[]{DateUtils.format(dataItem.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND),
264                             dataItem.getDataValue()};
265                     return valueArray;
266                 }).collect(Collectors.toList());
267                 seriesItem.setData(sData);
268                 series.add(seriesItem);
269             });
270             CommonResult.setLegend(legend);
271             CommonResult.setCategories(categories);
272             CommonResult.setSeries(series);
273         } catch (Exception ex) {
274             ex.printStackTrace();
275         }
276         return new CommonResult<BarLineDTO>().setData(CommonResult);
277     }
278
a3a072 279     @PostMapping("/export-plan/history-value")
ed4f78 280     @Operation(summary = "导出计划数据")
a3a072 281     public void exportPlanHistoryValue(HttpServletResponse response, HttpServletRequest
dc303c 282             request, @RequestBody ApiDataQueryDTO dto) throws Exception {
283         apiSecurityUtils.validate(request);
ed4f78 284         Calendar calendar = Calendar.getInstance();
285         calendar.set(Calendar.MILLISECOND, 0);
286         calendar.set(Calendar.SECOND, 0);
a3a072 287         Date endDate = dto.getEnd() == null ? DateUtils.addDateHours(calendar.getTime(), 2) : dto.getEnd();
288         Date startDate = dto.getStart() == null ? calendar.getTime() : dto.getStart();
ed4f78 289         ApiDataQueryDTO queryDto = new ApiDataQueryDTO();
a3a072 290         queryDto.setItemNo(dto.getItemNo());
ed4f78 291         queryDto.setStart(startDate);
292         queryDto.setEnd(endDate);
293         List<ApiDataValueDTO> list = planItemApi.queryPlanItemHistoryValue(queryDto);
a3a072 294         List<PlanItemValueExportVO> exportList = list.stream().map(item -> {
295             PlanItemValueExportVO exportVO = new PlanItemValueExportVO();
296             exportVO.setDataTime(DateUtils.format(item.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
df4f01 297             exportVO.setDataValue(BigDecimal.valueOf(item.getDataValue()).setScale(0, RoundingMode.HALF_UP).toString());
a3a072 298             return exportVO;
299         }).collect(Collectors.toList());
300         ExcelUtils.write(response, "计划数据.xls", "计划数据", PlanItemValueExportVO.class, exportList);
ed4f78 301     }
302
303     @PermitAll
304     @GetMapping("/query-ind/default-value")
305     @Operation(summary = "查询指标默认值")
dc303c 306     public CommonResult<List<ApiIndItemValueDTO>> queryIndItemDefaultValue(HttpServletResponse response, HttpServletRequest
307             request,@RequestParam String itemNo) throws Exception {
308         apiSecurityUtils.validate(request);
ed4f78 309         List<IndItemValueVO> list = indItemCollector.queryValue(itemNo);
310         List<ApiIndItemValueDTO> dtoList = new ArrayList<>();
311         list.forEach(item -> {
b0bd6c 312             if (item != null) {
313                 ApiIndItemValueDTO dto = new ApiIndItemValueDTO();
314                 dto.setDataTime(item.getDataTime());
315                 dto.setDataValue(item.getDataValue().doubleValue());
316                 dtoList.add(dto);
317             }
ed4f78 318         });
319         return success(dtoList);
320     }
321
322     @PermitAll
570ca8 323     @PostMapping("/query-ind/history-value")
ed4f78 324     @Operation(summary = "查询指标历史值")
570ca8 325     public CommonResult<List<ApiIndItemValueDTO>> queryIndItemHistoryValue(@RequestBody ApiIndItemQueryDTO dto) {
ed4f78 326         List<IndItemValueVO> list = indItemCollector.queryValue(dto.getItemNo(), dto.getStart(), dto.getEnd());
327         return success(ConvertUtils.sourceToTarget(list, ApiIndItemValueDTO.class));
328     }
e255ba 329
L 330     @PermitAll
859884 331     @PostMapping("/query-math-point/current-value")
e255ba 332     @Operation(summary = "根据计算点编号查询测点集合")
859884 333     public CommonResult<PageResult<ApiPointDTO>> queryMathPointCurrentValue(@RequestBody ApiPointPageReqVO reqVO) {
334         PageResult<ApiPointDTO> data = dataPointApi.queryMathPointCurrentValue(reqVO);
e255ba 335         return success(data);
L 336     }
337
a6de49 338 }