<?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.zehong.system.mapper.TDeviceInfoMapper">

    <resultMap type="TDeviceInfo" id="TDeviceInfoResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceAddr"    column="device_addr"    />
        <result property="deviceModel"    column="device_model"    />
        <result property="deviceType"    column="device_type"    />
        <result property="beyondEnterpriseId"    column="beyond_enterprise_id"    />
        <result property="beyondEnterpriseName"    column="beyond_enterprise_name"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="iconUrl"    column="icon_url"    />
        <result property="linkman"    column="linkman"    />
        <result property="phone"    column="phone"    />
        <result property="installationTime"    column="installation_time"    />
        <result property="inspectionTime"    column="inspection_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

<!--关联设备map-->
    <resultMap type="TDeviceInfoS" id="TDeviceInfoResultS">
        <result property="relationDeviceDetailId"    column="relation_device_detail_id"    />
        <result property="relationDeviceId"    column="relation_device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceModel"    column="device_model"    />
        <result property="deviceType"    column="device_type"    />
        <result property="iotNo"    column="iot_no"    />
        <result property="detectionMedium"    column="detection_medium"    />
        <result property="remarksn"    column="remarks"    />
    </resultMap>

    <sql id="selectTDeviceInfoVo">
        select a.device_id, a.device_name, a.device_code, a.device_addr, a.device_model,
        (CASE  a.device_type WHEN '1' THEN '阀井' WHEN '2' THEN '调压箱'WHEN '3' THEN '巡检员' WHEN '4' THEN '视频' end) as  device_type ,
        a.beyond_enterprise_id, a.longitude, a.latitude, a.icon_url, a.linkman, a.phone, a.installation_time, a.inspection_time, a.create_by, a.create_time, a.update_by, a.update_time, a.is_del, a.remarks
        ,b.enterprise_name as beyond_enterprise_name
        from t_device_info a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
    </sql>

    <select id="selectTDeviceInfoList" parameterType="TDeviceInfo" resultMap="TDeviceInfoResult">
        <include refid="selectTDeviceInfoVo"/>
        <where>
            a.is_del='0'
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and beyond_enterprise_id = #{beyondEnterpriseId}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="deviceAddr != null  and deviceAddr != ''"> and device_addr = #{deviceAddr}</if>
        </where>
        order by a.device_id desc
    </select>
    <select id="queryBasicInforByNameForBigWindows" resultType="map">
        SELECT
            device_id AS id,
            device_name AS NAME,
            CONCAT( '[[', longitude, ', ', latitude, ']]' ) AS coordinates,
            device_type AS type
        FROM
            t_device_info
        WHERE
          <if test="name != null  and name != ''"> device_name like concat('%', #{name}, '%') AND</if>
          longitude IS NOT NULL
          AND latitude IS NOT NULL UNION
        SELECT
            site_station_id AS id,
            site_station_name AS NAME,
            CONCAT( '[[', longitude, ', ', latitude, ']]' ) AS coordinates,
            '3' AS type
        FROM
            t_site_station_info
        WHERE
          <if test="name != null  and name != ''"> site_station_name like concat('%', #{name}, '%') AND</if>
          longitude IS NOT NULL
          AND latitude IS NOT NULL UNION
            select t1.userId as id,
            t1.nickName as NAME,
            CONCAT( '[[', t1.longitude, ', ', t1.latitude, ']]' ) AS coordinates,
            CASE
            t1.userType
            WHEN '1' THEN
            '4'
            WHEN '2' THEN
            '5'
            WHEN '3' THEN
            '6'
            END AS type
            from(

            select a.user_id AS userId,
            a.detector_id AS detectorId,
            a.detector_status AS detectorStatus,
            a.nick_name AS nickName,
            a.user_type AS userType,
            a.address AS address,
            a.longitude AS longitude,
            a.latitude AS latitude,
            a.linkman AS linkman,
            a.phone AS phone,
            a.email AS email,
            CASE a.detector_type
            WHEN '1' THEN '家用探测器'
            WHEN '2' THEN '工业探测器' END AS detectorType,
            COUNT(a.detector_id) AS detectorCount,
            SUM(CASE a.detector_status WHEN '0' THEN 1 ELSE 0 END) AS onLineNum,
            SUM(CASE a.detector_status WHEN '1' THEN 1 ELSE 0 END) AS offLineNum
            from(select
            a1.* ,
            a2.detector_id,
            a2.detector_type,
            a2.detector_status FROM t_detector_user a1
            LEFT JOIN t_detector_info a2 ON a1.user_id = a2.user_id  AND a2.is_del = '0'
            where a1.is_del = '0'
            <if test="name != null  and name != ''"> and a1.nick_name like concat('%', #{name}, '%')</if>
            and a1.longitude is not null and a1.latitude is not null) a
            group by a.user_id,a.detector_type

            ) t1 left join (

            select b.user_id AS userId,
            COUNT(b.id) AS historyAlarmNum,
            SUM(CASE b.is_cancel_alarm WHEN '0' THEN 1 ELSE 0 END) AS processingAlarmNum,
            COUNT(b.id) - SUM(CASE b.is_cancel_alarm WHEN '0' THEN 1 ELSE 0 END) AS cancelAlarmNum
            from(select b1.id,b1.is_cancel_alarm,b2.*
            from t_detector_report_data b1
            left join t_detector_info b2 on b1.detector_code = b2.detector_code
            right join t_detector_user b3 on b2.user_id = b3.user_id
            where b2.is_del = '0' and b3.is_del = '0'  ) b
            group by b.user_id,b.detector_type

            ) t2 on t2.userId = t1.userId  UNION

        SELECT
            video_manager_id AS id,
            video_name AS NAME,
            CONCAT( '[[', longitude, ', ', latitude, ']]' ) AS coordinates,
            '7' AS type
        FROM
            t_video_manager
        WHERE
          <if test="name != null  and name != ''"> video_name like concat('%', #{name}, '%') AND</if>
          longitude IS NOT NULL
          AND latitude IS NOT NULL UNION
        SELECT
            user_id AS id,
            nick_name AS NAME,
            CONCAT( '[[', longitude, ', ', latitude, ']]' ) AS coordinates,
            '8' AS type
        FROM
            t_yehuaqi_user
        WHERE
          <if test="name != null  and name != ''"> nick_name like concat('%', #{name}, '%') AND</if>
          longitude IS NOT NULL
          AND latitude IS NOT NULL UNION
        SELECT
            pipe_id AS id,
            pipe_addr AS NAME,
            coordinates,
            '9' AS type
        FROM
            t_pipe_info
        WHERE
          <if test="name != null  and name != ''"> pipe_addr like concat('%', #{name}, '%') AND</if>
          coordinates IS NOT NULL
    </select>
    <select id="selectDeviceList" parameterType="TDeviceInfo" resultMap="TDeviceInfoResult">
        SELECT * FROM t_device_info WHERE device_name like concat('%', #{deviceName}, '%')
    </select>


    <select id="selectTDeviceInfoById" parameterType="Long" resultMap="TDeviceInfoResult">
    select device_id, device_name, device_code, device_addr, device_model,device_type,
        beyond_enterprise_id, beyond_enterprise_name, longitude, latitude, icon_url, linkman, phone, installation_time, inspection_time, create_by, create_time, update_by, update_time, is_del, remarks from t_device_info
        where device_id = #{deviceId} and is_del='0'
    </select>

    <select id="getDeviceInfoAlarmList" resultType="Map">
        select a3.device_id as deviceId,
               a3.device_type as deviceType
        from t_relation_device_detail_info a1
        left join t_detector_info a2 on a1.iot_no = a2.detector_code
        right join t_device_info a3 on a1.relation_device_id = a3.device_id
        where a1.is_del = '0' and a2.is_del = '0' and a3.is_del = '0' and a2.detector_status = '2'
    </select>

    <insert id="insertTDeviceInfo" parameterType="TDeviceInfo" useGeneratedKeys="true" keyProperty="deviceId">
        insert into t_device_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceName != null">device_name,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceAddr != null">device_addr,</if>
            <if test="deviceModel != null">device_model,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id,</if>
            <if test="beyondEnterpriseName != null">beyond_enterprise_name,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="iconUrl != null">icon_url,</if>
            <if test="linkman != null">linkman,</if>
            <if test="phone != null">phone,</if>
            <if test="installationTime != null">installation_time,</if>
            <if test="inspectionTime != null">inspection_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceAddr != null">#{deviceAddr},</if>
            <if test="deviceModel != null">#{deviceModel},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
            <if test="beyondEnterpriseName != null">#{beyondEnterpriseName},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="iconUrl != null">#{iconUrl},</if>
            <if test="linkman != null">#{linkman},</if>
            <if test="phone != null">#{phone},</if>
            <if test="installationTime != null">#{installationTime},</if>
            <if test="inspectionTime != null">#{inspectionTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTDeviceInfo" parameterType="TDeviceInfo">
        update t_device_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceAddr != null">device_addr = #{deviceAddr},</if>
            <if test="deviceModel != null">device_model = #{deviceModel},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id = #{beyondEnterpriseId},</if>
            <if test="beyondEnterpriseName != null">beyond_enterprise_name = #{beyondEnterpriseName},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="iconUrl != null">icon_url = #{iconUrl},</if>
            <if test="linkman != null">linkman = #{linkman},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="installationTime != null">installation_time = #{installationTime},</if>
            <if test="inspectionTime != null">inspection_time = #{inspectionTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <update id="deleteTDeviceInfoById" parameterType="Long">
        update t_device_info set is_del='1' where device_id = #{deviceId}
    </update>

    <update id="deleteTDeviceInfoByIds" parameterType="String">
        update  t_device_info set is_del='1' where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>


    <insert id="insertDeviceDetailInfo">
        INSERT INTO t_relation_device_detail_info (relation_device_id,detection_medium,device_name,device_model,device_type,iot_no,remarks)
        VALUES
            (#{relationDeviceId},#{detectionMedium},#{deviceName},#{deviceModel},#{deviceType},#{iotNo},#{remarksn})
    </insert>

    <!--获取设备详细信息列表-->
    <select id="getdeviceDetailInfo" resultMap="TDeviceInfoResultS">
        select relation_device_detail_id,relation_device_id,device_name,device_model,device_type,iot_no,remarks,detection_medium
        from t_relation_device_detail_info  where relation_device_id =#{deviceId}
    </select>

    <!--查询无绑定的 关联设备信息-->
    <select id="selectDetailInfo" resultMap="TDeviceInfoResultS">
        select relation_device_detail_id,detection_medium,relation_device_id,device_name,device_model,iot_no,remarks, (CASE  device_type WHEN '1' THEN '压力表' WHEN '2' THEN '流量计' WHEN '3' THEN '探测器' end) as  device_type
        from t_relation_device_detail_info  where is_del='0' and relation_device_id is  null
        <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
        <if test="deviceModel != null  and deviceModel != ''"> and device_model like concat('%', #{deviceModel}, '%')</if>
        <if test="iotNo != null  and iotNo != ''"> and iot_no like concat('%', #{iotNo}, '%')</if>
        group by relation_device_detail_id
        order by relation_device_detail_id desc
    </select>

    <!--查询设备已经关联的关联设备-->
    <select id="selectDetailInfoList" resultMap="TDeviceInfoResultS">
        select relation_device_detail_id,detection_medium,relation_device_id,device_name,device_model,iot_no,remarks, (CASE  device_type WHEN '1' THEN '压力表' WHEN '2' THEN '流量计' WHEN '3' THEN '探测器' end) as  device_type
        from t_relation_device_detail_info  where relation_device_id=#{id} and relation_device_type=#{relationDeviceType}
    </select>

    <!--关联设备数据删除接口-->
    <update id="deleteDetailInfo">
        update  t_relation_device_detail_info set is_del='1' where relation_device_detail_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>

    <!--关联设备数据删除接口 单条删除-->
    <update id="deleteDetailinfOS">
        update t_relation_device_detail_info set is_del='1' where relation_device_detail_id = #{deviceId}
    </update>


    <!--将关联设备信息与设备进行绑定-->
    <update id="updatetRelationDeviceDetailInfo" parameterType="java.util.List">
        update t_relation_device_detail_info set relation_device_id = #{id} ,relation_device_type =#{relationDevicetype}
        where
        relation_device_detail_id in
        <foreach collection="gettDeviceInfoS" item="items" index="key" open="(" separator="," close=")">
           #{items.relationDeviceDetailId}
        </foreach>
    </update>

    <!--解绑 设备与关联设备关系-->
    <update id="deleteDeviceDetailInfo">
        update t_relation_device_detail_info set relation_device_id=NULL where relation_device_detail_id=#{deviceId}
    </update>
    <select id="selectDeviceNum" resultType="java.util.HashMap">
        SELECT SUM(IF(device_type=1,1,0)) AS fajing, SUM(IF(device_type=2,1,0)) AS tiaoya ,
        (SELECT COUNT(video_manager_id) FROM t_video_manager WHERE is_del = 0
            and beyond_enterprise_id IN
            <foreach item="enterpriseId" collection="enterpriseIds" open="(" separator="," close=")">
                #{enterpriseId}
            </foreach>
        ) AS videoNum,
        (SELECT COUNT(site_station_id) FROM t_site_station_info WHERE is_del = 0
            and beyond_enterprise_id IN
            <foreach item="enterpriseId" collection="enterpriseIds" open="(" separator="," close=")">
                #{enterpriseId}
            </foreach>
        ) AS stationNum
        FROM t_device_info
        WHERE is_del = 0 and beyond_enterprise_id IN
        <foreach item="enterpriseId" collection="enterpriseIds" open="(" separator="," close=")">
            #{enterpriseId}
        </foreach>
    </select>
</mapper>