refactor gao module

This commit is contained in:
Timi
2026-08-06 10:52:50 +08:00
parent 568563475e
commit 1709316ce1
67 changed files with 1770 additions and 3319 deletions
@@ -0,0 +1,171 @@
<?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.imyeyu.api.modules.gao.mapper.GaoEventRecordMapper">
<select id="count" resultType="java.lang.Long">
SELECT
COUNT(1)
FROM
`gao_event_record`
WHERE
TRUE
<if test="customerId != null">
AND `customer_id` = #{customerId}
</if>
<if test="eventId != null">
AND `event_id` = #{eventId}
</if>
<if test="beginAt != null">
AND `registered_at` &gt;= #{beginAt}
</if>
<if test="endAt != null">
AND `registered_at` &gt;= #{endAt}
</if>
AND `deleted_at` IS NULL
</select>
<sql id="rangeFrom">
FROM `gao_event_record` r
LEFT JOIN `gao_customer` c ON r.`customer_id` = c.`id`
</sql>
<sql id="rangeWhere">
r.`registered_at` &gt;= #{beginAt}
AND r.`registered_at` &lt;= #{endAt}
AND r.`deleted_at` IS NULL
<if test="customerId != null">
AND r.`customer_id` = #{customerId}
</if>
<if test="eventIdList != null and eventIdList.size() > 0">
AND r.`event_id` IN
<foreach collection="eventIdList" item="eventId" open="(" separator="," close=")">
#{eventId}
</foreach>
</if>
<if test="keyword != null and keyword.trim() != ''">
AND (
c.`code` LIKE CONCAT('%', #{keyword}, '%')
OR c.`name` LIKE CONCAT('%', #{keyword}, '%')
OR r.`remark` LIKE CONCAT('%', #{keyword}, '%')
OR r.`operator_user_id` LIKE CONCAT('%', #{keyword}, '%')
)
</if>
</sql>
<select id="countByRange" resultType="long">
SELECT
COUNT(1)
<include refid="rangeFrom"/>
WHERE
<include refid="rangeWhere"/>
</select>
<select id="selectByRange" resultType="com.imyeyu.api.modules.gao.entity.GaoEventRecord">
SELECT
r.*
<include refid="rangeFrom"/>
WHERE
<include refid="rangeWhere"/>
ORDER BY r.`registered_at` DESC, r.`created_at` DESC
LIMIT #{offset}, #{limit}
</select>
<sql id="rankWhere">
r.`deleted_at` IS NULL
AND c.`deleted_at` IS NULL
<if test="customerId != null and customerId != ''">
AND r.`customer_id` = #{customerId}
</if>
<if test="beginAt != null">
AND r.`registered_at` &gt;= #{beginAt}
</if>
<if test="endAt != null">
AND r.`registered_at` &lt;= #{endAt}
</if>
<if test="eventIdList != null and eventIdList.size() > 0">
AND r.`event_id` IN
<foreach collection="eventIdList" item="eventId" open="(" separator="," close=")">
#{eventId}
</foreach>
</if>
</sql>
<select id="countRankByRange" resultType="long">
SELECT
COUNT(DISTINCT r.`customer_id`)
FROM `gao_event_record` r
LEFT JOIN `gao_customer` c ON r.`customer_id` = c.`id`
WHERE
<include refid="rankWhere"/>
</select>
<select id="selectRankByRange" resultType="com.imyeyu.api.modules.gao.vo.GaoCustomerEventRankView">
WITH `daily_stat` AS (
SELECT
r.`customer_id` AS `customerId`,
DATE(FROM_UNIXTIME(r.`registered_at` / 1000)) AS `eventDate`,
COUNT(1) AS `dailyCount`,
MAX(r.`registered_at`) AS `latestRegisteredAt`
FROM `gao_event_record` r
LEFT JOIN `gao_customer` c ON r.`customer_id` = c.`id`
WHERE
<include refid="rankWhere"/>
GROUP BY
r.`customer_id`,
DATE(FROM_UNIXTIME(r.`registered_at` / 1000))
),
`rank_stat` AS (
SELECT
`customerId`,
COUNT(1) AS `totalEventDays`,
SUM(`dailyCount`) AS `totalEventCount`,
MAX(`latestRegisteredAt`) AS `latestRegisteredAt`
FROM `daily_stat`
GROUP BY `customerId`
),
`continuous_ordered` AS (
SELECT
`customerId`,
`eventDate`,
ROW_NUMBER() OVER (PARTITION BY `customerId` ORDER BY `eventDate` DESC) AS `dayIndex`
FROM `daily_stat`
),
`latest_date` AS (
SELECT
`customerId`,
MAX(`eventDate`) AS `latestEventDate`
FROM `daily_stat`
GROUP BY `customerId`
),
`continuous_stat` AS (
SELECT
o.`customerId`,
COUNT(1) AS `recentContinuousEventDays`
FROM `continuous_ordered` o
LEFT JOIN `latest_date` l ON o.`customerId` = l.`customerId`
WHERE DATE_ADD(o.`eventDate`, INTERVAL o.`dayIndex` DAY) = DATE_ADD(l.`latestEventDate`, INTERVAL 1 DAY)
GROUP BY o.`customerId`
),
`ranked` AS (
SELECT
ROW_NUMBER() OVER (
ORDER BY
r.`totalEventDays` DESC,
r.`totalEventCount` DESC,
r.`latestRegisteredAt` DESC,
r.`customerId`
) AS `rank`,
r.`customerId`,
r.`totalEventDays`,
r.`totalEventCount`,
COALESCE(c.`recentContinuousEventDays`, 0) AS `recentContinuousEventDays`,
r.`latestRegisteredAt`
FROM `rank_stat` r
LEFT JOIN `continuous_stat` c ON r.`customerId` = c.`customerId`
)
SELECT
`rank`,
`customerId`,
`totalEventDays`,
`totalEventCount`,
`recentContinuousEventDays`,
`latestRegisteredAt`
FROM `ranked`
ORDER BY `rank`
LIMIT #{offset}, #{limit}
</select>
</mapper>