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