潘志宝
2024-10-10 9100b0c14dca46366cfe79336eb85d5f0ca793af
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java
@@ -1,24 +1,20 @@
package com.iailab.module.data.api.controller;
import com.iailab.api.IFeignModelApi;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.data.common.dto.IndexQueryDTO;
import com.iailab.module.data.common.dto.echarts.BarLineDTO;
import com.iailab.module.data.common.dto.echarts.SeriesItem;
import com.iailab.module.data.common.utils.R;
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.dto.ApiPointValueQueryDTO;
import com.iailab.module.data.point.collection.PointCollector;
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 com.iailab.module.data.point.service.DaPointValueService;
import com.iailab.module.data.ind.collection.IndItemCollector;
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
import com.iailab.module.data.influxdb.service.InfluxDBService;
import com.iailab.module.data.api.dto.DeviceValueDTO;
import com.iailab.module.data.api.utils.ApiSecurityUtils;
import com.iailab.module.data.ind.dto.IndItemValueDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@@ -26,11 +22,14 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import static com.iailab.framework.common.pojo.CommonResult.success;
/**
 * @author PanZhibao
@@ -58,21 +57,16 @@
    @Resource
    private PointCollector pointCollector;
    @Resource
    private IndItemCollector indItemCollector;
    @Resource
    private IFeignModelApi feignModelApi;
    @PermitAll
    @PostMapping("/point/history")
    @Operation(summary = "point历史数据")
    public R pointHistory(HttpServletResponse response, HttpServletRequest
    public CommonResult<Map<String, List<Map<String, Object>>>> pointHistory(HttpServletResponse response, HttpServletRequest
            request, @RequestBody ApiPointValueQueryDTO queryDto) {
        try {
            apiSecurityUtils.validate(request);
            Map<String, List<Map<String, Object>>> data = new HashMap<>();
            if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
                return R.ok().put("data", data);
                return success(data);
            }
            if (queryDto.getStart() == null) {
                queryDto.setStart(new Date());
@@ -84,7 +78,7 @@
            params.put("pointNos", queryDto.getPointNos());
            List<DaPointDTO> pointList = daPointService.list(params);
            if (CollectionUtils.isEmpty(pointList)) {
                return R.ok().put("data", data);
                return success(data);
            }
            List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
                InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
@@ -93,23 +87,23 @@
                return pojo;
            }).collect(Collectors.toList());
            data = influxDBService.queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd());
            return R.ok().put("data", data);
            return success(data);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
            return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
        }
    }
    @PostMapping("/point/current")
    @Operation(summary = "point当前实时数据")
    public R pointCurrent(HttpServletResponse response, HttpServletRequest
    public CommonResult<Map<String, Object>> pointCurrent(HttpServletResponse response, HttpServletRequest
            request, @RequestBody List<String> pointNos) {
        try {
            // apiSecurityUtils.validate(request);
            Map<String, Object> data = pointCollector.getCurrentValue(pointNos);
            return R.ok().put("data", data);
            return success(data);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
            return new CommonResult<Map<String, Object>>().setMsg(ex.getMessage());
        }
    }
@@ -170,12 +164,12 @@
    @PostMapping("/pointRelation/history")
    @Operation(summary = "pointRelation历史数据")
    public R pointRelationHistory(HttpServletResponse response, HttpServletRequest
    public CommonResult<Map<String, List<Map<String, Object>>>> pointRelationHistory(HttpServletResponse response, HttpServletRequest
            request, @RequestBody ApiPointValueQueryDTO queryDto) {
        try {
            Map<String, List<Map<String, Object>>> data = new HashMap<>();
            if (CollectionUtils.isEmpty(queryDto.getPointNos())) {
                return R.ok().put("data", data);
                return success(data);
            }
            if (queryDto.getStart() == null) {
                queryDto.setStart(new Date());
@@ -185,11 +179,11 @@
            }
            data = daPointValueService.getHistoryList(queryDto);
            if (CollectionUtils.isEmpty(data)) {
                return R.ok().put("data", data);
                return success(data);
            }
            return R.ok().put("data", data);
            return success(data);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
            return new CommonResult<Map<String, List<Map<String, Object>>>>().setMsg(ex.getMessage());
        }
    }
@@ -210,73 +204,5 @@
            });
        }
        return CommonResult;
    }
    @PostMapping("/ind-item/values")
    @Operation(summary = "point当前实时数据")
    public R indItemValues(HttpServletResponse response, HttpServletRequest
            request, @RequestBody List<String> itemNos) {
        try {
            // apiSecurityUtils.validate(request);
            Map<String, List<IndItemValueDTO>> data = indItemCollector.getValueList(itemNos);
            return R.ok().put("data", data);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }
    @PostMapping("/ind-item/current-value")
    @Operation(summary = "point当前实时数据")
    public R indItemCurrentValue(HttpServletResponse response, HttpServletRequest
            request, @RequestBody List<String> itemNos) {
        try {
            Map<String, BigDecimal> data = new HashMap<>();
            // apiSecurityUtils.validate(request);
            Map<String, List<IndItemValueDTO>> dataList = indItemCollector.getValueList(itemNos);
            dataList.forEach((k, v) -> {
                data.put(k, CollectionUtils.isEmpty(v) ? null : v.get(0).getDataValue());
            });
            return R.ok().put("data", data);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }
    @PostMapping("/ind-item/values-trend")
    @Operation(summary = "point当前实时数据")
    public R indItemValuesTrend(HttpServletResponse response, HttpServletRequest
            request, @RequestBody List<String> itemNos) {
        try {
            apiSecurityUtils.validate(request);
            Map<String, Object> CommonResult = new HashMap<>();
            Map<String, List<IndItemValueDTO>> values = indItemCollector.getValueList(itemNos);
            CommonResult.put("values", values);
            Map<String, Map<String, Object>> trend = new HashMap<>();
            Map<String, Object> params = new HashMap<>(1);
            params.put("modelCode", "trend_analysis");
            values.forEach((k, v) -> {
                try {
                    List<double[][]> sampleDataList = new ArrayList<>();
                    List<IndItemValueDTO> nv = v.stream().filter(dto -> {
                        return 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 = feignModelApi.runModel(params, sampleDataList);
                    trend.put(k, trendItem);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            });
            CommonResult.put("trend", trend);
            return R.ok().put("data", CommonResult);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error(ex.getMessage());
        }
    }
}