From 849648e82e5dec96f9f30fcc9c9bd799268b1f4c Mon Sep 17 00:00:00 2001
From: wujianwei <wjw@11.com>
Date: 星期四, 09 四月 2026 10:40:44 +0800
Subject: [PATCH] 新增资金流水导入

---
 service/src/main/resources/mapper/cwgl/ReceivableBillManagementMapper.xml |  222 ++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 186 insertions(+), 36 deletions(-)

diff --git a/service/src/main/resources/mapper/cwgl/ReceivableBillManagementMapper.xml b/service/src/main/resources/mapper/cwgl/ReceivableBillManagementMapper.xml
index aba8560..ce7eb8a 100644
--- a/service/src/main/resources/mapper/cwgl/ReceivableBillManagementMapper.xml
+++ b/service/src/main/resources/mapper/cwgl/ReceivableBillManagementMapper.xml
@@ -33,7 +33,15 @@
         <result property="billGenerateDate"    column="bill_generate_date"    />
         <result property="billSendDate"    column="bill_send_date"    />
         <result property="billDueDate"    column="bill_due_date"    />
+        <result property="settlementCategory"    column="settlement_category"    />
+        <result property="settlementPeriod"    column="settlement_period"    />
+        <result property="ncSettlementDate"    column="nc_settlement_date"    />
+        <result property="ncSettlementAmount"    column="nc_settlement_amount"    />
+        <result property="accountRemark"    column="account_remark"    />
+        <result property="overdueInterest"    column="overdue_interest"    />
         <result property="status"    column="status"    />
+        <result property="auditStatus"    column="audit_status"    />
+        <result property="sourceSystemId"    column="source_system_id"    />
         <result property="remark"    column="remark"    />
         <result property="createBy"    column="create_by"    />
         <result property="createTime"    column="create_time"    />
@@ -47,38 +55,25 @@
         <result property="systemNo"    column="system_no"    />
         <result property="billName"    column="bill_name"    />
         <result property="customerName"    column="customer_name"    />
-        <result property="payee"    column="payee"    />
+
         <result property="responsiblePerson"    column="responsible_person"    />
         <result property="responsibleLeader"    column="responsible_leader"    />
         <result property="settlementMethod"    column="settlement_method"    />
         <result property="businessType"    column="business_type"    />
         <result property="promotionRequirement"    column="promotion_requirement"    />
-        <result property="isInternalSettlement"    column="is_internal_settlement"    />
-        <result property="internalSettlementUnit"    column="internal_settlement_unit"    />
-        <result property="documentCount"    column="document_count"    />
         <result property="totalAmount"    column="total_amount"    />
         <result property="currency"    column="currency"    />
-        <result property="discountAmount"    column="discount_amount"    />
         <result property="receivedAmount"    column="received_amount"    />
         <result property="pendingAmount"    column="pending_amount"    />
-        <result property="exchangeRate"    column="exchange_rate"    />
-        <result property="cnyAmount"    column="cny_amount"    />
-        <result property="periodType"    column="period_type"    />
-        <result property="businessStartDate"    column="business_start_date"    />
-        <result property="businessEndDate"    column="business_end_date"    />
+
+
         <result property="billingStartDate"    column="billing_start_date"    />
         <result property="billingEndDate"    column="billing_end_date"    />
-        <result property="billGenerateDate"    column="bill_generate_date"    />
-        <result property="billSendDate"    column="bill_send_date"    />
+
         <result property="billDueDate"    column="bill_due_date"    />
-        <result property="status"    column="status"    />
-        <result property="remark"    column="remark"    />
-        <result property="createBy"    column="create_by"    />
-        <result property="createTime"    column="create_time"    />
-        <result property="updateBy"    column="update_by"    />
-        <result property="updateTime"    column="update_time"    />
+
         <result property="dueIn30Days"    column="due_in_30_days"    />
-        <result property="overdueAmount"    column="overdue_amount"    />
+
         <result property="overdue1To30Days"    column="overdue_1_to_30_days"    />
         <result property="overdue31To60Days"    column="overdue_31_to_60_days"    />
         <result property="overdue61To90Days"    column="overdue_61_to_90_days"    />
@@ -89,7 +84,7 @@
     </resultMap>
 
     <sql id="selectReceivableBillManagementVo">
-        select thisTab.id, thisTab.system_no, thisTab.bill_name, thisTab.customer_name, thisTab.payee, thisTab.responsible_person, thisTab.responsible_leader, thisTab.settlement_method, thisTab.business_type, thisTab.promotion_requirement, thisTab.is_internal_settlement, thisTab.internal_settlement_unit, thisTab.document_count, thisTab.total_amount, thisTab.currency, thisTab.discount_amount, thisTab.received_amount, thisTab.pending_amount, thisTab.exchange_rate, thisTab.cny_amount, thisTab.period_type, thisTab.business_start_date, thisTab.business_end_date, thisTab.billing_start_date, thisTab.billing_end_date, thisTab.bill_generate_date, thisTab.bill_send_date, thisTab.bill_due_date, thisTab.due_in_30_days, thisTab.overdue_amount, thisTab.overdue_1_to_30_days, thisTab.overdue_31_to_60_days, thisTab.overdue_61_to_90_days, thisTab.overdue_91_to_180_days, thisTab.overdue_181_to_365_days, thisTab.overdue_over_1_year, thisTab.overdue_days, thisTab.status, thisTab.remark, thisTab.create_by, thisTab.create_time, thisTab.update_by, thisTab.update_time, thisTab.deleted from receivable_bill_management AS thisTab
+        select thisTab.id, thisTab.system_no, thisTab.bill_name, thisTab.customer_name, thisTab.payee, thisTab.responsible_person, thisTab.responsible_leader, thisTab.settlement_method, thisTab.business_type, thisTab.promotion_requirement, thisTab.is_internal_settlement, thisTab.internal_settlement_unit, thisTab.document_count, thisTab.total_amount, thisTab.currency, thisTab.discount_amount, thisTab.received_amount, thisTab.pending_amount, thisTab.exchange_rate, thisTab.cny_amount, thisTab.period_type, thisTab.business_start_date, thisTab.business_end_date, thisTab.billing_start_date, thisTab.billing_end_date, thisTab.bill_generate_date, thisTab.bill_send_date, thisTab.bill_due_date, thisTab.due_in_30_days, thisTab.overdue_amount, thisTab.overdue_1_to_30_days, thisTab.overdue_31_to_60_days, thisTab.overdue_61_to_90_days, thisTab.overdue_91_to_180_days, thisTab.overdue_181_to_365_days, thisTab.overdue_over_1_year, thisTab.overdue_days, thisTab.settlement_category, thisTab.settlement_period, thisTab.nc_settlement_date, thisTab.nc_settlement_amount, thisTab.account_remark, thisTab.overdue_interest, thisTab.status, thisTab.audit_status, thisTab.source_system_id, thisTab.remark, thisTab.create_by, thisTab.create_time, thisTab.update_by, thisTab.update_time, thisTab.deleted from receivable_bill_management AS thisTab
     </sql>
     <sql id="selectReceivableBillManagementVoCount">
         select count(0) from receivable_bill_management as thisTab
@@ -123,6 +118,12 @@
         <if test="billGenerateDate != null "> and thisTab.bill_generate_date = #{billGenerateDate}</if>
         <if test="billSendDate != null "> and thisTab.bill_send_date = #{billSendDate}</if>
         <if test="billDueDate != null "> and thisTab.bill_due_date = #{billDueDate}</if>
+        <if test="settlementCategory != null  and settlementCategory != ''"> and thisTab.settlement_category = #{settlementCategory}</if>
+        <if test="settlementPeriod != null  and settlementPeriod != ''"> and thisTab.settlement_period = #{settlementPeriod}</if>
+        <if test="ncSettlementDate != null "> and thisTab.nc_settlement_date = #{ncSettlementDate}</if>
+        <if test="ncSettlementAmount != null "> and thisTab.nc_settlement_amount = #{ncSettlementAmount}</if>
+        <if test="accountRemark != null  and accountRemark != ''"> and thisTab.account_remark like concat('%', #{accountRemark}, '%')</if>
+        <if test="overdueInterest != null "> and thisTab.overdue_interest = #{overdueInterest}</if>
         <if test="status != null and status != ''">
             <choose>
                 <when test="status == -1">
@@ -193,7 +194,15 @@
             <if test="billGenerateDate != null">bill_generate_date,</if>
             <if test="billSendDate != null">bill_send_date,</if>
             <if test="billDueDate != null">bill_due_date,</if>
+            <if test="settlementCategory != null and settlementCategory != ''">settlement_category,</if>
+            <if test="settlementPeriod != null and settlementPeriod != ''">settlement_period,</if>
+            <if test="ncSettlementDate != null">nc_settlement_date,</if>
+            <if test="ncSettlementAmount != null">nc_settlement_amount,</if>
+            <if test="accountRemark != null and accountRemark != ''">account_remark,</if>
+            <if test="overdueInterest != null">overdue_interest,</if>
             <if test="status != null">status,</if>
+            <if test="auditStatus != null">audit_status,</if>
+            <if test="sourceSystemId != null">source_system_id,</if>
             <if test="remark != null">remark,</if>
             <if test="createBy != null">create_by,</if>
             <if test="createTime != null">create_time,</if>
@@ -229,7 +238,15 @@
             <if test="billGenerateDate != null">#{billGenerateDate},</if>
             <if test="billSendDate != null">#{billSendDate},</if>
             <if test="billDueDate != null">#{billDueDate},</if>
+            <if test="settlementCategory != null and settlementCategory != ''">#{settlementCategory},</if>
+            <if test="settlementPeriod != null and settlementPeriod != ''">#{settlementPeriod},</if>
+            <if test="ncSettlementDate != null">#{ncSettlementDate},</if>
+            <if test="ncSettlementAmount != null">#{ncSettlementAmount},</if>
+            <if test="accountRemark != null and accountRemark != ''">#{accountRemark},</if>
+            <if test="overdueInterest != null">#{overdueInterest},</if>
             <if test="status != null">#{status},</if>
+            <if test="auditStatus != null">#{auditStatus},</if>
+            <if test="sourceSystemId != null">#{sourceSystemId},</if>
             <if test="remark != null">#{remark},</if>
             <if test="createBy != null">#{createBy},</if>
             <if test="createTime != null">#{createTime},</if>
@@ -242,11 +259,11 @@
     <insert id="insertReceivableBillManagementBatch" parameterType="java.util.List"  useGeneratedKeys="true" keyProperty="id">
         insert into receivable_bill_management
         <trim prefix="(" suffix=") values" suffixOverrides=",">
-            system_no,bill_name,customer_name,payee,responsible_person,responsible_leader,settlement_method,business_type,promotion_requirement,is_internal_settlement,internal_settlement_unit,document_count,total_amount,currency,discount_amount,received_amount,pending_amount,exchange_rate,cny_amount,period_type,business_start_date,business_end_date,billing_start_date,billing_end_date,bill_generate_date,bill_send_date,bill_due_date,status,remark,create_by,create_time,update_by,update_time,deleted,
+            system_no,bill_name,customer_name,payee,responsible_person,responsible_leader,settlement_method,business_type,promotion_requirement,is_internal_settlement,internal_settlement_unit,document_count,total_amount,currency,discount_amount,received_amount,pending_amount,exchange_rate,cny_amount,period_type,business_start_date,business_end_date,billing_start_date,billing_end_date,bill_generate_date,bill_send_date,bill_due_date,settlement_category,settlement_period,nc_settlement_date,nc_settlement_amount,account_remark,overdue_interest,status,audit_status,source_system_id,remark,create_by,create_time,update_by,update_time,deleted,
         </trim>
         <foreach item="item" index="index" collection="list" separator=",">
             <trim prefix="(" suffix=") " suffixOverrides=",">
-                #{item.systemNo},#{item.billName},#{item.customerName},#{item.payee},#{item.responsiblePerson},#{item.responsibleLeader},#{item.settlementMethod},#{item.businessType},#{item.promotionRequirement},#{item.isInternalSettlement},#{item.internalSettlementUnit},#{item.documentCount},#{item.totalAmount},#{item.currency},#{item.discountAmount},#{item.receivedAmount},#{item.pendingAmount},#{item.exchangeRate},#{item.cnyAmount},#{item.periodType},#{item.businessStartDate},#{item.businessEndDate},#{item.billingStartDate},#{item.billingEndDate},#{item.billGenerateDate},#{item.billSendDate},#{item.billDueDate},#{item.status},#{item.remark},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.deleted},
+                #{item.systemNo},#{item.billName},#{item.customerName},#{item.payee},#{item.responsiblePerson},#{item.responsibleLeader},#{item.settlementMethod},#{item.businessType},#{item.promotionRequirement},#{item.isInternalSettlement},#{item.internalSettlementUnit},#{item.documentCount},#{item.totalAmount},#{item.currency},#{item.discountAmount},#{item.receivedAmount},#{item.pendingAmount},#{item.exchangeRate},#{item.cnyAmount},#{item.periodType},#{item.businessStartDate},#{item.businessEndDate},#{item.billingStartDate},#{item.billingEndDate},#{item.billGenerateDate},#{item.billSendDate},#{item.billDueDate},#{item.settlementCategory},#{item.settlementPeriod},#{item.ncSettlementDate},#{item.ncSettlementAmount},#{item.accountRemark},#{item.overdueInterest},#{item.status},#{item.auditStatus},#{item.sourceSystemId},#{item.remark},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.deleted},
             </trim>
         </foreach>
     </insert>
@@ -282,7 +299,15 @@
             <if test="billGenerateDate != null">bill_generate_date = #{billGenerateDate},</if>
             <if test="billSendDate != null">bill_send_date = #{billSendDate},</if>
             <if test="billDueDate != null">bill_due_date = #{billDueDate},</if>
+            <if test="settlementCategory != null and settlementCategory != ''">settlement_category = #{settlementCategory},</if>
+            <if test="settlementPeriod != null and settlementPeriod != ''">settlement_period = #{settlementPeriod},</if>
+            <if test="ncSettlementDate != null">nc_settlement_date = #{ncSettlementDate},</if>
+            <if test="ncSettlementAmount != null">nc_settlement_amount = #{ncSettlementAmount},</if>
+            <if test="accountRemark != null and accountRemark != ''">account_remark = #{accountRemark},</if>
+            <if test="overdueInterest != null">overdue_interest = #{overdueInterest},</if>
             <if test="status != null">status = #{status},</if>
+            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
+            <if test="sourceSystemId != null">source_system_id = #{sourceSystemId},</if>
             <if test="remark != null">remark = #{remark},</if>
             <if test="createBy != null">create_by = #{createBy},</if>
             <if test="createTime != null">create_time = #{createTime},</if>
@@ -324,7 +349,15 @@
                 <if test="item.billGenerateDate != null">bill_generate_date = #{item.billGenerateDate},</if>
                 <if test="item.billSendDate != null">bill_send_date = #{item.billSendDate},</if>
                 <if test="item.billDueDate != null">bill_due_date = #{item.billDueDate},</if>
+                <if test="item.settlementCategory != null and item.settlementCategory != ''">settlement_category = #{item.settlementCategory},</if>
+                <if test="item.settlementPeriod != null and item.settlementPeriod != ''">settlement_period = #{item.settlementPeriod},</if>
+                <if test="item.ncSettlementDate != null">nc_settlement_date = #{item.ncSettlementDate},</if>
+                <if test="item.ncSettlementAmount != null">nc_settlement_amount = #{item.ncSettlementAmount},</if>
+                <if test="item.accountRemark != null and item.accountRemark != ''">account_remark = #{item.accountRemark},</if>
+                <if test="item.overdueInterest != null">overdue_interest = #{item.overdueInterest},</if>
                 <if test="item.status != null">status = #{item.status},</if>
+                <if test="item.auditStatus != null">audit_status = #{item.auditStatus},</if>
+                <if test="item.sourceSystemId != null">source_system_id = #{item.sourceSystemId},</if>
                 <if test="item.remark != null">remark = #{item.remark},</if>
                 <if test="item.createBy != null">create_by = #{item.createBy},</if>
                 <if test="item.createTime != null">create_time = #{item.createTime},</if>
@@ -365,9 +398,37 @@
 
     <!-- 鏌ヨ搴旀敹璐﹀崟璐﹂緞鍒嗘瀽鍒楄〃 -->
     <select id="selectReceivableBillAgingAnalysisList" parameterType="com.ruoyi.cwgl.domain.ReceivableBillManagement" resultMap="ReceivableBillAgingAnalysisVoResult">
-        <include refid="selectReceivableBillManagementVo"/>
+        SELECT 
+            thisTab.*,
+            rib_latest.latest_invoice_time as latestInvoiceTime,
+            COALESCE(rib_total.total_invoice_amount, 0) as totalInvoiceAmount,
+            CASE 
+                WHEN thisTab.pending_amount > COALESCE(rib_total.total_invoice_amount, 0) 
+                THEN thisTab.pending_amount - COALESCE(rib_total.total_invoice_amount, 0)
+                ELSE 0 
+            END as unInvoicedAmount
+        FROM receivable_bill_management thisTab
+        
+        -- 鑾峰彇鏈�鏂板紑绁ㄦ棩鏈�
+        LEFT JOIN (
+            SELECT head_id, MAX(invoice_time) as latest_invoice_time
+            FROM receivable_invoice_business
+            WHERE status = 1
+            GROUP BY head_id
+        ) rib_latest ON thisTab.id = rib_latest.head_id
+        
+        -- 鑾峰彇寮�绁ㄦ�婚噾棰�
+        LEFT JOIN (
+            SELECT head_id, SUM(invoicing_amount) as total_invoice_amount
+            FROM receivable_invoice_business
+            WHERE status = 1
+            GROUP BY head_id
+        ) rib_total ON thisTab.id = rib_total.head_id
+        
         <where>
-            <include refid="whereCondition"/>
+            <if test="systemNo != null  and systemNo != ''"> and thisTab.system_no = #{systemNo}</if>
+            <if test="customerName != null  and customerName != ''"> and  thisTab.customer_name like concat('%', #{customerName}, '%')</if>
+
         </where>
         ORDER BY customer_name, bill_due_date
     </select>
@@ -376,71 +437,160 @@
     <update id="batchUpdateAgingAnalysisData">
         UPDATE receivable_bill_management 
         SET 
-            -- 鍙洿鏂板綋鍓嶆椂娈电殑鏁版嵁锛屽叾浠栨椂娈典繚鐣欏師鍊�
+
             due_in_30_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date > CURDATE() 
-                     AND DATEDIFF(bill_due_date, CURDATE()) <= 30 
+                     AND DATEDIFF(bill_due_date, CURDATE()) &lt;= 30
                 THEN pending_amount 
-                ELSE due_in_30_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE due_in_30_days  
             END,
             overdue_amount = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                 THEN pending_amount 
-                ELSE overdue_amount  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_amount  
             END,
-            -- 鍙洿鏂板綋鍓嶉�炬湡鏃舵鐨勯噾棰濓紝鍏朵粬鏃舵淇濈暀鍘嗗彶鏁版嵁
+
             overdue_1_to_30_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) BETWEEN 1 AND 30 
                 THEN pending_amount 
-                ELSE overdue_1_to_30_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_1_to_30_days  
             END,
             overdue_31_to_60_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) BETWEEN 31 AND 60 
                 THEN pending_amount 
-                ELSE overdue_31_to_60_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_31_to_60_days  
             END,
             overdue_61_to_90_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) BETWEEN 61 AND 90 
                 THEN pending_amount 
-                ELSE overdue_61_to_90_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_61_to_90_days  
             END,
             overdue_91_to_180_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) BETWEEN 91 AND 180 
                 THEN pending_amount 
-                ELSE overdue_91_to_180_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_91_to_180_days  
             END,
             overdue_181_to_365_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) BETWEEN 181 AND 365 
                 THEN pending_amount 
-                ELSE overdue_181_to_365_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_181_to_365_days  
             END,
             overdue_over_1_year = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                      AND DATEDIFF(CURDATE(), bill_due_date) &gt; 365 
                 THEN pending_amount 
-                ELSE overdue_over_1_year  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_over_1_year  
             END,
             overdue_days = CASE 
                 WHEN pending_amount > 0 AND bill_due_date IS NOT NULL 
                      AND bill_due_date &lt;= CURDATE() 
                 THEN DATEDIFF(CURDATE(), bill_due_date) 
-                ELSE overdue_days  -- 淇濈暀鍘嗗彶鏁版嵁
+                ELSE overdue_days  
             END,
             update_time = NOW()
         WHERE pending_amount > 0
     </update>
 
+    <!-- 鏌ヨ搴旀敹璐﹀崟璐︽鍒嗘瀽鍒楄〃 -->
+    <select id="selectReceivableBillAccountAnalysisList" parameterType="com.ruoyi.cwgl.domain.ReceivableBillManagement" resultType="com.ruoyi.cwgl.domain.vo.ReceivableBillAccountAnalysisVo">
+        SELECT 
+            thisTab.id,
+            thisTab.system_no as systemNo,
+            thisTab.bill_name as billName,
+            thisTab.customer_name as customerName,
+            thisTab.payee,
+            thisTab.responsible_person as responsiblePerson,
+            thisTab.responsible_leader as responsibleLeader,
+            thisTab.settlement_method as settlementMethod,
+            thisTab.business_type as businessType,
+            thisTab.promotion_requirement as promotionRequirement,
+            thisTab.settlement_category as settlementCategory,
+            thisTab.settlement_period as settlementPeriod,
+            thisTab.total_amount as totalAmount,
+            thisTab.received_amount as receivedAmount,
+            thisTab.pending_amount as pendingAmount,
+            thisTab.nc_settlement_date as ncSettlementDate,
+            thisTab.nc_settlement_amount as ncSettlementAmount,
+            thisTab.account_remark as accountRemark,
+            thisTab.overdue_interest as overdueInterest,
+            thisTab.bill_due_date as billDueDate,
+            thisTab.overdue_days as overdueDays,
+            thisTab.status,
+            thisTab.create_time as createTime,
+            thisTab.update_time as updateTime,
+            
+            -- 寮�绁ㄧ浉鍏冲瓧娈�
+            rib_latest.invoice_time as latestInvoiceTime,
+            COALESCE(rib_total.total_invoice_amount, 0) as totalInvoiceAmount,
+            CASE 
+                WHEN thisTab.total_amount > COALESCE(rib_total.total_invoice_amount, 0) 
+                THEN thisTab.total_amount - COALESCE(rib_total.total_invoice_amount, 0)
+                ELSE 0 
+            END as unInvoicedAmount,
+            
+            -- 杩樻鐩稿叧瀛楁
+            rbsd_latest.latest_repayment_date as latestRepaymentDate,
+            COALESCE(rbsd_latest.latest_repayment_amount, 0) as latestRepaymentAmount
+
+        FROM receivable_bill_management thisTab
+
+        -- 鑾峰彇鏈�鏂板紑绁ㄦ棩鏈�
+        LEFT JOIN (
+            SELECT head_id, MAX(invoice_time) as invoice_time
+            FROM receivable_invoice_business
+            WHERE status = 1
+            GROUP BY head_id
+        ) rib_latest ON thisTab.id = rib_latest.head_id
+        
+        -- 鑾峰彇寮�绁ㄦ�婚噾棰�
+        LEFT JOIN (
+            SELECT head_id, SUM(invoicing_amount) as total_invoice_amount
+            FROM receivable_invoice_business
+            WHERE status = 1
+            GROUP BY head_id
+        ) rib_total ON thisTab.id = rib_total.head_id
+        
+        -- 鑾峰彇鏈�鏂拌繕娆句俊鎭�
+        LEFT JOIN (
+            SELECT 
+                r1.bill_id, 
+                r1.receipt_date as latest_repayment_date, 
+                r1.receipt_amount as latest_repayment_amount
+            FROM receivable_bill_settlement_detail r1
+            INNER JOIN (
+                SELECT bill_id, MAX(receipt_date) as max_receipt_date
+                FROM receivable_bill_settlement_detail
+                WHERE deleted = 0
+                GROUP BY bill_id
+            ) r2 ON r1.bill_id = r2.bill_id AND r1.receipt_date = r2.max_receipt_date
+            WHERE r1.deleted = 0
+            -- 濡傛灉鍚屼竴澶╂湁澶氭潯璁板綍锛屽彇閲戦鏈�澶х殑閭f潯
+            AND r1.receipt_amount = (
+                SELECT MAX(receipt_amount) 
+                FROM receivable_bill_settlement_detail r3 
+                WHERE r3.bill_id = r1.bill_id AND r3.receipt_date = r1.receipt_date AND r3.deleted = 0
+            )
+        ) rbsd_latest ON thisTab.id = rbsd_latest.bill_id
+        
+        <where>
+            <if test="systemNo != null  and systemNo != ''"> and thisTab.system_no = #{systemNo}</if>
+            <if test="customerName != null  and customerName != ''"> and  thisTab.customer_name like concat('%', #{customerName}, '%')</if>
+
+        </where>
+        ORDER BY thisTab.customer_name, thisTab.bill_due_date
+    </select>
+
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0