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