houzhongjian
2024-10-30 a28ca3f36d0ace05966a8c0fac1e4b5fe921f882
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/admin/ApiDataController.java
对比新文件
@@ -0,0 +1,178 @@
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.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.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.point.common.PointDataTypeEnum;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.point.service.DaPointService;
import com.iailab.module.data.api.dto.DeviceValueDTO;
import com.iailab.module.data.api.utils.ApiSecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
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
 * @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;
    @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.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND),
                            getFormatValue(item.getDataType(), 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);
    }
    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;
    }
}