diff --git a/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduebank/LoanOverdueBankMapper.xml b/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduebank/LoanOverdueBankMapper.xml index 0db7ef825b..9433de82f2 100644 --- a/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduebank/LoanOverdueBankMapper.xml +++ b/anrui-riskcenter/anrui-riskcenter-biz/src/main/java/com/yxt/anrui/riskcenter/biz/loanoverduebank/LoanOverdueBankMapper.xml @@ -43,17 +43,17 @@ SELECT - - a1.`loanContractNo`, - - a1.borrowerName loanName, - a1.borrowerSid loanSid, - - a1.`bankName`, - - a1.`bankContractNo`, - - COUNT(a1.sid) AS period, - + a1.loanContractNo, + a1.borrowerName AS loanName, + a1.borrowerSid AS loanSid, + a1.bankName, + a1.bankContractNo, + COUNT(a1.sid) AS period, -- 逾期期数 ( - 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.yq_total + + IFNULL(lbpv_total.bankBeInter, 0) + + IFNULL(fin_total.overdueMoney, 0) + ) AS overdueMoney, a1.customerSid, - a1.customer customerName - FROM (SELECT lrpd.sid, - - lrpd.`loanContractNo`, - lrpd.borrowerName, - - lrpd.`bankName`, - lrpd.`bankContractNo`, - lrpd.dueMoney, - ( - lrpd.`dueMoney` - SUM(IFNULL(lrh.actualMoney, 0)) - ) AS yq_total, - lrpd.customer, - lrpd.customerSid, - lrpd.borrowerSid, - lrpd.busVinSid - 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.customer AS customerName + FROM ( + -- ✅ 计算逾期金额 yq_total + SELECT + lrpd.sid, + lrpd.loanContractNo, + lrpd.borrowerName, + lrpd.bankName, + lrpd.bankContractNo, + lrpd.dueMoney, + (lrpd.dueMoney - COALESCE(SUM(lrh.actualMoney), 0)) AS yq_total, + lrpd.customer, + lrpd.customerSid, + lrpd.borrowerSid, + lrpd.busVinSid + FROM loan_repayment_plan_details lrpd + LEFT JOIN loan_repayment_history lrh + ON lrh.planDetailSid = lrpd.sid + WHERE + lrpd.dueDate + < + #{stopDate} -- ✅ 避免 DATE_FORMAT 影响索引 + AND lrpd.useOrgSid = #{useOrgSid} + GROUP BY lrpd.sid, lrpd.loanContractNo, lrpd.borrowerName, lrpd.bankName, + lrpd.bankContractNo, lrpd.dueMoney, lrpd.customer, + lrpd.customerSid, lrpd.borrowerSid, lrpd.busVinSid + 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} -- ✅ 保留动态查询条件 + ;