<?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.TSpecialUserMapper">
    
    <resultMap type="TSpecialUser" id="TSpecialUserResult">
        <result property="specialId"    column="special_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="controlPlan"    column="control_plan"    />
        <result property="conStartTime"    column="con_start_time"    />
        <result property="conEndTime"    column="con_end_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="nickName"    column="nick_name"    />
        <result property="phone"    column="phone"    />
        <result property="userOwnId"    column="user_own_id"    />
    </resultMap>

    <sql id="selectTSpecialUserVo">
         SELECT s.special_id, s.user_id, s.user_type, s.control_plan, s.con_start_time, s.con_end_time,
        s.create_time, s.update_time, s.remark, s.user_own_id,u.`nick_name`,u.`phone`,u.address
        FROM t_special_user s
        LEFT JOIN t_detector_user u ON u.`user_id` = s.`user_id`
    </sql>

    <select id="selectTSpecialUserList" parameterType="TSpecialUser" resultMap="TSpecialUserResult">
        <include refid="selectTSpecialUserVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="annual != null and annual !=''"> and con_end_time like concat('%',#{annual},'%') </if>
            <if test="enterpriseId != null and enterpriseId !=''"> and enterprise_id = #{enterpriseId}</if>
            <if test="userType != null  and userType != ''"> and user_type = #{userType}</if>
            <if test="conStartTime != null "> and con_start_time = #{conStartTime}</if>
            <if test="conEndTime != null "> and con_end_time = #{conEndTime}</if>
            <if test="nickName != null "> and (u.nick_name  LIKE CONCAT('%',#{nickName},'%') or  u.phone  LIKE CONCAT('%',#{nickName},'%'))</if>
        </where>
    </select>
    
    <select id="selectTSpecialUserById" parameterType="Long" resultMap="TSpecialUserResult">
        <include refid="selectTSpecialUserVo"/>
        where special_id = #{specialId}
    </select>
        
    <insert id="insertTSpecialUser" parameterType="TSpecialUser">
        insert into t_special_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="controlPlan != null">control_plan,</if>
            <if test="conStartTime != null">con_start_time,</if>
            <if test="conEndTime != null">con_end_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="userOwnId != null">user_own_id,</if>
            <if test="enterpriseId != null">enterprise_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="controlPlan != null">#{controlPlan},</if>
            <if test="conStartTime != null">#{conStartTime},</if>
            <if test="conEndTime != null">#{conEndTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="userOwnId != null">#{userOwnId},</if>
            <if test="enterpriseId != null">#{enterpriseId},</if>
         </trim>
    </insert>

    <insert id="inserts" parameterType="TSpecialUser">
        insert into t_special_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            user_type,
            control_plan,
            con_start_time,
            con_end_time,
            create_time,
            update_time,
            remark,
            user_own_id,
            enterprise_id
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.userId},
            #{item.userType},
           #{item.controlPlan},
            #{item.conStartTime},
            #{item.conEndTime},
            #{item.createTime},
            #{item.updateTime},
            #{item.remark},
            #{item.userOwnId},
            #{item.enterpriseId})
        </foreach>
    </insert>


    <update id="updateTSpecialUser" parameterType="TSpecialUser">
        update t_special_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="controlPlan != null">control_plan = #{controlPlan},</if>
            <if test="conStartTime != null">con_start_time = #{conStartTime},</if>
            <if test="conEndTime != null">con_end_time = #{conEndTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="userOwnId != null">user_own_id = #{userOwnId},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
        </trim>
        where special_id = #{specialId}
    </update>

    <delete id="deleteTSpecialUserById" parameterType="Long">
        delete from t_special_user where special_id = #{specialId}
    </delete>

    <delete id="deleteTSpecialUserByIds" parameterType="String">
        delete from t_special_user where special_id in 
        <foreach item="specialId" collection="array" open="(" separator="," close=")">
            #{specialId}
        </foreach>
    </delete>
</mapper>