<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yzt.airconditionersystem.mapper.FacilityAlarmMapper">

    <resultMap id="BaseResultMap" type="com.yzt.airconditionersystem.bean.FacilityAlarm">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="time" column="time" jdbcType="TIMESTAMP"/>
            <result property="alarmContent" column="alertm_content" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="gatewayId" column="gateway_id" jdbcType="VARCHAR"/>
            <result property="lastingTime" column="lasting_time" jdbcType="VARCHAR"/>
            <result property="operatorName" column="operatorName" jdbcType="VARCHAR"/>
            <result property="district" column="disctrict" jdbcType="VARCHAR"/>
            <result property="operatorId" column="operatorId" jdbcType="VARCHAR"/>
    </resultMap>


    <sql id="Base_Column_List">
        id,time,alarm_content,
        status,lasting_time
    </sql>

    <select id="selectByGatewayIdAndCompanyId" resultType="com.yzt.airconditionersystem.bean.FacilityAlarm" >
        SELECT DISTINCT
            f.id,
            f.time,
            f.alarm_content AS alarmContent,
            f.gateway_id AS gatewayId,
            f.lasting_time AS lastingTime,
            f.status,
            c.name AS companyName,
            c.district,
            c.detailed_address AS detailedAddress,
            c.id AS companyId
        FROM
            facility_alarm f
        INNER JOIN
            gateway g
        ON
            f.gateway_id = gateway_id
        INNER JOIN
            company c
        ON
            g.company_id = c.id
        <where>
            <if test="companyIds != '' and companyIds != null" >
                <foreach collection="companyIds" item="companyId" index="index"
                    open="c.id in (" separator="," close=")" nullable="false">
                    #{companyId}
                </foreach>
            </if>
        </where>
        ORDER BY f.time DESC
    </select>

</mapper>
