From b5e1bceabb36fe1d1e5e88397280d12e7044c6dd Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期五, 06 十一月 2026 15:32:31 +0800
Subject: [PATCH] 完善设备采集、安全管理及采购业务

---
 yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml b/yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml
index a88fb3a..b4bf868 100644
--- a/yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml
+++ b/yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml
@@ -26,4 +26,23 @@
                 AND deleted = 0)
     </select>
 
-</mapper>
\ No newline at end of file
+    <select id="selectPriceAnalysis" resultType="cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.purchase.ErpPurchasePriceRespVO">
+        SELECT ranked.product_id, MAX(m.name) AS product_name, MAX(m.code) AS product_bar_code,
+               SUM(ranked.quantity) AS total_quantity,
+               ROUND(SUM(ranked.product_price * ranked.quantity) / NULLIF(SUM(ranked.quantity), 0), 4) AS avg_price,
+               MIN(ranked.product_price) AS min_price, MAX(ranked.product_price) AS max_price,
+               MAX(CASE WHEN ranked.row_num = 1 THEN ranked.product_price END) AS latest_price,
+               MAX(CASE WHEN ranked.row_num = 2 THEN ranked.product_price END) AS previous_price
+        FROM (
+            SELECT i.product_id, i.product_price, i.count AS quantity,
+                   ROW_NUMBER() OVER (PARTITION BY i.product_id ORDER BY o.order_time DESC, o.id DESC, i.id DESC) AS row_num
+            FROM erp_purchase_order_items i JOIN erp_purchase_order o ON o.id = i.order_id
+            WHERE i.deleted = 0 AND o.deleted = 0 AND o.status = 20
+              AND i.product_id IS NOT NULL AND i.product_price IS NOT NULL AND i.count > 0
+              <if test="productId != null">AND i.product_id = #{productId}</if>
+              <if test="beginTime != null">AND o.order_time &gt;= #{beginTime}</if>
+              <if test="endTime != null">AND o.order_time &lt;= #{endTime}</if>
+        ) ranked LEFT JOIN mdm_item m ON m.id = ranked.product_id AND m.deleted = 0
+        GROUP BY ranked.product_id ORDER BY ranked.product_id
+    </select>
+</mapper>

--
Gitblit v1.9.3