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

    <resultMap type="TYehuaqiUser" id="TYehuaqiUserResult">
        <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="deviceNum"    column="deviceNum"    />
        <result property="zxnum"    column="zxnum"    />
        <result property="beyondEnterpriseName"    column="beyond_enterprise_name"    />
        <result property="villageId"    column="f_village_id"    />
        <result property="villageName"    column="village_name"    />
        <result property="errorMsg"    column="error_msg"    />
        <result property="userOwnId"    column="user_own_id"    />
    </resultMap>

    <resultMap type="TYehuaqiUser" id="TYehuaqiUserExportResult">
        <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="deviceNum"    column="deviceNum"    />
        <result property="zxnum"    column="zxnum"    />
        <result property="beyondEnterpriseName"    column="beyond_enterprise_name"    />
        <result property="villageId"    column="f_village_id"    />
        <result property="villageName"    column="village_name"    />
        <result property="errorMsg"    column="error_msg"    />

        <!-- collection 是用于建立一对多中集合属性的对应关系
        ofType 用于指定集合元素的数据类型
        -->
        <collection property="userManageSafetyDeviceExportVos" ofType="YeHuaqiUserManageSafetyDeviceExportVo">
            <id property="userOwnId" column="user_id"/>
            <result property="fRelationDeviceType" column="f_relation_device_type"/>
            <result property="fDeviceName" column="f_device_name"/>
            <result property="fDeviceModel" column="f_device_model"/>
            <result property="fIotNo" column="f_iot_no"/>
            <result property="fDetectionMedium" column="f_detection_medium"/>
            <result property="fDeviceInstallTime" column="f_device_install_time"/>
            <result property="fDeviceInstallPosition" column="f_device_install_position"/>
            <result property="fHead" column="f_head"/>
            <result property="fPhone" column="f_phone"/>
        </collection>

    </resultMap>

    <sql id="selectTYehuaqiUserVo">
        select user_id, username, nick_name, beyond_enterprise_id, user_type, address, longitude, latitude, linkman,
               phone, email, create_time, update_time, is_del, remarks ,f_village_id,error_msg,user_own_id from t_yehuaqi_user
    </sql>

    <select id="queryByUserOwnIdAndEnterpriseIdForThirdParty" resultMap="TYehuaqiUserResult">
        <include refid="selectTYehuaqiUserVo"/>
        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="selectTYehuaqiUserList" parameterType="TYehuaqiUser" resultMap="TYehuaqiUserResult">

        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 ,
        '液化气' 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 as beyond_enterprise_name,a.f_village_id,v.f_village_name as village_name
        from t_yehuaqi_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>
        </where>
        order by a.user_id desc

    </select>
    <select id="selectTYehuaqiUserListTwo" parameterType="TYehuaqiUser" resultMap="TYehuaqiUserResult">
          SELECT t2.zs as deviceNum,t2.zx as zxnum,t1.* FROM t_yehuaqi_user t1
          LEFT JOIN (SELECT MAX(user_id)user_id,COUNT(user_id)zs,COUNT(detector_status)zx
          FROM t_yehuaqi_device GROUP BY user_id) t2 ON t1.user_id=t2.user_id
        <where>
            <if test="userId != null  and userId != ''"> and t1.user_id = #{userId}</if>
        </where>
    </select>

    <select id="countByBeyondEnterpriseId" parameterType="String" resultType="int">
        select count(*) from t_yehuaqi_user where error_msg is not null
        <if test="beyondEnterpriseId != null and beyondEnterpriseId != '' and beyondEnterpriseId != '-2'">
            and beyond_enterprise_id = #{beyondEnterpriseId}
        </if>
    </select>


    <select id="selectTYehuaqiUserById" parameterType="Long" resultMap="TYehuaqiUserResult">
        <include refid="selectTYehuaqiUserVo"/>
        where user_id = #{userId}
    </select>
    <select id="selectByUserOwnIdAndBeyondEnterpriseId" resultMap="TYehuaqiUserResult">
        <include refid="selectTYehuaqiUserVo"/>
        where beyond_enterprise_id = #{beyondEnterpriseId} and user_own_id = #{userOwnId}
    </select>

    <select id="selectByUsernameAndPhoneAndAddress" resultMap="TYehuaqiUserResult">

        <include refid="selectTYehuaqiUserVo"/>
        where nick_name = #{nickName} and phone = #{phone} and address = #{address}
    </select>

    <select id="queryErrorDetectorUserList" parameterType="string" resultMap="TYehuaqiUserResult">
        select * from t_yehuaqi_user where error_msg is not null
        <if test="depId != null and depId != '' and depId != '-2'">
            and beyondEnterpriseId = #{depId}
        </if>
    </select>

    <select id="queryByUserOwnIdsAndEnterpriseId" resultMap="TYehuaqiUserResult">
        <include refid="selectTYehuaqiUserVo"/>
        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="selectForExportTYehuaqiUserList" parameterType="TYehuaqiUser" resultMap="TYehuaqiUserExportResult">

        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 ,
        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 as beyond_enterprise_name,a.f_village_id,v.f_village_name as 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_yehuaqi_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_yehuaqiuser_safety_device_info deviceInfo on a.user_id = deviceInfo.f_relation_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>
        order by a.user_id desc
    </select>

    <insert id="insertBatch" parameterType="list" useGeneratedKeys="true" keyProperty="userId">
        insert into t_yehuaqi_user (nick_name, beyond_enterprise_name,beyond_enterprise_id, user_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.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="insertBatchForThirdParty" parameterType="list" useGeneratedKeys="true" keyProperty="userId">
        insert into t_yehuaqi_user (nick_name, beyond_enterprise_name,beyond_enterprise_id, user_type, village_name,f_village_id,
        address, linkman, phone, remarks, error_msg,user_own_id,longitude,latitude)
        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.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}, #{item.longitude}, #{item.latitude}
            )
        </foreach>
    </insert>
    <insert id="insertTYehuaqiUser" parameterType="TYehuaqiUser" useGeneratedKeys="true" keyProperty="userId">
        insert into t_yehuaqi_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null">username,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id,</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="linkman != null">linkman,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
            <if test="villageId != null">f_village_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null">#{username},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</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="linkman != null">#{linkman},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="villageId != null">#{villageId},</if>
         </trim>
    </insert>

    <update id="updateBatch" parameterType="list">

        <foreach collection="list" item="item"  index="index" separator=";">
            UPDATE t_yehuaqi_user
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.nickName != null">nick_name = #{item.nickName},</if>
                <if test="item.userType != null">user_type = #{item.userType},</if>
                <if test="item.villageId != null">f_village_id = #{item.villageId},</if>
                <if test="item.address != null">address = #{item.address},</if>
                <if test="item.longitude != null">longitude = #{item.longitude},</if>
                <if test="item.latitude != null">latitude = #{item.latitude},</if>
                <if test="item.linkman != null">linkman = #{item.linkman},</if>
                <if test="item.phone != null">phone = #{item.phone},</if>
                <if test="item.remarks != null">remarks = #{item.remarks},</if>
            </trim>
            WHERE user_id = #{item.userId}
        </foreach>
    </update>

    <update id="updateTYehuaqiUser" parameterType="TYehuaqiUser">
        update t_yehuaqi_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null">username = #{username},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="beyondEnterpriseId != null">beyond_enterprise_id = #{beyondEnterpriseId},</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="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>
        </trim>
        where user_id = #{userId}
    </update>

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

    <delete id="deleteTYehuaqiUserByIds" parameterType="String">
        delete from t_yehuaqi_user where user_id in 
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
</mapper>