gaoluyang
2026-11-03 006020bc9a87dc40c2e73bc50f244b348b831de1
yudao-module-erp/src/main/resources/mapper/statistics/ErpPurchaseStatisticsMapper.xml
@@ -10,9 +10,6 @@
               <if test="endTime != null">
                   AND in_time &lt; #{endTime}
               </if>
               <if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
                   AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
               </if>
               AND deleted = 0) -
            (SELECT IFNULL(SUM(total_price), 0)
                FROM erp_purchase_return
@@ -20,10 +17,135 @@
                <if test="endTime != null">
                    AND return_time &lt; #{endTime}
                </if>
                <if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
                    AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
                </if>
                AND deleted = 0)
    </select>
</mapper>
    <select id="selectPurchaseSummary"
            resultType="cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.purchase.ErpPurchaseSummaryRespVO">
        SELECT
            (SELECT COUNT(1) FROM erp_purchase_order WHERE deleted = 0) AS totalOrderCount,
            (SELECT IFNULL(SUM(total_price), 0) FROM erp_purchase_order WHERE deleted = 0) AS totalOrderAmount,
            (SELECT COUNT(1) FROM erp_purchase_in WHERE deleted = 0) AS totalInCount,
            (SELECT IFNULL(SUM(total_price), 0) FROM erp_purchase_in WHERE deleted = 0) AS totalInAmount,
            (SELECT COUNT(1) FROM erp_purchase_return WHERE deleted = 0) AS totalReturnCount,
            (SELECT IFNULL(SUM(total_price), 0) FROM erp_purchase_return WHERE deleted = 0) AS totalReturnAmount
    </select>
    <select id="selectSupplierStatistics"
            resultType="cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.purchase.ErpPurchaseSupplierStatisticsRespVO">
        SELECT s.id AS supplierId,
               s.name AS supplierName,
               IFNULL(o.order_count, 0) AS orderCount,
               IFNULL(o.order_amount, 0) AS orderAmount,
               IFNULL(i.in_count, 0) AS inCount,
               IFNULL(i.in_amount, 0) AS inAmount,
               IFNULL(r.return_count, 0) AS returnCount,
               IFNULL(r.return_amount, 0) AS returnAmount
        FROM srm_supplier s
        INNER JOIN (
            SELECT supplier_id,
                   COUNT(1) AS order_count,
                   IFNULL(SUM(total_price), 0) AS order_amount
            FROM erp_purchase_order
            WHERE deleted = 0
              AND supplier_id IS NOT NULL
              <if test="reqVO.supplierId != null">
                  AND supplier_id = #{reqVO.supplierId}
              </if>
              <if test="reqVO.orderTime != null and reqVO.orderTime.length == 2">
                  AND order_time BETWEEN #{reqVO.orderTime[0]} AND #{reqVO.orderTime[1]}
              </if>
            GROUP BY supplier_id
        ) o ON o.supplier_id = s.id
        LEFT JOIN (
            SELECT po.supplier_id,
                   COUNT(1) AS in_count,
                   IFNULL(SUM(pi.total_price), 0) AS in_amount
            FROM erp_purchase_in pi
            INNER JOIN erp_purchase_order po ON po.id = pi.order_id AND po.deleted = 0
            WHERE pi.deleted = 0
              <if test="reqVO.supplierId != null">
                  AND po.supplier_id = #{reqVO.supplierId}
              </if>
              <if test="reqVO.orderTime != null and reqVO.orderTime.length == 2">
                  AND po.order_time BETWEEN #{reqVO.orderTime[0]} AND #{reqVO.orderTime[1]}
              </if>
            GROUP BY po.supplier_id
        ) i ON i.supplier_id = s.id
        LEFT JOIN (
            SELECT po.supplier_id,
                   COUNT(1) AS return_count,
                   IFNULL(SUM(pr.total_price), 0) AS return_amount
            FROM erp_purchase_return pr
            INNER JOIN erp_purchase_order po ON po.id = pr.order_id AND po.deleted = 0
            WHERE pr.deleted = 0
              <if test="reqVO.supplierId != null">
                  AND po.supplier_id = #{reqVO.supplierId}
              </if>
              <if test="reqVO.orderTime != null and reqVO.orderTime.length == 2">
                  AND po.order_time BETWEEN #{reqVO.orderTime[0]} AND #{reqVO.orderTime[1]}
              </if>
            GROUP BY po.supplier_id
        ) r ON r.supplier_id = s.id
        WHERE s.deleted = 0
        <if test="reqVO.supplierId != null">
            AND s.id = #{reqVO.supplierId}
        </if>
        ORDER BY orderAmount DESC, supplierId ASC
    </select>
    <select id="selectPriceAnalysis"
            resultType="cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.purchase.ErpPurchasePriceAnalysisRespVO">
        SELECT item.product_id AS productId,
               AVG(item.product_price) AS avgPrice,
               MIN(item.product_price) AS minPrice,
               MAX(item.product_price) AS maxPrice,
               (SELECT i2.product_price
                FROM erp_purchase_order_items i2
                INNER JOIN erp_purchase_order o2 ON o2.id = i2.order_id AND o2.deleted = 0
                WHERE i2.deleted = 0
                  AND i2.product_id = item.product_id
                ORDER BY o2.order_time DESC, i2.id DESC
                LIMIT 1) AS latestPrice
        FROM erp_purchase_order_items item
        INNER JOIN erp_purchase_order o ON o.id = item.order_id AND o.deleted = 0
        WHERE item.deleted = 0
          <if test="reqVO.productId != null">
              AND item.product_id = #{reqVO.productId}
          </if>
          <if test="reqVO.orderTime != null and reqVO.orderTime.length == 2">
              AND o.order_time BETWEEN #{reqVO.orderTime[0]} AND #{reqVO.orderTime[1]}
          </if>
        GROUP BY item.product_id
        ORDER BY productId ASC
    </select>
    <select id="selectUncompletedOrders"
            resultType="cn.iocoder.yudao.module.erp.controller.admin.statistics.vo.purchase.ErpPurchaseUncompletedOrderRespVO">
        SELECT o.id AS orderId,
               o.no AS orderNo,
               s.name AS supplierName,
               o.order_time AS orderTime,
               o.total_price AS orderAmount,
               IFNULL((SELECT IFNULL(SUM(i.total_price), 0)
                       FROM erp_purchase_in i
                       WHERE i.order_id = o.id AND i.deleted = 0 AND i.status = 20), 0) AS inAmount,
               GREATEST(DATEDIFF(CURDATE(), DATE(o.order_time)) - 30, 0) AS daysOverdue
        FROM erp_purchase_order o
        LEFT JOIN srm_supplier s ON s.id = o.supplier_id AND s.deleted = 0
        WHERE o.deleted = 0
          AND o.status = 20
          AND (o.in_status IS NULL OR o.in_status != 2)
          <if test="reqVO.supplierId != null">
              AND o.supplier_id = #{reqVO.supplierId}
          </if>
          <if test="reqVO.orderNo != null and reqVO.orderNo != ''">
              AND o.no LIKE CONCAT('%', #{reqVO.orderNo}, '%')
          </if>
          <if test="reqVO.orderTime != null and reqVO.orderTime.length == 2">
              AND o.order_time BETWEEN #{reqVO.orderTime[0]} AND #{reqVO.orderTime[1]}
          </if>
        ORDER BY o.order_time ASC, o.id ASC
    </select>
</mapper>