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

import java.util.Date;
import java.util.List;
import java.util.Map;

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.system.mapper.BmsDevicesMapper;
import com.evobms.project.system.domain.BmsDevices;
import com.evobms.project.system.service.IBmsDevicesService;

/**
 * BBOX管理Service业务层处理
 *
 * @author 田志阳
 * @date 2025-10-10
 */
@Service
public class BmsDevicesServiceImpl extends ServiceImpl<BmsDevicesMapper, BmsDevices> implements IBmsDevicesService
{
    @Autowired
    private BmsDevicesMapper bmsDevicesMapper;

    /**
     * 查询BBOX管理
     *
     * @param id BBOX管理主键
     * @return BBOX管理
     */
    @Override
    public BmsDevices selectBmsDevicesById(String id)
    {
        return bmsDevicesMapper.selectBmsDevicesById(id);
    }

    /**
     * 查询BBOX管理列表
     *
     * @param bmsDevices BBOX管理
     * @return BBOX管理
     */
    @Override
    public List<BmsDevices> selectBmsDevicesList(BmsDevices bmsDevices)
    {
        LambdaQueryWrapper<BmsDevices> qw = new LambdaQueryWrapper<>();
        if (bmsDevices != null) {
            if (bmsDevices.getDeviceId() != null && !bmsDevices.getDeviceId().isEmpty()) {
                qw.eq(BmsDevices::getDeviceId, bmsDevices.getDeviceId());
            }
            Map<String, Object> params = bmsDevices.getParams();
            if (params != null) {
                Date begin = DateUtils.parseDate(params.get("beginTime"));
                Date end = DateUtils.parseDate(params.get("endTime"));
                if (begin != null) {
                    qw.and(w -> w.ge(BmsDevices::getUpdateTime, begin).or().ge(BmsDevices::getCreateTime, begin));
                }
                if (end != null) {
                    qw.and(w -> w.le(BmsDevices::getUpdateTime, end).or().le(BmsDevices::getCreateTime, end));
                }
            }
        }
        qw.orderByDesc(BmsDevices::getUpdateTime).orderByDesc(BmsDevices::getCreateTime);
        return bmsDevicesMapper.selectList(qw);
    }

    /**
     * 新增BBOX管理
     *
     * @param bmsDevices BBOX管理
     * @return 结果
     */
    @Override
    public int insertBmsDevices(BmsDevices bmsDevices)
    {
        bmsDevices.setCreateTime(DateUtils.getNowDate());
        return bmsDevicesMapper.insertBmsDevices(bmsDevices);
    }

    /**
     * 修改BBOX管理
     *
     * @param bmsDevices BBOX管理
     * @return 结果
     */
    @Override
    public int  updateBmsDevices(BmsDevices bmsDevices)
    {
        bmsDevices.setUpdateTime(DateUtils.getNowDate());
        return bmsDevicesMapper.updateBmsDevices(bmsDevices);
    }

    /**
     * 批量删除BBOX管理
     *
     * @param ids 需要删除的BBOX管理主键
     * @return 结果
     */
    @Override
    public int deleteBmsDevicesByIds(String[] ids)
    {
        return bmsDevicesMapper.deleteBmsDevicesByIds(ids);
    }

    /**
     * 删除BBOX管理信息
     *
     * @param id BBOX管理主键
     * @return 结果
     */
    @Override
    public int deleteBmsDevicesById(String id)
    {
        return bmsDevicesMapper.deleteBmsDevicesById(id);
    }

    @Override
    public BmsDevices selectBmsDevicesBySn(String deviceSn) {
        LambdaQueryWrapper<BmsDevices> bmslq = new LambdaQueryWrapper<>();
        bmslq.eq(BmsDevices::getDeviceSn, deviceSn);
        return baseMapper.selectOne(bmslq);
    }

    @Override
    public BmsDevices selectBmsDevicesList1(BmsDevices bmsDevices) {
        LambdaQueryWrapper<BmsDevices> eq = new LambdaQueryWrapper<>();
        if (bmsDevices != null) {
                eq.eq(BmsDevices::getDeviceSn, bmsDevices.getDeviceSn());
            return baseMapper.selectOne(eq);
        }
        return null;
    }

}
