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.SubsystemVoltageMapper;
import com.evobms.project.battery.domain.SubsystemVoltage;
import com.evobms.project.battery.service.ISubsystemVoltageService;

/**
 * 可充电储能子系统电压信息Service业务层处理
 *
 * @author 田志阳
 * @date 2025-11-15
 */
@Service
public class SubsystemVoltageServiceImpl extends ServiceImpl<SubsystemVoltageMapper, SubsystemVoltage> implements ISubsystemVoltageService
{
    @Autowired
    private SubsystemVoltageMapper subsystemVoltageMapper;

    /**
     * 查询可充电储能子系统电压信息
     *
     * @param id 可充电储能子系统电压信息主键
     * @return 可充电储能子系统电压信息
     */
    @Override
    public SubsystemVoltage selectSubsystemVoltageById(Long id)
    {
        return subsystemVoltageMapper.selectSubsystemVoltageById(id);
    }

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

    /**
     * 新增可充电储能子系统电压信息
     *
     * @param subsystemVoltage 可充电储能子系统电压信息
     * @return 结果
     */
    @Override
    public int insertSubsystemVoltage(SubsystemVoltage subsystemVoltage)
    {
        return subsystemVoltageMapper.insertSubsystemVoltage(subsystemVoltage);
    }

    /**
     * 修改可充电储能子系统电压信息
     *
     * @param subsystemVoltage 可充电储能子系统电压信息
     * @return 结果
     */
    @Override
    public int updateSubsystemVoltage(SubsystemVoltage subsystemVoltage)
    {
        return subsystemVoltageMapper.updateSubsystemVoltage(subsystemVoltage);
    }

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

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

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