<?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.qw.mapper.QwSessionMapper">

    <resultMap type="QwSession" id="QwSessionResult">
        <result property="sessionId"    column="session_id"    />
        <result property="chatId"    column="chat_id"    />
        <result property="qwExtWxId"    column="qw_ext_wx_id"    />
        <result property="qwExtId"    column="qw_ext_id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="qwUserId"    column="qw_user_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="userType"    column="user_type"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="companyUserId"    column="company_user_id"    />
    </resultMap>

    <sql id="selectQwSessionVo">
        select session_id,qw_ext_wx_id, chat_id, qw_ext_id, corp_id, qw_user_id, create_time, update_time, status, company_id, user_type, nick_name, avatar, company_user_id from qw_session
    </sql>

    <select id="selectQwSessionList" parameterType="QwSession" resultMap="QwSessionResult">
        <include refid="selectQwSessionVo"/>
        <where>
            <if test="chatId != null  and chatId != ''"> and chat_id = #{chatId}</if>
            <if test="qwExtId != null "> and qw_ext_id = #{qwExtId}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
    </select>

    <select id="selectQwSessionBySessionId" parameterType="Long" resultMap="QwSessionResult">
        <include refid="selectQwSessionVo"/>
        where session_id = #{sessionId}
    </select>

    <insert id="insertQwSession" parameterType="QwSession" useGeneratedKeys="true" keyProperty="sessionId">
        insert into qw_session
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chatId != null">chat_id,</if>
            <if test="qwExtId != null">qw_ext_id,</if>
            <if test="qwExtWxId != null">qw_ext_wx_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="qwUserId != null">qw_user_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="userType != null">user_type,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="companyUserId != null">company_user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chatId != null">#{chatId},</if>
            <if test="qwExtId != null">#{qwExtId},</if>
            <if test="qwExtWxId != null">#{qwExtWxId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="qwUserId != null">#{qwUserId},</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="userType != null">#{userType},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
         </trim>
    </insert>

    <update id="updateQwSession" parameterType="QwSession">
        update qw_session
        <trim prefix="SET" suffixOverrides=",">
            <if test="chatId != null">chat_id = #{chatId},</if>
            <if test="qwExtId != null">qw_ext_id = #{qwExtId},</if>
            <if test="qwExtWxId != null">qw_ext_wx_id = #{qwExtWxId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</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="userType != null">user_type = #{userType},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
        </trim>
        where session_id = #{sessionId}
    </update>

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

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