<?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.AiRebateRecordMapper">
    
    <resultMap type="AiRebateRecord" id="AiRebateRecordResult">
        <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="superiorId"    column="superior_id"    />
        <result property="subordinateId"    column="subordinate_id"    />
        <result property="amount"    column="amount"    />
        <result property="superiorUserId"    column="superiorUserId"    />
        <result property="subordinateUserId"    column="subordinateUserId"    />
    </resultMap>

    <sql id="selectAiRebateRecordVo">
        select r.id, r.del_flag, r.create_by, r.create_time, r.update_by, r.update_time, r.remark, r.superior_id, r.subordinate_id, r.amount, su.user_id superiorUserId, lu.user_id subordinateUserId from ai_rebate_record r
        left join ai_user su on su.id = r.superior_id
        left join ai_user lu on lu.id = r.subordinate_id
    </sql>

    <select id="selectAiRebateRecordList" parameterType="AiRebateRecord" resultMap="AiRebateRecordResult">
        <include refid="selectAiRebateRecordVo"/>
        <where>  
            <if test="superiorUserId != null "> and su.user_id = #{superiorUserId}</if>
            <if test="subordinateUserId != null "> and lu.user_id = #{subordinateUserId}</if>
            <if test="superiorId != null "> and r.superior_id = #{superiorId}</if>
            <if test="subordinateId != null "> and r.subordinate_id = #{subordinateId}</if>
            <if test="amount != null "> and r.amount = #{amount}</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="selectAiRebateRecordById" parameterType="Long" resultMap="AiRebateRecordResult">
        <include refid="selectAiRebateRecordVo"/>
        where id = #{id}
    </select>

    <select id="getTotalAmount" resultType="java.math.BigDecimal">
        select sum(amount) from ai_rebate_record
        <where>
            <if test="superiorId != null "> and superior_id = #{superiorId}</if>
            <if test="subordinateId != null "> and subordinate_id = #{subordinateId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(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(create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
    </select>

    <select id="getAiRebateRecordList" resultMap="AiRebateRecordResult">
        select su.user_id superiorUserId, lu.user_id subordinateUserId, r.create_time, amount from ai_rebate_record r
        left join ai_user su on su.id = r.superior_id
        left join ai_user lu on lu.id = r.subordinate_id
        where r.superior_id = #{superiorId}
    </select>

    <insert id="insertAiRebateRecord" parameterType="AiRebateRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ai_rebate_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="superiorId != null">superior_id,</if>
            <if test="subordinateId != null">subordinate_id,</if>
            <if test="amount != null">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="superiorId != null">#{superiorId},</if>
            <if test="subordinateId != null">#{subordinateId},</if>
            <if test="amount != null">#{amount},</if>
         </trim>
    </insert>

    <update id="updateAiRebateRecord" parameterType="AiRebateRecord">
        update ai_rebate_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="superiorId != null">superior_id = #{superiorId},</if>
            <if test="subordinateId != null">subordinate_id = #{subordinateId},</if>
            <if test="amount != null">amount = #{amount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiRebateRecordById" parameterType="Long">
        delete from ai_rebate_record where id = #{id}
    </delete>

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