From e731bf5f64d71ed93a570d505c52dc44d491175e Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 22 九月 2026 11:21:33 +0800
Subject: [PATCH] fix(home): 修复首页采购品分布和出入库趋势数据统计问题

---
 src/main/resources/mapper/stock/StockOutRecordMapper.xml |  188 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 187 insertions(+), 1 deletions(-)

diff --git a/src/main/resources/mapper/stock/StockOutRecordMapper.xml b/src/main/resources/mapper/stock/StockOutRecordMapper.xml
index 182665c..adcb16f 100644
--- a/src/main/resources/mapper/stock/StockOutRecordMapper.xml
+++ b/src/main/resources/mapper/stock/StockOutRecordMapper.xml
@@ -15,7 +15,193 @@
         <result column="update_time" property="updateTime" />
         <result column="create_user" property="createUser" />
         <result column="update_user" property="updateUser" />
-        <result column="tenant_id" property="tenantId" />
     </resultMap>
 
+    <select id="listPage" resultType="com.ruoyi.stock.dto.StockOutRecordDto">
+        WITH RECURSIVE product_tree AS (
+        SELECT id
+        FROM product
+        WHERE id = #{params.topParentProductId}
+
+        UNION ALL
+
+        SELECT p.id
+        FROM product p
+        INNER JOIN product_tree pt ON p.parent_id = pt.id
+        )
+        SELECT
+        sor.*,
+        p.product_name as productName,
+        pm.model,
+        pm.unit,
+        u.nick_name as createBy,
+        <!-- 娌瑰搧鍑哄簱鍙粦澶氫釜閿�鍞彴璐︼紙澶氬鎴凤級锛宻or.sales_ledger_id 鍙瓨棣栭」锛屾晠浠庣粦瀹氳〃姹囨�诲叏閮ㄥ悎鍚屽彿锛�
+             鏈粦瀹氱粦瀹氳〃鐨勫瓨閲忚浠嶉��鍥� sor.sales_ledger_id 鍏宠仈鐨勫崟涓悎鍚屽彿 -->
+        IFNULL(
+            (select group_concat(distinct slb.sales_contract_no separator ',')
+             from stock_out_record_sales_ledger b
+                      join sales_ledger slb on slb.id = b.sales_ledger_id
+             where b.stock_out_record_id = sor.id),
+            sl.sales_contract_no) as salesContractNo
+        FROM stock_out_record as sor
+        LEFT JOIN product_model as pm on sor.product_model_id = pm.id
+        LEFT JOIN product as p on pm.product_id = p.id
+        LEFT JOIN sys_user as u on sor.create_user = u.user_id
+        LEFT JOIN sales_ledger as sl on sl.id = sor.sales_ledger_id
+        <where>
+            <if test="params.timeStr != null and params.timeStr != ''">
+                and sor.create_time like concat('%',#{params.timeStr},'%')
+            </if>
+            <if test="params.productName != null and params.productName != ''">
+                and p.product_name like concat('%',#{params.productName},'%')
+            </if>
+            <if test="params.model != null and params.model != ''">
+                and pm.model like concat('%',#{params.model},'%')
+            </if>
+            <if test="params.batchNo != null and params.batchNo != ''">
+                and sor.batch_no like concat('%',#{params.batchNo},'%')
+            </if>
+            <if test="params.productModelId != null and params.productModelId > 0">
+                and sor.product_model_id = #{params.productModelId}
+            </if>
+            <if test="params.type != null and params.type != ''">
+                and sor.type = #{params.type}
+            </if>
+            <if test="params.recordType != null and params.recordType != ''">
+                and sor.record_type = #{params.recordType}
+            </if>
+            <if test="params.topParentProductId != null and params.topParentProductId > 0">
+                and p.id in (select id from product_tree)
+            </if>
+        </where>
+        order by sor.id desc
+    </select>
+    <!-- 鎸夋潵婧愭槑缁嗘煡鍑哄簱璁板綍锛氬姞 /*data_scope*/ 璺宠繃鏁版嵁鏉冮檺锛屽惁鍒欐崲璐﹀彿浼氭煡涓嶅埌璁板綍锛�
+         瀵艰嚧濮斿鐨勩�屽畬鎴愭牎楠屻�嶈闈欓粯缁曡繃銆併�屼綔搴熼噴鏀惧崰鐢ㄣ�嶅け鏁� -->
+    <select id="selectByRecordIds" resultType="com.ruoyi.stock.pojo.StockOutRecord">
+        select /*data_scope*/ *
+        from stock_out_record
+        where record_type = #{recordType}
+        and record_id in
+        <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
+            #{recordId}
+        </foreach>
+    </select>
+
+    <!-- 娌瑰搧鍑哄簱鍙粦瀹氱殑閿�鍞彴璐︿笅鎷夈��
+         sales_ledger 涓嶅湪鏁版嵁鏉冮檺鐧藉悕鍗曢噷锛屾寜 create_user/dept_id 杩囨护浼氳娌瑰簱/鍙戣揣瑙掕壊閫変笉鍒伴攢鍞缓鐨勫彴璐︼紝
+         鎵�浠ュ姞 /*data_scope*/ 璺宠繃鏁版嵁鏉冮檺锛涘彧璇绘煡璇紝涓嶆寜 ledger_type 杩囨护锛堜唬鍌ㄥ彴璐︿篃瑕佽兘缁戯級 -->
+    <select id="bindableSalesLedgerPage" resultType="com.ruoyi.sales.pojo.SalesLedger">
+        select /*data_scope*/ id,
+        sales_contract_no,
+        customer_contract_no,
+        project_name,
+        entry_date,
+        salesman,
+        customer_id,
+        customer_name,
+        ledger_type,
+        oil_depot_id,
+        oil_depot_name,
+        tank_id,
+        tank_no,
+        approval_status,
+        contract_amount
+        from sales_ledger
+        where 1=1
+        <if test="req.salesContractNo != null and req.salesContractNo != ''">
+            and sales_contract_no like concat('%',#{req.salesContractNo},'%')
+        </if>
+        <if test="req.customerName != null and req.customerName != ''">
+            and customer_name like concat('%',#{req.customerName},'%')
+        </if>
+        <if test="req.projectName != null and req.projectName != ''">
+            and project_name like concat('%',#{req.projectName},'%')
+        </if>
+        order by id desc
+    </select>
+
+    <select id="listStockOutRecordExportData" resultType="com.ruoyi.stock.execl.StockOutRecordExportData">
+        SELECT
+        sor.*,
+        p.product_name as productName,
+        pm.model,
+        pm.unit,
+        u.nick_name as createBy
+        FROM stock_out_record as sor
+        LEFT JOIN product_model as pm on sor.product_model_id = pm.id
+        LEFT JOIN product as p on pm.product_id = p.id
+        LEFT JOIN sys_user as u on sor.create_user = u.user_id
+        <where>
+            <if test="params.timeStr != null and params.timeStr != ''">
+                and sor.create_time like concat('%',#{params.timeStr},'%')
+            </if>
+            <if test="params.productName != null and params.productName != ''">
+                and p.product_name like concat('%',#{params.productName},'%')
+            </if>
+            <if test="params.type != null and params.type != ''">
+                and sor.type = #{params.type}
+            </if>
+            <if test="params.recordType != null and params.recordType != ''">
+                and sor.record_type = #{params.recordType}
+            </if>
+        </where>
+        order by sor.id desc
+    </select>
+
+    <!-- 璐㈠姟绠$悊 路 閿�鍞嚭搴撳彴璐︼紙鏀舵/寮�绁ㄥ�欓�夛級銆�
+         涓ょ被鏉ユ簮锛氣憼 record_type='13' 鐨勬墜宸ュ彂璐у崟鍑哄簱锛屽鎴�/鏄庣粏/鍙戣揣缂栧彿閮借蛋 shipping_info锛�
+         鈶� 娌瑰搧鍑哄簱锛坱ype 涓虹┖锛夋寜銆屽嚭搴撳崟脳瀹㈡埛銆嶆媶鍒扮粦瀹氳锛屼竴涓嚭搴撳崟缁戝嚑涓彴璐﹀氨鍑哄嚑琛岋紝
+         閲戦鍙畻璇ュ鎴峰垎鎽婄殑閭i儴鍒嗭紙b.quantity 脳 鍗曚环锛夛紝涓嶅啀鏄暣鍗曟暟閲忋��
+         鐢� LEFT JOIN 鑰屼笉鏄� UNION ALL锛欴ataScopeSqlInterceptor 鐪嬪埌 from 鍚庨潰绱ц窡 '(' 浼氭暣鏉℃斁琛岋紝
+         鎶婁袱鏉″垎鏀寘鎴愭淳鐢熻〃浼氳鍑哄簱鍗曠殑鏁版嵁鏉冮檺闈欓粯澶辨晥銆� -->
+    <select id="listPageAccountSales" resultType="com.ruoyi.account.bean.vo.sales.SalesOutboundVo">
+    SELECT
+        sor.id,
+        sor.outbound_batches,
+        IFNULL(b.customer_name, sl.customer_name) AS customerName,
+        sor.create_time as shippingDate,
+        IFNULL(pb.product_name, p.product_name) AS productName,
+        IFNULL(pmb.model, pm.model) as specification_model,
+        IFNULL(slpb.tax_rate, slp.tax_rate) as tax_rate,
+        CASE
+            WHEN b.id IS NOT NULL THEN b.quantity * slpb.tax_inclusive_unit_price
+            ELSE sor.stock_out_num * slp.tax_inclusive_unit_price
+        END as outboundAmount,
+        s.shipping_no,
+        IFNULL(slb.sales_contract_no, sl.sales_contract_no) as sales_contract_no,
+        b.id AS bindingId,
+        IFNULL(b.customer_id, sl.customer_id) AS customer_id
+        FROM stock_out_record sor
+        <!-- 鍙湁娌瑰搧鍑哄簱鎵嶅幓缁戠粦瀹氳〃锛涙墜宸ュ彂璐у崟鍑哄簱鐨� type 涔熸槸绌猴紝浣嗗畠 record_type='13' 涓旀病鏈夌粦瀹氳锛�
+             b.id 涓� NULL 鏃朵笅闈㈢粺涓�鍥炶惤鍒� shipping_info 閭d竴濂� -->
+        LEFT JOIN stock_out_record_sales_ledger b ON b.stock_out_record_id = sor.id
+        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 and slp.type = 1
+        LEFT JOIN sales_ledger slb ON slb.id = b.sales_ledger_id
+        LEFT JOIN sales_ledger_product slpb ON slpb.id = b.sales_ledger_product_id and slpb.type = 1
+        left join product_model pm on slp.product_model_id = pm.id
+        left join product p on pm.product_id = p.id
+        left join product_model pmb on sor.product_model_id = pmb.id
+        left join product pb on pmb.product_id = pb.id
+        WHERE sor.approval_status=1
+        AND (
+            sor.record_type='13'
+            OR (b.id IS NOT NULL AND (sor.type IS NULL OR sor.type = ''))
+        )
+        <if test="req.outboundBatches != null and req.outboundBatches != ''">
+            AND sor.outbound_batches LIKE CONCAT('%',#{req.outboundBatches},'%')
+        </if>
+        <if test="req.customerName != null and req.customerName != ''">
+            AND IFNULL(b.customer_name, sl.customer_name) LIKE CONCAT('%',#{req.customerName},'%')
+        </if>
+        <if test="req.customerId != null ">
+            AND IFNULL(b.customer_id, sl.customer_id) = #{req.customerId}
+        </if>
+        <if test="req.startDate != null and req.endDate != null">
+            AND IFNULL(s.shipping_date, sor.create_time) BETWEEN #{req.startDate} AND #{req.endDate}
+        </if>
+        order by sor.id DESC, IFNULL(b.id, 0)
+    </select>
 </mapper>

--
Gitblit v1.9.3