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