<?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.AiRechargeGiftRecordMapper">
    
    <resultMap type="AiRechargeGiftRecord" id="AiRechargeGiftRecordResult">
        <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="orderNum"    column="order_num"    />
        <result property="rechargeGearTitle"    column="recharge_gear_title"    />
        <result property="gearAmount"    column="gear_amount"    />
        <result property="giftAmount"    column="gift_amount"    />
        <result property="uuid"    column="uuid"    />
    </resultMap>

    <sql id="selectAiRechargeGiftRecordVo">
        select r.id, r.del_flag, r.create_by, r.create_time, r.update_by, r.update_time, r.remark, r.user_id, r.order_num, r.recharge_gear_title, r.gear_amount, r.gift_amount, u.user_id uuid from ai_recharge_gift_record r
        left join ai_user u on u.id = r.user_id
    </sql>

    <select id="selectAiRechargeGiftRecordList" parameterType="AiRechargeGiftRecord" resultMap="AiRechargeGiftRecordResult">
        <include refid="selectAiRechargeGiftRecordVo"/>
        <where>  
            <if test="uuid != null "> and u.user_id = #{uuid}</if>
            <if test="userId != null "> and r.user_id = #{userId}</if>
            <if test="orderNum != null  and orderNum != ''"> and r.orderNum like concat('%', #{orderNum}, '%')</if>
            <if test="rechargeGearTitle != null  and rechargeGearTitle != ''"> and recharge_gear_title = #{rechargeGearTitle}</if>
            <if test="gearAmount != null "> and r.gear_amount = #{gearAmount}</if>
            <if test="giftAmount != null "> and r.gift_amount = #{giftAmount}</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="selectAiRechargeGiftRecordById" parameterType="Long" resultMap="AiRechargeGiftRecordResult">
        <include refid="selectAiRechargeGiftRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertAiRechargeGiftRecord" parameterType="AiRechargeGiftRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ai_recharge_gift_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="orderNum != null">order_num,</if>
            <if test="rechargeGearTitle != null">recharge_gear_title,</if>
            <if test="gearAmount != null">gear_amount,</if>
            <if test="giftAmount != null">gift_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="orderNum != null">#{orderNum},</if>
            <if test="rechargeGearTitle != null">#{rechargeGearTitle},</if>
            <if test="gearAmount != null">#{gearAmount},</if>
            <if test="giftAmount != null">#{giftAmount},</if>
         </trim>
    </insert>

    <update id="updateAiRechargeGiftRecord" parameterType="AiRechargeGiftRecord">
        update ai_recharge_gift_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="userId != null">user_id = #{userId},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="rechargeGearTitle != null">recharge_gear_title = #{rechargeGearTitle},</if>
            <if test="gearAmount != null">gear_amount = #{gearAmount},</if>
            <if test="giftAmount != null">gift_amount = #{giftAmount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiRechargeGiftRecordById" parameterType="Long">
        delete from ai_recharge_gift_record where id = #{id}
    </delete>

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