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

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.evobms.project.vehicledata.mapper.VehicleLocationMapper;
import com.evobms.project.vehicledata.domain.VehicleLocation;
import com.evobms.project.vehicledata.service.IVehicleLocationService;

/**
 * 车辆位置数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-15
 */
@Service
public class VehicleLocationServiceImpl extends ServiceImpl<VehicleLocationMapper, VehicleLocation> implements IVehicleLocationService
{
    @Autowired
    private VehicleLocationMapper vehicleLocationMapper;

    /**
     * 查询车辆位置数据
     *
     * @param id 车辆位置数据主键
     * @return 车辆位置数据
     */
    @Override
    public VehicleLocation selectVehicleLocationById(Long id)
    {
        return vehicleLocationMapper.selectVehicleLocationById(id);
    }

    /**
     * 查询车辆位置数据列表
     *
     * @param vehicleLocation 车辆位置数据
     * @return 车辆位置数据
     */
    @Override
    public List<VehicleLocation> selectVehicleLocationList(VehicleLocation vehicleLocation)
    {
        LambdaQueryWrapper<VehicleLocation> qw = new LambdaQueryWrapper<>();
        if (vehicleLocation != null) {
            if (vehicleLocation.getDeviceId() != null && !vehicleLocation.getDeviceId().isEmpty()) {
                qw.eq(VehicleLocation::getDeviceId, vehicleLocation.getDeviceId());
            }
            if (vehicleLocation.getTimestamp() != null) {
                qw.ge(VehicleLocation::getTimestamp, vehicleLocation.getTimestamp());
            }
            java.util.Map<String, Object> params = vehicleLocation.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(VehicleLocation::getTimestamp, begin);
                }
                if (end != null) {
                    qw.le(VehicleLocation::getTimestamp, end);
                }
            }
        }
        qw.orderByDesc(VehicleLocation::getTimestamp);
        return vehicleLocationMapper.selectList(qw);
    }

    /**
     * 新增车辆位置数据
     *
     * @param vehicleLocation 车辆位置数据
     * @return 结果
     */
    @Override
    public int insertVehicleLocation(VehicleLocation vehicleLocation)
    {
        return vehicleLocationMapper.insertVehicleLocation(vehicleLocation);
    }

    /**
     * 修改车辆位置数据
     *
     * @param vehicleLocation 车辆位置数据
     * @return 结果
     */
    @Override
    public int updateVehicleLocation(VehicleLocation vehicleLocation)
    {
        return vehicleLocationMapper.updateVehicleLocation(vehicleLocation);
    }

    /**
     * 批量删除车辆位置数据
     *
     * @param ids 需要删除的车辆位置数据主键
     * @return 结果
     */
    @Override
    public int deleteVehicleLocationByIds(Long[] ids)
    {
        return vehicleLocationMapper.deleteVehicleLocationByIds(ids);
    }

    /**
     * 删除车辆位置数据信息
     *
     * @param id 车辆位置数据主键
     * @return 结果
     */
    @Override
    public int deleteVehicleLocationById(Long id)
    {
        return vehicleLocationMapper.deleteVehicleLocationById(id);
    }

    @Override
    public VehicleLocation selectLatestByDeviceId(String deviceId) {
        LambdaQueryWrapper<VehicleLocation> qw = new LambdaQueryWrapper<>();
        qw.eq(VehicleLocation::getDeviceId, deviceId)
          .orderByDesc(VehicleLocation::getTimestamp)
          .last("limit 1");
        return vehicleLocationMapper.selectOne(qw);
    }

    @Override
    public List<VehicleLocation> selectByDeviceAndRange(String deviceId, Date start, Date end, Integer limit) {
        LambdaQueryWrapper<VehicleLocation> qw = new LambdaQueryWrapper<>();
        qw.eq(VehicleLocation::getDeviceId, deviceId)
          .ge(VehicleLocation::getTimestamp, start)
          .le(VehicleLocation::getTimestamp, end)
          .orderByAsc(VehicleLocation::getTimestamp);
        if (limit != null && limit > 0) {
            qw.last("limit " + limit);
        }
        return vehicleLocationMapper.selectList(qw);
    }
}
