package com.imdroid.secapi.dto;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface TrafficDeviceMappingMapper extends BaseMapper<TrafficDeviceMapping> {
    @Select("SELECT * FROM traffic_device_mappings WHERE deviceid = #{deviceId} AND endtime IS NULL LIMIT 1")
    TrafficDeviceMapping findActiveByDeviceId(String deviceId);

    @Select("SELECT * FROM traffic_device_mappings WHERE iccid = #{iccid} AND endtime IS NULL LIMIT 1")
    TrafficDeviceMapping findActiveByIccid(String iccid);

    @Select("SELECT * FROM traffic_device_mappings WHERE deviceid = #{deviceId} ORDER BY starttime DESC")
    List<TrafficDeviceMapping> findHistoryByDeviceId(String deviceId);

    @Update("UPDATE traffic_device_mappings SET endtime = NOW() WHERE id = #{id}")
    int endMapping(Integer id);
}