package com.community.huiju.dao;

import com.community.huiju.model.TpEquipment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TpEquipmentMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(TpEquipment record);

    int insertSelective(TpEquipment record);

    TpEquipment selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(TpEquipment record);

    int updateByPrimaryKey(TpEquipment record);

    /**
     * 根据小区获取 监控列表
     * @param communityId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select " +
            "tm.*, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.url_id) as monitoringUrl, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.security_guard_tel_id) as securityGuardTel, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.security_room_tel_id) as securityRoomTel, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.address_id) as monitoringAddress " +
            "FROM " +
            "tp_equipment tm " +
            "where tm.community_id = #{communityId} and tm.equipment_type = 1")
    List<TpEquipment> selectByCommunityId(@Param("communityId") Integer communityId);

    /**
     * 根据小区获取 监控列表 HLS
     * @param communityId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select " +
            "tm.*, " +
            "(select `key` FROM tp_configuration WHERE id = tm.url_id) as monitoringUrl, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.security_guard_tel_id) as securityGuardTel, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.security_room_tel_id) as securityRoomTel, " +
            "(select configuration_value FROM tp_configuration WHERE id = tm.address_id) as monitoringAddress " +
            "FROM " +
            "tp_equipment tm " +
            "where tm.community_id = #{communityId} and tm.equipment_type = 1")
    List<TpEquipment> selectHlsByCommunityId(@Param("communityId") Integer communityId);

    /**
     * 根据小区 和 房间号 查询所有设备
     * @param communityId
     * @param roomNoId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select " +
            "te.* " +
            "from tp_room_no_equipment trne left join tp_equipment te " +
            "on trne.equipment_id = te.id " +
            "where trne.community_id=#{communityId} and trne.room_no_id=#{roomNoId} and te.equipment_type=#{equipmentType}")
    List<TpEquipment> selectByCommunityIdAndRoomNoId(@Param("communityId") Integer communityId, @Param("roomNoId") Integer roomNoId, @Param("equipmentType") Integer equipmentType);


    /**
     * 根据小区 和 单元号 查询所有设备
     * @param communityId
     * @param unitId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select " +
            "te.* " +
            "from tp_unit_equipment trne left join tp_equipment te " +
            "on trne.equipment_id = te.id " +
            "where trne.community_id=#{communityId} and trne.unit_id=#{unitId} and te.equipment_type=#{equipmentType}")
    List<TpEquipment> selectByCommunityIdAndUnitId(@Param("communityId") Integer communityId, @Param("unitId") Integer unitId, @Param("equipmentType") Integer equipmentType);

}