package com.zksy.base.audio.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.audio.domain.NoiseInfo;
import com.zksy.base.audio.service.NoiseInfoService;
import com.zksy.base.audio.mapper.NoiseInfoMapper;
import com.zksy.common.utils.StringUtils;
import org.springframework.stereotype.Service;

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

/**
* @author Administrator
* @description 针对表【noise_info(音频信息表)】的数据库操作Service实现
* @createDate 2026-01-16 15:23:32
*/
@Service
public class NoiseInfoServiceImpl extends ServiceImpl<NoiseInfoMapper, NoiseInfo>
    implements NoiseInfoService{
    @Override
    public IPage<NoiseInfo> queryPage(Page<NoiseInfo> page, String encode, String startTime, String endTime) {
        LambdaQueryWrapper<NoiseInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 设备编号过滤
        if (StringUtils.hasText(encode)) {
            queryWrapper.eq(NoiseInfo::getEncode, encode);
        }
        // 时间范围过滤
        if (StringUtils.hasText(startTime) && StringUtils.hasText(endTime)) {
            queryWrapper.between(NoiseInfo::getTs, startTime, endTime);
        }
        // 按时间倒序
        queryWrapper.orderByDesc(NoiseInfo::getTs);
        return baseMapper.selectPage(page, queryWrapper);
    }

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

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

    @Override
    public IPage<NoiseInfo> queryAbnormalData(Page<NoiseInfo> page, Double threshold, String startTime, String endTime) {
        return baseMapper.queryAbnormalData(page, threshold, startTime, endTime);
    }

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

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




