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