From 2c38fb7ab234af82a0575c828e7c2b696f74c441 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期二, 21 四月 2026 09:51:24 +0800
Subject: [PATCH] feat: 采购业务汇总统计接口

---
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml |  124 ++++++++++++++++++++++++++++++-----------
 1 files changed, 91 insertions(+), 33 deletions(-)

diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index d8655c9..46db172 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -7,31 +7,33 @@
     <select id="selectSalesLedgerProductList" resultType="com.ruoyi.sales.pojo.SalesLedgerProduct">
         SELECT
         T1.*,
+        pl.stock_status AS purchase_stock_status,
         CASE
-        WHEN t2.qualitity-t2.locked_quantity >= T1.quantity THEN 1
+        WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >= IFNULL(T1.quantity, 0) THEN 1
         ELSE 0
         END as has_sufficient_stock
         FROM
         sales_ledger_product T1
         LEFT JOIN stock_inventory t2 ON T1.product_model_id = t2.product_model_id
+        LEFT JOIN purchase_ledger pl ON T1.sales_ledger_id = pl.id AND T1.type = 2
         <where>
-            1=1
-            <if test="salesLedgerProduct.salesLedgerId != null and salesLedgerProduct.salesLedgerId != '' ">
+            <if test="salesLedgerProduct.salesLedgerId != null">
                 AND T1.sales_ledger_id = #{salesLedgerProduct.salesLedgerId}
             </if>
-            <if test="salesLedgerProduct.type != null and salesLedgerProduct.type != '' ">
+            <if test="salesLedgerProduct.type != null">
                 AND T1.type = #{salesLedgerProduct.type}
             </if>
         </where>
         ORDER BY T1.register_date DESC
     </select>
+
     <select id="selectSalesLedgerProductByMainId" resultType="com.ruoyi.sales.pojo.SalesLedgerProduct">
         select slp.*
         from quality_inspect qi
                  left join production_product_main ppm on qi.product_main_id = ppm.id
                  left join product_work_order pwo on ppm.work_order_id = pwo.id
                  left join product_order po on pwo.product_order_id = po.id
-                 left join sales_ledger_product slp on po.sale_ledger_product_id = slp.id
+                 left join sales_ledger_product slp on po.sale_ledger_product_id = slp.id and slp.type = 1
         where qi.product_main_id = #{productMainId}
 
 
@@ -102,36 +104,88 @@
         </where>
         order by slp.register_date desc
     </select>
+
     <select id="procurementBusinessSummaryListPage"
             resultType="com.ruoyi.purchase.dto.ProcurementBusinessSummaryDto">
         SELECT
-        slp.product_category AS productCategory,
-        slp.specification_model AS specificationModel,
-        sl.supplier_name AS supplierName,
-        SUM(slp.quantity) AS purchaseNum,
-        SUM(slp.tax_inclusive_total_price) AS purchaseAmount,
-        COUNT(DISTINCT slp.sales_ledger_id) AS purchaseTimes,
-        <!-- 骞冲潎鍗曚环 = 鎬婚噰璐噾棰�/鎬婚噰璐暟閲忥紝淇濈暀2浣嶅皬鏁帮紝閬垮厤闄�0 -->
-        ROUND(IF(SUM(slp.quantity) = 0, 0, SUM(slp.tax_inclusive_total_price) / SUM(slp.quantity)), 2) AS averagePrice,
-        <!-- 璇ヤ骇鍝佸ぇ绫讳笅鏈�鍚庝竴涓綍鍏ユ棩鏈燂紙鍙栧彴璐︿富琛ㄧ殑entry_date锛� -->
-        MAX(sl.entry_date) AS entryDate
+            slp.product_category AS productCategory,
+            slp.specification_model AS specificationModel,
+            sl.supplier_name AS supplierName,
+            SUM(slp.quantity) AS purchaseNum,
+            SUM(slp.tax_inclusive_total_price) AS purchaseAmount,
+            COUNT(DISTINCT slp.sales_ledger_id) AS purchaseTimes,
+            <!-- 骞冲潎鍗曚环 = 鎬婚噰璐噾棰�/鎬婚噰璐暟閲忥紝淇濈暀2浣嶅皬鏁帮紝閬垮厤闄�0 -->
+            ROUND(IF(SUM(slp.quantity) = 0, 0, SUM(slp.tax_inclusive_total_price) / SUM(slp.quantity)), 2) AS averagePrice,
+            <!-- 璇ュ垎缁勪笅鏈�鍚庝竴涓綍鍏ユ棩鏈燂紙鍙栧彴璐︿富琛ㄧ殑entry_date锛� -->
+            MAX(sl.entry_date) AS entryDate,
+            COALESCE(SUM(rop.return_quantity), 0) AS return_quantity,
+            COALESCE(SUM(rop.allocated_return_amount), 0) AS return_amount,
+            pm.thickness
         FROM sales_ledger_product slp
-        <!-- 鍏宠仈鍙拌处涓昏〃锛氳幏鍙栧綍鍏ユ棩鏈焑ntry_date -->
-        LEFT JOIN purchase_ledger sl ON slp.sales_ledger_id = sl.id
+            <!-- 鍏宠仈鍙拌处涓昏〃锛氳幏鍙栧綍鍏ユ棩鏈焑ntry_date -->
+            LEFT JOIN purchase_ledger sl ON slp.sales_ledger_id = sl.id
+            LEFT JOIN (
+                SELECT
+                    t1.sales_ledger_product_id,
+                    t1.return_quantity,
+                    IF(q.sum_qty = 0 OR q.sum_qty IS NULL, 0, (t2.total_amount * (t1.return_quantity / q.sum_qty))) AS allocated_return_amount
+                FROM purchase_return_order_products t1
+                    INNER JOIN purchase_return_orders t2 ON t2.id = t1.purchase_return_order_id
+                    INNER JOIN (
+                        SELECT purchase_return_order_id, SUM(return_quantity) AS sum_qty
+                        FROM purchase_return_order_products
+                        GROUP BY purchase_return_order_id
+                    ) q ON q.purchase_return_order_id = t1.purchase_return_order_id
+            ) rop ON rop.sales_ledger_product_id = slp.id
+            LEFT JOIN product_model pm ON pm.id = slp.product_model_id
         WHERE slp.type = 2 <!-- 鍥哄畾绛涢�夛細閲囪喘鍙拌处锛坱ype=2锛� -->
-        <!-- 閲囪喘鏃ユ湡绛涢�夛細鍙�夋潯浠� -->
-        <if test="req.entryDateStart != null and req.entryDateEnd != null">
-            AND sl.entry_date BETWEEN #{req.entryDateStart} AND #{req.entryDateEnd} <!-- 鏃堕棿鑼冨洿锛氶潪绌烘湁鏁� -->
-        </if>
-        <!-- 浜у搧澶х被绛涢�夛細鍙�夋潯浠� -->
-        <if test="req.productCategory != null and req.productCategory != ''">
-            AND slp.product_category = #{req.productCategory}
-        </if>
-        <!-- 鎸変骇鍝佸ぇ绫诲垎缁勮仛鍚� -->
-        GROUP BY slp.product_category
-        <!-- 鎸変骇鍝佸ぇ绫绘帓搴� -->
-        ORDER BY slp.product_category
+            <!-- 閲囪喘鏃ユ湡绛涢�夛細鍙�夋潯浠� -->
+            <if test="req.entryDateStart != null and req.entryDateEnd != null">
+                AND sl.entry_date BETWEEN #{req.entryDateStart} AND #{req.entryDateEnd} <!-- 鏃堕棿鑼冨洿锛氶潪绌烘湁鏁� -->
+            </if>
+            <!-- 浜у搧澶х被绛涢�夛細鍙�夋潯浠� -->
+            <if test="req.productCategory != null and req.productCategory != ''">
+                AND slp.product_category = #{req.productCategory}
+            </if>
+        <!-- 鎸夊垎缁勭淮搴﹁仛鍚堬紙閬垮厤 ONLY_FULL_GROUP_BY 閿欒涓庝笉纭畾鍊硷級 -->
+        GROUP BY
+            slp.product_category,
+            slp.specification_model,
+            sl.supplier_name,
+            pm.thickness
+        ORDER BY
+            slp.product_category
     </select>
+
+    <select id="procurementBusinessSummaryStatistics"
+            resultType="com.ruoyi.purchase.dto.ProcurementBusinessSummaryStatisticsDto">
+        SELECT
+            COALESCE(SUM(slp.tax_inclusive_total_price), 0) AS purchaseTotalAmount,
+            COALESCE(COUNT(DISTINCT slp.product_category), 0) AS productCategoryCount,
+            COALESCE(SUM(rop.allocated_return_amount), 0) AS returnTotalAmount
+        FROM sales_ledger_product slp
+            LEFT JOIN purchase_ledger sl ON slp.sales_ledger_id = sl.id
+            LEFT JOIN (
+                SELECT
+                    t1.sales_ledger_product_id,
+                    IF(q.sum_qty = 0 OR q.sum_qty IS NULL, 0, (t2.total_amount * (t1.return_quantity / q.sum_qty))) AS allocated_return_amount
+                FROM purchase_return_order_products t1
+                    INNER JOIN purchase_return_orders t2 ON t2.id = t1.purchase_return_order_id
+                    INNER JOIN (
+                        SELECT purchase_return_order_id, SUM(return_quantity) AS sum_qty
+                        FROM purchase_return_order_products
+                        GROUP BY purchase_return_order_id
+                    ) q ON q.purchase_return_order_id = t1.purchase_return_order_id
+            ) rop ON rop.sales_ledger_product_id = slp.id
+        WHERE slp.type = 2
+            <if test="req.entryDateStart != null and req.entryDateEnd != null">
+                AND sl.entry_date BETWEEN #{req.entryDateStart} AND #{req.entryDateEnd}
+            </if>
+            <if test="req.productCategory != null and req.productCategory != ''">
+                AND slp.product_category = #{req.productCategory}
+            </if>
+    </select>
+
     <select id="selectProductBomStructure" resultType="com.ruoyi.sales.dto.LossProductModelDto">
         select
             a.model,
@@ -144,7 +198,7 @@
                      p.product_name,
                      ps.unit_quantity * slp.quantity AS single_quantity
               FROM sales_ledger sl
-                       LEFT JOIN sales_ledger_product slp ON slp.sales_ledger_id = sl.id
+                       LEFT JOIN sales_ledger_product slp ON slp.sales_ledger_id = sl.id and slp.type = 1
                        LEFT JOIN product_model pm ON pm.id = slp.product_model_id
                        LEFT JOIN product_bom pb ON pb.product_model_id = pm.id
                        LEFT JOIN product_structure ps ON pb.id = ps.bom_id
@@ -196,11 +250,15 @@
     </select>
 
     <select id="selectProductCountByTypeAndDate" resultType="int">
-        SELECT COUNT(*)
+        SELECT IFNULL(COUNT(*), 0)
         FROM sales_ledger_product
         WHERE type = #{type}
-        AND register_date &gt;= #{startDate}
-        AND register_date &lt;= #{endDate}
+        <if test="startDate != null">
+            AND register_date &gt;= #{startDate}
+        </if>
+        <if test="endDate != null">
+            AND register_date &lt;= #{endDate}
+        </if>
     </select>
 
     <select id="selectRawMaterialExpense" resultType="java.math.BigDecimal">

--
Gitblit v1.9.3