<?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.AiBalanceChangeRecordMapper">
    
    <resultMap type="AiBalanceChangeRecord" id="AiBalanceChangeRecordResult">
        <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="userId"    column="user_id"    />
        <result property="type"    column="type"    />
        <result property="changeAmount"    column="change_amount"    />
        <result property="resultAmount"    column="result_amount"    />
        <result property="nickname"    column="nickname"    />
        <result property="uuid"    column="uuid"    />
    </resultMap>

    <sql id="selectAiBalanceChangeRecordVo">
        select r.id, r.del_flag, r.create_by, r.create_time, r.update_by, r.update_time, r.remark, r.user_id, r.type, r.change_amount, r.result_amount, u.nickname, u.user_id uuid from ai_balance_change_record r
        left join ai_user u on u.id = r.user_id
    </sql>

    <select id="selectAiBalanceChangeRecordList" parameterType="AiBalanceChangeRecord" resultMap="AiBalanceChangeRecordResult">
        <include refid="selectAiBalanceChangeRecordVo"/>
        <where>  
            <if test="nickname != null and nickname != '' "> and u.nickname like concat('%', #{nickname}, '%') </if>
            <if test="userId != null "> and r.user_id = #{userId}</if>
            <if test="uuid != null "> and u.user_id = #{uuid}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="changeAmount != null "> and change_amount = #{changeAmount}</if>
            <if test="resultAmount != null "> and result_amount = #{resultAmount}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(r.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(r.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by r.id desc
    </select>
    
    <select id="selectAiBalanceChangeRecordById" parameterType="Long" resultMap="AiBalanceChangeRecordResult">
        <include refid="selectAiBalanceChangeRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertAiBalanceChangeRecord" parameterType="AiBalanceChangeRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ai_balance_change_record
        <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="userId != null">user_id,</if>
            <if test="type != null">type,</if>
            <if test="changeAmount != null">change_amount,</if>
            <if test="resultAmount != null">result_amount,</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="userId != null">#{userId},</if>
            <if test="type != null">#{type},</if>
            <if test="changeAmount != null">#{changeAmount},</if>
            <if test="resultAmount != null">#{resultAmount},</if>
         </trim>
    </insert>

    <update id="updateAiBalanceChangeRecord" parameterType="AiBalanceChangeRecord">
        update ai_balance_change_record
        <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="type != null">type = #{type},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="changeAmount != null">change_amount = #{changeAmount},</if>
            <if test="resultAmount != null">result_amount = #{resultAmount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiBalanceChangeRecordById" parameterType="Long">
        delete from ai_balance_change_record where id = #{id}
    </delete>

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