<?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.TDetectorInfoMapper">
    
    <resultMap type="TDetectorInfo" id="TDetectorInfoResult">
        <result property="detectorId"    column="detector_id"    />
        <result property="enterpriseId"    column="enterprise_id"    />
        <result property="userId"    column="user_id"    />
        <result property="detectorName"    column="detector_name"    />
        <result property="detectorCode"    column="detector_code"    />
        <result property="detectorAddr"    column="detector_addr"    />
        <result property="detectorType"    column="detector_type"    />
        <result property="medium"    column="medium"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="iotNo"    column="iot_no"    />
        <result property="linkman"    column="linkman"    />
        <result property="phone"    column="phone"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="detectorStatus"    column="detector_status"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
        <result property="beyondDevicename"    column="beyondDevicename"    />
        <result property="enterpriseName"    column="enterprise_name"    />
    </resultMap>

    <sql id="selectTDetectorInfoVo">
        select detector_id, enterprise_id, user_id, detector_name, detector_code, detector_addr, detector_type, medium, longitude, latitude, iot_no, linkman, phone, create_time, update_time, alarm_time, detector_status, is_del, remarks from t_detector_info
    </sql>

    <select id="selectTDetectorInfoList" parameterType="TDetectorInfo" resultMap="TDetectorInfoResult">
        <include refid="selectTDetectorInfoVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="enterpriseId != null and enterpriseId != 0 "> and enterprise_id = #{enterpriseId}</if>
            <if test="detectorName != null  and detectorName != ''"> and detector_name like concat('%', #{detectorName}, '%')</if>
            <if test="detectorCode != null  and detectorCode != ''"> and detector_code = #{detectorCode}</if>
            <if test="detectorAddr != null  and detectorAddr != ''"> and detector_addr = #{detectorAddr}</if>
            <if test="detectorType != null  and detectorType != ''"> and detector_type = #{detectorType}</if>
            <if test="medium != null  and medium != ''"> and medium = #{medium}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="iotNo != null  and iotNo != ''"> and iot_no = #{iotNo}</if>
            <if test="linkman != null  and linkman != ''"> and linkman = #{linkman}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
            <if test="alarmTime != null "> and alarm_time = #{alarmTime}</if>
            <if test="detectorStatus != null  and detectorStatus != ''"> and detector_status = #{detectorStatus}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
        order by detector_status desc, alarm_time desc
    </select>
    
    <select id="selectTDetectorInfoById" parameterType="Long" resultMap="TDetectorInfoResult">
        <include refid="selectTDetectorInfoVo"/>
        where detector_id = #{detectorId}
    </select>

    <select id="selectTDetectorInfoByCode" parameterType="String" resultMap="TDetectorInfoResult">
        <include refid="selectTDetectorInfoVo"/>
        where detector_code = #{detectorCode}
        and is_del = '0'
    </select>
        
    <insert id="insertTDetectorInfo" parameterType="TDetectorInfo" useGeneratedKeys="true" keyProperty="detectorId">
        insert into t_detector_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="enterpriseId != null and enterpriseId != 0">enterprise_id,</if>
            <if test="detectorName != null">detector_name,</if>
            <if test="detectorCode != null">detector_code,</if>
            <if test="detectorAddr != null">detector_addr,</if>
            <if test="detectorType != null">detector_type,</if>
            <if test="medium != null">medium,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="iotNo != null">iot_no,</if>
            <if test="linkman != null">linkman,</if>
            <if test="phone != null">phone,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="detectorStatus != null">detector_status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="enterpriseId != null and enterpriseId != 0">#{enterpriseId},</if>
            <if test="detectorName != null">#{detectorName},</if>
            <if test="detectorCode != null">#{detectorCode},</if>
            <if test="detectorAddr != null">#{detectorAddr},</if>
            <if test="detectorType != null">#{detectorType},</if>
            <if test="medium != null">#{medium},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="iotNo != null">#{iotNo},</if>
            <if test="linkman != null">#{linkman},</if>
            <if test="phone != null">#{phone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="detectorStatus != null">#{detectorStatus},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>
    <insert id="insertBatch" parameterType="java.util.List" >
        insert into t_detector_info (
        user_id, detector_name, detector_code, detector_addr, detector_type, medium, longitude, latitude, linkman, phone, create_time
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.userId,jdbcType=INTEGER}, #{item.detectorName,jdbcType=VARCHAR}, #{item.detectorCode,jdbcType=VARCHAR},
            #{item.detectorAddr,jdbcType=VARCHAR}, #{item.detectorType,jdbcType=VARCHAR}, #{item.medium,jdbcType=VARCHAR},
            #{item.longitude,jdbcType=DECIMAL}, #{item.latitude,jdbcType=DECIMAL}, #{item.linkman,jdbcType=VARCHAR},
            #{item.phone,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <update id="updateBatch" parameterType="java.util.List">
        update t_detector_info d
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="user_id=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.userId != null">
                        when detector_code=#{item.detectorCode} then #{item.userId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="detector_name=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.detectorName != null &amp;&amp; item.detectorName != ''">
                        when detector_code=#{item.detectorCode} then #{item.detectorName}
                    </if>
                </foreach>
            </trim>
            <trim prefix="detector_addr=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.detectorAddr != null &amp;&amp; item.detectorAddr != ''">
                        when detector_code=#{item.detectorCode} then #{item.detectorAddr}
                    </if>
                </foreach>
            </trim>
            <trim prefix="detector_type=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.detectorType != null &amp;&amp; item.detectorType != ''">
                        when detector_code=#{item.detectorCode} then #{item.detectorType}
                    </if>
                </foreach>
            </trim>
            <trim prefix="medium=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.medium != null &amp;&amp; item.medium != ''">
                        when detector_code=#{item.detectorCode} then #{item.medium}
                    </if>
                </foreach>
            </trim>
            <trim prefix="longitude=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.longitude != null &amp;&amp; item.longitude != ''">
                        when detector_code=#{item.detectorCode} then #{item.longitude}
                    </if>
                </foreach>
            </trim>
            <trim prefix="latitude=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.latitude != null &amp;&amp; item.latitude != ''">
                        when detector_code=#{item.detectorCode} then #{item.latitude}
                    </if>
                </foreach>
            </trim>
            <trim prefix="linkman=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.linkman != null &amp;&amp; item.linkman != ''">
                        when detector_code=#{item.detectorCode} then #{item.linkman}
                    </if>
                </foreach>
            </trim>
            <trim prefix="phone=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.phone != null &amp;&amp; item.phone != ''">
                        when detector_code=#{item.detectorCode} then #{item.phone}
                    </if>
                </foreach>
            </trim>
            <trim prefix="is_del=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.isDel != null &amp;&amp; item.isDel != ''">
                        when detector_code=#{item.detectorCode} then #{item.isDel}
                    </if>
                </foreach>
            </trim>
            <trim prefix="detector_status=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.detectorStatus != null &amp;&amp; item.detectorStatus != ''">
                        when detector_code=#{item.detectorCode} then #{item.detectorStatus}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.updateTime != null">
                        when detector_code=#{item.detectorCode} then #{item.updateTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="alarm_time=case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    when detector_code=#{item.detectorCode} then #{item.alarmTime}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="item" index="index">
            detector_code=#{item.detectorCode}
        </foreach>
    </update>

    <update id="updateTDetectorInfo" parameterType="TDetectorInfo">
        update t_detector_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="detectorName != null">detector_name = #{detectorName},</if>
            <if test="detectorCode != null">detector_code = #{detectorCode},</if>
            <if test="detectorAddr != null">detector_addr = #{detectorAddr},</if>
            <if test="detectorType != null">detector_type = #{detectorType},</if>
            <if test="medium != null">medium = #{medium},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="iotNo != null">iot_no = #{iotNo},</if>
            <if test="linkman != null">linkman = #{linkman},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            alarm_time = #{alarmTime},
            <if test="detectorStatus != null">detector_status = #{detectorStatus},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where detector_id = #{detectorId}
    </update>

    <delete id="deleteTDetectorInfoById" parameterType="Long">
        delete from t_detector_info where detector_id = #{detectorId}
    </delete>

    <delete id="deleteTDetectorInfoByIds" parameterType="String">
        delete from t_detector_info where detector_id in 
        <foreach item="detectorId" collection="array" open="(" separator="," close=")">
            #{detectorId}
        </foreach>
    </delete>
    <select id="selectDetectorNum" resultType="java.util.HashMap">
        SELECT IFNULL(SUM(IF(detector_status=0,1,0)),0) AS changNum,IFNULL(SUM(detector_status=1),0) AS liNum,
        IFNULL(SUM(IF(detector_status=2,1,0)),0) AS baoNum 
        FROM t_detector_info WHERE is_del = 0
    </select>

    <select id="selectDetectorMointor" parameterType="TDetectorInfo" resultMap="TDetectorInfoResult">
        SELECT
          device.device_name AS beyondDevicename,
          detector.detector_name,
          detector.detector_code,
          enterprise.enterprise_name,
          detector.medium,
          detector.detector_status,
          detector.update_time AS reportTime
        FROM
          t_relation_device_detail_info relation
        INNER JOIN t_detector_info detector ON detector.detector_code = relation.iot_no
        INNER JOIN t_device_info device ON device.device_id = relation.relation_device_id
        INNER JOIN t_enterprise_info enterprise ON enterprise.enterprise_id = device.beyond_enterprise_id
        <where>
            relation.device_type = '3' AND relation.is_del = '0'
            <if test="enterpriseId != null  and enterpriseId != ''"> and enterprise.enterprise_id = #{enterpriseId}</if>
            <if test="detectorName != null  and detectorName != ''"> and detector.detector_name like concat('%', #{detectorName}, '%')</if>
            <if test="detectorCode != null  and detectorCode != ''"> and detector.detector_code like concat('%', #{detectorCode}, '%')</if>
        </where>
    </select>
</mapper>