<?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.ruoyi.ems.mapper.EmsDeviceMapper">

    <resultMap type="com.ruoyi.ems.domain.EmsDevice" id="EmsDeviceResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"        column="device_code"    />
        <result property="deviceName"        column="device_name"    />
        <result property="deviceType"        column="device_type"    />
        <result property="deviceTypeName"    column="device_type_name"    />
        <result property="deviceStatus"      column="device_status"    />
        <result property="areaType"          column="area_type"    />
        <result property="refArea"           column="ref_area"    />
        <result property="refFacs"           column="ref_facs"    />
        <result property="refFacsName"       column="ref_facs_name"    />
        <result property="subsystemCode"     column="subsystem_code"    />
        <result property="subsystemName"     column="subsystem_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectEmsDeviceVo">
        select
            d.`id`, d.`device_code`, d.`device_name`, d.`device_type`, dt.`type_name` as device_type_name, d.`device_status`, d.`area_type`, d.`ref_area`, d.`ref_facs`, f.`facs_name` as ref_facs_name, d.`create_time`, d.`update_time`, d.`subsystem_code`, s.`system_name` as subsystem_name
        from adm_ems_device d
            LEFT JOIN dim_devc_type dt ON d.`device_type` = dt.`type_code`
            LEFT JOIN adm_ems_facs f ON d.`ref_facs` = f.`facs_code`
            LEFT JOIN adm_ems_subsystem s ON d.`subsystem_code` = s.`system_code`
    </sql>

    <select id="selectEmsDeviceList" parameterType="com.ruoyi.ems.domain.EmsDevice" resultMap="EmsDeviceResult">
        <include refid="selectEmsDeviceVo"/>
        <where>
            <if test="deviceCode != null  and deviceCode != ''"> and d.`device_code` = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''"> and d.`device_name` like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null "> and d.`device_type` = #{deviceType}</if>
            <if test="deviceStatus != null "> and d.`device_status` = #{deviceStatus}</if>
            <if test="areaType != null and areaType != ''"> and d.`area_type` = #{areaType}</if>
            <if test="refArea != null  and refArea != ''"> and d.`ref_area` = #{refArea}</if>
            <if test="refFacs != null  and refFacs != ''"> and d.`ref_facs` = #{refFacs}</if>
            <if test="subsystemCode != null  and subsystemCode != ''"> and d.`subsystem_code` = #{subsystemCode}</if>
        </where>
    </select>

    <!-- 递归查询 区域/建筑/区块 下的设备 -->
    <select id="selectByAreaTree" parameterType="com.ruoyi.ems.domain.vo.QueryDevice" resultMap="EmsDeviceResult">
        <include refid="selectEmsDeviceVo"/>
        <where>
            <if test="deviceCode != null  and deviceCode != ''"> and d.`device_code` = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''"> and d.`device_name` like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null "> and d.`device_type` = #{deviceType}</if>
            <if test="refFacs != null  and refFacs != ''"> and d.`ref_facs` = #{refFacs}</if>
            <if test="subsystemCode != null  and subsystemCode != ''"> and d.`subsystem_code` = #{subsystemCode}</if>
            <if test="areaType == 'Area' and refArea != null and refArea != ''">
                and ((d.`area_type` = 'Area' and d.`ref_area` = #{refArea})
                <if test="buildingCodes != null">
                   or (d.`area_type` = 'Building' and d.`ref_area` in
                    <foreach item="buildingCode" collection="buildingCodes" open="(" separator="," close=")">
                        #{buildingCode}
                    </foreach>)
                    <if test="zoningCodes != null">
                        or (d.`area_type` = 'Zoning' and d.`ref_area` in
                        <foreach item="zoningCode" collection="zoningCodes" open="(" separator="," close=")">
                            #{zoningCode}
                        </foreach>)
                    </if>
                </if>)
            </if>
            <if test="areaType == 'Building' and refArea != null and refArea != ''">
                and ((d.`area_type` = 'Building' and d.`ref_area` = #{refArea})
                <if test="zoningCodes != null">
                   or (d.`area_type` = 'Zoning' and d.`ref_area` in
                    <foreach item="zoningCode" collection="zoningCodes" open="(" separator="," close=")">
                        #{zoningCode}
                    </foreach>)
                </if>)
            </if>
            <if test="areaType == 'Zoning' and refArea != null and refArea != ''">
                and d.`area_type` = 'Zoning' and d.`ref_area` = #{refArea}
            </if>
        </where>
        ORDER BY d.`area_type`, d.`ref_area`
    </select>

    <select id="selectEmsDeviceById" parameterType="Long" resultMap="EmsDeviceResult">
        <include refid="selectEmsDeviceVo"/>
        where d.`id` = #{id}
    </select>

    <select id="selectEmsDeviceByCode" parameterType="java.lang.String" resultMap="EmsDeviceResult">
        <include refid="selectEmsDeviceVo"/>
        where d.`device_code` = #{code}
    </select>

    <insert id="insertEmsDevice" parameterType="com.ruoyi.ems.domain.EmsDevice" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="areaType != null">area_type,</if>
            <if test="refArea != null">ref_area,</if>
            <if test="refFacs != null">ref_facs,</if>
            <if test="subsystemCode != null">subsystem_code,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="areaAncestors !=null">area_ancestors,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="areaType != null">#{areaType},</if>
            <if test="refArea != null">#{refArea},</if>
            <if test="refFacs != null">#{refFacs},</if>
            <if test="subsystemCode != null">#{subsystemCode},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="areaAncestors !=null">#{areaAncestors},</if>
         </trim>
    </insert>

    <update id="updateEmsDevice" parameterType="com.ruoyi.ems.domain.EmsDevice">
        update adm_ems_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="areaType != null">area_type = #{areaType},</if>
            <if test="refArea != null">ref_area = #{refArea},</if>
            <if test="refFacs != null">ref_facs = #{refFacs},</if>
            <if test="subsystemCode != null">subsystem_code = #{subsystemCode},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="areaAncestors !=null">area_ancestors = #{areaAncestors},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsDeviceById" parameterType="Long">
        delete from adm_ems_device where id = #{id}
    </delete>

    <delete id="deleteEmsDeviceByIds" parameterType="String">
        delete from adm_ems_device where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectDevicePath" resultType="String">
        SELECT
        <if test="areaType == 'Area'">
            area.area_code path
        </if>
        <if test="areaType == 'Building'">
            CONCAT( area.area_code, ',', building.bldg_code) path
        </if>
        <if test="areaType == 'Zoning'">
            CONCAT( area.area_code, ',', building.bldg_code, ',', zone.zoning_code ) path
        </if>
        FROM
        adm_service_area area
        <if test="areaType == 'Building' or areaType == 'Zoning'">
            INNER JOIN adm_area_building building ON area.area_code = building.area_code
        </if>
        <if test="areaType == 'Zoning'">
            INNER JOIN adm_area_building_zoning zone ON building.bldg_code = zone.bldg_code
        </if>
        <where>
            <if test="areaType == 'Area'">
                area.area_code = #{refArea}
            </if>
            <if test="areaType == 'Building'">
                building.bldg_code = #{refArea}
            </if>
            <if test="areaType == 'Zoning'">
                zone.zoning_code = #{refArea}
            </if>
        </where>
        limit 1
    </select>
    <select id="cntDeviceStatus">
        SELECT
        device_status deviceStatus,
        count( id ) cnt
        FROM
        adm_ems_device
        <where>
            <if test="areaCode !=null and areaCode!=''">
                area_ancestors like CONCAT('%',#{areaCode},'%')
            </if>
        </where>
        GROUP BY
        device_status
    </select>

    <select id="calcDeviceOnlineSummary">
        SELECT
            device_status  deviceStatus,
            devcType.type_name typeName,
            count( devc.id ) total,
            SUM( CASE WHEN device_status = '0' THEN 1 ELSE 0 END ) AS onlineCount
        FROM
            adm_ems_device devc
                INNER JOIN dim_devc_type devcType ON devc.device_type = devcType.type_code
        <where>
            <if test="areaCode !=null and areaCode!=''">
                area_ancestors like CONCAT('%',#{areaCode},'%')
            </if>
        </where>
        GROUP BY
            device_type
    </select>

</mapper>
