<?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.AiRechargeMapper">
    
    <resultMap type="AiRecharge" id="AiRechargeResult">
        <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="orderNum"    column="order_num"    />
        <result property="userId"    column="user_id"    />
        <result property="receiveWallet"    column="receive_wallet"    />
        <result property="paymentWallet"    column="payment_wallet"    />
        <result property="hash"    column="hash"    />
        <result property="creditedTime"    column="credited_time"    />
        <result property="amount"    column="amount"    />
        <result property="gearAmount"    column="gear_amount"    />
        <result property="giftAmount"    column="gift_amount"    />
        <result property="giveAmount"    column="give_amount"    />
        <result property="gearId"    column="gear_id"    />
        <result property="source"    column="source"    />
        <result property="uuid"    column="uuid"    />
        <result property="payType"    column="pay_type"    />
    </resultMap>

    <sql id="selectAiRechargeVo">
        select r.id, r.del_flag, r.create_by, r.create_time, r.update_by, r.update_time, r.remark, r.order_num, r.user_id, r.receive_wallet, r.payment_wallet, r.hash, r.credited_time, r.amount, r.gear_amount, r.gift_amount, r.give_amount, r.gear_id, r.source, u.user_id uuid from ai_recharge r
        left join ai_user u on u.id = r.user_id
    </sql>

    <select id="selectAiRechargeList" parameterType="AiRecharge" resultMap="AiRechargeResult">
        <include refid="selectAiRechargeVo"/>
        <where>  
            <if test="orderNum != null  and orderNum != ''"> and r.order_num like concat('%', #{orderNum}, '%')</if>
            <if test="userId != null "> and r.user_id = #{userId}</if>
            <if test="uuid != null "> and u.user_id = #{uuid}</if>
            <if test="receiveWallet != null  and receiveWallet != ''"> and r.receive_wallet = #{receiveWallet}</if>
            <if test="paymentWallet != null  and paymentWallet != ''"> and r.payment_wallet = #{paymentWallet}</if>
            <if test="hash != null  and hash != ''"> and r.hash = #{hash}</if>
            <if test="source != null  and source != ''"> and r.source = #{source}</if>
            <if test="creditedTime != null"><!-- 开始时间检索 -->
                AND date_format(r.credited_time,'%Y%m%d') = date_format(#{creditedTime},'%Y%m%d')
            </if>
            <if test="createTime != null"><!-- 开始时间检索 -->
                AND date_format(r.create_time,'%Y%m%d') = date_format(#{createTime},'%Y%m%d')
            </if>
        </where>
        order by id desc
    </select>

    <select id="getAiRechargeList" resultMap="AiRechargeResult">
        select r.id, r.remark, r.order_num, r.user_id, r.receive_wallet, r.payment_wallet, r.hash, r.credited_time,r.create_time , r.gear_amount, r.gift_amount, r.give_amount, r.gear_id, r.pay_type
        from ai_recharge r
        <where>
            credited_time is not null
            <if test="orderNum != null  and orderNum != ''"> and r.order_num like concat('%', #{orderNum}, '%')</if>
            <if test="userId != null "> and r.user_id = #{userId}</if>
            <if test="uuid != null "> and u.user_id = #{uuid}</if>
            <if test="receiveWallet != null  and receiveWallet != ''"> and r.receive_wallet = #{receiveWallet}</if>
            <if test="paymentWallet != null  and paymentWallet != ''"> and r.payment_wallet = #{paymentWallet}</if>
            <if test="hash != null  and hash != ''"> and r.hash = #{hash}</if>
            <if test="source != null  and source != ''"> and r.source = #{source}</if>
            <if test="creditedTime != null"><!-- 开始时间检索 -->
                AND date_format(r.credited_time,'%Y%m%d') = date_format(#{creditedTime},'%Y%m%d')
            </if>
            <if test="createTime != null"><!-- 开始时间检索 -->
                AND date_format(r.create_time,'%Y%m%d') = date_format(#{createTime},'%Y%m%d')
            </if>
        </where>
        order by id desc
    </select>
    
    <select id="selectAiRechargeById" parameterType="Long" resultMap="AiRechargeResult">
        <include refid="selectAiRechargeVo"/>
        where id = #{id}
    </select>

    <select id="getAiRechargeByOrderNum" resultMap="AiRechargeResult">
        <include refid="selectAiRechargeVo"/>
        where order_num = #{orderNum}
    </select>

    <select id="getTotalAmount" resultType="java.math.BigDecimal">
        select COALESCE(sum(gear_amount), 0) from ai_recharge
        <where>
            credited_time is not null
            <if test="orderNum != null  and orderNum != ''"> and order_num like concat('%', #{orderNum}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="receiveWallet != null  and receiveWallet != ''"> and receive_wallet = #{receiveWallet}</if>
            <if test="paymentWallet != null  and paymentWallet != ''"> and payment_wallet = #{paymentWallet}</if>
            <if test="hash != null  and hash != ''"> and hash = #{hash}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="creditedTime != null"><!-- 开始时间检索 -->
                AND date_format(credited_time,'%Y%m%d') = date_format(#{creditedTime},'%Y%m%d')
            </if>
            <if test="createTime != null"><!-- 开始时间检索 -->
                AND date_format(create_time,'%Y%m%d') = date_format(#{createTime},'%Y%m%d')
            </if>
        </where>
    </select>



    <insert id="insertAiRecharge" parameterType="AiRecharge" useGeneratedKeys="true" keyProperty="id">
        insert into ai_recharge
        <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="orderNum != null">order_num,</if>
            <if test="userId != null">user_id,</if>
            <if test="receiveWallet != null">receive_wallet,</if>
            <if test="paymentWallet != null">payment_wallet,</if>
            <if test="hash != null">hash,</if>
            <if test="creditedTime != null">credited_time,</if>
            <if test="amount != null">amount,</if>
            <if test="gearId != null">gear_id,</if>
            <if test="source != null">source,</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="orderNum != null">#{orderNum},</if>
            <if test="userId != null">#{userId},</if>
            <if test="receiveWallet != null">#{receiveWallet},</if>
            <if test="paymentWallet != null">#{paymentWallet},</if>
            <if test="hash != null">#{hash},</if>
            <if test="creditedTime != null">#{creditedTime},</if>
            <if test="amount != null">#{amount},</if>
            <if test="gearId != null">#{gearId},</if>
            <if test="source != null">#{source},</if>
         </trim>
    </insert>

    <update id="updateAiRecharge" parameterType="AiRecharge">
        update ai_recharge
        <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="orderNum != null">order_num = #{orderNum},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="receiveWallet != null">receive_wallet = #{receiveWallet},</if>
            <if test="paymentWallet != null">payment_wallet = #{paymentWallet},</if>
            <if test="hash != null">hash = #{hash},</if>
            <if test="creditedTime != null">credited_time = #{creditedTime},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="source != null">source = #{source},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiRechargeById" parameterType="Long">
        delete from ai_recharge where id = #{id}
    </delete>

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