<?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.fs.chat.mapper.ChatSessionMapper">

    <resultMap type="ChatSession" id="ChatSessionResult">
        <result property="sessionId"    column="session_id"    />
        <result property="sessionCode"    column="session_code"    />
        <result property="userId"    column="user_id"    />
        <result property="kfId"    column="kf_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
        <result property="companyId"    column="company_id"    />
        <result property="isLook"    column="is_look"    />
    </resultMap>

    <sql id="selectChatSessionVo">
        select session_id,company_id, session_code, user_id, kf_id, create_time, update_time, status,is_look  from chat_session
    </sql>

    <select id="selectChatSessionList" parameterType="ChatSession" resultMap="ChatSessionResult">
        <include refid="selectChatSessionVo"/>
        <where>
            <if test="sessionCode != null  and sessionCode != ''"> and session_code = #{sessionCode}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="kfId != null  and kfId != ''"> and kf_id = #{kfId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="isLook != null "> and is_look = #{isLook}</if>
        </where>
    </select>

    <select id="selectChatSessionBySessionId" parameterType="Long" resultMap="ChatSessionResult">
        <include refid="selectChatSessionVo"/>
        where session_id = #{sessionId}
    </select>

    <insert id="insertChatSession" parameterType="ChatSession" useGeneratedKeys="true" keyProperty="sessionId">
        insert into chat_session
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sessionCode != null">session_code,</if>
            <if test="userId != null">user_id,</if>
            <if test="kfId != null">kf_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isLook != null">is_look,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sessionCode != null">#{sessionCode},</if>
            <if test="userId != null">#{userId},</if>
            <if test="kfId != null">#{kfId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isLook != null">#{isLook},</if>
         </trim>
    </insert>

    <update id="updateChatSession" parameterType="ChatSession">
        update chat_session
        <trim prefix="SET" suffixOverrides=",">
            <if test="sessionCode != null">session_code = #{sessionCode},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="kfId != null">kf_id = #{kfId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isLook != null">is_look = #{isLook},</if>
        </trim>
        where session_id = #{sessionId}
    </update>

    <delete id="deleteChatSessionBySessionId" parameterType="Long">
        delete from chat_session where session_id = #{sessionId}
    </delete>

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