package com.zksy.base.radar.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.radar.domain.RadarData;
import com.zksy.base.radar.service.RadarDataService;
import com.zksy.base.radar.mapper.RadarDataMapper;
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 针对表【radar_data(平升雷达流量计数据表)】的数据库操作Service实现
* @createDate 2026-01-16 15:37:38
*/
@Service
public class RadarDataServiceImpl extends ServiceImpl<RadarDataMapper, RadarData>
    implements RadarDataService{
    @Override
    public IPage<RadarData> queryPage(Page<RadarData> page, String deviceCode, LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<RadarData> queryWrapper = new LambdaQueryWrapper<>();
        // 设备编码过滤
        if (StringUtils.hasText(deviceCode)) {
            queryWrapper.eq(RadarData::getDeviceCode, deviceCode);
        }
        // 时间范围过滤
        if (startTime != null && endTime != null) {
            queryWrapper.between(RadarData::getCreateTime, startTime, endTime);
        }
        // 按创建时间倒序
        queryWrapper.orderByDesc(RadarData::getCreateTime);
        return baseMapper.selectPage(page, queryWrapper);
    }

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

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

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

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

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

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




