<?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.AiRechargeGiftMapper">
    
    <resultMap type="AiRechargeGift" id="AiRechargeGiftResult">
        <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="isFristRecharge"    column="is_frist_recharge"    />
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <collection property="gearList" javaType="java.util.List" ofType="java.lang.Long" select="selectGearList" column="id" />
    </resultMap>

    <resultMap type="AiRechargeGiftGear" id="AiRechargeGiftGearResult">
        <result property="id"    column="id"    />
        <result property="rechargeId"    column="recharge_id"    />
        <result property="rechargeAmount"    column="recharge_amount"    />
        <result property="giftAmount"    column="gift_amount"    />
        <result property="giveAmount"    column="give_amount"    />
        <result property="title"    column="title"    />
        <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"    />
    </resultMap>

    <select id="selectGearList" parameterType="AiRechargeGiftGear" resultMap="AiRechargeGiftGearResult">
        select id, recharge_id, recharge_amount, gift_amount, give_amount, title, del_flag, create_by, create_time, update_by, update_time, remark from ai_recharge_gift_gear
        where  recharge_id = #{id}
    </select>

    <sql id="selectAiRechargeGiftVo">
        select id, del_flag, create_by, create_time, update_by, update_time, remark, is_frist_recharge, status, type, start_time, end_time,pay_type,api_url from ai_recharge_gift
    </sql>

    <select id="selectAiRechargeGiftList" parameterType="AiRechargeGift" resultMap="AiRechargeGiftResult">
        <include refid="selectAiRechargeGiftVo"/>
        <where>  
            <if test="isFristRecharge != null "> and is_frist_recharge = #{isFristRecharge}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
    </select>
    
    <select id="selectAiRechargeGiftById" parameterType="Long" resultMap="AiRechargeGiftResult">
        <include refid="selectAiRechargeGiftVo"/>
        where id = #{id}
    </select>

    <select id="isActivityAvailable" resultType="java.lang.Boolean">
        SELECT
            -- 核心逻辑：仅当状态为开启 且 当前时间在有效期内，才返回true
            CASE
                WHEN status = 0 AND (type =0 OR NOW() BETWEEN start_time AND end_time) THEN TRUE
                ELSE FALSE
                END AS isAvailable
        FROM
            ai_recharge_gift
        WHERE
            id = #{id}
          AND del_flag = 0
        UNION ALL
        SELECT FALSE WHERE NOT EXISTS (
        SELECT 1 FROM ai_recharge_gift WHERE id = #{id} AND del_flag = 0
        )
        LIMIT 1  -- 确保只返回一条结果
    </select>

    <insert id="insertAiRechargeGift" parameterType="AiRechargeGift" useGeneratedKeys="true" keyProperty="id">
        insert into ai_recharge_gift
        <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="isFristRecharge != null">is_frist_recharge,</if>
            <if test="status != null">status,</if>
            <if test="type != null">type,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</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="isFristRecharge != null">#{isFristRecharge},</if>
            <if test="status != null">#{status},</if>
            <if test="type != null">#{type},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
         </trim>
    </insert>

    <update id="updateAiRechargeGift" parameterType="AiRechargeGift">
        update ai_recharge_gift
        <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="isFristRecharge != null">is_frist_recharge = #{isFristRecharge},</if>
            <if test="status != null">status = #{status},</if>
            <if test="type != null">type = #{type},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateAllStatus">
        update ai_recharge_gift set status = 1 where status = 0
    </update>

    <delete id="deleteAiRechargeGiftById" parameterType="Long">
        delete from ai_recharge_gift where id = #{id}
    </delete>

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