package com.evobms.project.battery.service.impl;

import java.util.List;
import com.evobms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evobms.project.battery.mapper.ExtremeValuesMapper;
import com.evobms.project.battery.domain.ExtremeValues;
import com.evobms.project.battery.service.IExtremeValuesService;

/**
 * 电池极值数据Service业务层处理
 *
 * @author 田志阳
 * @date 2025-10-14
 */
@Service
public class ExtremeValuesServiceImpl extends ServiceImpl<ExtremeValuesMapper, ExtremeValues> implements IExtremeValuesService
{
    @Autowired
    private ExtremeValuesMapper extremeValuesMapper;

    /**
     * 查询电池极值数据
     *
     * @param id 电池极值数据主键
     * @return 电池极值数据
     */
    @Override
    public ExtremeValues selectExtremeValuesById(Long id)
    {
        return extremeValuesMapper.selectExtremeValuesById(id);
    }

    /**
     * 查询电池极值数据列表
     *
     * @param extremeValues 电池极值数据
     * @return 电池极值数据
     */
    @Override
    public List<ExtremeValues> selectExtremeValuesList(ExtremeValues extremeValues)
    {
        LambdaQueryWrapper<ExtremeValues> qw = new LambdaQueryWrapper<>();
        if (extremeValues != null) {
            if (extremeValues.getDeviceId() != null && !extremeValues.getDeviceId().isEmpty()) {
                qw.eq(ExtremeValues::getDeviceId, extremeValues.getDeviceId());
            }
            if (extremeValues.getTimestamp() != null) {
                qw.ge(ExtremeValues::getTimestamp, extremeValues.getTimestamp());
            }
            java.util.Map<String, Object> params = extremeValues.getParams();
            if (params != null) {
                java.util.Date begin = com.evobms.common.utils.DateUtils.parseDate(params.get("beginTime"));
                java.util.Date end = com.evobms.common.utils.DateUtils.parseDate(params.get("endTime"));
                if (begin != null) {
                    qw.ge(ExtremeValues::getTimestamp, begin);
                }
                if (end != null) {
                    qw.le(ExtremeValues::getTimestamp, end);
                }
            }
        }
        qw.orderByDesc(ExtremeValues::getTimestamp);
        return extremeValuesMapper.selectList(qw);
    }

    /**
     * 新增电池极值数据
     *
     * @param extremeValues 电池极值数据
     * @return 结果
     */
    @Override
    public int insertExtremeValues(ExtremeValues extremeValues)
    {
        extremeValues.setCreateTime(DateUtils.getNowDate());
        return extremeValuesMapper.insertExtremeValues(extremeValues);
    }

    /**
     * 修改电池极值数据
     *
     * @param extremeValues 电池极值数据
     * @return 结果
     */
    @Override
    public int updateExtremeValues(ExtremeValues extremeValues)
    {
        extremeValues.setUpdateTime(DateUtils.getNowDate());
        return extremeValuesMapper.updateExtremeValues(extremeValues);
    }

    /**
     * 批量删除电池极值数据
     *
     * @param ids 需要删除的电池极值数据主键
     * @return 结果
     */
    @Override
    public int deleteExtremeValuesByIds(Long[] ids)
    {
        return extremeValuesMapper.deleteExtremeValuesByIds(ids);
    }

    /**
     * 删除电池极值数据信息
     *
     * @param id 电池极值数据主键
     * @return 结果
     */
    @Override
    public int deleteExtremeValuesById(Long id)
    {
        return extremeValuesMapper.deleteExtremeValuesById(id);
    }

    @Override
    public ExtremeValues selectLatestByDeviceId(String deviceId) {
        return extremeValuesMapper.selectLatestByDeviceId(deviceId);
    }

    @Override
    public ExtremeValues selectLatestByDeviceIdBefore(String deviceId, java.util.Date beforeTime) {
        return extremeValuesMapper.selectLatestByDeviceIdBefore(deviceId, beforeTime);
    }
}
