<?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.ruoyi.system.mapper.TSearchLocationInfoMapper">

    <resultMap type="TSearchLocationInfo" id="TSearchLocationInfoResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="searchValue" column="search_value"/>
        <result property="deviceId" column="device_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="SearchLocationInfoVo" id="TSearchLocationInfoVoResult">
        <result property="id" column="id"/>
        <result property="deviceId" column="device_id"/>
        <result property="userId" column="user_id"/>
        <result property="deviceTel" column="device_tel"/>
        <result property="deviceCardId" column="device_card_id"/>
        <result property="deviceIdCode" column="device_id_code"/>
        <result property="userNickName" column="user_nick_name"/>
        <result property="userGender" column="user_gender"/>
        <result property="userTel1" column="user_tel_1"/>
        <result property="userTel2" column="user_tel_2"/>
        <result property="userTel3" column="user_tel_3"/>
        <result property="userCardId" column="user_card_id"/>
        <result property="fenceId" column="fence_id"/>
    </resultMap>

    <sql id="selectTSearchLocationInfoVo">
        select id, user_id, search_value, device_id, create_time, update_time
        from t_search_location_info
    </sql>

    <sql id="selectTSearchLocationInfoMainVo">
        SELECT t1.id        AS device_id,
               t1.userid    AS user_id,
               t1.telno     AS device_tel,
               t1.sfzid     AS device_card_id,
               t1.device_id AS device_id_code,
               t2.`name`    AS user_nick_name,
               t2.gender    AS user_gender,
               t2.tel_one   AS user_tel_1,
               t2.tel_two   AS user_tel_2,
               t2.tel_three AS user_tel_3,
               t2.sfzid     AS user_card_id,
               t3.id		AS fence_id
        FROM t_device_list t1
        JOIN
             t_user_profile t2
        ON
             t1.userid = t2.id
        JOIN
             t_electronic_fence t3
        ON
             t1.id	= t3.device_id
    </sql>

    <select id="selectTSearchLocationInfoList" parameterType="TSearchLocationInfo"
            resultMap="TSearchLocationInfoResult">
        <include refid="selectTSearchLocationInfoVo"/>
        <where>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="searchValue != null  and searchValue != ''">and search_value = #{searchValue}</if>
            <if test="deviceId != null ">and device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="selectTSearchLocationInfoById" parameterType="Long" resultMap="TSearchLocationInfoResult">
        <include refid="selectTSearchLocationInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectSearchLocationInfoVoBySearchValue" parameterType="String" resultMap="TSearchLocationInfoVoResult">
        <include refid="selectTSearchLocationInfoMainVo"/>
        WHERE
        t3.del_state = 0
        AND(
            t2.`name` LIKE concat('%', #{searchValue}, '%')
            OR
            t1.telno LIKE concat('%', #{searchValue}, '%')
            OR
            t1.device_id LIKE concat('%', #{searchValue}, '%')
        )
    </select>

    <insert id="insertTSearchLocationInfo" parameterType="TSearchLocationInfo" useGeneratedKeys="true" keyProperty="id">
        insert into t_search_location_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="searchValue != null and searchValue != ''">search_value,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="searchValue != null and searchValue != ''">#{searchValue},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateTSearchLocationInfo" parameterType="TSearchLocationInfo">
        update t_search_location_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="searchValue != null and searchValue != ''">search_value = #{searchValue},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTSearchLocationInfoById" parameterType="Long">
        delete
        from t_search_location_info
        where id = #{id}
    </delete>

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