feat:余额统计

This commit is contained in:
dzc committed 2025-10-23 15:43:32 +08:00
1 parent 5a3d9a4a11
commit 1a9c7d3655
1 file changed
+88 -88
@@ -194,49 +194,20 @@
</select>
<select id="selectTotalBalance" resultType="java.lang.Long">
SELECT
SUM(a.balance)
SELECT SUM(latest_balance.balance)
FROM (
SELECT DISTINCT a.user_id,
m.wallet_type_id,
FIRST_VALUE(a.balance) OVER (
PARTITION BY a.user_id, m.wallet_type_id
ORDER BY a.create_time DESC
) as balance
FROM wallet_transaction a
LEFT JOIN wallet_manage m on a.wallet_id = m.id
INNER JOIN wallet_manage m
ON a.wallet_id = m.id
<where>
a.del_flag = 0
<if test="dto.walletRecordMonth != null">
AND a.create_time = (
SELECT MAX(sub.create_time)
FROM wallet_transaction sub
LEFT JOIN wallet_manage wallet on wallet.id = sub.wallet_id
WHERE sub.user_id = a.user_id
AND DATE_FORMAT(sub.create_time, '%Y-%m') &lt;= #{dto.walletRecordMonth}
<if test="dto.userId != null">
AND sub.user_id = #{dto.userId}
</if>
<if test="dto.userIds != null and dto.userIds.size() != 0">
and sub.user_id in
<foreach collection="dto.userIds" item="v" open="(" close=")" separator=",">
#{v}
</foreach>
</if>
<if test="dto.deptIds != null and dto.deptIds.size() != 0">
<choose>
<when test="dto.isInclude != null and dto.isInclude == true">
and wallet.dept_id IN
</when>
<when test="dto.isInclude != null and dto.isInclude == false">
and wallet.dept_id NOT IN
</when>
<otherwise>
and wallet.dept_id IN
</otherwise>
</choose>
<foreach collection="dto.deptIds" item="v" open="(" close=")" separator=",">
#{v}
</foreach>
</if>
<if test="dto.walletTypeId != null">
AND wallet.wallet_type_id = #{dto.walletTypeId}
</if>
)
</if>
a.del_flag = '0'
AND DATE_FORMAT(a.create_time, '%Y-%m') &lt;= #{dto.walletRecordMonth}
<if test="dto.userId != null">
AND a.user_id = #{dto.userId}
</if>
@@ -252,13 +223,13 @@
<if test="dto.deptIds != null and dto.deptIds.size() != 0">
<choose>
<when test="dto.isInclude != null and dto.isInclude == true">
and m.dept_id IN
and a.dept_id IN
</when>
<when test="dto.isInclude != null and dto.isInclude == false">
and m.dept_id NOT IN
and a.dept_id NOT IN
</when>
<otherwise>
and m.dept_id IN
and a.dept_id IN
</otherwise>
</choose>
<foreach collection="dto.deptIds" item="v" open="(" close=")" separator=",">
@@ -266,50 +237,79 @@
</foreach>
</if>
</where>
) latest_balance
</select>
<!-- SELECT SUM(latest_balance.balance)-->
<!-- FROM (-->
<!-- SELECT DISTINCT a.user_id,-->
<!-- m.wallet_type_id,-->
<!-- FIRST_VALUE(a.balance) OVER (-->
<!-- PARTITION BY a.user_id, m.wallet_type_id-->
<!-- ORDER BY a.create_time DESC-->
<!-- ) as balance-->
<!-- FROM wallet_transaction a-->
<!-- INNER JOIN wallet_manage m-->
<!-- ON a.wallet_id = m.id-->
<!-- <where>-->
<!-- a.del_flag = '0'-->
<!-- AND DATE_FORMAT(a.create_time, '%Y-%m') &lt;= #{dto.walletRecordMonth}-->
<!-- <if test="dto.userId != null">-->
<!-- AND a.user_id = #{dto.userId}-->
<!-- </if>-->
<!-- <if test="dto.walletTypeId != null">-->
<!-- AND m.wallet_type_id = #{dto.walletTypeId}-->
<!-- </if>-->
<!-- <if test="dto.userIds != null and dto.userIds.size() != 0">-->
<!-- and a.user_id in-->
<!-- <foreach collection="dto.userIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- <if test="dto.deptIds != null and dto.deptIds.size() != 0">-->
<!-- <choose>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == true">-->
<!-- and m.dept_id IN-->
<!-- </when>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == false">-->
<!-- and m.dept_id NOT IN-->
<!-- </when>-->
<!-- <otherwise>-->
<!-- and m.dept_id IN-->
<!-- </otherwise>-->
<!-- </choose>-->
<!-- <foreach collection="dto.deptIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- </where>-->
<!-- ) latest_balance-->
<!-- SELECT-->
<!-- SUM(a.balance)-->
<!-- FROM wallet_transaction a-->
<!-- LEFT JOIN wallet_manage m on a.wallet_id = m.id-->
<!-- <where>-->
<!-- a.del_flag = 0-->
<!-- <if test="dto.walletRecordMonth != null">-->
<!-- AND a.create_time = (-->
<!-- SELECT MAX(sub.create_time)-->
<!-- FROM wallet_transaction sub-->
<!-- LEFT JOIN wallet_manage wallet on wallet.id = sub.wallet_id-->
<!-- WHERE sub.user_id = a.user_id-->
<!-- AND DATE_FORMAT(sub.create_time, '%Y-%m') &lt;= #{dto.walletRecordMonth}-->
<!-- <if test="dto.userId != null">-->
<!-- AND sub.user_id = #{dto.userId}-->
<!-- </if>-->
<!-- <if test="dto.userIds != null and dto.userIds.size() != 0">-->
<!-- and sub.user_id in-->
<!-- <foreach collection="dto.userIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- <if test="dto.deptIds != null and dto.deptIds.size() != 0">-->
<!-- <choose>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == true">-->
<!-- and wallet.dept_id IN-->
<!-- </when>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == false">-->
<!-- and wallet.dept_id NOT IN-->
<!-- </when>-->
<!-- <otherwise>-->
<!-- and wallet.dept_id IN-->
<!-- </otherwise>-->
<!-- </choose>-->
<!-- <foreach collection="dto.deptIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- <if test="dto.walletTypeId != null">-->
<!-- AND wallet.wallet_type_id = #{dto.walletTypeId}-->
<!-- </if>-->
<!-- )-->
<!-- </if>-->
<!-- <if test="dto.userId != null">-->
<!-- AND a.user_id = #{dto.userId}-->
<!-- </if>-->
<!-- <if test="dto.walletTypeId != null">-->
<!-- AND m.wallet_type_id = #{dto.walletTypeId}-->
<!-- </if>-->
<!-- <if test="dto.userIds != null and dto.userIds.size() != 0">-->
<!-- and a.user_id in-->
<!-- <foreach collection="dto.userIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- <if test="dto.deptIds != null and dto.deptIds.size() != 0">-->
<!-- <choose>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == true">-->
<!-- and m.dept_id IN-->
<!-- </when>-->
<!-- <when test="dto.isInclude != null and dto.isInclude == false">-->
<!-- and m.dept_id NOT IN-->
<!-- </when>-->
<!-- <otherwise>-->
<!-- and m.dept_id IN-->
<!-- </otherwise>-->
<!-- </choose>-->
<!-- <foreach collection="dto.deptIds" item="v" open="(" close=")" separator=",">-->
<!-- #{v}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- </where>-->
</mapper>