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

    <resultMap type="QwUser" id="QwUserResult">
        <result property="id"    column="id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="qwUserName"    column="qw_user_name"    />
        <result property="department"    column="department"    />
        <result property="openid"    column="openid"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="status"    column="status"    />
        <result property="isDel"    column="is_del"    />
        <result property="welcomeText"    column="welcome_text"    />
        <result property="welcomeImage"    column="welcome_image"    />
        <result property="isSendMsg"    column="is_send_msg"    />
        <result property="appKey"    column="app_key"    />
        <result property="contactWay"    column="contact_way"    />
        <result property="configId"    column="config_id"    />
        <result property="qwHookId"    column="qw_hook_id"    />
        <result property="fastGptRoleId"    column="fastGpt_role_id"    />
        <result property="loginStatus"    column="login_status"    />
        <result property="toolStatus"    column="tool_status"    />
        <result property="loginCodeUrl"    column="login_code_url"    />
        <result property="version"    column="version"    />
    </resultMap>

    <sql id="selectQwUserVo">
        select id, qw_user_id,config_id,contact_way,app_key, qw_user_name, department, openid, company_id, company_user_id, corp_id, status, is_del, welcome_text, welcome_image, is_send_msg,app_key,qw_hook_id,fastGpt_role_id,login_status,tool_status,login_code_url,version from qw_user
    </sql>

    <select id="selectQwUserList" parameterType="QwUser" resultMap="QwUserResult">
        <include refid="selectQwUserVo"/>
        <where>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
            <if test="qwUserName != null  and qwUserName != ''"> and qw_user_name like concat('%', #{qwUserName}, '%')</if>
            <if test="department != null  and department != ''"> and department = #{department}</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="welcomeText != null  and welcomeText != ''"> and welcome_text = #{welcomeText}</if>
            <if test="welcomeImage != null  and welcomeImage != ''"> and welcome_image = #{welcomeImage}</if>
            <if test="isSendMsg != null "> and is_send_msg = #{isSendMsg}</if>
            <if test="fastGptRoleId != null "> and fastGpt_role_id = #{fastGptRoleId}</if>
            <if test="loginStatus != null "> and login_status = #{loginStatus}</if>
            <if test="toolStatus != null "> and tool_status = #{toolStatus}</if>
            <if test="loginCodeUrl != null "> and login_code_url = #{loginCodeUrl}</if>
            <if test="version != null "> and version = #{version}</if>
        </where>
    </select>

    <select id="selectQwUserById" parameterType="Long" resultMap="QwUserResult">
        <include refid="selectQwUserVo"/>
        where id = #{id}
    </select>

    <insert id="insertQwUser" parameterType="QwUser" useGeneratedKeys="true" keyProperty="id">
        insert into qw_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="qwUserName != null">qw_user_name,</if>
            <if test="department != null">department,</if>
            <if test="openid != null">openid,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="corpId != null">corp_id,</if>
            <if test="status != null">status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="welcomeText != null">welcome_text,</if>
            <if test="welcomeImage != null">welcome_image,</if>
            <if test="isSendMsg != null">is_send_msg,</if>
            <if test="contactWay != null">contact_way,</if>
            <if test="configId != null">config_id,</if>
            <if test="appKey != null">app_key,</if>
            <if test="qwHookId != null">qw_hook_id,</if>
            <if test="fastGptRoleId != null">fastGpt_role_id,</if>
            <if test="loginStatus != null">login_status,</if>
            <if test="toolStatus != null">tool_status,</if>
            <if test="loginCodeUrl != null">login_code_url,</if>
            <if test="version != null">version,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="qwUserName != null">#{qwUserName},</if>
            <if test="department != null">#{department},</if>
            <if test="openid != null">#{openid},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="corpId != null">#{corpId},</if>
            <if test="status != null">#{status},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="welcomeText != null">#{welcomeText},</if>
            <if test="welcomeImage != null">#{welcomeImage},</if>
            <if test="isSendMsg != null">#{isSendMsg},</if>
            <if test="contactWay != null">#{contactWay},</if>
            <if test="configId != null">#{configId},</if>
            <if test="appKey != null">#{appKey},</if>
            <if test="qwHookId != null">#{qwHookId},</if>
            <if test="fastGptRoleId != null">#{fastGptRoleId},</if>
            <if test="loginStatus != null">#{loginStatus},</if>
            <if test="toolStatus != null">#{tool_status},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <update id="updateQwUser" parameterType="QwUser">
        update qw_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="qwUserName != null">qw_user_name = #{qwUserName},</if>
            <if test="department != null">department = #{department},</if>
            <if test="openid != null">openid = #{openid},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="welcomeText != null">welcome_text = #{welcomeText},</if>
            <if test="welcomeImage != null">welcome_image = #{welcomeImage},</if>
            <if test="isSendMsg != null">is_send_msg = #{isSendMsg},</if>
            <if test="contactWay != null">contact_way = #{contactWay},</if>
            <if test="configId != null">config_id = #{configId},</if>
            <if test="appKey != null">app_key = #{appKey},</if>
            <if test="qwHookId != null">qw_hook_id = #{qwHookId},</if>
            <if test="fastGptRoleId != null">fastGpt_role_id = #{fastGptRoleId},</if>
            <if test="loginStatus != null">login_status = #{loginStatus},</if>
            <if test="toolStatus != null">tool_status = #{toolStatus},</if>
            <if test="loginCodeUrl != null">login_code_url = #{loginCodeUrl},</if>
            <if test="version != null">version = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateQwUserByAppKey" parameterType="QwUser">
        update qw_user set login_status = #{loginStatus},remark = NULL
        <if test="version != null">,version = #{version} </if>
        where app_key = #{appKey}
    </update>

    <update id="updateQwUserByAppKeyTool" parameterType="QwUser">
        update qw_user set tool_status = #{toolStatus}
        where app_key = #{appKey}
    </update>

    <update id="updateQwUserByAppKeyToUrl" parameterType="QwUser">
        update qw_user set login_code_url = #{loginCodeUrl}
        where app_key = #{appKey}
    </update>

    <update id="updateQwUserByCompanyUserId" parameterType="String">
        update qw_user set status = 0 , is_del= 2 , company_user_id=null where company_user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </update>

    <delete id="deleteQwUserById" parameterType="Long">
        delete from qw_user where id = #{id}
    </delete>

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


    <select id="selectQwUserByUserIds" parameterType="java.util.List" resultMap="QwUserResult">
        <include refid="selectQwUserVo"/>
        WHERE id IN
        <foreach item="userId" collection="list" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </select>

    <select id="getCompanyUserListByWxIdAndCorpId" resultType="com.fs.qw.domain.QwUser">
        select * from qw_user where corp_id = #{corpId} and qw_user_id in <foreach collection="ids" open="(" separator="," close=")" item="item">#{item}</foreach>
    </select>
</mapper>
