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

import java.util.List;
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.SubsystemTemperatureMapper;
import com.evobms.project.battery.domain.SubsystemTemperature;
import com.evobms.project.battery.service.ISubsystemTemperatureService;

/**
 * 可充电储能子系统温度信息Service业务层处理
 *
 * @author 田志阳
 * @date 2025-11-15
 */
@Service
public class SubsystemTemperatureServiceImpl extends ServiceImpl<SubsystemTemperatureMapper, SubsystemTemperature> implements ISubsystemTemperatureService
{
    @Autowired
    private SubsystemTemperatureMapper subsystemTemperatureMapper;

    /**
     * 查询可充电储能子系统温度信息
     *
     * @param id 可充电储能子系统温度信息主键
     * @return 可充电储能子系统温度信息
     */
    @Override
    public SubsystemTemperature selectSubsystemTemperatureById(Long id)
    {
        return subsystemTemperatureMapper.selectSubsystemTemperatureById(id);
    }

    /**
     * 查询可充电储能子系统温度信息列表
     *
     * @param subsystemTemperature 可充电储能子系统温度信息
     * @return 可充电储能子系统温度信息
     */
    @Override
    public List<SubsystemTemperature> selectSubsystemTemperatureList(SubsystemTemperature subsystemTemperature)
    {
        LambdaQueryWrapper<SubsystemTemperature> qw = new LambdaQueryWrapper<>();
        if (subsystemTemperature != null) {
            if (subsystemTemperature.getDeviceId() != null && !subsystemTemperature.getDeviceId().isEmpty()) {
                qw.eq(SubsystemTemperature::getDeviceId, subsystemTemperature.getDeviceId());
            }
            if (subsystemTemperature.getTimestamp() != null) {
                qw.ge(SubsystemTemperature::getTimestamp, subsystemTemperature.getTimestamp());
            }
            java.util.Map<String, Object> params = subsystemTemperature.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(SubsystemTemperature::getTimestamp, begin);
                }
                if (end != null) {
                    qw.le(SubsystemTemperature::getTimestamp, end);
                }
            }
        }
        qw.orderByDesc(SubsystemTemperature::getTimestamp);
        return subsystemTemperatureMapper.selectList(qw);
    }

    /**
     * 新增可充电储能子系统温度信息
     *
     * @param subsystemTemperature 可充电储能子系统温度信息
     * @return 结果
     */
    @Override
    public int insertSubsystemTemperature(SubsystemTemperature subsystemTemperature)
    {
        return subsystemTemperatureMapper.insertSubsystemTemperature(subsystemTemperature);
    }

    /**
     * 修改可充电储能子系统温度信息
     *
     * @param subsystemTemperature 可充电储能子系统温度信息
     * @return 结果
     */
    @Override
    public int updateSubsystemTemperature(SubsystemTemperature subsystemTemperature)
    {
        return subsystemTemperatureMapper.updateSubsystemTemperature(subsystemTemperature);
    }

    /**
     * 批量删除可充电储能子系统温度信息
     *
     * @param ids 需要删除的可充电储能子系统温度信息主键
     * @return 结果
     */
    @Override
    public int deleteSubsystemTemperatureByIds(Long[] ids)
    {
        return subsystemTemperatureMapper.deleteSubsystemTemperatureByIds(ids);
    }

    /**
     * 删除可充电储能子系统温度信息信息
     *
     * @param id 可充电储能子系统温度信息主键
     * @return 结果
     */
    @Override
    public int deleteSubsystemTemperatureById(Long id)
    {
        return subsystemTemperatureMapper.deleteSubsystemTemperatureById(id);
    }

    @Override
    public SubsystemTemperature selectLatestByDeviceAndSubsystem(String deviceId, Integer subsystemNo) {
        return subsystemTemperatureMapper.selectLatestByDeviceAndSubsystem(deviceId, subsystemNo);
    }
}
