| | |
| | | </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, |
| | | COALESCE(NULLIF(SUM(t1.return_quantity), 0), 0) AS return_quantity, |
| | | COALESCE(SUM(t2.total_amount), 0) AS return_amount, |
| | | pm.thickness |
| | | 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 |
| | | <!-- 关联台账主表:获取录入日期entry_date --> |
| | | LEFT JOIN purchase_ledger sl ON slp.sales_ledger_id = sl.id |
| | | left join purchase_return_order_products as t1 on t1.sales_ledger_product_id = slp.id |
| | | left join purchase_return_orders as t2 on t2.id = t1.purchase_return_order_id |
| | | left join product_model pm on pm.id = slp.product_model_id |
| | | <!-- 关联台账主表:获取录入日期entry_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 <!-- 固定筛选:采购台账(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> |
| | | <!-- 按产品大类分组聚合 --> |
| | | 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, |
| | |
| | | FROM product_tree); |
| | | |
| | | </select> |
| | | |
| | | <select id="selectSalesLedgerProductTotals" resultType="com.ruoyi.sales.dto.SalesLedgerProductTotalsDto"> |
| | | SELECT |
| | | slp.sales_ledger_id AS salesLedgerId, |
| | | COALESCE(SUM(slp.quantity), 0) AS totalQuantity, |
| | | COALESCE(SUM(COALESCE(slp.settle_total_area, slp.actual_total_area, 0)), 0) AS totalArea |
| | | FROM sales_ledger_product slp |
| | | WHERE slp.sales_ledger_id IN |
| | | <foreach collection="salesLedgerIds" item="id" open="(" separator="," close=")"> |
| | | #{id} |
| | | </foreach> |
| | | <if test="type != null"> |
| | | AND slp.type = #{type} |
| | | </if> |
| | | GROUP BY slp.sales_ledger_id |
| | | </select> |
| | | </mapper> |