<?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.ChatRoleMapper">

    <resultMap type="ChatRole" id="ChatRoleResult">
        <result property="roleId"    column="role_id"    />
        <result property="roleName"    column="role_name"    />
        <result property="companyId"    column="company_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="roleType"    column="role_type"    />
        <result property="configJson"    column="config_json"    />
        <result property="mode"    column="mode"    />
        <result property="kfId"    column="kf_id"    />
        <result property="kfUrl"    column="kf_url"    />
        <result property="kfMediaId"    column="kf_media_id"    />

        <result property="modeConfigJson"    column="mode_config_json"    />
    </resultMap>

    <sql id="selectChatRoleVo">
        select role_id, role_name, company_id, create_time, update_time, role_type, config_json, mode, kf_id, kf_url,kf_media_id, mode_config_json from chat_role
    </sql>

    <select id="selectChatRoleList" parameterType="ChatRole" resultMap="ChatRoleResult">
        <include refid="selectChatRoleVo"/>
        <where>
            <if test="roleName != null  and roleName != ''"> and role_name like concat('%', #{roleName}, '%')</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="roleType != null "> and role_type = #{roleType}</if>
            <if test="configJson != null  and configJson != ''"> and config_json = #{configJson}</if>
            <if test="mode != null "> and mode = #{mode}</if>
            <if test="kfId != null  and kfId != ''"> and kf_id = #{kfId}</if>
            <if test="kfUrl != null  and kfUrl != ''"> and kf_url = #{kfUrl}</if>
            <if test="modeConfigJson != null  and modeConfigJson != ''"> and mode_config_json = #{modeConfigJson}</if>
        </where>

    </select>

    <select id="selectChatRoleByRoleId" parameterType="Long" resultMap="ChatRoleResult">
        <include refid="selectChatRoleVo"/>
        where role_id = #{roleId}
    </select>

    <insert id="insertChatRole" parameterType="ChatRole" useGeneratedKeys="true" keyProperty="roleId">
        insert into chat_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roleName != null">role_name,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="roleType != null">role_type,</if>
            <if test="configJson != null">config_json,</if>
            <if test="mode != null">mode,</if>
            <if test="kfId != null">kf_id,</if>
            <if test="kfUrl != null">kf_url,</if>
            <if test="kfMediaId != null">kf_media_id,</if>
            <if test="modeConfigJson != null">mode_config_json,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="roleName != null">#{roleName},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="roleType != null">#{roleType},</if>
            <if test="configJson != null">#{configJson},</if>
            <if test="mode != null">#{mode},</if>
            <if test="kfId != null">#{kfId},</if>
            <if test="kfUrl != null">#{kfUrl},</if>
            <if test="kfMediaId != null">#{kfMediaId},</if>
            <if test="modeConfigJson != null">#{modeConfigJson},</if>
         </trim>
    </insert>

    <update id="updateChatRole" parameterType="ChatRole">
        update chat_role
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleName != null">role_name = #{roleName},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="roleType != null">role_type = #{roleType},</if>
            <if test="configJson != null">config_json = #{configJson},</if>
            <if test="mode != null">mode = #{mode},</if>
            <if test="kfId != null">kf_id = #{kfId},</if>
            <if test="kfUrl != null">kf_url = #{kfUrl},</if>
            <if test="kfMediaId != null">kf_media_id = #{kfMediaId},</if>
            <if test="modeConfigJson != null">mode_config_json = #{modeConfigJson},</if>
        </trim>
        where role_id = #{roleId}
    </update>

    <delete id="deleteChatRoleByRoleId" parameterType="Long">
        delete from chat_role where role_id = #{roleId}
    </delete>

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