<?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 <= #{endPlannedDate}
|
</if>
|
<if test="startActualDate !=null">
|
and t.actual_payment_date >= #{startActualDate}
|
</if>
|
<if test="endActualDate !=null">
|
and t.actual_payment_date <= #{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 <= #{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 <= #{endPlannedDate}
|
</if>
|
<if test="startActualDate !=null">
|
and t.actual_payment_date >= #{startActualDate}
|
</if>
|
<if test="endActualDate !=null">
|
and t.actual_payment_date <= #{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 <= #{endPlannedDate}
|
</if>
|
<if test="startActualDate !=null">
|
and t.actual_payment_date >= #{startActualDate}
|
</if>
|
<if test="endActualDate !=null">
|
and t.actual_payment_date <= #{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 <= #{endPlannedDate}
|
</if>
|
<if test="startActualDate !=null">
|
and t.actual_payment_date >= #{startActualDate}
|
</if>
|
<if test="endActualDate !=null">
|
and t.actual_payment_date <= #{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 <= #{endPlannedDate}
|
</if>
|
<if test="startActualDate !=null">
|
and t.actual_payment_date >= #{startActualDate}
|
</if>
|
<if test="endActualDate !=null">
|
and t.actual_payment_date <= #{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 <= 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 <= #{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 <= #{endPlannedDate}
|
</if>
|
<!-- 未实付条件:实际金额为空或小于拟付金额 -->
|
and (t.actual_payment_amount is null
|
or t.actual_payment_amount = 0
|
or t.actual_payment_amount <![CDATA[ < ]]> 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>
|