<?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.ai.mapper.AiUserMapper">
    
    <resultMap type="AiUser" id="AiUserResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="username"    column="username"    />
        <result property="nickname"    column="nickname"    />
        <result property="gender"    column="gender"    />
        <result property="avatar"    column="avatar"    />
        <result property="phone"    column="phone"    />
        <result property="password"    column="password"    />
        <result property="openid"    column="openid"    />
        <result property="status"    column="status"    />
        <result property="email"    column="email"    />
        <result property="birthday"    column="birthday"    />
        <result property="invitationCode"    column="invitation_code"    />
        <result property="paymentUrl"    column="payment_url"    />
        <result property="loginTime"    column="login_time"    />
        <result property="balance"    column="balance"    />
        <result property="superiorId"    column="superior_id"    />
        <result property="superiorName"    column="superiorName"    />
        <result property="superiorUuid"    column="superiorUuid"    />
        <result property="userId"    column="user_id"    />
        <result property="source"    column="source"    />
        <result property="ip"    column="ip"    />
        <result property="country"    column="country"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectAiUserVo">
        select id, del_flag, create_by, create_time, update_by, update_time, remark, username, nickname, gender, avatar, phone, password, openid, status, email, birthday, invitation_code, payment_url, login_time, balance, superior_id, user_id, source, ip, country, dept_id from ai_user
    </sql>

    <select id="selectAiUserList" parameterType="AiUser" resultMap="AiUserResult">
        select u.id, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.remark, u.username, u.nickname, u.gender, u.avatar, u.phone, u.password, u.openid, u.status, u.email, u.birthday, u.invitation_code, u.payment_url, u.login_time, u.balance, u.superior_id, u.user_id, u.source, u.ip, u.country, u.dept_id, d.dept_name, au.user_id superiorUuid, au.username superiorName from ai_user u
        left join ai_user au on au.id = u.superior_id
        left join sys_dept d on d.dept_id = u.dept_id and d.del_flag = '0'
        <where>  
            <if test="nickname != null  and nickname != ''"> and u.nickname like concat('%', #{nickname}, '%')</if>
            <if test="username != null  and username != ''"> and u.username like concat('%', #{username}, '%')</if>
            <if test="superiorName != null  and superiorName != ''"> and au.nickname like concat('%', #{superiorName}, '%')</if>
            <if test="superiorUuid != null "> and au.user_id = #{superiorUuid}</if>
            <if test="gender != null "> and u.gender = #{gender}</if>
            <if test="userId != null  and userId != ''"> and u.user_id = #{userId}</if>
            <if test="avatar != null  and avatar != ''"> and u.avatar = #{avatar}</if>
            <if test="phone != null  and phone != ''"> and u.phone = #{phone}</if>
            <if test="password != null  and password != ''"> and u.password = #{password}</if>
            <if test="openid != null  and openid != ''"> and u.openid = #{openid}</if>
            <if test="status != null "> and u.status = #{status}</if>
            <if test="email != null  and email != ''"> and u.email = #{email}</if>
            <if test="birthday != null "> and u.birthday = #{birthday}</if>
            <if test="invitationCode != null  and invitationCode != ''"> and u.invitation_code = #{invitationCode}</if>
            <if test="paymentUrl != null  and paymentUrl != ''"> and u.payment_url = #{paymentUrl}</if>
            <if test="loginTime != null "> and u.login_time = #{loginTime}</if>
            <if test="balance != null "> and u.balance = #{balance}</if>
            <if test="superiorId != null "> and u.superior_id = #{superiorId}</if>
            <if test="id != null "> and u.id = #{id}</if>
            <if test="source != null "> and u.source = #{source}</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
        </where>
        order by u.id desc
    </select>
    
    <select id="selectAiUserById" parameterType="Long" resultMap="AiUserResult">
        select u.id, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.remark, u.username, u.nickname, u.gender, u.avatar, u.phone, u.password, u.openid, u.status, u.email, u.birthday, u.invitation_code, u.payment_url, u.login_time, u.balance, u.superior_id, u.user_id, u.source, u.ip, u.country, u.dept_id, d.dept_name
        from ai_user u
        left join sys_dept d on d.dept_id = u.dept_id and d.del_flag = '0'
        where u.id = #{id}
    </select>

    <select id="getUserInfo" parameterType="Long" resultMap="AiUserResult">
        select del_flag, create_by, create_time, update_by, update_time, remark, username, nickname, gender, avatar, phone, password, openid, status, email, birthday, invitation_code, payment_url, login_time, balance, superior_id, user_id, source, ip, country, dept_id from ai_user
        where id = #{id}
    </select>

    <select id="countAiUserByDeptId" resultType="int">
        select count(1) from ai_user where del_flag = '0' and dept_id = #{deptId}
    </select>
    <select id="selectPasswordById" resultType="java.lang.String">
        select password from ai_user where id = #{id}
    </select>

    <insert id="insertAiUser" parameterType="AiUser" useGeneratedKeys="true" keyProperty="id">
        insert into ai_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="username != null">username,</if>
            <if test="nickname != null">nickname,</if>
            <if test="gender != null">gender,</if>
            <if test="avatar != null">avatar,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="password != null">password,</if>
            <if test="openid != null">openid,</if>
            <if test="status != null">status,</if>
            <if test="email != null">email,</if>
            <if test="birthday != null">birthday,</if>
            <if test="invitationCode != null">invitation_code,</if>
            <if test="paymentUrl != null">payment_url,</if>
            <if test="loginTime != null">login_time,</if>
            <if test="balance != null">balance,</if>
            <if test="superiorId != null">superior_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="source != null">source,</if>
            <if test="ip != null">ip,</if>
            <if test="country != null">country,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="username != null">#{username},</if>
            <if test="nickname != null">#{nickname},</if>
            <if test="gender != null">#{gender},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="password != null">#{password},</if>
            <if test="openid != null">#{openid},</if>
            <if test="status != null">#{status},</if>
            <if test="email != null">#{email},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="invitationCode != null">#{invitationCode},</if>
            <if test="paymentUrl != null">#{paymentUrl},</if>
            <if test="loginTime != null">#{loginTime},</if>
            <if test="balance != null">#{balance},</if>
            <if test="superiorId != null">#{superiorId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="source != null">#{source},</if>
            <if test="country != null">#{country},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateAiUser" parameterType="AiUser">
        update ai_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="username != null">username = #{username},</if>
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="password != null">password = #{password},</if>
            <if test="openid != null">openid = #{openid},</if>
            <if test="status != null">status = #{status},</if>
            <if test="email != null">email = #{email},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="invitationCode != null">invitation_code = #{invitationCode},</if>
            <if test="paymentUrl != null">payment_url = #{paymentUrl},</if>
            <if test="loginTime != null">login_time = #{loginTime},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="superiorId != null">superior_id = #{superiorId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="source != null">source = #{source},</if>
            <if test="ip != null">ip = #{ip},</if>
            <if test="country != null">country = #{country},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateUserBalance">
        UPDATE users
        SET balance = balance + #{amount}
        WHERE id = #{id};
    </update>

    <delete id="deleteAiUserById" parameterType="Long">
        delete from ai_user where id = #{id}
    </delete>

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