chengf
2026-03-18 56931dc90c4e070d279a0cbf5822af010c481812
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?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="mpFifthPaymentRecordServiceDaoImpl">
 
 
    <!-- 保存第五次拟付实付记录信息 add by wuxw 2018-07-03 -->
    <insert id="saveMpFifthPaymentRecordInfo" parameterType="Map">
        insert into mp_fifth_payment_record(
        mp_id, fifth_planned_payment_amount, planned_payment_date, category,
        reimburser, actual_payment_amount, actual_payment_date, id, payment_count, payment_target
        ) values (
        #{mpId},
        <if test="fifthPlannedPaymentAmount == null">0</if>
        <if test="fifthPlannedPaymentAmount != null">#{fifthPlannedPaymentAmount}</if>,
        #{plannedPaymentDate}, #{category},
        #{reimburser},
        <if test="actualPaymentAmount == null">0</if>
        <if test="actualPaymentAmount != null">#{actualPaymentAmount}</if>,
        #{actualPaymentDate}, #{id},
        <if test="paymentCount == null">0</if>
        <if test="paymentCount != null">#{paymentCount}</if>, #{paymentTarget}
        )
    </insert>
 
    <select id="getMpFifthPaymentRecordInfoAll" parameterType="Map" resultType="Map">
        select sum(mfpr.fifth_planned_payment_amount) as '拟付合计',
        sum(mfpr.actual_payment_amount) as '实付合计',
        sum(mfpr.fifth_planned_payment_amount) - sum(mfpr.actual_payment_amount) as '未付金额',
        0 as '垫付金额',
        sum(mfpr.fifth_planned_payment_amount) - sum(mfpr.actual_payment_amount) as '未付合计-到帐口径'
        from mp_fifth_payment_record mfpr
        where mp_id = #{mpId}
        group by mp_id
    </select>
 
    <select id="getMpFifthPaymentRecordInfoAll2" parameterType="Map" resultType="Map">
        SELECT
        -- A:简化写法,(100 - 费率)*0.01 等价于 (1 - 费率/100)
        SUM(mp.owners_committee_amount * (100 - mp.management_fee_ratio) * 0.01) AS 'A',
        -- B:业主委员会金额 - 质保金 - 管理费,逻辑保持不变
        SUM(mp.owners_committee_amount - mp.quality_guarantee_amount - (mp.owners_committee_amount * mp.management_fee_ratio * 0.01)) AS 'B',
        -- 修正嵌套 sum 问题:先算 mfpr 拟付合计,再减去管理费
        -- 注意:如果 mfpr 有多行,先求和再参与计算
        (SELECT IFNULL(SUM(fifth_planned_payment_amount), 0)
        FROM mp_fifth_payment_record
        WHERE mp_id = #{mpId})  -- 拟付合计(单一值)
        - SUM(mp.owners_committee_amount * mp.management_fee_ratio * 0.01) AS 'C'  -- 自定义别名C,对应你要的计算逻辑
        FROM maintenance_payment mp
        LEFT JOIN mp_fifth_payment_record mfpr ON mfpr.mp_id = mp.id
        WHERE mp.id = #{mpId}
        GROUP BY mp.id;  -- 显式分组,避免聚合函数警告
    </select>
 
 
    <!-- 查询第五次拟付实付记录信息 add by wuxw 2018-07-03 -->
    <select id="getMpFifthPaymentRecordInfo" parameterType="Map" resultType="Map">
        select
        t.mp_id,t.mp_id mpId,
        t.fifth_planned_payment_amount,t.fifth_planned_payment_amount fifthPlannedPaymentAmount,
        t.planned_payment_date,t.planned_payment_date plannedPaymentDate,
        t.category,
        t.reimburser,
        t.actual_payment_amount,t.actual_payment_amount actualPaymentAmount,
        t.actual_payment_date,t.actual_payment_date actualPaymentDate,
        t.id,
        t.payment_count, t.payment_count paymentCount
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="fifthPlannedPaymentAmount !=null">
            and t.fifth_planned_payment_amount = #{fifthPlannedPaymentAmount}
        </if>
        <if test="plannedPaymentDate !=null">
            and t.planned_payment_date = #{plannedPaymentDate}
        </if>
        <if test="category !=null and category != ''">
            and t.category = #{category}
        </if>
        <if test="reimburser !=null and reimburser != ''">
            and t.reimburser = #{reimburser}
        </if>
        <if test="actualPaymentAmount !=null">
            and t.actual_payment_amount = #{actualPaymentAmount}
        </if>
        <if test="actualPaymentDate !=null">
            and t.actual_payment_date = #{actualPaymentDate}
        </if>
        <if test="id !=null">
            and t.id = #{id}
        </if>
        <if test="paymentCount !=null">
            and t.payment_count = #{paymentCount}
        </if>
        <!-- 时间范围查询 -->
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="startActualDate !=null">
            and t.actual_payment_date >= #{startActualDate}
        </if>
        <if test="endActualDate !=null">
            and t.actual_payment_date &lt;= #{endActualDate}
        </if>
        <!-- 类别筛选 -->
        <if test="categoryList !=null and categoryList.size() > 0">
            and t.category in
            <foreach collection="categoryList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by t.id desc
        <if test="page != -1 and page != null ">
            limit #{page}, #{row}
        </if>
 
    </select>
 
    <select id="queryMpPaymentRecordAll" parameterType="Map" resultType="Map">
        SELECT
        mp.id AS mp_id,
        COALESCE(mpr_stats.打印合计, 0) AS 打印合计,
        COALESCE(mpr_stats.到账总额, 0) AS 到账总额,
        -- 处理打印合计为0的情况,避免显示NULL
        CASE
        WHEN COALESCE(mpr_stats.打印合计, 0) = 0 THEN '0.00%'
        ELSE CONCAT(ROUND(mpr_stats.到账总额 / mpr_stats.打印合计 * 100, 2), '%')
        END AS 到账率,
        COALESCE(mpr_stats.打印合计 - mpr_stats.到账总额, 0) AS 尚缺金额,
        CONCAT(COALESCE(mp.management_fee_ratio, 0), '%') AS 管理费占比,
        COALESCE(occ_stats.业委会总金额, 0) AS `业委会金额(应付金额A)`,
        COALESCE(oqg_stats.质保金总金额, 0) AS `质保金金额(应付金额B)`,
        -- 应付金额C计算逻辑
        CASE
        WHEN COALESCE(occ_stats.业委会总金额, 0) = 0 THEN 0
        ELSE ROUND(
        COALESCE(mpr_stats.到账总额, 0) -
        (
        COALESCE(mpr_stats.到账总额, 0) / COALESCE(occ_stats.业委会总金额, 0)
        * COALESCE(mp.management_fee_ratio, 0)
        ),
        2
        )
        END AS 应付金额C,
        mp.payee_name as 支付公司名称或个人名字,
        mp.id_card_number as 身份证号,
        mp.bank_name as 开户银行,
        mp.bank_account as 开户账号,
        COALESCE(mfpr_stats.拟付金额, 0) AS 拟付金额,
        COALESCE(mfpr_stats.实付金额, 0) AS 实付金额,
        COALESCE(mfpr_stats.拟付金额 - mfpr_stats.实付金额, 0) AS 未付金额,
        COALESCE(mfpr_stats.payment_count_sum, 0) AS 支付次数合计
        FROM maintenance_payment mp
        -- 关联聚合后的mpr统计数据(统一校对规则)
        LEFT JOIN (
        SELECT
        mp_id,
        SUM(print_amount) AS 打印合计,
        SUM(arrival_amount) AS 到账总额
        FROM mp_payment_record
        GROUP BY mp_id
        ) AS mpr_stats ON mpr_stats.mp_id = mp.id COLLATE utf8mb4_0900_ai_ci
        -- 关联聚合后的occ统计数据(统一校对规则)
        LEFT JOIN (
        SELECT
        mp_id,
        SUM(quota) AS 业委会总金额
        FROM owners_committee_convention
        GROUP BY mp_id
        ) AS occ_stats ON occ_stats.mp_id = mp.id COLLATE utf8mb4_0900_ai_ci
        -- 关联聚合后的质保金统计数据(统一校对规则)
        LEFT JOIN (
        SELECT
        mp_id,
        SUM(quality_guarantee_amount) AS 质保金总金额
        FROM owner_quality_guarantee
        GROUP BY mp_id
        ) AS oqg_stats ON oqg_stats.mp_id = mp.id COLLATE utf8mb4_0900_ai_ci
        -- 关联聚合后的mfpr统计数据(统一校对规则)
        LEFT JOIN (
        SELECT
        mp_id,
        SUM(fifth_planned_payment_amount) AS 拟付金额,
        SUM(actual_payment_amount) AS 实付金额,
        SUM(payment_count) AS payment_count_sum
        FROM mp_fifth_payment_record
        GROUP BY mp_id
        ) AS mfpr_stats ON mfpr_stats.mp_id = mp.id COLLATE utf8mb4_0900_ai_ci
        -- 按mp.id分组(包含所有非聚合字段,符合only_full_group_by规则)
        WHERE mp.id = #{id}
        GROUP BY
        mp.id,  -- 主表主键
        mpr_stats.打印合计, mpr_stats.到账总额,
        mp.management_fee_ratio,
        occ_stats.业委会总金额,
        oqg_stats.质保金总金额,
        mp.payee_name, mp.id_card_number, mp.bank_name, mp.bank_account,
        mfpr_stats.拟付金额, mfpr_stats.实付金额, mfpr_stats.payment_count_sum;
    </select>
 
 
    <!-- 删除第五次拟付实付记录信息 -->
    <delete id="deleteMpFifthPaymentRecordInfo" parameterType="Map">
        delete from mp_fifth_payment_record t
        where 1 = 1
        <if test="id != null">
            and t.id = #{id}
        </if>
        <if test="mpId != null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="category != null and category != ''">
            and t.category = #{category}
        </if>
        <if test="paymentCount != null">
            and t.payment_count = #{paymentCount}
        </if>
        <if test="startPlannedDate != null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate != null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
    </delete>
 
    <!-- 修改第五次拟付实付记录信息 add by wuxw 2018-07-03 -->
    <update id="updateMpFifthPaymentRecordInfo" parameterType="Map">
        update mp_fifth_payment_record t set t.id = #{id}
        <if test="newBId != null and newBId != ''">
            ,t.b_id = #{newBId}
        </if>
        <if test="mpId !=null and mpId != ''">
            , t.mp_id = #{mpId}
        </if>
        <if test="fifthPlannedPaymentAmount !=null">
            , t.fifth_planned_payment_amount = #{fifthPlannedPaymentAmount}
        </if>
        <if test="plannedPaymentDate !=null">
            , t.planned_payment_date = #{plannedPaymentDate}
        </if>
        <if test="category !=null and category != ''">
            , t.category = #{category}
        </if>
        <if test="reimburser !=null and reimburser != ''">
            , t.reimburser = #{reimburser}
        </if>
        <if test="actualPaymentAmount !=null">
            , t.actual_payment_amount = #{actualPaymentAmount}
        </if>
        <if test="actualPaymentDate !=null">
            , t.actual_payment_date = #{actualPaymentDate}
        </if>
        <if test="paymentCount !=null">
            , t.payment_count = #{paymentCount}
        </if>
        where 1=1
        <if test="id !=null">
            and t.id = #{id}
        </if>
 
    </update>
 
    <!-- 查询第五次拟付实付记录数量 add by wuxw 2018-07-03 -->
    <select id="queryMpFifthPaymentRecordsCount" parameterType="Map" resultType="Map">
        select count(1) count
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="fifthPlannedPaymentAmount !=null">
            and t.fifth_planned_payment_amount = #{fifthPlannedPaymentAmount}
        </if>
        <if test="plannedPaymentDate !=null">
            and t.planned_payment_date = #{plannedPaymentDate}
        </if>
        <if test="category !=null and category != ''">
            and t.category = #{category}
        </if>
        <if test="reimburser !=null and reimburser != ''">
            and t.reimburser = #{reimburser}
        </if>
        <if test="actualPaymentAmount !=null">
            and t.actual_payment_amount = #{actualPaymentAmount}
        </if>
        <if test="actualPaymentDate !=null">
            and t.actual_payment_date = #{actualPaymentDate}
        </if>
        <if test="id !=null">
            and t.id = #{id}
        </if>
        <if test="paymentCount !=null">
            and t.payment_count = #{paymentCount}
        </if>
        <!-- 时间范围查询 -->
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="startActualDate !=null">
            and t.actual_payment_date >= #{startActualDate}
        </if>
        <if test="endActualDate !=null">
            and t.actual_payment_date &lt;= #{endActualDate}
        </if>
    </select>
 
    <!-- 批量插入第五次拟付实付记录 -->
    <insert id="saveMpFifthPaymentRecords" parameterType="Map">
        insert into mp_fifth_payment_record(
        mp_id, fifth_planned_payment_amount, planned_payment_date, category,
        reimburser, actual_payment_amount, actual_payment_date, id, payment_count
        ) values
        <foreach collection="mpFifthPaymentRecordPos" item="item" separator=",">
            ( #{item.mpId},
            <if test="item.fifthPlannedPaymentAmount == null">0</if><if test="item.fifthPlannedPaymentAmount != null">#{item.fifthPlannedPaymentAmount}</if>,
            #{item.plannedPaymentDate}, #{item.category},
            #{item.reimburser},
            <if test="item.actualPaymentAmount == null">0</if><if test="item.actualPaymentAmount != null">#{item.actualPaymentAmount}</if>,
            #{item.actualPaymentDate}, #{id},
            <if test="item.paymentCount == null">0</if><if test="item.paymentCount != null">#{item.paymentCount}</if>)
        </foreach>
    </insert>
 
    <!-- 查询第五次拟付实付统计信息 -->
    <select id="queryMpFifthPaymentRecordStatistics" parameterType="Map" resultType="Map">
        select
        count(1) as total_count,
        sum(t.fifth_planned_payment_amount) as total_planned_amount,
        sum(t.actual_payment_amount) as total_actual_amount,
        (sum(t.fifth_planned_payment_amount) - sum(t.actual_payment_amount)) as total_difference,
        avg(t.fifth_planned_payment_amount) as avg_planned_amount,
        avg(t.actual_payment_amount) as avg_actual_amount,
        min(t.planned_payment_date) as earliest_planned_date,
        max(t.planned_payment_date) as latest_planned_date,
        min(t.actual_payment_date) as earliest_actual_date,
        max(t.actual_payment_date) as latest_actual_date,
        sum(t.payment_count) as total_payment_count,
        avg(t.payment_count) as avg_payment_count,
        max(t.payment_count) as max_payment_count,
        min(t.payment_count) as min_payment_count
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="startActualDate !=null">
            and t.actual_payment_date >= #{startActualDate}
        </if>
        <if test="endActualDate !=null">
            and t.actual_payment_date &lt;= #{endActualDate}
        </if>
        <if test="category !=null and category != ''">
            and t.category = #{category}
        </if>
        <if test="reimburser !=null and reimburser != ''">
            and t.reimburser = #{reimburser}
        </if>
    </select>
 
    <!-- 按类别分组统计 -->
    <select id="queryMpFifthPaymentRecordByCategory" parameterType="Map" resultType="Map">
        select
        t.category,
        count(1) as count,
        sum(t.fifth_planned_payment_amount) as planned_amount_sum,
        sum(t.actual_payment_amount) as actual_amount_sum,
        (sum(t.fifth_planned_payment_amount) - sum(t.actual_payment_amount)) as difference_sum,
        sum(t.payment_count) as payment_count_sum,
        avg(t.payment_count) as payment_count_avg
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="startActualDate !=null">
            and t.actual_payment_date >= #{startActualDate}
        </if>
        <if test="endActualDate !=null">
            and t.actual_payment_date &lt;= #{endActualDate}
        </if>
        group by t.category
        order by planned_amount_sum desc
    </select>
 
    <!-- 按报销人分组统计 -->
    <select id="queryMpFifthPaymentRecordByReimburser" parameterType="Map" resultType="Map">
        select
        t.reimburser,
        count(1) as count,
        sum(t.fifth_planned_payment_amount) as planned_amount_sum,
        sum(t.actual_payment_amount) as actual_amount_sum,
        (sum(t.fifth_planned_payment_amount) - sum(t.actual_payment_amount)) as difference_sum,
        sum(t.payment_count) as payment_count_sum,
        avg(t.payment_count) as payment_count_avg
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="startActualDate !=null">
            and t.actual_payment_date >= #{startActualDate}
        </if>
        <if test="endActualDate !=null">
            and t.actual_payment_date &lt;= #{endActualDate}
        </if>
        group by t.reimburser
        order by planned_amount_sum desc
    </select>
 
    <!-- 查询拟付与实付对比分析 -->
    <select id="queryPaymentComparisonAnalysis" parameterType="Map" resultType="Map">
        select
        date_format(t.planned_payment_date, '%Y-%m') as month,
        count(1) as total_count,
        sum(t.fifth_planned_payment_amount) as total_planned,
        sum(t.actual_payment_amount) as total_actual,
        (sum(t.fifth_planned_payment_amount) - sum(t.actual_payment_amount)) as total_difference,
        avg(t.fifth_planned_payment_amount) as avg_planned,
        avg(t.actual_payment_amount) as avg_actual,
        sum(case when t.actual_payment_amount >= t.fifth_planned_payment_amount then 1 else 0 end) as over_payment_count,
        sum(case when t.actual_payment_amount &lt;= t.fifth_planned_payment_amount then 1 else 0 end) as under_payment_count,
        sum(t.payment_count) as total_payment_count,
        avg(t.payment_count) as avg_payment_count
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <if test="category !=null and category != ''">
            and t.category = #{category}
        </if>
        group by date_format(t.planned_payment_date, '%Y-%m')
        order by month desc
    </select>
 
    <!-- 查询未实付的记录 -->
    <select id="queryUnpaidRecords" parameterType="Map" resultType="Map">
        select
        t.mp_id,t.mp_id mpId,
        t.fifth_planned_payment_amount,t.fifth_planned_payment_amount fifthPlannedPaymentAmount,
        t.planned_payment_date,t.planned_payment_date plannedPaymentDate,
        t.category,
        t.reimburser,
        t.actual_payment_amount,t.actual_payment_amount actualPaymentAmount,
        t.actual_payment_date,t.actual_payment_date actualPaymentDate,
        t.id,
        t.payment_count, t.payment_count paymentCount,
        (t.fifth_planned_payment_amount - coalesce(t.actual_payment_amount, 0)) as unpaid_amount,
        datediff(curdate(), t.planned_payment_date) as overdue_days
        from mp_fifth_payment_record t
        where 1 =1
        <if test="mpId !=null and mpId != ''">
            and t.mp_id = #{mpId}
        </if>
        <if test="startPlannedDate !=null">
            and t.planned_payment_date >= #{startPlannedDate}
        </if>
        <if test="endPlannedDate !=null">
            and t.planned_payment_date &lt;= #{endPlannedDate}
        </if>
        <!-- 未实付条件:实际金额为空或小于拟付金额 -->
        and (t.actual_payment_amount is null
        or t.actual_payment_amount = 0
        or t.actual_payment_amount <![CDATA[ &lt; ]]> t.fifth_planned_payment_amount)
        order by t.planned_payment_date asc
        <if test="page != -1 and page != null ">
            limit #{page}, #{row}
        </if>
    </select>
 
</mapper>