1021 lines
43 KiB
XML
1021 lines
43 KiB
XML
<?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.promo.mapper.PromoTurnoverMapper">
|
||
|
||
<select id="getAnchorCount" parameterType="Integer" resultType="int">
|
||
SELECT COUNT(*) FROM tt_user WHERE parent_id = #{userId}
|
||
</select>
|
||
|
||
<select id="getPlayersTurnover"
|
||
parameterType="com.ruoyi.promo.domain.param.GetPlayersTurnoverParam"
|
||
resultType="BigDecimal">
|
||
SELECT
|
||
SUM(ABS(total))
|
||
FROM
|
||
tt_user_blend_ercash
|
||
WHERE
|
||
type = 0
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
AND user_id IN
|
||
<foreach item="id" index="index" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
<if test="beginTime != null and beginTime != ''">
|
||
AND create_time >= #{beginTime}
|
||
</if>
|
||
<if test="endTime != null and endTime != ''">
|
||
AND create_time <= #{endTime}
|
||
</if>
|
||
</select>
|
||
|
||
<select id="getBatchedTurnover" resultType="java.util.HashMap">
|
||
SELECT
|
||
COALESCE(SUM(ABS(t.total)), 0) AS totalTurnover,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01')
|
||
AND t.create_time < DATE_FORMAT(CURDATE(), '%Y-%m-01')
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS lastMonth,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01')
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS currentMonth,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= DATE_SUB(CURDATE(), INTERVAL (WEEKDAY(CURDATE()) + 7) DAY)
|
||
AND t.create_time < DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS lastWeek,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS currentWeek,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
|
||
AND t.create_time < CURDATE()
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS lastDay,
|
||
COALESCE(SUM(CASE WHEN t.create_time >= CURDATE()
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS currentDay
|
||
FROM (
|
||
SELECT create_time, total
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 0
|
||
AND create_time >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
AND user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
UNION ALL
|
||
SELECT create_time, total
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 0
|
||
AND create_time >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
AND user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
) t
|
||
</select>
|
||
|
||
<select id="getBatchedCommissionData" resultType="java.util.HashMap">
|
||
SELECT
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{todayStart} AND create_time < #{todayEnd}
|
||
THEN amount ELSE 0 END), 0) AS todayRecharge,
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd}
|
||
THEN amount ELSE 0 END), 0) AS yesterdayRecharge,
|
||
COALESCE(SUM(CASE WHEN source IN (60) AND type = 0
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{todayStart} AND create_time < #{todayEnd}
|
||
THEN ABS(total) ELSE 0 END), 0) AS todayNewGame,
|
||
COALESCE(SUM(CASE WHEN source IN (60) AND type = 0
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd}
|
||
THEN ABS(total) ELSE 0 END), 0) AS yesterdayNewGame,
|
||
COALESCE(SUM(CASE WHEN source IN (2, 4, 40, 38) AND type = 0
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{todayStart} AND create_time < #{todayEnd}
|
||
THEN ABS(total) ELSE 0 END), 0) AS todayClassic,
|
||
COALESCE(SUM(CASE WHEN source IN (2, 4, 40, 38) AND type = 0
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd}
|
||
THEN ABS(total) ELSE 0 END), 0) AS yesterdayClassic
|
||
FROM tt_user_blend_ercash
|
||
WHERE (
|
||
(source = 1 AND type = 1 AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
OR
|
||
(source IN (60, 2, 4, 40, 38) AND type = 0 AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
)
|
||
</select>
|
||
|
||
<select id="getBatchedWelfare" resultType="java.util.HashMap">
|
||
SELECT
|
||
COALESCE(SUM(CASE WHEN create_time >= #{todayStart} AND create_time < #{todayEnd}
|
||
THEN total ELSE 0 END), 0) AS todayWelfare,
|
||
COALESCE(SUM(CASE WHEN create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd}
|
||
THEN total ELSE 0 END), 0) AS yesterdayWelfare
|
||
FROM tt_user_blend_ercash
|
||
WHERE user_id = #{userId} AND type = 1 AND source = 13
|
||
</select>
|
||
|
||
<!--
|
||
首页推广返利看板:一次性查询各游戏今日/昨日/本周/累计消费及充值。
|
||
gameUserIds 用于游戏消费,rechargeUserIds 用于充值;两者在招商场景下可能不同。
|
||
-->
|
||
<select id="getGameCommissionData" resultType="java.util.HashMap">
|
||
SELECT
|
||
COALESCE(SUM(CASE WHEN source = 2 AND type = 0 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN ABS(total) ELSE 0 END), 0) AS todayClassicBox,
|
||
COALESCE(SUM(CASE WHEN source = 40 AND type = 0 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN ABS(total) ELSE 0 END), 0) AS todayLuckyUpgrade,
|
||
COALESCE(SUM(CASE WHEN source = 4 AND type = 0 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN ABS(total) ELSE 0 END), 0) AS todayBattle,
|
||
COALESCE(SUM(CASE WHEN source = 38 AND type = 0 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN ABS(total) ELSE 0 END), 0) AS todayCrazyWheel,
|
||
COALESCE(SUM(CASE WHEN source = 60 AND type = 0 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN ABS(total) ELSE 0 END), 0) AS todayOlympus,
|
||
COALESCE(SUM(CASE WHEN source = 2 AND type = 0 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN ABS(total) ELSE 0 END), 0) AS yesterdayClassicBox,
|
||
COALESCE(SUM(CASE WHEN source = 40 AND type = 0 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN ABS(total) ELSE 0 END), 0) AS yesterdayLuckyUpgrade,
|
||
COALESCE(SUM(CASE WHEN source = 4 AND type = 0 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN ABS(total) ELSE 0 END), 0) AS yesterdayBattle,
|
||
COALESCE(SUM(CASE WHEN source = 38 AND type = 0 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN ABS(total) ELSE 0 END), 0) AS yesterdayCrazyWheel,
|
||
COALESCE(SUM(CASE WHEN source = 60 AND type = 0 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN ABS(total) ELSE 0 END), 0) AS yesterdayOlympus,
|
||
COALESCE(SUM(CASE WHEN source = 2 AND type = 0 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN ABS(total) ELSE 0 END), 0) AS weekClassicBox,
|
||
COALESCE(SUM(CASE WHEN source = 40 AND type = 0 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN ABS(total) ELSE 0 END), 0) AS weekLuckyUpgrade,
|
||
COALESCE(SUM(CASE WHEN source = 4 AND type = 0 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN ABS(total) ELSE 0 END), 0) AS weekBattle,
|
||
COALESCE(SUM(CASE WHEN source = 38 AND type = 0 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN ABS(total) ELSE 0 END), 0) AS weekCrazyWheel,
|
||
COALESCE(SUM(CASE WHEN source = 60 AND type = 0 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN ABS(total) ELSE 0 END), 0) AS weekOlympus,
|
||
COALESCE(SUM(CASE WHEN source = 2 AND type = 0 THEN ABS(total) ELSE 0 END), 0) AS totalClassicBox,
|
||
COALESCE(SUM(CASE WHEN source = 40 AND type = 0 THEN ABS(total) ELSE 0 END), 0) AS totalLuckyUpgrade,
|
||
COALESCE(SUM(CASE WHEN source = 4 AND type = 0 THEN ABS(total) ELSE 0 END), 0) AS totalBattle,
|
||
COALESCE(SUM(CASE WHEN source = 38 AND type = 0 THEN ABS(total) ELSE 0 END), 0) AS totalCrazyWheel,
|
||
COALESCE(SUM(CASE WHEN source = 60 AND type = 0 THEN ABS(total) ELSE 0 END), 0) AS totalOlympus,
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1 AND create_time >= #{todayStart} AND create_time < #{todayEnd} THEN amount ELSE 0 END), 0) AS todayRecharge,
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1 AND create_time >= #{yesterdayStart} AND create_time < #{yesterdayEnd} THEN amount ELSE 0 END), 0) AS yesterdayRecharge,
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1 AND create_time >= #{weekStart} AND create_time < #{weekEnd} THEN amount ELSE 0 END), 0) AS weekRecharge,
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1 THEN amount ELSE 0 END), 0) AS totalRecharge
|
||
FROM (
|
||
SELECT user_id, source, type, amount, total, create_time
|
||
FROM tt_user_blend_ercash
|
||
WHERE (
|
||
(source IN (2, 4, 38, 40, 60) AND type = 0 AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">#{id}</foreach>)
|
||
OR
|
||
(source = 1 AND type = 1 AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>)
|
||
)
|
||
UNION ALL
|
||
SELECT user_id, source, type, amount, total, create_time
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE (
|
||
(source IN (2, 4, 38, 40, 60) AND type = 0 AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">#{id}</foreach>)
|
||
OR
|
||
(source = 1 AND type = 1 AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>)
|
||
)
|
||
) t
|
||
</select>
|
||
|
||
<select id="getLast10DaysTurnover" parameterType="List" resultType="com.ruoyi.promo.domain.vo.DayTurnoverVO">
|
||
SELECT
|
||
date_range.date,
|
||
IFNULL(SUM(ABS(tt_user_blend_ercash.amount)), 0) AS amount
|
||
FROM
|
||
(
|
||
SELECT CURDATE() - INTERVAL 9 DAY AS date
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 8 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 7 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 6 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 5 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 4 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 3 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 2 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 1 DAY
|
||
UNION ALL
|
||
SELECT CURDATE() - INTERVAL 0 DAY
|
||
) AS date_range
|
||
LEFT JOIN tt_user_blend_ercash ON DATE(tt_user_blend_ercash.create_time) = date_range.date
|
||
AND tt_user_blend_ercash.type = '0'
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND tt_user_blend_ercash.source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
AND tt_user_blend_ercash.user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
GROUP BY date_range.date
|
||
</select>
|
||
|
||
<select id="getLast10DaysInvite" parameterType="Integer" resultType="com.ruoyi.promo.domain.vo.DayInviteVO">
|
||
SELECT
|
||
DATE(date_range.date) AS date,
|
||
COUNT(tt_user.user_id) AS anchorCount
|
||
FROM
|
||
(
|
||
SELECT CURDATE() - INTERVAL a.a DAY AS date
|
||
FROM
|
||
(
|
||
SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
|
||
) AS a
|
||
) AS date_range
|
||
LEFT JOIN tt_user ON DATE(tt_user.create_time) = DATE(date_range.date)
|
||
AND tt_user.user_type = '02'
|
||
AND tt_user.parent_id IN
|
||
(
|
||
SELECT user_id
|
||
FROM tt_user
|
||
WHERE user_type = '01'
|
||
AND parent_id = #{userId}
|
||
)
|
||
GROUP BY DATE(date_range.date)
|
||
ORDER BY DATE(date_range.date)
|
||
</select>
|
||
|
||
<select id="getLast10DaysInviteByUserIds" resultType="com.ruoyi.promo.domain.vo.DayInviteVO">
|
||
SELECT
|
||
DATE(date_range.date) AS date,
|
||
COUNT(tt_user.user_id) AS anchorCount
|
||
FROM
|
||
(
|
||
SELECT CURDATE() - INTERVAL a.a DAY AS date
|
||
FROM
|
||
(
|
||
SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
|
||
) AS a
|
||
) AS date_range
|
||
LEFT JOIN tt_user ON DATE(tt_user.create_time) = DATE(date_range.date)
|
||
<if test="userIds != null and userIds.size() > 0">
|
||
AND tt_user.user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">#{id}</foreach>
|
||
</if>
|
||
GROUP BY DATE(date_range.date)
|
||
ORDER BY DATE(date_range.date)
|
||
</select>
|
||
|
||
<select id="getAnchorDayTurnover" parameterType="com.ruoyi.promo.domain.dto.AnchorDayTurnoverDTO" resultType="com.ruoyi.promo.domain.vo.AnchorDayTurnoverVO">
|
||
SELECT
|
||
daily.user_id,
|
||
daily.parent_id,
|
||
daily.nick_name,
|
||
tu_parent.nick_name AS parent_nick_name,
|
||
daily.daily_total,
|
||
daily.date,
|
||
total.total_by_parent
|
||
FROM
|
||
(
|
||
SELECT
|
||
tube.user_id,
|
||
tu.parent_id,
|
||
tu.nick_name,
|
||
SUM(ABS(tube.total)) AS daily_total,
|
||
DATE(tube.create_time) AS date
|
||
FROM
|
||
tt_user_blend_ercash AS tube
|
||
LEFT JOIN
|
||
tt_user AS tu ON tube.user_id = tu.user_id
|
||
LEFT JOIN
|
||
tt_user AS tu_parent ON tu.parent_id = tu_parent.user_id
|
||
WHERE
|
||
tube.type = 0
|
||
AND tube.user_id IN
|
||
<foreach item="id" index="index" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND tube.source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
<if test="parentId != null">
|
||
AND tu.parent_id = #{parentId}
|
||
</if>
|
||
<if test="parentNickName != null and parentNickName != ''">
|
||
AND tu_parent.nick_name like concat('%', #{parentNickName}, '%')
|
||
</if>
|
||
<if test="beginTime != null and beginTime != ''">
|
||
AND date_format(tube.create_time,'%y%m%d') >= date_format(#{beginTime},'%y%m%d')
|
||
</if>
|
||
<if test="endTime != null and endTime != ''">
|
||
AND date_format(tube.create_time,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
|
||
</if>
|
||
GROUP BY
|
||
tube.user_id, tu.parent_id, DATE(tube.create_time)
|
||
) AS daily
|
||
JOIN
|
||
(
|
||
SELECT
|
||
tu.parent_id,
|
||
SUM(ABS(tube.total)) AS total_by_parent
|
||
FROM
|
||
tt_user_blend_ercash AS tube
|
||
LEFT JOIN
|
||
tt_user AS tu ON tube.user_id = tu.user_id
|
||
LEFT JOIN
|
||
tt_user AS tu_parent ON tu.parent_id = tu_parent.user_id
|
||
WHERE
|
||
tube.type = 0
|
||
AND tube.user_id IN
|
||
<foreach item="id" index="index" collection="userIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
<if test="sourceCodes != null and sourceCodes.size() > 0">
|
||
AND tube.source IN
|
||
<foreach item="code" collection="sourceCodes" open="(" separator="," close=")">
|
||
#{code}
|
||
</foreach>
|
||
</if>
|
||
<if test="parentId != null">
|
||
AND tu.parent_id = #{parentId}
|
||
</if>
|
||
<if test="parentNickName != null and parentNickName != ''">
|
||
AND tu_parent.nick_name like concat('%', #{parentNickName}, '%')
|
||
</if>
|
||
<if test="beginTime != null and beginTime != ''">
|
||
AND date_format(tube.create_time,'%y%m%d') >= date_format(#{beginTime},'%y%m%d')
|
||
</if>
|
||
<if test="endTime != null and endTime != ''">
|
||
AND date_format(tube.create_time,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
|
||
</if>
|
||
GROUP BY
|
||
tu.parent_id
|
||
) AS total
|
||
ON
|
||
daily.parent_id = total.parent_id
|
||
JOIN
|
||
tt_user AS tu_parent ON daily.parent_id = tu_parent.user_id
|
||
ORDER BY
|
||
daily.date DESC
|
||
</select>
|
||
|
||
<select id="getPurchaseByUserId" parameterType="Integer" resultType="com.ruoyi.domain.entity.TtUserBlendErcash">
|
||
SELECT * FROM tt_user_blend_ercash WHERE type = 0 AND user_id = #{userId}
|
||
</select>
|
||
|
||
<select id="getCommissionRateByUserId" parameterType="Integer" resultType="BigDecimal">
|
||
SELECT commission_rate FROM tt_user WHERE user_id = #{userId}
|
||
</select>
|
||
|
||
<update id="updateCommissionRate">
|
||
UPDATE tt_user SET commission_rate = #{commissionRate} WHERE user_id = #{userId}
|
||
</update>
|
||
|
||
<select id="getCommissionList" parameterType="Integer" resultType="com.ruoyi.domain.entity.TtCommissionRecord">
|
||
SELECT * FROM tt_commission_record WHERE user_id = #{userId}
|
||
</select>
|
||
|
||
<select id="getLastMonthTotalUserExpenditure" resultType="com.ruoyi.promo.domain.dto.TotalUserExpenditureDTO">
|
||
SELECT
|
||
tu.user_id, tu.user_name, tu.user_type, tu.parent_id, tu.commission_rate,
|
||
SUM(ABS(tube.amount)) AS amount
|
||
FROM
|
||
tt_user AS tu
|
||
LEFT JOIN
|
||
tt_user_blend_ercash AS tube ON tu.user_id = tube.user_id
|
||
WHERE
|
||
tu.user_type = 02
|
||
AND tube.type = 0
|
||
AND tube.create_time >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01')
|
||
AND tube.create_time < DATE_FORMAT(CURDATE(), '%Y-%m-01')
|
||
GROUP BY
|
||
tu.user_id
|
||
</select>
|
||
|
||
<select id="getBusinessUsers" resultType="com.ruoyi.domain.entity.sys.TtUser">
|
||
SELECT * FROM tt_user WHERE user_type = '04' AND del_flag = '0'
|
||
</select>
|
||
|
||
<!--
|
||
获取佣金数据时,使用快照字段直接过滤(高效、准确)
|
||
🔥 新增:根据业务 ID (招商) 统计其下级所有消费和充值
|
||
-->
|
||
<select id="getWeeklyCommissionDataWithSnapshot" resultType="java.util.HashMap">
|
||
SELECT
|
||
COALESCE(SUM(CASE WHEN source = 1 AND type = 1
|
||
AND business_id = #{businessUserId}
|
||
THEN amount ELSE 0 END), 0) AS recharge,
|
||
COALESCE(SUM(CASE WHEN source IN (60) AND type = 0
|
||
AND business_id = #{businessUserId} AND anchor_id IS NOT NULL
|
||
THEN ABS(total) ELSE 0 END), 0) AS newGameConsume,
|
||
COALESCE(SUM(CASE WHEN source IN (2, 4, 40, 38) AND type = 0
|
||
AND business_id = #{businessUserId} AND anchor_id IS NOT NULL
|
||
THEN ABS(total) ELSE 0 END), 0) AS classicGameConsume
|
||
FROM (
|
||
SELECT create_time, user_id, source, type, amount, total, business_id, anchor_id
|
||
FROM tt_user_blend_ercash
|
||
WHERE create_time >= #{startTime}
|
||
AND create_time < #{endTime}
|
||
AND (
|
||
-- 只统计属于该招商的下级产生的充值记录
|
||
(source = 1 AND type = 1 AND business_id = #{businessUserId})
|
||
OR
|
||
-- 只统计属于该招商的下级产生的游戏消费记录
|
||
(source IN (60, 2, 4, 40, 38) AND type = 0 AND business_id = #{businessUserId})
|
||
)
|
||
UNION ALL
|
||
SELECT create_time, user_id, source, type, amount, total, business_id, anchor_id
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE create_time >= #{startTime}
|
||
AND create_time < #{endTime}
|
||
AND (
|
||
-- 只统计属于该招商的下级产生的充值记录
|
||
(source = 1 AND type = 1 AND business_id = #{businessUserId})
|
||
OR
|
||
-- 只统计属于该招商的下级产生的游戏消费记录
|
||
(source IN (60, 2, 4, 40, 38) AND type = 0 AND business_id = #{businessUserId})
|
||
)
|
||
) t
|
||
</select>
|
||
|
||
<!--
|
||
按日期的周结佣金数据(使用快照字段优化)
|
||
🔥 改进:基于 business_id 直接统计,无需复杂的 EXISTS JOIN
|
||
-->
|
||
<select id="getWeeklyDailyCommissionData" resultType="java.util.HashMap">
|
||
SELECT
|
||
DATE(t.create_time) AS date,
|
||
COALESCE(SUM(CASE WHEN t.source = 1 AND t.type = 1
|
||
THEN t.amount ELSE 0 END), 0) AS recharge,
|
||
COALESCE(SUM(CASE WHEN t.source IN (60) AND t.type = 0
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS newGameConsume,
|
||
COALESCE(SUM(CASE WHEN t.source IN (2, 4, 40, 38) AND t.type = 0
|
||
THEN ABS(t.total) ELSE 0 END), 0) AS classicGameConsume
|
||
FROM (
|
||
SELECT create_time, user_id, source, type, amount, total
|
||
FROM tt_user_blend_ercash
|
||
WHERE create_time >= #{weekStartTime}
|
||
AND create_time < #{weekEndTime}
|
||
AND (
|
||
(source = 1 AND type = 1 AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
OR
|
||
(source IN (60, 2, 4, 40, 38) AND type = 0 AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
)
|
||
UNION ALL
|
||
SELECT create_time, user_id, source, type, amount, total
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE create_time >= #{weekStartTime}
|
||
AND create_time < #{weekEndTime}
|
||
AND (
|
||
(source = 1 AND type = 1 AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
OR
|
||
(source IN (60, 2, 4, 40, 38) AND type = 0 AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>)
|
||
)
|
||
) t
|
||
GROUP BY DATE(t.create_time)
|
||
ORDER BY DATE(t.create_time) ASC
|
||
</select>
|
||
|
||
<insert id="insertCommissionRecord" parameterType="com.ruoyi.domain.entity.TtCommissionRecord" useGeneratedKeys="true" keyProperty="id">
|
||
INSERT INTO tt_commission_record (
|
||
user_id,
|
||
commission,
|
||
summary_time,
|
||
settlement_start_time,
|
||
settlement_end_time,
|
||
total_flow,
|
||
rate,
|
||
claim_status,
|
||
claim_time
|
||
) VALUES (
|
||
#{userId},
|
||
#{commission},
|
||
#{summaryTime},
|
||
#{settlementStartTime},
|
||
#{settlementEndTime},
|
||
#{totalFlow},
|
||
#{rate},
|
||
#{claimStatus},
|
||
#{claimTime}
|
||
)
|
||
</insert>
|
||
|
||
<select id="getSettledTotalCommission" resultType="BigDecimal">
|
||
SELECT COALESCE(SUM(commission), 0) FROM tt_commission_record WHERE user_id = #{userId}
|
||
</select>
|
||
|
||
<select id="getRealTimeTotalCommission" resultType="BigDecimal">
|
||
SELECT COALESCE(SUM(t.amount), 0) FROM (
|
||
SELECT amount FROM tt_user_blend_ercash
|
||
WHERE user_id = #{userId}
|
||
AND type = 1
|
||
AND source IN (13, 27, 47, 49)
|
||
UNION ALL
|
||
SELECT amount FROM tt_user_blend_ercash_archive
|
||
WHERE user_id = #{userId}
|
||
AND type = 1
|
||
AND source IN (13, 27, 47, 49)
|
||
) t
|
||
</select>
|
||
|
||
<select id="countCommissionRecord" resultType="int">
|
||
SELECT COUNT(*) FROM tt_commission_record
|
||
WHERE user_id = #{userId}
|
||
AND settlement_start_time = #{settlementStartTime}
|
||
</select>
|
||
|
||
<!-- 佣金明细记录:当前用户的佣金收入流水,解析remark获取下级用户ID、实际佣金比例、消费金额和原始游戏来源 -->
|
||
<select id="getCommissionDetailRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
DATE_FORMAT(ube.create_time, '%Y-%m-%d %H:%i:%s') AS `localTime`,
|
||
ube.user_id AS `parentUserId`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
WHEN ube.source = 27 THEN tp.subordinate_user_id
|
||
ELSE NULL
|
||
END AS `subUserId`,
|
||
COALESCE(sub_user.phone_number, tp_user.phone_number) AS `phone`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%rate:%'
|
||
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1)
|
||
ELSE COALESCE(u.commission_rate, 0)
|
||
END AS `commissionRate`,
|
||
ube.amount AS `commissionAmount`,
|
||
ube.source AS `sourceGame`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%amount:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'amount:', -1), ',', 1) AS DECIMAL(19, 2))
|
||
WHEN ube.source = 27 AND ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
WHEN ube.source = 27 AND tp.recharge_price IS NOT NULL
|
||
THEN tp.recharge_price
|
||
WHEN ube.source = 27 AND u.commission_rate IS NOT NULL AND u.commission_rate != 0
|
||
THEN ABS(ube.amount) * 100 / u.commission_rate
|
||
WHEN ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) * 100 / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
ELSE NULL
|
||
END AS `consumeAmount`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%src:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'src:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE NULL
|
||
END AS `originalSource`
|
||
FROM (
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
UNION ALL
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
) ube
|
||
LEFT JOIN tt_user u ON ube.user_id = u.user_id
|
||
LEFT JOIN tt_user sub_user ON sub_user.user_id = (
|
||
CASE WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE NULL END
|
||
)
|
||
LEFT JOIN tt_promotion_record tp ON ube.source = 27
|
||
AND tp.user_id = ube.user_id
|
||
AND ABS(tp.rebate - ABS(ube.amount)) < 0.01
|
||
AND ABS(TIMESTAMPDIFF(SECOND, tp.create_time, ube.create_time)) <= 5
|
||
LEFT JOIN tt_user tp_user ON tp.subordinate_user_id = tp_user.user_id
|
||
WHERE ube.user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">#{id}</foreach>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(ube.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND (ube.remark LIKE CONCAT('%subId:', #{subUserId}, ',%') OR tp.subordinate_user_id = #{subUserId})
|
||
</if>
|
||
ORDER BY ube.create_time DESC
|
||
</select>
|
||
|
||
<!-- 招商佣金(实时计算口径):整队普通用户游戏消费流水,主表 UNION 归档表 -->
|
||
<sql id="businessGameConsumeRows">
|
||
SELECT create_time, user_id, source, total
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 0 AND source IN (2, 4, 38, 40, 60)
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
UNION ALL
|
||
SELECT create_time, user_id, source, total
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 0 AND source IN (2, 4, 38, 40, 60)
|
||
AND user_id IN
|
||
<foreach item="id" collection="gameUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
</sql>
|
||
|
||
<!-- 招商充值返利明细:查询所有下级充值记录(主表+归档表),JOIN tt_promotion_record 获取下级手机号 -->
|
||
<select id="getBusinessRechargeDetailRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
DATE_FORMAT(r.create_time, '%Y-%m-%d %H:%i:%s') AS `localTime`,
|
||
r.user_id AS `subUserId`,
|
||
u.phone_number AS `phone`,
|
||
r.amount AS `consumeAmount`,
|
||
27 AS `originalSource`
|
||
FROM (
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
UNION ALL
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
) r
|
||
LEFT JOIN tt_user u ON r.user_id = u.user_id
|
||
<where>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(r.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND r.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
ORDER BY r.create_time DESC
|
||
</select>
|
||
|
||
<!-- 招商充值返利日报:按 下级+日期 分组 -->
|
||
<select id="getBusinessRechargeDailyRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
r.user_id AS `userId`,
|
||
u.phone_number AS `phone`,
|
||
27 AS `gameType`,
|
||
SUM(r.amount) AS `consumeAmount`,
|
||
DATE_FORMAT(r.create_time, '%Y-%m-%d') AS `date`
|
||
FROM (
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
UNION ALL
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
) r
|
||
LEFT JOIN tt_user u ON r.user_id = u.user_id
|
||
<where>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(r.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND r.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
GROUP BY r.user_id, DATE_FORMAT(r.create_time, '%Y-%m-%d')
|
||
ORDER BY `date` DESC
|
||
</select>
|
||
|
||
<!-- 招商充值返利汇总:按 下级 分组 -->
|
||
<select id="getBusinessRechargeSummaryRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
r.user_id AS `userId`,
|
||
u.phone_number AS `phone`,
|
||
27 AS `gameType`,
|
||
SUM(r.amount) AS `totalFlow`,
|
||
COUNT(*) AS `gameCount`
|
||
FROM (
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
UNION ALL
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source = 1
|
||
AND user_id IN
|
||
<foreach item="id" collection="rechargeUserIds" open="(" separator="," close=")">#{id}</foreach>
|
||
) r
|
||
LEFT JOIN tt_user u ON r.user_id = u.user_id
|
||
<where>
|
||
<if test="startDate != null and startDate != ''">
|
||
AND r.create_time >= #{startDate}
|
||
</if>
|
||
<if test="endDate != null and endDate != ''">
|
||
AND r.create_time <= #{endDate}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND r.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
GROUP BY r.user_id
|
||
ORDER BY totalFlow DESC
|
||
</select>
|
||
|
||
<!-- 招商佣金明细:按招商自身费率对每笔游戏消费实时计算逐笔明细(发放仍周结,见 CommissionSettlementTask) -->
|
||
<select id="getBusinessCommissionDetailRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
DATE_FORMAT(c.create_time, '%Y-%m-%d %H:%i:%s') AS `localTime`,
|
||
c.user_id AS `subUserId`,
|
||
u.phone_number AS `phone`,
|
||
c.source AS `originalSource`,
|
||
ABS(c.total) AS `consumeAmount`
|
||
FROM (
|
||
<include refid="businessGameConsumeRows"/>
|
||
) c
|
||
LEFT JOIN tt_user u ON u.user_id = c.user_id
|
||
<where>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(c.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND c.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
ORDER BY c.create_time DESC
|
||
</select>
|
||
|
||
<!-- 招商佣金日报:按 下级+游戏+日期 分组,消费金额在 SQL 汇总,佣金在 Java 按招商费率计算 -->
|
||
<select id="getBusinessCommissionDailyRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
c.user_id AS `userId`,
|
||
u.phone_number AS `phone`,
|
||
c.source AS `gameType`,
|
||
SUM(ABS(c.total)) AS `consumeAmount`,
|
||
DATE_FORMAT(c.create_time, '%Y-%m-%d') AS `date`
|
||
FROM (
|
||
<include refid="businessGameConsumeRows"/>
|
||
) c
|
||
LEFT JOIN tt_user u ON u.user_id = c.user_id
|
||
<where>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(c.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND c.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
GROUP BY c.user_id, c.source, DATE_FORMAT(c.create_time, '%Y-%m-%d')
|
||
ORDER BY `date` DESC, c.source
|
||
</select>
|
||
|
||
<!-- 招商佣金汇总:按 下级+游戏 分组,消费金额在 SQL 汇总,佣金在 Java 按招商费率计算 -->
|
||
<select id="getBusinessCommissionSummaryRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
c.user_id AS `userId`,
|
||
u.phone_number AS `phone`,
|
||
c.source AS `gameType`,
|
||
SUM(ABS(c.total)) AS `totalFlow`,
|
||
COUNT(*) AS `gameCount`
|
||
FROM (
|
||
<include refid="businessGameConsumeRows"/>
|
||
) c
|
||
LEFT JOIN tt_user u ON u.user_id = c.user_id
|
||
<where>
|
||
<if test="startDate != null and startDate != ''">
|
||
AND c.create_time >= #{startDate}
|
||
</if>
|
||
<if test="endDate != null and endDate != ''">
|
||
AND c.create_time <= #{endDate}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND c.user_id = #{subUserId}
|
||
</if>
|
||
</where>
|
||
GROUP BY c.user_id, c.source
|
||
ORDER BY totalFlow DESC
|
||
</select>
|
||
|
||
<!-- 佣金日报:按下级+具体游戏来源+日期分组,解析remark获取下级用户ID、消费金额和原始游戏来源 -->
|
||
<select id="getCommissionDailyRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
`parentUserId`,
|
||
`userId`,
|
||
`phone`,
|
||
`gameType`,
|
||
SUM(`commissionAmount`) AS `commissionAmount`,
|
||
SUM(`consumeAmount`) AS `consumeAmount`,
|
||
`date`
|
||
FROM (
|
||
SELECT
|
||
ube.user_id AS `parentUserId`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.user_id
|
||
END AS `userId`,
|
||
sub_user.phone_number AS `phone`,
|
||
CASE
|
||
WHEN ube.source = 27 THEN 27
|
||
WHEN ube.remark LIKE '%src:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'src:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.source
|
||
END AS `gameType`,
|
||
ube.amount AS `commissionAmount`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%amount:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'amount:', -1), ',', 1) AS DECIMAL(19, 2))
|
||
WHEN ube.source = 27 AND ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
WHEN ube.source = 27 AND u.commission_rate IS NOT NULL AND u.commission_rate != 0
|
||
THEN ABS(ube.amount) * 100 / u.commission_rate
|
||
WHEN ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) * 100 / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
ELSE NULL
|
||
END AS `consumeAmount`,
|
||
DATE_FORMAT(ube.create_time, '%Y-%m-%d') AS `date`
|
||
FROM (
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
UNION ALL
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
) ube
|
||
LEFT JOIN tt_user u ON ube.user_id = u.user_id
|
||
LEFT JOIN tt_user sub_user ON sub_user.user_id = (
|
||
CASE WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.user_id END
|
||
)
|
||
WHERE ube.user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">#{id}</foreach>
|
||
<if test="date != null and date != ''">
|
||
AND DATE(ube.create_time) = #{date}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND ube.remark LIKE CONCAT('%subId:', #{subUserId}, ',%')
|
||
</if>
|
||
) t
|
||
GROUP BY `parentUserId`, `userId`, `phone`, `gameType`, `date`
|
||
ORDER BY `date` DESC, `gameType`
|
||
</select>
|
||
|
||
<!-- 佣金汇总:按下级+具体游戏来源分组,解析remark获取下级用户ID、消费金额和原始游戏来源 -->
|
||
<select id="getCommissionSummaryRecords" resultType="java.util.HashMap">
|
||
SELECT
|
||
`parentUserId`,
|
||
`userId`,
|
||
`phone`,
|
||
`gameType`,
|
||
SUM(`consumeAmount`) AS `totalFlow`,
|
||
SUM(`commissionAmount`) AS `totalCommission`,
|
||
COUNT(*) AS `gameCount`
|
||
FROM (
|
||
SELECT
|
||
ube.user_id AS `parentUserId`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.user_id
|
||
END AS `userId`,
|
||
sub_user.phone_number AS `phone`,
|
||
CASE
|
||
WHEN ube.source = 27 THEN 27
|
||
WHEN ube.remark LIKE '%src:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'src:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.source
|
||
END AS `gameType`,
|
||
ube.amount AS `commissionAmount`,
|
||
CASE
|
||
WHEN ube.remark LIKE '%amount:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'amount:', -1), ',', 1) AS DECIMAL(19, 2))
|
||
WHEN ube.source = 27 AND ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
WHEN ube.source = 27 AND u.commission_rate IS NOT NULL AND u.commission_rate != 0
|
||
THEN ABS(ube.amount) * 100 / u.commission_rate
|
||
WHEN ube.remark LIKE '%rate:%'
|
||
THEN ABS(ube.amount) * 100 / CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'rate:', -1), ',', 1) AS DECIMAL(19, 4))
|
||
ELSE NULL
|
||
END AS `consumeAmount`
|
||
FROM (
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
UNION ALL
|
||
SELECT user_id, source, type, amount, create_time, remark
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source IN (13, 27, 47, 49, 62)
|
||
) ube
|
||
LEFT JOIN tt_user u ON ube.user_id = u.user_id
|
||
LEFT JOIN tt_user sub_user ON sub_user.user_id = (
|
||
CASE WHEN ube.remark LIKE '%subId:%'
|
||
THEN CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(ube.remark, 'subId:', -1), ',', 1) AS UNSIGNED)
|
||
ELSE ube.user_id END
|
||
)
|
||
WHERE ube.user_id IN
|
||
<foreach item="id" collection="userIds" open="(" separator="," close=")">#{id}</foreach>
|
||
<if test="startDate != null and startDate != ''">
|
||
AND ube.create_time >= #{startDate}
|
||
</if>
|
||
<if test="endDate != null and endDate != ''">
|
||
AND ube.create_time <= #{endDate}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND ube.remark LIKE CONCAT('%subId:', #{subUserId}, ',%')
|
||
</if>
|
||
) t
|
||
GROUP BY `parentUserId`, `userId`, `phone`, `gameType`
|
||
ORDER BY totalCommission DESC
|
||
</select>
|
||
|
||
<!-- 下级充值报表(递归查询所有下级,按直属主播分组) -->
|
||
<select id="getSubordinateRechargeRecords" resultType="java.util.HashMap">
|
||
WITH RECURSIVE sub_tree AS (
|
||
-- 直属下级(主播),anchor_id = 自己的 user_id
|
||
SELECT user_id, parent_id, user_type, user_id AS anchor_id
|
||
FROM tt_user
|
||
WHERE parent_id = #{parentId}
|
||
AND (is_blank_account IS NULL OR is_blank_account != 1)
|
||
UNION ALL
|
||
-- 间接下级(普通用户),anchor_id = 其上级主播的 user_id
|
||
SELECT u.user_id, u.parent_id, u.user_type, st.anchor_id
|
||
FROM tt_user u
|
||
INNER JOIN sub_tree st ON u.parent_id = st.user_id
|
||
WHERE (u.is_blank_account IS NULL OR u.is_blank_account != 1)
|
||
)
|
||
SELECT
|
||
ube.user_id AS `userId`,
|
||
u.parent_id AS `parentId`,
|
||
u.phone_number AS `phone`,
|
||
u.user_type AS `userType`,
|
||
st.anchor_id AS `anchorId`,
|
||
DATE_FORMAT(ube.create_time, '%Y-%m-%d %H:%i:%s') AS `rechargeTime`,
|
||
ube.amount AS `rechargeAmount`,
|
||
COALESCE(tp.rebate, 0) AS `parentCommission`
|
||
FROM (
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash
|
||
WHERE type = 1 AND source = 1
|
||
<if test="month != null and month != ''">
|
||
AND DATE_FORMAT(create_time, '%Y-%m') = #{month}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND user_id = #{subUserId}
|
||
</if>
|
||
UNION ALL
|
||
SELECT user_id, amount, create_time
|
||
FROM tt_user_blend_ercash_archive
|
||
WHERE type = 1 AND source = 1
|
||
<if test="month != null and month != ''">
|
||
AND DATE_FORMAT(create_time, '%Y-%m') = #{month}
|
||
</if>
|
||
<if test="subUserId != null">
|
||
AND user_id = #{subUserId}
|
||
</if>
|
||
) ube
|
||
INNER JOIN tt_user u ON ube.user_id = u.user_id
|
||
INNER JOIN sub_tree st ON u.user_id = st.user_id
|
||
LEFT JOIN tt_promotion_record tp ON tp.subordinate_user_id = ube.user_id
|
||
AND tp.user_id = #{parentId}
|
||
AND tp.recharge_price = ube.amount
|
||
AND ABS(TIMESTAMPDIFF(SECOND, tp.create_time, ube.create_time)) <= 60
|
||
<if test="phone != null and phone != ''">
|
||
WHERE u.phone_number LIKE CONCAT('%', #{phone}, '%')
|
||
</if>
|
||
ORDER BY ube.create_time DESC
|
||
</select>
|
||
</mapper> |