package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.ReplenisherDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 补货员-设备关联Mapper
 */
@Mapper
public interface ReplenisherDeviceMapper extends BaseMapper<ReplenisherDevice> {

    /**
     * 获取补货员绑定的所有设备ID列表
     */
    @Select("SELECT device_id FROM t_replenisher_device WHERE replenisher_id = #{replenisherId}")
    List<String> selectDeviceIdsByReplenisherId(@Param("replenisherId") Long replenisherId);

    /**
     * 获取设备绑定的所有关联记录
     */
    @Select("SELECT * FROM t_replenisher_device WHERE device_id = #{deviceId} ORDER BY create_time DESC")
    List<ReplenisherDevice> selectByDeviceId(@Param("deviceId") String deviceId);

    /**
     * 批量删除某补货员的设备绑定
     */
    @Delete("DELETE FROM t_replenisher_device WHERE replenisher_id = #{replenisherId}")
    int deleteByReplenisherId(@Param("replenisherId") Long replenisherId);

    /**
     * 批量删除某设备的补货员绑定
     */
    @Delete("DELETE FROM t_replenisher_device WHERE device_id = #{deviceId}")
    int deleteByDeviceId(@Param("deviceId") String deviceId);

    /**
     * 检查绑定是否已存在
     */
    @Select("SELECT COUNT(*) FROM t_replenisher_device WHERE replenisher_id = #{replenisherId} AND device_id = #{deviceId}")
    int countByReplenisherIdAndDeviceId(@Param("replenisherId") Long replenisherId,
                                        @Param("deviceId") String deviceId);

    /**
     * 批量插入关联（忽略已存在）
     */
    int batchInsertIgnore(@Param("list") List<ReplenisherDevice> list);
}
