<?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.dora.mapper.FanPromotionCommissionMapper">

    <resultMap id="FanPromotionCommissionResultMap" type="com.dora.entity.FanPromotionCommission">
        <id column="id" property="id"/>
        <result column="promoter_id" property="promoterId"/>
        <result column="fan_id" property="fanId"/>
        <result column="order_id" property="orderId"/>
        <result column="order_amount" property="orderAmount"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="commission_level" property="commissionLevel"/>
        <result column="commission_amount" property="commissionAmount"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="settled_at" property="settledAt"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <select id="selectByPromoterId" resultMap="FanPromotionCommissionResultMap">
        SELECT * FROM fan_promotion_commission 
        WHERE promoter_id = #{promoterId} AND is_deleted = 0
        ORDER BY create_time DESC
    </select>

    <select id="sumSettledCommissionByPromoterId" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(commission_amount), 0) FROM fan_promotion_commission 
        WHERE promoter_id = #{promoterId} AND status = 1 AND is_deleted = 0
    </select>

    <select id="sumPendingCommissionByPromoterId" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(commission_amount), 0) FROM fan_promotion_commission 
        WHERE promoter_id = #{promoterId} AND status = 0 AND is_deleted = 0
    </select>

    <!-- 插入推广分成记录 -->
    <insert id="insert" parameterType="com.dora.entity.FanPromotionCommission" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO fan_promotion_commission (
            promoter_id, fan_id, order_id, order_amount, commission_rate,
            commission_level, commission_amount, status, create_time, settled_at, is_deleted
        ) VALUES (
            #{promoterId}, #{fanId}, #{orderId}, #{orderAmount}, #{commissionRate},
            #{commissionLevel}, #{commissionAmount}, #{status}, #{createTime}, #{settledAt}, #{isDeleted}
        )
    </insert>

    <!-- 更新分成状态 -->
    <update id="updateStatus">
        UPDATE fan_promotion_commission
        SET status = #{status}, settled_at = #{settledAt}
        WHERE id = #{id} AND is_deleted = 0
    </update>

    <!-- 检查订单是否已有推广分成记录 -->
    <select id="existsByOrderId" resultType="java.lang.Boolean">
        SELECT COUNT(*) > 0 FROM fan_promotion_commission
        WHERE order_id = #{orderId} AND is_deleted = 0
    </select>

    <!-- 根据推广人ID查询推广分成详情（包含关联信息） -->
    <select id="selectPromotionIncomeDetailsByPromoterId" resultType="com.dora.dto.UserBalanceDto$PromotionIncomeItem">
        SELECT 
            fpc.id as commissionId,
            o.order_no as orderNo,
            fpc.order_amount as orderAmount,
            fpc.fan_id as fanUserId,
            u.username as fanUsername,
            fpc.commission_level as commissionLevel,
            CONCAT('Lv', fpc.commission_level) as levelName,
            fpc.commission_rate as commissionRate,
            fpc.commission_amount as commissionAmount,
            fpc.create_time as commissionTime,
            fpc.settled_at as settledAt,
            CONCAT('【推广收益】粉丝 ', IFNULL(u.username, '未知用户'), ' 购买会员获得Lv', fpc.commission_level, '推广分成 - 订单金额', CAST(fpc.order_amount AS CHAR), '元，分成', CAST(fpc.commission_amount AS CHAR), '元(', CAST(ROUND(fpc.commission_rate * 100, 1) AS CHAR), '%)') as description
        FROM fan_promotion_commission fpc
        LEFT JOIN `order` o ON fpc.order_id = o.id
        LEFT JOIN user u ON fpc.fan_id = u.id
        WHERE fpc.promoter_id = #{promoterId} 
        AND fpc.status = 1 
        AND fpc.is_deleted = 0
        ORDER BY fpc.settled_at DESC
    </select>

</mapper> 