chengf
2025-08-22 5bfe05fa33b82fb055806125c7bacaac688241f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?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="reportFeeServiceDaoImpl">
    <select id="repostInFee" resultType="java.util.Map">
        SELECT
        fee_type_cd AS 'fee_type_cd',
        detail_year,
        ROUND(SUM(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN receivable_amount ELSE 0 END), 2) AS '该年应缴总额',
        ROUND(
        CASE
        WHEN SUM(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN receivable_amount ELSE 0 END) = 0 THEN 0
        ELSE SUM(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN received_amount ELSE 0 END)
        / SUM(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN receivable_amount ELSE 0 END) * 100
        END, 2
        ) AS '当年收缴率',
 
        ROUND(SUM(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN discount_amount ELSE 0 END), 2) AS '折扣金额',
 
        <foreach collection="yearList" item="year" separator=",">
            <if test="year != endYear">  <!-- 排除最后一年,最后一年单独处理为“当年实缴” -->
                ROUND(SUM(CASE WHEN detail_year = ${year} THEN received_amount ELSE 0 END), 2) AS '${year}年实缴'
            </if>
        </foreach>,
 
        ROUND(SUM(CASE WHEN detail_year = ${endYear} THEN receivable_amount ELSE 0 END), 2) AS '当年预算',
        ROUND(SUM(CASE WHEN detail_year = ${endYear} THEN received_amount ELSE 0 END), 2) AS '${endYear}年实缴',
        ROUND(SUM(CASE WHEN detail_year = ${endYear} THEN discount_amount ELSE 0 END), 2) AS '当年折扣总额',
        ROUND(SUM(CASE WHEN detail_year = ${endYear} THEN receivable_amount - received_amount ELSE 0 END), 2) AS '当年欠款',
 
        <foreach collection="monthList" item="month" separator=",">
            ROUND(SUM(CASE WHEN detail_year = ${endYear} AND detail_month = ${month} THEN received_amount ELSE 0 END), 2) AS '当年${month}月实缴'
        </foreach>,
 
        ROUND(AVG(CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN receivable_amount ELSE NULL END), 2) AS '每月费用',
        COUNT(DISTINCT CASE WHEN detail_year BETWEEN ${startYear} AND ${endYear} THEN CONCAT(detail_year, '-', detail_month) END) AS '应收月份数',
 
        CASE
        WHEN detail_year IS NULL THEN '类型总计'
        ELSE CAST(detail_year AS CHAR)
        END AS '费用所属年份'
 
        FROM
        pay_fee_detail_month
        WHERE
        community_id = #{communityId}
        AND detail_year BETWEEN ${startYear} AND ${endYear}  <!-- 动态年份过滤 -->
        GROUP BY
        fee_type_cd, detail_year
        WITH ROLLUP;
    </select>
    <insert id="saveReport" parameterType="Map">
        INSERT INTO report_query_record (
            id,
            refresh_time,
            end_year,
            community_id,
            community_name,
            report_content,
            operator,
            operator_id,
            query_status,
            create_time,
            update_time
        ) VALUES (
                     #{id},
                     now(),
                     #{endYear},
                     #{communityId},
                     null,
                     #{reportContent},
                     #{operator},
                     null,
                     #{queryStatus},
                     now(),
                     now()
                 )
    </insert>
    <select id="queryReport" resultType="java.util.Map">
        SELECT
        id,
        refresh_time AS refreshTime,
        end_year AS endYear,
        community_id AS communityId,
        community_name AS communityName,
        report_content AS reportContent,
        operator,
        operator_id AS operatorId,
        query_status AS queryStatus,
        create_time AS createTime,
        update_time AS updateTime
        FROM report_query_record
        WHERE 1 = 1
        <if test="communityId != null and communityId != ''">
            AND community_id = #{communityId}
        </if>
        <if test="endYear != null and endYear != ''">
            AND end_year = #{endYear}
        </if>
        <if test="queryStatus != null and queryStatus != ''">
            AND query_status = #{queryStatus}
        </if>
        <if test="operatorId != null and operatorId != ''">
            AND operator_id = #{operatorId}
        </if>
        <if test="startTime != null and startTime != ''">
            AND create_time >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND create_time &lt;= #{endTime}
        </if>
        ORDER BY create_time DESC
        <if test="pageNum != null and pageSize != null">
            LIMIT #{pageNum}, #{pageSize}
        </if>
    </select>
</mapper>