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