houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.param.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.iailab.common.enums.CdbmEnum;
import com.iailab.module.data.api.IFeignDataApi;
import com.iailab.module.data.dto.ApiDataDTO;
import com.iailab.module.data.dto.FeignQueryPointDTO;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.module.model.handler.ModelHandler;
import com.iailab.module.param.dto.PageParamDTO;
import com.iailab.module.param.dto.PageParamResultValueDTO;
import com.iailab.module.param.dto.PointDataValueDTO;
import com.iailab.module.param.dto.ResultPointAndTrendDataDTO;
import com.iailab.module.param.service.PageParmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author DongYukun
 * @Description
 * @createTime 2024年5月10日 15:06:00
 */
@RestController
@RequestMapping("/param/page")
@Tag(name = "原煤趋势分析")
@Slf4j
public class PageParamController {
 
    private final String[] rowCoalPageCode = {"ymrxl","ymhf","ymsf","ymlf","ymrz"};
    private final String[] washPageCode = {"hmcl","xxkcl","xzkcl","hmhf","hmlf","hmsf","hmrz"};
 
    private final String[] wyPageCode = {"dxhm","xxkwy","xzkwy","gswy","xkzz","hcwyds","hcwyls"};
 
    @Resource
    PageParmService pageParmService;
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    @Resource
    private ModelHandler modelHandler;
 
    @GetMapping("{page}")
    @Operation(summary = "趋势分析数据")
    public CommonResult<List<PageParamResultValueDTO>> get(@PathVariable("page") String page) {
        List<PageParamDTO> dtoList = pageParmService.selectPageParamByPage(page);
        List<PageParamResultValueDTO> resultData = new ArrayList<>();
        switch (page){
            case "rowCoal":
                resultData = collateCommonResultValueDate(dtoList,rowCoalPageCode);
                break;
            case "wash":
                resultData = collateCommonResultValueDate(dtoList,washPageCode);
                break;
            case "wy":
                resultData = collateCommonResultValueDate(dtoList,wyPageCode);
                break;
        }
        return new CommonResult<List<PageParamResultValueDTO>>().setData(resultData);
    }
 
    @GetMapping("/point/{page}")
    @Operation(summary = "获取point数据")
    public CommonResult<List<ResultPointAndTrendDataDTO>> getPointData(@PathVariable("page") String page) {
        List<ResultPointAndTrendDataDTO> result;
        String pointLength = pageParmService.selectValue(page, "point_length");
        String point = pageParmService.selectValue(page, "point")==null?"":pageParmService.selectValue(page, "point");
        List<String> pointList = Arrays.asList(point.split(";"));
        log.info("页面需要的point值:" + pointList);
        result = getPoints(pointList,pointLength);
        log.info("查询point返回的values" + result);
        Map<String, Object> params = new HashMap<>(1);
        params.put("modelCode", "trend_analysis");
        result.forEach(e -> {
            try {
                List<double[][]> sampleDataList = new ArrayList<>();
                List<PointDataValueDTO> nv = e.getDataValue().stream().filter(dto -> dto.getDataValue() != null).collect(Collectors.toList());
                double[][] mix = new double[nv.size()][1];
                for (int i = 0; i < nv.size(); i++) {
                    mix[i][0] = nv.get(i).getDataValue().doubleValue();
                }
                sampleDataList.add(mix);
                Map<String, Object> trendItem = runModel(params, sampleDataList);
                log.info("根据point查询的values查询模型返回的trend" + trendItem);
                e.setTrendValue(trendItem);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        return new CommonResult<List<ResultPointAndTrendDataDTO>>().setData(result);
    }
 
    private List<PageParamResultValueDTO> collateCommonResultValueDate(List<PageParamDTO> dtoList, String[] pageCode){
        List<PageParamResultValueDTO> resultDataList = new ArrayList<>();
        dtoList.forEach(e->{
            if(Arrays.asList(pageCode).contains(e.getParamCode())){
                resultDataList.add(JSONObject.parseObject(e.getParamValue(), PageParamResultValueDTO.class));
            }
        });
        return resultDataList;
    }
 
    public List<ResultPointAndTrendDataDTO> getPoints(List<String> pointList,String length){
        List<ResultPointAndTrendDataDTO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(pointList)) {
            return result;
        }
        try {
            pointList.forEach(point->{
                ResultPointAndTrendDataDTO dto = new ResultPointAndTrendDataDTO();
                dto.setPointNo(point);
                dto.setChartName(CdbmEnum.getEumByName(point).getDesc());
                dto.setUnit(CdbmEnum.getEumByName(point).getUnit());
                dto.setLength(length);
                dto.setDataValue(getPointData(point,length));
                result.add(dto);
                }
            );
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
 
    public List<PointDataValueDTO> getPointData(String param, String length){
        FeignQueryPointDTO dto = new FeignQueryPointDTO();
        dto.setEndTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -Integer.parseInt(length));
        dto.setStartTime(calendar.getTime());
        dto.setPointCode(CdbmEnum.getEumByName(param).getCode());
        dto.setType("float");
        log.info("开始获取point数据");
        List<ApiDataDTO> pointValues = feignDataApi.queryPointValues(dto);
        log.info("获取的point数据pointValues:" + pointValues);
        List<PointDataValueDTO> dots = new ArrayList<>();
        pointValues.forEach(e->{
                    PointDataValueDTO pointDataValueDTO = new PointDataValueDTO();
                    pointDataValueDTO.setItemNo(param);
                    pointDataValueDTO.setDataTime(new SimpleDateFormat("yyyy-MM-dd").format(e.getTimeStamp()));
                    pointDataValueDTO.setDataValue(BigDecimal.valueOf(e.getDataValue()));
                    dots.add(pointDataValueDTO);
            }
        );
        log.info("转换成的PointDataValueDTOS:" + dots);
        return dots;
    }
    public Map<String, Object> runModel(Map<String, Object> params, List<double[][]> sampleDataList) {
        Map<String, Object> result = new HashMap<>();
        try {
            String modelCode = (String) params.get("modelCode");
            if (StringUtils.isBlank(modelCode)) {
                return result;
            }
            if (CollectionUtils.isEmpty(sampleDataList)) {
                return result;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MILLISECOND, 0);
            result = modelHandler.run(modelCode, sampleDataList);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}