package com.zksy.base.telemetry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.pressure.domain.FirefightingPressure;
import com.zksy.base.telemetry.domain.TelemetryData;
import com.zksy.base.telemetry.service.TelemetryDataService;
import com.zksy.base.telemetry.mapper.TelemetryDataMapper;
import com.zksy.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【telemetry_data(平升遥测终端协议数据表)】的数据库操作Service实现
* @createDate 2026-01-16 15:43:42
*/
@Service
public class TelemetryDataServiceImpl extends ServiceImpl<TelemetryDataMapper, TelemetryData>
    implements TelemetryDataService{
    @Override
    public IPage<TelemetryData> queryPage(Page<TelemetryData> page, String deviceCode, LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<TelemetryData> queryWrapper = new LambdaQueryWrapper<>();
        // 设备编码过滤
        if (StringUtils.hasText(deviceCode)) {
            queryWrapper.eq(TelemetryData::getDeviceCode, deviceCode);
        }
        // 时间范围过滤
        if (startTime != null && endTime != null) {
            queryWrapper.between(TelemetryData::getCreateTime, startTime, endTime);
        }
        // 按创建时间倒序
        queryWrapper.orderByDesc(TelemetryData::getCreateTime);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> statTelemetryByDevice(LocalDateTime startTime, LocalDateTime endTime) {
        return baseMapper.statTelemetryByDevice(startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> statTelemetryTrendByHour(String deviceCode, LocalDateTime startTime, LocalDateTime endTime) {
        return baseMapper.statTelemetryTrendByHour(deviceCode, startTime, endTime);
    }

    @Override
    public IPage<TelemetryData> queryAbnormalData(Page<TelemetryData> page, Double pressureThreshold, Double tempThreshold,
                                                  Double speedThreshold, LocalDateTime startTime, LocalDateTime endTime) {
        return baseMapper.queryAbnormalData(page, pressureThreshold, tempThreshold, speedThreshold, startTime, endTime);
    }

    @Override
    public TelemetryData getLatestDataByDevice(String deviceCode) {
        if (!StringUtils.hasText(deviceCode)) {
            return null;
        }
        return baseMapper.getLatestDataByDevice(deviceCode);
    }

    @Override
    public List<String> getAllDeviceCodes() {
        LambdaQueryWrapper<TelemetryData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TelemetryData::getDeviceCode).groupBy(TelemetryData::getDeviceCode);
        return baseMapper.selectObjs(queryWrapper).stream()
                .map(obj -> (String) obj).collect(Collectors.toList());
    }

    @Override
    public List<String> getDeviceGroup() {
        return lambdaQuery()
                .select(TelemetryData::getDeviceCode)
                .groupBy(TelemetryData::getDeviceCode)
                .list()
                .stream()
                .map(TelemetryData::getDeviceCode)
                .map(String::valueOf)
                .collect(Collectors.toList());
    }
}




