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> get(@PathVariable("page") String page) { List dtoList = pageParmService.selectPageParamByPage(page); List 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>().setData(resultData); } @GetMapping("/point/{page}") @Operation(summary = "获取point数据") public CommonResult> getPointData(@PathVariable("page") String page) { List result; String pointLength = pageParmService.selectValue(page, "point_length"); String point = pageParmService.selectValue(page, "point")==null?"":pageParmService.selectValue(page, "point"); List pointList = Arrays.asList(point.split(";")); log.info("页面需要的point值:" + pointList); result = getPoints(pointList,pointLength); log.info("查询point返回的values" + result); Map params = new HashMap<>(1); params.put("modelCode", "trend_analysis"); result.forEach(e -> { try { List sampleDataList = new ArrayList<>(); List 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 trendItem = runModel(params, sampleDataList); log.info("根据point查询的values查询模型返回的trend" + trendItem); e.setTrendValue(trendItem); } catch (Exception ex) { ex.printStackTrace(); } }); return new CommonResult>().setData(result); } private List collateCommonResultValueDate(List dtoList, String[] pageCode){ List 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 getPoints(List pointList,String length){ List 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 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 pointValues = feignDataApi.queryPointValues(dto); log.info("获取的point数据pointValues:" + pointValues); List 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 runModel(Map params, List sampleDataList) { Map 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; } }