<?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.TDeviceUserMapper">
    
    <resultMap type="TDeviceUser" id="TDeviceUserResult">
        <result property="userId"    column="user_id"    />
        <result property="username"    column="username"    />
        <result property="township"    column="township"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceType"    column="device_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="medium"    column="medium"    />
        <result property="unit"    column="unit"    />
        <result property="status"    column="status"    />
        <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="iconType"    column="iconType"    />
    </resultMap>

    <sql id="selectTDeviceUserVo">
        select user_id, username, township, device_code, device_type, address, longitude, latitude, linkman, phone, email, medium, unit, status, create_time, update_time, is_del, remarks from t_device_user
    </sql>

    <select id="selectTDeviceUserList" parameterType="TDeviceUser" resultMap="TDeviceUserResult">
        <include refid="selectTDeviceUserVo"/>
        <where>  
            <if test="username != null  and username != ''"> and username like concat('%', #{username}, '%')</if>
            <if test="township != null  and township != ''"> and township = #{township}</if>
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="linkman != null  and linkman != ''"> and linkman = #{linkman}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="medium != null  and medium != ''"> and medium = #{medium}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
    </select>
    
    <select id="selectTDeviceUserById" parameterType="Long" resultMap="TDeviceUserResult">
        <include refid="selectTDeviceUserVo"/>
        where user_id = #{userId}
    </select>
    <select id="selectTDeviceUserByCode" resultMap="TDeviceUserResult">
        <include refid="selectTDeviceUserVo"/>
        where device_code = #{code}
    </select>

    <select id="allListbyTownship" resultMap="TDeviceUserResult" >
        SELECT *,"18" as iconType FROM t_device_user WHERE township IS NOT NULL GROUP BY  township
    </select>
    <insert id="insertTDeviceUser" parameterType="TDeviceUser" useGeneratedKeys="true" keyProperty="userId">
        insert into t_device_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null">username,</if>
            <if test="township != null">township,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceType != null">device_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="medium != null">medium,</if>
            <if test="unit != null">unit,</if>
            <if test="status != null">status,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null">#{username},</if>
            <if test="township != null">#{township},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceType != null">#{deviceType},</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="medium != null">#{medium},</if>
            <if test="unit != null">#{unit},</if>
            <if test="status != null">#{status},</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>
         </trim>
    </insert>

    <update id="updateTDeviceUser" parameterType="TDeviceUser">
        update t_device_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null">username = #{username},</if>
            <if test="township != null">township = #{township},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceType != null">device_type = #{deviceType},</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="medium != null">medium = #{medium},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="status != null">status = #{status},</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="deleteTDeviceUserById" parameterType="Long">
        delete from t_device_user where user_id = #{userId}
    </delete>

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