<?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="gasType"    column="gas_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"    />
        <result property="villageId"    column="f_village_id"    />
        <result property="villageName"    column="f_village_name"    />
        <result property="errorMsg"    column="error_msg"    />
        <result property="userOwnId"    column="user_own_id"    />
    </resultMap>

    <resultMap type="TDetectorUser" id="TDetectorUserExportResult">
        <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="gasType"    column="gas_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"    />
        <result property="villageId"    column="f_village_id"    />
        <result property="villageName"    column="f_village_name"    />
        <result property="errorMsg"    column="error_msg"    />
        <result property="userOwnId"    column="user_own_id"    />
        <!-- collection 是用于建立一对多中集合属性的对应关系
        ofType 用于指定集合元素的数据类型
        -->
        <collection property="userManageSafetyDeviceExportVos" ofType="UserManageSafetyDeviceExportVo">
            <id property="userOwnId" column="user_id"/>
            <result property="relationDeviceType" column="f_relation_device_type"/>
            <result property="deviceName" column="f_device_name"/>
            <result property="deviceModel" column="f_device_model"/>
            <result property="fIotNo" column="f_iot_no"/>
            <result property="detectionMedium" column="f_detection_medium"/>
            <result property="deviceInstallTime" column="f_device_install_time"/>
            <result property="deviceInstallPosition" column="f_device_install_position"/>
            <result property="head" column="f_head"/>
            <result property="phone" column="f_phone"/>
        </collection>
    </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="gasType"    column="gas_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, gas_type,address, longitude, latitude, linkman, phone, email,
               create_time, update_time, is_del, remarks,f_village_id,error_msg,user_own_id from t_detector_user
    </sql>

    <select id="queryErrorDetectorUserList" parameterType="string" resultMap="TDetectorUserResult">
        select *,beyond_enterprise_name as enterprise_name from t_detector_user where error_msg is not null
            <if test="depId != null and depId != '' and depId != '-2'">
                and beyondEnterpriseId = #{depId}
            </if>
            <if test="gasType != null  and gasType == 1"> and gas_type =#{gasType}</if>
            <if test="gasType != null  and gasType == 0"> and (gas_type =#{gasType} or gas_type is null)</if>
    </select>

    <select id="queryByUserOwnIdAndEnterpriseIdForThirdParty" resultMap="TDetectorUserResult">
        <include refid="selectTDetectorUserVo"/>
        where beyond_enterprise_id = #{enterpriseId} and user_own_id in
        <foreach collection="userOwnIds" index="i" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="queryByUserOwnIdsAndEnterpriseId" resultMap="TDetectorUserResult">
        <include refid="selectTDetectorUserVo"/>
        where beyond_enterprise_id = #{enterpriseId} and user_own_id in
        <foreach collection="userOwnIds" close=")" separator="," open="(" item="item" index="i">
            #{item}
        </foreach>
    </select>
    <select id="selectForExportTDetectorUserList" parameterType="TDetectorUser" resultMap="TDetectorUserExportResult">
        select a.user_id,
        a.username,
        a.nick_name,
        (CASE a.user_type WHEN '1' THEN '居民用户' WHEN '2' THEN '工商业用户' WHEN '4' THEN '餐饮单位用户'end) as   user_type ,
        (CASE a.gas_type WHEN '0' THEN '天然气' WHEN '1' THEN '液化气' end) as gas_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,a.f_village_id,v.f_village_name,
        deviceInfo.f_relation_device_type,
        deviceInfo.f_device_name,
        deviceInfo.f_device_model,
        deviceInfo.f_iot_no,
        deviceInfo.f_detection_medium,
        deviceInfo.f_device_install_time,
        deviceInfo.f_device_install_position,
        deviceInfo.f_head,
        deviceInfo.f_phone
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        left join t_user_manage_village v on a.f_village_id=v.f_village_id
        left join t_gasuser_safety_device_info deviceInfo on a.user_id = deviceInfo.f_relation_gasUser_id
        <where> a.is_del = '0' and a.error_msg is null
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and a.beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="gasType != null  and gasType == 1"> and a.gas_type =#{gasType}</if>
            <if test="gasType != null  and gasType == 0"> and (a.gas_type =#{gasType} or a.gas_type is null)</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="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 '4' THEN '餐饮单位用户'end) as   user_type ,
        (CASE a.gas_type WHEN '0' THEN '天然气' WHEN '1' THEN '液化气' end) as gas_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,a.f_village_id,v.f_village_name
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        left join t_user_manage_village v on a.f_village_id=v.f_village_id
        <where> a.is_del = '0' and a.error_msg is null
            <if test="beyondEnterpriseId != null  and beyondEnterpriseId != -2"> and a.beyond_enterprise_id =#{beyondEnterpriseId}</if>
            <if test="gasType != null  and gasType == 1"> and a.gas_type =#{gasType}</if>
            <if test="gasType != null  and gasType == 0"> and (a.gas_type =#{gasType} or a.gas_type is null)</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="newSelectTDetectorUserList" parameterType="TDetectorUser" resultMap="TDetectorUserResult">
        select a.user_id,
               a.username,
               a.nick_name,
               (CASE a.user_type WHEN '1' THEN '居民用户' WHEN '2' THEN '工商业用户' WHEN '4' THEN '餐饮单位用户'end) as   user_type ,
               (CASE a.gas_type WHEN '0' THEN '天然气' WHEN '1' THEN '液化气' end) as gas_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,a.f_village_id,v.f_village_name
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        left join t_user_manage_village v on a.f_village_id=v.f_village_id
        <where> a.is_del = '0' and a.error_msg is null
            <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' and a.error_msg is null
            <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 '4' THEN '餐饮单位用户'end) as   user_type ,
               (CASE a.gas_type WHEN '0' THEN '天然气' WHEN '1' THEN '液化气' end) as gas_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.error_msg is null
            <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.gas_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,a.f_village_id
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        where a.user_id = #{userId}  and a.error_msg is null
    </select>
    <select id="selectUserByOwnId" parameterType="string" resultMap="TDetectorUserResult">
        select a.user_id, a.beyond_enterprise_id,a.username, a.nick_name, a.user_type,a.gas_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,a.f_village_id
        from t_detector_user a left join t_enterprise_info b on a.beyond_enterprise_id=b.enterprise_id
        where a.user_own_id = #{userOwnId}
    </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.gas_type AS gasType,
               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' and a1.error_msg is null and a1.user_type != '2'
                    <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' and b3.error_msg is null and b3.user_type != '2'
                    <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="queryUserIdLinkGasBottleIdByVillageIds" resultType="com.zehong.system.domain.dto.UserLinkGasBottleIdDTO">
        SELECT
            duser.user_id as userId,
            duser.f_village_id as villageId,
            userlinkgas.f_gas_bottle_files_id as gasBottleFilesId
        FROM
            t_detector_user duser
                LEFT JOIN t_detector_user_link_gas_bottle userlinkgas
                          on duser.user_id = userlinkgas.f_user_id
        WHERE
            duser.user_type = 1
          AND duser.f_village_id IS NOT NULL
          and duser.f_village_id in
        <foreach collection="villageIds" close=")" separator="," open="(" item="item" index="i">
            #{item}
        </foreach>
    </select>

    <select id="queryUserIdLinkGasBottleTitleByVillageIds" resultType="com.zehong.system.domain.dto.UserLinkGasBottleTitleDTO">
        SELECT
        duser.user_id as userId,
        duser.f_village_id as villageId,
        userlinkgas.f_gas_bottle_files_id as gasBottleFilesId,
        gasBottleFiles.f_q_rcode as rcode
        FROM
        t_detector_user duser
        LEFT JOIN t_detector_user_link_gas_bottle userlinkgas
        on duser.user_id = userlinkgas.f_user_id
        LEFT JOIN t_lpg_gas_bottle_files gasBottleFiles
        on userlinkgas.f_gas_bottle_files_id = gasBottleFiles.f_gas_bottle_files_id
        WHERE
        duser.user_type = 1
        AND duser.f_village_id IS NOT NULL
        and duser.f_village_id in
        <foreach collection="villageIds" close=")" separator="," open="(" item="item" index="i">
            #{item}
        </foreach>
    </select>
    <select id="queryDuserVilSafDevInfoOnlyResidents" resultType="map" parameterType="TDetectorUser">
        SELECT
            userVillage.f_village_id AS villageId,
            userVillage.f_village_name AS villageName,
            userVillage.longitude,
            userVillage.latitude,
            ( SELECT count(*) FROM t_detector_user duser WHERE duser.f_village_id = villageId AND duser.user_type = 1 ) AS userNum,
            (
                SELECT
                    count(*)
                FROM
                    t_gasuser_safety_device_info deviceinfo
                        LEFT JOIN t_detector_user duser ON deviceinfo.f_relation_gasUser_id = duser.user_id
                WHERE
                    duser.f_village_id = villageId
                  AND duser.user_type = 1
                  AND ( deviceinfo.is_del = 0 OR deviceinfo.is_del IS NULL )
            ) AS deviceNum,
            '6' AS iconType,
            ( SELECT count(*) FROM t_detector_user_link_gas_bottle userlinkgas LEFT JOIN t_detector_user duser ON userlinkgas.f_user_id = duser.user_id WHERE duser.f_village_id = villageId ) AS gasBottle
        FROM
            t_user_manage_village userVillage
        WHERE
            userVillage.longitude IS NOT NULL
          AND userVillage.latitude IS NOT NULL
        <if test="villageId != null  and villageId != ''"> and duser.f_village_id = #{villageId}</if>
    </select>
    <select id="queryDuserVilSafDevInfoOnlyResidentsInit" resultType="TDetectorUserVillageSafetyDeviceInfoVo" parameterType="TDetectorUser">
        SELECT
        dUser.user_id as userId,
        dUser.nick_name as nickName,
        dUser.phone,
        dUser.address,
        dUser.longitude as userLongitude,
        dUser.latitude as userLatitude,
        dUser.user_type as userType,
        dUser.gas_type as gasType,
        userVillage.f_village_id as villageId,
        userVillage.f_village_name as villageName,
        userVillage.longitude,
        userVillage.latitude,
        region.f_name AS regionName,
        safetyDeviceInfo.f_gasUser_safety_device_id as gasUserSafetyDeviceId,
        safetyDeviceInfo.f_iot_no as iotNo,
        safetyDeviceInfo.f_device_name as deviceName,
        safetyDeviceInfo.f_detection_medium as detctionMedium,
        safetyDeviceInfo.f_device_install_time as deviceInstallTime
        FROM
        t_detector_user dUser
        LEFT JOIN t_user_manage_village userVillage ON dUser.f_village_id = userVillage.f_village_id
        LEFT JOIN t_gasuser_safety_device_info safetyDeviceInfo on safetyDeviceInfo.f_relation_gasUser_id = dUser.user_id
        LEFT JOIN t_county_level_region region ON region.f_id = userVillage.f_region
        <where> (safetyDeviceInfo.is_del = '0' or safetyDeviceInfo.is_del is null)
            and dUser.user_type = 1
            <if test="villageId != null  and villageId != ''"> and dUser.f_village_id = #{villageId}</if>
        </where>


    </select>
    <select id="queryDuserVilSafDevInfo" resultType="TDetectorUserVillageSafetyDeviceInfoVo" parameterType="TDetectorUser">
        SELECT
            dUser.user_id as userId,
            dUser.nick_name as nickName,
            dUser.phone,
            dUser.address,
            dUser.longitude as userLongitude,
            dUser.latitude as userLatitude,
            dUser.user_type as userType,
            dUser.gas_type as gasType,
            userVillage.f_village_id as villageId,
            userVillage.f_village_name as villageName,
            userVillage.longitude,
            userVillage.latitude,
            region.f_name AS regionName,
            safetyDeviceInfo.f_gasUser_safety_device_id as gasUserSafetyDeviceId,
            safetyDeviceInfo.f_iot_no as iotNo,
            safetyDeviceInfo.f_device_name as deviceName,
            safetyDeviceInfo.f_detection_medium as detctionMedium,
            safetyDeviceInfo.f_device_install_time as deviceInstallTime
        FROM
            t_detector_user dUser
                LEFT JOIN t_user_manage_village userVillage ON dUser.f_village_id = userVillage.f_village_id
                LEFT JOIN t_gasuser_safety_device_info safetyDeviceInfo on safetyDeviceInfo.f_relation_gasUser_id = dUser.user_id
                LEFT JOIN t_county_level_region region ON region.f_id = userVillage.f_region
        <where> (safetyDeviceInfo.is_del = '0' or safetyDeviceInfo.is_del is null)
            and dUser.user_type in (2,3,4)
            and dUser.longitude is not null and dUser.latitude is not null
            and dUser.gas_type is not null
            <if test="userId != null  and userId != ''"> and dUser.user_id = #{userId}</if>
            <if test="villageId != null  and villageId != ''"> and dUser.f_village_id = #{villageId}</if>
        </where>

    </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' and a2.error_msg is null
    </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="gasType != null">gas_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>
            <if test="villageId != null">f_village_id,</if>
            <if test="errorMsg != null">error_msg,</if>
            <if test="userOwnId != null">user_own_id,</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="gasType != null">#{gasType},</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>
            <if test="villageId != null">#{villageId},</if>
            <if test="errorMsg != null">#{errorMsg},</if>
            <if test="userOwnId != null">#{userOwnId},</if>
         </trim>
    </insert>

    <insert id="insertBatchForThirdParty" parameterType="list" useGeneratedKeys="true" keyProperty="userId">

        insert into t_detector_user (nick_name, beyond_enterprise_name,beyond_enterprise_id, user_type, gas_type, village_name,f_village_id,
        address, linkman, phone, remarks, error_msg,user_own_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.nickName,jdbcType=VARCHAR}, #{item.beyondEnterpriseName,jdbcType=VARCHAR}, #{item.beyondEnterpriseId,jdbcType=VARCHAR},
            #{item.userType,jdbcType=VARCHAR}, #{item.gasType,jdbcType=VARCHAR}, #{item.villageName,jdbcType=VARCHAR},
            #{item.villageId,jdbcType=DECIMAL},
            #{item.address,jdbcType=VARCHAR}, #{item.linkman,jdbcType=VARCHAR}, #{item.phone,jdbcType=VARCHAR},
            #{item.remarks,jdbcType=VARCHAR}, #{item.errorMsg,jdbcType=VARCHAR},#{item.userOwnId,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
    <insert id="insertBatch" parameterType="list" useGeneratedKeys="true" keyProperty="userId">

        insert into t_detector_user (nick_name, beyond_enterprise_name,beyond_enterprise_id, user_type, gas_type, village_name,f_village_id,
        address, linkman, phone, remarks, error_msg,user_own_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.nickName,jdbcType=VARCHAR}, #{item.beyondEnterpriseName,jdbcType=VARCHAR}, #{item.beyondEnterpriseId,jdbcType=VARCHAR},
            #{item.userType,jdbcType=VARCHAR}, #{item.gasType,jdbcType=VARCHAR}, #{item.villageName,jdbcType=VARCHAR},
             #{item.villageId,jdbcType=DECIMAL},
            #{item.address,jdbcType=VARCHAR}, #{item.linkman,jdbcType=VARCHAR}, #{item.phone,jdbcType=VARCHAR},
            #{item.remarks,jdbcType=VARCHAR}, #{item.errorMsg,jdbcType=VARCHAR},#{item.userOwnId,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
    <update id="updateBatch" parameterType="list">

        <foreach collection="list" item="item"  index="index" separator=";">
            UPDATE t_detector_user
            <set>
                nick_name = #{item.nickName},
                user_type = #{item.userType},
                gas_type = #{item.gasType},
                f_village_id = #{item.villageId},
                beyond_enterprise_id = #{item.beyondEnterpriseId},
                address = #{item.address},
                linkman = #{item.linkman},
                phone = #{item.phone},
                remarks = #{item.remarks},
                error_msg = null,
                user_own_id = #{item.userOwnId}
            </set>
            WHERE user_id = #{item.userId}
        </foreach>

    </update>

    <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="gasType != null">gas_type = #{gasType},</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>
            <if test="villageId != null">f_village_id = #{villageId},</if>
            <if test="userOwnId != null">user_own_id = #{userOwnId},</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 '4' THEN
                    '煤改气用户'
                END
            ) AS user_type,
            (
            CASE a.gas_type
            WHEN '0' THEN
            '天然气'
            WHEN '1' THEN
            '液化气'
            END
            ) AS gas_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' and a.error_msg is null
            <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>

    <select id="countByBeyondEnterpriseId" parameterType="String" resultType="int">
        select count(*) from t_detector_user where error_msg is not null
        <if test="beyondEnterpriseId != null and beyondEnterpriseId != '' and beyondEnterpriseId != '-2'">
            and beyond_enterprise_id = #{beyondEnterpriseId}
        </if>
        <if test="gasType != null  and gasType == 1"> and gas_type =#{gasType}</if>
        <if test="gasType != null  and gasType == 0"> and (gas_type =#{gasType} or gas_type is null)</if>
    </select>
    <delete id="clearByBeyondEnterpriseId" parameterType="String">
        delete from t_detector_user where error_msg is not null
        <if test="beyondEnterpriseId != null and beyondEnterpriseId != '' and beyondEnterpriseId != '-2'">
            and beyond_enterprise_id = #{beyondEnterpriseId}
        </if>
        <if test="gasType != null  and gasType == 1"> and gas_type =#{gasType}</if>
        <if test="gasType != null  and gasType == 0"> and (gas_type =#{gasType} or gas_type is null)</if>
    </delete>

    <select id="summaryUser" resultType="java.util.HashMap">
        SELECT IFNULL(SUM(IF(u.user_type=1,1,0)),0) AS jmNum,
        IFNULL(SUM(IF(u.user_type=2,1,0)),0) AS syNum,
        IFNULL(SUM(IF(u.user_type=3,1,0)),0) AS gyNum,
        IFNULL(SUM(IF(u.user_type=1 AND su.special_id IS NOT NULL,1,0)),0) AS jmtNum,
        IFNULL(SUM(IF(u.user_type=2 AND su.special_id IS NOT NULL,1,0)),0) AS sytNum,
        IFNULL(SUM(IF(u.user_type=3 AND su.special_id IS NOT NULL,1,0)),0) AS gytNum
        FROM t_detector_user u
        LEFT JOIN t_special_user su ON u.user_id = su.`user_id`
    </select>
</mapper>
