package tech.glinfo.enbao.modules.sh.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tech.glinfo.enbao.modules.sh.entity.ShFamilyRoomEntity;

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

/**
 * @author lyt
 * @date 2022/1/5 10:50
 */
@Mapper
public interface OtherShFamilyRoomDao extends BaseMapper<ShFamilyRoomEntity> {


    @Select("<script>" +
            "DELETE t1, t2 FROM sh_family_room AS t1 LEFT JOIN sh_device AS t2 ON t1.id = t2.room_id " +
            "WHERE t1.id = ${id}" +
            " </script>")
    void delete(Integer id);


    @Select("<script>" +
            "SELECT a.id, a.name, COUNT(b.id) `device` FROM sh_family_room a, sh_device b WHERE a.id = b.room_id AND a.family_id = ${familyId}" +
            " </script>")
    List<Map<String, Object>> list(Integer familyId);


    @Select("<script>" +
            " update sh_device set room_id = NULL where id = ${deviceId}" +
            " </script>")
    void removeRelation(Integer deviceId);

}
