<?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.TDetectorUserMapper">

    <resultMap type="TDetectorUser" id="TDetectorUserResult">
        <result property="userId"    column="user_id"    />
        <result property="username"    column="username"    />
        <result property="nickName"    column="nick_name"    />
        <result property="beyondEnterpriseId"    column="beyond_enterprise_id"    />
        <result property="userType"    column="user_type"    />
        <result property="address"    column="address"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="linkman"    column="linkman"    />
        <result property="phone"    column="phone"    />
        <result property="email"    column="email"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTDetectorUserVo">
        select user_id, username, nick_name, user_type, address, longitude, latitude, linkman, phone, email, is_del, remarks from t_detector_user
    </sql>

    <select id="selectTDetectorUserList" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        select user_id, username, nick_name,  (CASE user_type WHEN '1' THEN '居民用户' WHEN '2' THEN '商业用户'WHEN '3' THEN '工业用户'end) as   user_type , address, longitude, latitude, linkman, phone, email, is_del, remarks from t_detector_user
        <where> is_del = '0'
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and username like concat('%', #{username}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
        </where>
        group by user_id desc
    </select>

    <select id="selectTDetectorUser" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        <include refid="selectTDetectorUserVo"/>
        <where>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="username != null  and username != ''"> and username = #{username}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name = #{nickName}</if>
        </where>
        group by user_id desc
    </select>

    <select id="selectTDetectorUserById" parameterType="Long" resultMap="TDetectorUserResult">
        <include refid="selectTDetectorUserVo"/>
        where user_id = #{userId}
    </select>

    <select id="countTDetectorUser" resultType="TDetectorUserVO" parameterType="TDetectorUser">
        select t1.*,
			 IFNULL(t2.historyAlarmNum,0) AS historyAlarmNum,
			 IFNULL(t2.processingAlarmNum,0) AS processingAlarmNum,
			 IFNULL(t2.cancelAlarmNum,0) AS cancelAlarmNum
        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.detector_id,
					    a1.detector_type,
                        a1.detector_status,
					    a2.* from t_detector_info a1
				right join t_detector_user a2 on a1.user_id = a2.user_id
				<where> a1.is_del = '0' and a2.is_del = '0'
                    <if test="userId != null  and userId != ''"> and a2.user_id = #{userId}</if>
                </where>) 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'
                    <if test="userId != null  and userId != ''"> and b3.user_id = #{userId}</if>
				 </where>) b
		    group by b.user_id,b.detector_type

        ) t2 on t2.userId = t1.userId
    </select>

    <select id="getDetectorUserAlarmList" resultType="Map">
        select a2.user_id as userId
        from t_detector_info a1
        right join t_detector_user a2 on a1.user_id = a2.user_id
        where a1.is_del = '0' and a2.is_del = '0' and a1.detector_status = '2'
    </select>

    <insert id="insertTDetectorUser" parameterType="TDetectorUser" useGeneratedKeys="true" keyProperty="userId">
        insert into t_detector_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null">username,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="userType != null">user_type,</if>
            <if test="address != null">address,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id,</if>
            <if test="linkman != null">linkman,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null">#{username},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="userType != null">#{userType},</if>
            <if test="address != null">#{address},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
            <if test="linkman != null">#{linkman},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTDetectorUser" parameterType="TDetectorUser">
        update t_detector_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null">username = #{username},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="address != null">address = #{address},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="linkman != null">linkman = #{linkman},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteTDetectorUserById" parameterType="Long">
        delete from t_detector_user where user_id = #{userId}
    </delete>

    <delete id="deleteTDetectorUserByIds" parameterType="String">
        delete from t_detector_user where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
    <select id="selectUserNum" resultType="java.util.HashMap">
        SELECT SUM(IF(user_type=1,1,0)) AS juminNum,SUM(IF(user_type=2,1,0)) AS shangNum,
        SUM(IF(user_type=3,1,0)) AS gongNum FROM t_detector_user WHERE is_del = 0
    </select>
</mapper>