<?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="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
        <result property="beyondEnterpriseName"    column="enterprise_name"    />
    </resultMap>

    <resultMap type="TDetectorUserInspectVo" id="TDetectorUserInspectResult">
        <result property="userId"    column="user_id"    />
        <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="linkman"    column="linkman"    />
        <result property="phone"    column="phone"    />
        <result property="beyondEnterpriseName"    column="enterprise_name"    />
        <result property="isInspect"    column="is_inspect"    />
        <result property="inspector"    column="inspector"    />
        <result property="inspectDate"    column="inspect_date"    />
    </resultMap>

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

    <select id="selectTDetectorUserList" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        select a.user_id, a.username, a.nick_name,  (CASE a.user_type WHEN '1' THEN '居民用户' WHEN '2' THEN '商业用户'WHEN '3' THEN '工业用户'WHEN '4' THEN '煤改气用户'end) as   user_type , a.address, a.longitude, a.latitude, a.linkman, a.phone, a.email, a.create_time, a.update_time, a.is_del, a.remarks,
        b.enterprise_name
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        <where> a.is_del = '0'
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and a.beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and a.username like concat('%', #{username}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and (a.nick_name like concat('%', #{nickName}, '%') or a.username like concat('%', #{nickName}, '%'))</if>
            <if test="isInspect != null  and isInspect == 0">
             and a.user_id not in
                <foreach item="hasInspectUser" collection="hasInspectUser" open="(" separator="," close=")">
                    #{hasInspectUser}
                </foreach>
            </if>
            <if test="isInspect != null and isInspect == 1">
                and a.user_id in
                <foreach item="hasInspectUser" collection="hasInspectUser" open="(" separator="," close=")">
                    #{hasInspectUser}
                </foreach>
            </if>
        </where>
        order by a.user_id desc
    </select>
    <select id="selectTDetectorListstatus" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        SELECT a.user_id, a.username, a.nick_name , a.address,
        a.longitude, a.latitude, a.linkman, a.phone, a.email, a.create_time,
        a.update_time, a.is_del, a.remarks,IF(inspect.status IS NULL,0,inspect.`status`) AS taskId

        FROM t_detector_user a
        LEFT JOIN (
        SELECT * FROM (SELECT MAX(inspect_id) AS inId  FROM t_work_task_inspect
        WHERE task_id = #{taskId} GROUP BY receive_id) t1
        LEFT JOIN t_work_task_inspect t ON t1.inId = t.inspect_id
        ) inspect  ON inspect.receive_id = a.user_id
        <where> a.is_del = '0'
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and a.beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and a.username like concat('%', #{username}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and (a.nick_name like concat('%', #{nickName}, '%') or a.username like concat('%', #{nickName}, '%'))</if>
        </where>

    </select>
    <select id="selectTDetectorUser" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        select a.user_id, a.beyond_enterprise_id,a.username, a.nick_name,  (CASE a.user_type WHEN '1' THEN '居民用户' WHEN '2' THEN '商业用户'WHEN '3' THEN '工业用户'WHEN '4' THEN '煤改气用户'end) as   user_type , a.address, a.longitude, a.latitude, a.linkman, a.phone, a.email, a.create_time, a.update_time, a.is_del, a.remarks,
        b.enterprise_name
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        <where>
            <if test="isDel != null  and isDel != ''"> and a.is_del = #{isDel}</if>
            <if test="username != null  and username != ''"> and a.username = #{username}</if>
            <if test="nickName != null  and nickName != ''"> and a.nick_name = #{nickName}</if>
        </where>
        group by a.user_id
        order by a.user_id desc
    </select>

    <select id="selectTDetectorUserById" parameterType="Long" resultMap="TDetectorUserResult">
       select a.user_id, a.beyond_enterprise_id,a.username, a.nick_name, a.user_type, a.address, a.longitude, a.latitude,
              a.linkman, a.phone, a.email, a.create_time, a.update_time, a.is_del, a.remarks, b.enterprise_name
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        where a.user_id = #{userId}
    </select>

    <select id="countTDetectorUserForMassMarks" resultType="TDetectorUserVO" parameterType="TDetectorUser">
        select a.user_id AS userId,
               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
        from(select   a2.* from  t_detector_user a2) a
    </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.* ,
                    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="userId != null  and userId != ''"> and a1.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="beyondEnterpriseId != null">beyond_enterprise_id = #{beyondEnterpriseId},</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="createTime != null">create_time = #{createTime},</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 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 IFNULL(SUM(IF(user_type=1,1,0)),0) AS juminNum,IFNULL(SUM(IF(user_type=2,1,0)),0) AS shangNum,
        IFNULL(SUM(IF(user_type=3,1,0)),0) AS gongNum FROM t_detector_user WHERE is_del = 0
    </select>

    <!--查询用户统计信息-->
    <select id="userStatistics" resultType="com.zehong.system.domain.TDetectorUserCount">
    SELECT
      (select count(user_id) from t_detector_user   where  is_del='0'   <if test="id != -2"> and beyond_enterprise_id = #{id}</if>)as totalNumberUsers,
      (select count(user_id) from t_detector_user   where user_type=1 and is_del='0'  <if test="id != -2"> and beyond_enterprise_id = #{id}</if>)as residentUsers,
      (select count(user_id) from t_detector_user   where user_type=2 and is_del='0'  <if test="id != -2"> and beyond_enterprise_id = #{id}</if>)as businessUser,
      (select count(user_id) from t_detector_user   where user_type=3 and is_del='0'  <if test="id != -2"> and beyond_enterprise_id = #{id}</if>)as industrialUsers
       FROM `t_detector_user` limit 0,1
    </select>

    <select id="getDetectorUserInspectInfo" parameterType="TDetectorUserInspectVo" resultMap="TDetectorUserInspectResult">
        SELECT
            a.user_id,
            a.username,
            a.nick_name,
            (
                CASE a.user_type
                WHEN '1' THEN
                    '居民用户'
                WHEN '2' THEN
                    '商业用户'
                WHEN '3' THEN
                    '工业用户'
                WHEN '4' THEN
                    '煤改气用户'
                END
            ) AS user_type,
            a.address,
            a.longitude,
            a.latitude,
            a.linkman,
            a.phone,
            b.enterprise_name,
            if(ISNULL(recent.inspect_id),'0',recent.status)as is_inspect,
            person.person_name as inspector,
            recent.create_time as inspect_date
        FROM
            t_detector_user a
        LEFT JOIN t_enterprise_info b ON a.beyond_enterprise_id = b.enterprise_id
        LEFT JOIN (
         SELECT
          task.*
         FROM t_work_task_inspect task,(SELECT max(inspect_id)as inspect_id FROM t_work_task_inspect WHERE task_id = #{taskId} group by receive_id)last
         WHERE task.inspect_id = last.inspect_id
        )recent ON a.user_id = recent.receive_id
        LEFT JOIN t_line_patrol_person person ON person.person_id = recent.member_id
        <where> a.is_del = '0'
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and a.beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and a.username like concat('%', #{username}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and a.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="linkman != null  and linkman != ''"> and a.linkman = #{linkman}</if>
            <if test="isInspect != null  and isInspect == 0">
                and recent.inspect_id is null
            </if>
            <if test="isInspect != null and isInspect == 1">
                and recent.status  = 1
            </if>
            <if test="isInspect != null and isInspect == 2">
                and recent.status  = 2
            </if>
            <if test="inspectBeginTime != null and inspectEndTime != null">
                AND recent.create_time BETWEEN #{inspectBeginTime} and #{inspectEndTime}
            </if>
            <if test="inspector != null and inspector != ''">
                AND person.person_name like concat('%', #{inspector}, '%')
            </if>
            <if test="address != null and address != ''">
                AND a.address like concat('%', #{address}, '%')
            </if>
        </where>
        ORDER BY a.create_time DESC
</select>
</mapper>