diff --git a/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduefin/LoanOverdueFinMapper.xml b/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduefin/LoanOverdueFinMapper.xml index 0cd3df507a..135d71695c 100644 --- a/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduefin/LoanOverdueFinMapper.xml +++ b/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduefin/LoanOverdueFinMapper.xml @@ -3,17 +3,17 @@ - SELECT a1.busVinSid AS busViNSid, - RIGHT(a1.vinNo, 8) AS vinNo, - + SELECT + a1.busVinSid AS busViNSid, + RIGHT(a1.vinNo, 8) AS vinNo, ( - SUM(a1.yq_total) + IFNULL( - (SELECT SUM(lbpv.`bankBeInter`) - FROM loan_be_padsincere_veh lbpv - WHERE lbpv.saleVehSid = a1.busVinSid), - 0 - ) + IFNULL( - (select ifnull(SUM(a.reveivableMoney), 0) - IFNULL(sum(a.m), 0) - FROM ( - select fjj.reveivableMoney, - (select IFNULL(SUM(s.subscriptionMoney), 0) - FROM anrui_fin.fin_selected_receivables_detailed AS s - WHERE s.auditState = '3' - AND s.receivablesSid = fjj.sid) as m - from anrui_fin.fin_uncollected_receivables_detailed_jr fjj - where fjj.busSid in (SELECT lrpd.sid - FROM loan_repayment_plan_details lrpd - WHERE DATE_FORMAT(lrpd.dueDate, '%Y-%m-%d') < #{stopDate} - and lrpd.busVinSid = a1.busVinSid) - and fjj.payCostTitleKey = '006') a), - 0 - ) - ) AS overdueMoney, - a1.customer AS customerName, - a1.customerSid as customerSid, - a1.borrowerSid loanSid, - a1.borrowerName loanName - FROM (SELECT lrpd.sid, - lrpd.busVinSid, - lrpd.borrowerName, - lrpd.`vinNo`, - lrpd.dueMoney, - ( - lrpd.`dueMoney` - SUM(IFNULL(lrh.actualMoney, 0)) - ) AS yq_total, - lrpd.borrowerSid, - lrpd.customerSid, - lrpd.customer - FROM `loan_repayment_plan_details` lrpd - LEFT JOIN `loan_repayment_history` lrh - ON lrh.planDetailSid = lrpd.`sid` - LEFT JOIN anrui_buscenter.`bus_sales_order_vehicle` bv - ON lrpd.busVinSid = bv.sid - WHERE DATE_FORMAT(lrpd.dueDate, '%Y-%m-%d') < #{stopDate} - and lrpd.useOrgSid = #{useOrgSid} - GROUP BY lrpd.`sid` - HAVING (yq_total > 0)) a1 + a1.yq_total + + IFNULL(lbpv_total.bankBeInter, 0) + + IFNULL(fin_total.overdueMoney, 0) + ) AS overdueMoney, + a1.customer AS customerName, + a1.customerSid AS customerSid, + a1.borrowerSid AS loanSid, + a1.borrowerName AS loanName + FROM ( + -- ✅ 计算逾期金额 yq_total + SELECT + lrpd.sid, + lrpd.busVinSid, + lrpd.borrowerName, + lrpd.vinNo, + lrpd.dueMoney, + (lrpd.dueMoney - COALESCE(SUM(lrh.actualMoney), 0)) AS yq_total, + lrpd.borrowerSid, + lrpd.customerSid, + lrpd.customer + FROM loan_repayment_plan_details lrpd + LEFT JOIN loan_repayment_history lrh + ON lrh.planDetailSid = lrpd.sid + WHERE + lrpd.dueDate + < + #{stopDate} -- ✅ 直接比较日期,避免索引失效 + AND lrpd.useOrgSid = #{useOrgSid} + GROUP BY lrpd.sid, lrpd.busVinSid, lrpd.borrowerName, lrpd.vinNo, + lrpd.dueMoney, lrpd.borrowerSid, lrpd.customerSid, lrpd.customer + HAVING yq_total > 0 + ) a1 + LEFT JOIN ( + -- ✅ 计算 bankBeInter 总金额 + SELECT saleVehSid, SUM(bankBeInter) AS bankBeInter + FROM loan_be_padsincere_veh + GROUP BY saleVehSid + ) lbpv_total ON lbpv_total.saleVehSid = a1.busVinSid + LEFT JOIN ( + -- ✅ 计算 overdueMoney + SELECT + lrpd.busVinSid, + IFNULL(SUM(fjj.reveivableMoney), 0) - IFNULL(SUM(fin_sub.m), 0) AS overdueMoney + FROM anrui_fin.fin_uncollected_receivables_detailed_jr fjj + JOIN loan_repayment_plan_details lrpd + ON fjj.busSid = lrpd.sid + AND lrpd.dueDate + < + #{stopDate} -- ✅ 避免 DATE_FORMAT + LEFT JOIN ( + SELECT receivablesSid, SUM(subscriptionMoney) AS m + FROM anrui_fin.fin_selected_receivables_detailed + WHERE auditState = '3' + GROUP BY receivablesSid + ) fin_sub ON fin_sub.receivablesSid = fjj.sid + WHERE fjj.payCostTitleKey = '006' + GROUP BY lrpd.busVinSid + ) fin_total ON fin_total.busVinSid = a1.busVinSid - ${ew.sqlSegment} - + ${ew.sqlSegment} -- ✅ 添加动态查询条件 + ;