package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.Device;
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 DeviceMapper extends BaseMapper<Device> {

    /**
     * 根据设备ID查询设备
     */
    @Select("SELECT * FROM t_device WHERE device_id = #{deviceId}")
    Device selectByDeviceId(@Param("deviceId") String deviceId);
    
    /**
     * 根据门状态查询设备列表
     */
    @Select("SELECT * FROM t_device WHERE door_status = #{doorStatus}")
    List<Device> selectByDoorStatus(@Param("doorStatus") String doorStatus);
    
    /**
     * 更新设备门状态
     */
    @Update("UPDATE t_device SET door_status = #{doorStatus} WHERE device_id = #{deviceId}")
    int updateDoorStatus(@Param("deviceId") String deviceId, @Param("doorStatus") String doorStatus);
    
    /**
     * 批量更新设备门状态
     */
    @Update("<script>" +
            "UPDATE t_device SET door_status = CASE device_id " +
            "<foreach collection='deviceStatusMap' item='status' index='deviceId'>" +
            "WHEN #{deviceId} THEN #{status} " +
            "</foreach>" +
            "END " +
            "WHERE device_id IN " +
            "<foreach collection='deviceStatusMap.keySet()' item='deviceId' open='(' separator=',' close=')'>" +
            "#{deviceId}" +
            "</foreach>" +
            "</script>")
    int batchUpdateDoorStatus(@Param("deviceStatusMap") java.util.Map<String, String> deviceStatusMap);
}
