<?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.AiOrderMapper">
    
    <resultMap type="AiOrder" id="AiOrderResult">
        <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="type"    column="type"    />
        <result property="amount"    column="amount"    />
        <result property="result"    column="result"    />
        <result property="status"    column="status"    />
        <result property="source"    column="source"    />
        <result property="text"    column="text"    />
        <result property="isTop"    column="is_top"    />
        <result property="img1"    column="img1"    />
        <result property="img2"    column="img2"    />
        <result property="mode"    column="mode"    />
        <result property="duration"    column="duration"    />
        <result property="resolution"    column="resolution"    />
        <result property="ratio"    column="ratio"    />
        <result property="model"    column="model"    />
        <result property="videoParams"    column="video_params"    />
    </resultMap>

    <sql id="selectAiOrderVo">
        select ao.id, ao.del_flag, ao.create_by, ao.create_time, ao.update_by, ao.update_time, ao.remark, ao.order_num, ao.user_id, ao.type, ao.amount, ao.result, ao.status, ao.source, ao.text, ao.is_top, ao.img1, ao.img2, ao.mode, ao.duration, ao.resolution, ao.ratio, ao.model, ao.video_params, au.user_id uuid from ai_order ao
        left join ai_user au on au.id = ao.user_id
    </sql>

    <select id="selectAiOrderList" parameterType="AiOrder" resultMap="AiOrderResult">
        <include refid="selectAiOrderVo"/>
        <where>  
            <if test="orderNum != null  and orderNum != ''"> and ao.orderNum like concat('%', #{orderNum}, '%')</if>
            <if test="text != null  and text != ''"> and ao.text like concat('%', #{text}, '%')</if>
            <if test="userId != null "> and ao.user_id = #{userId}</if>
            <if test="uuid != null "> and au.user_id = #{uuid}</if>
            <if test="isTop != null "> and ao.is_top = #{isTop}</if>
            <if test="type != null "> and ao.type = #{type}</if>
            <if test="amount != null "> and ao.amount = #{amount}</if>
            <if test="result != null  and result != ''"> and ao.result = #{result}</if>
            <if test="status != null "> and ao.status = #{status}</if>
            <if test="source != null "> and ao.source = #{source}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(ao.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(ao.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by ao.id desc
    </select>
    
    <select id="selectAiOrderById" parameterType="Long" resultMap="AiOrderResult">
        <include refid="selectAiOrderVo"/>
        where id = #{id}
    </select>

    <select id="getAiOrderByResult" resultType="com.ruoyi.ai.domain.AiOrder">
        <include refid="selectAiOrderVo"/>
        where result = #{result}
    </select>

    <select id="getSumAmountByUserId" resultType="java.math.BigDecimal">
        SELECT COALESCE(sum(amount), 0) from ai_order where user_id = #{userId}
    </select>

    <insert id="insertAiOrder" parameterType="AiOrder" useGeneratedKeys="true" keyProperty="id">
        insert into ai_order
        <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="type != null">type,</if>
            <if test="amount != null">amount,</if>
            <if test="result != null">result,</if>
            <if test="status != null">status,</if>
            <if test="source != null">source,</if>
            <if test="text != null">text,</if>
            <if test="isTop != null">is_top,</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="type != null">#{type},</if>
            <if test="amount != null">#{amount},</if>
            <if test="result != null">#{result},</if>
            <if test="status != null">#{status},</if>
            <if test="source != null">#{source},</if>
            <if test="text != null">#{text},</if>
            <if test="isTop != null">#{isTop},</if>
         </trim>
    </insert>

    <update id="updateAiOrder" parameterType="AiOrder">
        update ai_order
        <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="type != null">type = #{type},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="result != null">result = #{result},</if>
            <if test="status != null">status = #{status},</if>
            <if test="source != null">source = #{source},</if>
            <if test="text != null">text = #{text},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiOrderById" parameterType="Long">
        delete from ai_order where id = #{id}
    </delete>

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