From 7dfff767d0fae788ba2bf67a707ce41836bd0f42 Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期二, 13 十月 2026 14:03:35 +0800
Subject: [PATCH] fix: 优化客户往来查询逻辑,调整应收金额计算方式以避免尾差问题
---
src/main/resources/mapper/basic/CustomerMapper.xml | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 252 insertions(+), 0 deletions(-)
diff --git a/src/main/resources/mapper/basic/CustomerMapper.xml b/src/main/resources/mapper/basic/CustomerMapper.xml
new file mode 100644
index 0000000..404fdd9
--- /dev/null
+++ b/src/main/resources/mapper/basic/CustomerMapper.xml
@@ -0,0 +1,252 @@
+<?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="com.ruoyi.basic.mapper.CustomerMapper">
+
+ <!-- 閫氱敤鏌ヨ鏄犲皠缁撴灉 -->
+ <resultMap id="BaseResultMap" type="com.ruoyi.basic.pojo.Customer">
+ <id column="id" property="id" />
+ </resultMap>
+ <select id="listPage" resultType="com.ruoyi.basic.vo.CustomerVo">
+ select
+ c.*,
+ u.nick_name usage_user_name,
+ (
+ select group_concat(u2.user_name separator ', ')
+ from customer_user cu
+ left join sys_user u2 on cu.user_id = u2.user_id
+ where cu.customer_id = c.id
+ and cu.user_id != c.usage_user
+ ) as together_user_names,
+ (
+ select group_concat(cu.user_id separator ',')
+ from customer_user cu
+ where cu.customer_id = c.id
+ and cu.user_id != c.usage_user
+ ) as user_ids_str
+ from customer c
+ left join sys_user u on c.usage_user = u.user_id
+ <where>
+ <if test="c.customerName != null and c.customerName != ''">
+ and customer_name like concat('%', #{c.customerName}, '%')
+ </if>
+ <if test="c.customerType != null and c.customerType != ''">
+ and customer_type = #{c.customerType}
+ </if>
+ <!-- 鍏捣鏌ヨ锛歵ype = 1锛堝叕娴峰鎴凤級-->
+ <if test="c.type != null and c.type == 1">
+ and type = #{c.type}
+ </if>
+ <!-- 绉佹捣鏌ヨ锛歵ype = 0锛堢娴峰鎴凤級鎴栬�� type = 1锛堝叕娴峰鎴凤級涓斿凡琚垎閰嶏紝骞朵笖鏄嚜宸遍鐢ㄣ�佽嚜宸卞垱寤烘垨鑰呭叡浜粰鑷繁鐨勫鎴� -->
+ <if test="c.type != null and c.type == 0">
+ and (
+ (type = #{c.type} or (type = 1 and is_assigned = 1))
+ and (
+ c.usage_user = #{loginUserId}
+ or c.create_user = #{loginUserId}
+ or exists (
+ select 1 from customer_user cu
+ where cu.customer_id = c.id
+ and cu.user_id = #{loginUserId}
+ )
+ )
+ )
+ </if>
+ </where>
+ order by c.id desc
+ </select>
+
+ <select id="list" resultType="com.ruoyi.basic.vo.CustomerVo">
+ select
+ c.*,
+ u.user_name usage_user_name,
+ (
+ select group_concat(u2.user_name separator ', ')
+ from customer_user cu
+ left join sys_user u2 on cu.user_id = u2.user_id
+ where cu.customer_id = c.id
+ and cu.user_id != c.usage_user
+ ) as together_user_names,
+ (
+ select group_concat(cu.user_id separator ',')
+ from customer_user cu
+ where cu.customer_id = c.id
+ and cu.user_id != c.usage_user
+ ) as user_ids_str
+ from customer c
+ left join sys_user u on c.usage_user = u.user_id
+ <where>
+ <if test="c.ids != null and c.ids.length > 0">
+ and c.id in
+ <foreach collection="c.ids" item="id" open="(" separator="," close=")">
+ #{id}
+ </foreach>
+ </if>
+ <if test="c.customerName != null and c.customerName != ''">
+ and customer_name like concat('%', #{c.customerName}, '%')
+ </if>
+ <if test="c.customerType != null and c.customerType != ''">
+ and customer_type = #{c.customerType}
+ </if>
+ <!-- 鍏捣鏌ヨ锛歵ype = 1锛堝叕娴峰鎴凤級-->
+ <if test="c.type != null and c.type == 1">
+ and type = #{c.type}
+ </if>
+ <!-- 绉佹捣鏌ヨ锛歵ype = 0锛堢娴峰鎴凤級鎴栬�� type = 1锛堝叕娴峰鎴凤級涓斿凡琚垎閰嶏紝骞朵笖鏄嚜宸遍鐢ㄣ�佽嚜宸卞垱寤烘垨鑰呭叡浜粰鑷繁鐨勫鎴� -->
+ <if test="c.type != null and c.type == 0">
+ and (
+ (type = #{c.type} or (type = 1 and is_assigned = 1))
+ and (
+ c.usage_user = #{loginUserId}
+ or c.create_user = #{loginUserId}
+ or exists (
+ select 1 from customer_user cu
+ where cu.customer_id = c.id
+ and cu.user_id = #{loginUserId}
+ )
+ )
+ )
+ </if>
+ </where>
+ order by c.id desc
+ </select>
+ <!--
+ 瀹㈡埛寰�鏉ワ紙鍥炴鍙拌处涓昏〃锛夛細
+ 搴旀敹閲戦 = 鍚勯攢鍞彴璐︾殑搴旀敹閲戦涔嬪拰锛�
+ 鍗曟潯鍙拌处搴旀敹閲戦 = 璇ュ彴璐﹀凡鍑哄簱閲戦 鈭� 宸查��璐ф 鈭� 宸插洖娆鹃噾棰濓紙涓嶅皬浜� 0锛夈��
+ 鍗炽�屽洖娆鹃噾棰濄�嶆槸绱鍥炴锛屻�屽簲鏀堕噾棰濄�嶆槸杩樻病鏀跺洖鏉ョ殑閮ㄥ垎銆�
+ 娉ㄦ剰 鍑哄簱閲戦 = sum(鍑哄簱鏁伴噺 * 6浣嶅皬鏁扮殑鍚◣鍗曚环)锛屼細甯︽湁銆屽垎銆嶄互涓嬬殑闆跺ご锛�
+ 鑰屽洖娆炬槸鎸夊垎褰曞叆鐨勬暣鏁伴噾棰濓紝涓よ�呯浉鍑忎細鍑虹幇 0.0045銆�0.004 杩欑被鏃犳剰涔夌殑灏惧樊锛�
+ 鍥犳鍗曟潯鍙拌处鐨勫簲鏀堕噾棰濇寜鍒嗚绠楋紝鍒嗕互涓嬬殑灏惧樊涓嶈鍏ャ��
+ 娉ㄦ剰 蹇呴』鍏堟寜鍙拌处绠楀ソ搴旀敹銆佸啀鎸夊鎴锋眹鎬伙紙涓� customewTransactionsDetails 鍙e緞涓�鑷达級銆�
+ 鑻ュ厛鎸夊鎴锋眹鎬诲啀鍙栨暣锛屽悇瀹跺彴璐︾殑灏惧樊浼氱疮鍔狅紝鍑虹幇銆屼富琛� 0.01銆佹槑缁嗗叏鏄� 0銆嶇殑瀵逛笉涓婇棶棰樸��
+ 娉ㄦ剰 鍑哄簱閲戦鎸夊彴璐� (sales_ledger.id) 鑱氬悎锛屽悓涓�鍙戣揣鍗曞鎵瑰彿鍑哄簱鏃朵笉浼氶噸澶嶈鏁般��
+ -->
+ <select id="customewTransactions" resultType="com.ruoyi.sales.vo.CustomerTransactionsVo">
+ select T1.customer_id,
+ c.customer_name,
+ T1.contractAmounts,
+ IFNULL(T2.receiptPaymentAmount, 0) AS receiptPaymentAmount,
+ IFNULL(T5.receiptableAmount, 0) AS receiptableAmount
+ from (select customer_id, sum(contract_amount) as contractAmounts from sales_ledger group by customer_id) T1
+ left join (select customer_id, sum(collection_amount) as receiptPaymentAmount from account_sales_collection group by customer_id) T2 on T1.customer_id = T2.customer_id
+ left join (
+ select
+ sl.customer_id,
+ sum(GREATEST(
+ TRUNCATE(IFNULL(T3.outboundAmount, 0) - IFNULL(T4.returnAmount, 0)
+ - IFNULL(T6.receiptPaymentAmount, 0), 2),
+ 0)) AS receiptableAmount
+ from sales_ledger sl
+ left join (
+ select
+ sl.id,
+ sum(t.collection_amount) as receiptPaymentAmount
+ from (
+ select
+ ascc.collection_amount,
+ s.sales_ledger_id
+ from account_sales_collection ascc
+ left join stock_out_record sor on FIND_IN_SET(sor.id, ascc.stock_out_record_ids) > 0
+ left join shipping_info s on sor.record_id = s.id
+ where sor.record_type='13'
+ and sor.approval_status=1
+ group by ascc.id, s.sales_ledger_id
+ ) t
+ left join sales_ledger sl on t.sales_ledger_id = sl.id
+ group by sl.id
+ ) T6 on T6.id = sl.id
+ left join (
+ SELECT
+ sl.id,
+ sum(sor.stock_out_num * slp.tax_inclusive_unit_price) as outboundAmount
+ FROM stock_out_record sor
+ left join shipping_info s on sor.record_id = s.id
+ LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
+ LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id
+ WHERE sor.record_type='13'
+ and sor.approval_status=1
+ and slp.type = 1
+ group by sl.id
+ ) T3 on T3.id = sl.id
+ left join (
+ select
+ sl.id,
+ sum(rm.refund_amount) as returnAmount
+ from return_management rm
+ left join shipping_info si on rm.shipping_id = si.id
+ left join sales_ledger sl on si.sales_ledger_id = sl.id
+ where rm.status=1
+ group by sl.id
+ ) T4 on T4.id = sl.id
+ group by sl.customer_id
+ ) T5 on T5.customer_id = T1.customer_id
+ left join customer c on T1.customer_id = c.id
+ <where>
+ <if test="customerName!=null and customerName!=''">
+ AND c.customer_name LIKE CONCAT('%', #{customerName}, '%')
+ </if>
+ </where>
+ order by T1.customer_id desc
+ </select>
+ <!--
+ 瀹㈡埛寰�鏉ユ槑缁嗭紙鎸夐攢鍞彴璐︼級锛氬簲鏀堕噾棰� = 璇ュ彴璐﹀凡鍑哄簱 鈭� 宸查��璐� 鈭� 宸插洖娆俱��
+ 鍗曟潯鍥炴鍙粺璁′竴娆★細鍚屼竴鍥炴鍙兘鍚屾椂鍛戒腑璇ュ彴璐︾殑澶氭潯鍑哄簱璁板綍锛�
+ 鍏堢敤 GROUP BY ascc.id, sl.id 鍘婚噸锛屽啀鎸夊彴璐︽眰鍜屻��
+ 閲戦鍚屾牱鎸夊垎锛�2 浣嶅皬鏁帮級璁$畻锛屽垎浠ヤ笅鐨勫熬宸笉璁″叆銆�
+ -->
+ <select id="customewTransactionsDetails"
+ resultType="com.ruoyi.sales.vo.CustomerTransactionsDetailsVo">
+ select sl.id salesLedgerId,
+ sl.sales_contract_no,
+ sl.execution_date,
+ sl.contract_amount,
+ IFNULL(T1.receiptPaymentAmount, 0) AS receiptPaymentAmount,
+ GREATEST(
+ TRUNCATE(IFNULL(T2.outboundAmount, 0) - IFNULL(T3.returnAmount, 0)
+ - IFNULL(T1.receiptPaymentAmount, 0), 2),
+ 0) AS receiptableAmount
+ from sales_ledger sl
+ left join (
+ select
+ sl.id,
+ sum(t.collection_amount) as receiptPaymentAmount
+ from (
+ select
+ ascc.collection_amount,
+ s.sales_ledger_id
+ from account_sales_collection ascc
+ left join stock_out_record sor on FIND_IN_SET(sor.id, ascc.stock_out_record_ids) > 0
+ left join shipping_info s on sor.record_id = s.id
+ where sor.record_type='13'
+ and sor.approval_status=1
+ group by ascc.id, s.sales_ledger_id
+ ) t
+ left join sales_ledger sl on t.sales_ledger_id = sl.id
+ group by sl.id
+ )T1 on T1.id = sl.id
+ left join (
+ SELECT
+ sl.id,
+ sum(sor.stock_out_num * slp.tax_inclusive_unit_price) as outboundAmount
+ FROM stock_out_record sor
+ left join shipping_info s on sor.record_id = s.id
+ LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
+ LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id
+ WHERE sor.record_type='13'
+ and sor.approval_status=1
+ and slp.type = 1
+ group by sl.id
+ )T2 on T2.id = sl.id
+ left join (
+ select sl.id,
+ sum(rm.refund_amount) as returnAmount
+ from return_management rm
+ left join shipping_info si on rm.shipping_id = si.id
+ left join sales_ledger sl on si.sales_ledger_id = sl.id
+ where rm.status=1
+ group by sl.id
+ )T3 on T3.id = sl.id
+ where sl.customer_id = #{customerId}
+ order by sl.id desc
+ </select>
+</mapper>
--
Gitblit v1.9.3