2026-06-29 940c8481d2fdcf51341db4ccd6fd6fcc2d43debb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.wms.dal.mysql.home.WmsHomeStatisticsMapper">
 
    <!-- 按单据类型和状态统计单据数量 -->
    <select id="selectOrderCountGroupByTypeAndStatus" resultType="java.util.Map">
        SELECT orderType, status, SUM(count) AS count
        FROM (
            SELECT ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@RECEIPT.type} AS orderType,
                   status,
                   COUNT(*) AS count
            FROM wms_receipt_order
            WHERE deleted = 0
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY status
            UNION ALL
            SELECT ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@SHIPMENT.type} AS orderType,
                   status,
                   COUNT(*) AS count
            FROM wms_shipment_order
            WHERE deleted = 0
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY status
            UNION ALL
            SELECT ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@MOVEMENT.type} AS orderType,
                   status,
                   COUNT(*) AS count
            FROM wms_movement_order
            WHERE deleted = 0
              <if test="warehouseId != null">
                  AND (source_warehouse_id = #{warehouseId} OR target_warehouse_id = #{warehouseId})
              </if>
            GROUP BY status
            UNION ALL
            SELECT ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@CHECK.type} AS orderType,
                   status,
                   COUNT(*) AS count
            FROM wms_check_order
            WHERE deleted = 0
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY status
        ) t
        GROUP BY orderType, status
        ORDER BY orderType, status
    </select>
 
    <!-- 按天聚合单据数量 -->
    <select id="selectDailyOrderTrend" resultType="java.util.Map">
        SELECT date, orderType, SUM(count) AS count
        FROM (
            SELECT DATE_FORMAT(order_time, '%Y-%m-%d') AS date,
                   ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@RECEIPT.type} AS orderType,
                   COUNT(*) AS count
            FROM wms_receipt_order
            WHERE deleted = 0
              AND order_time &gt;= #{beginTime}
              AND order_time &lt; #{endTime}
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d')
            UNION ALL
            SELECT DATE_FORMAT(order_time, '%Y-%m-%d') AS date,
                   ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@SHIPMENT.type} AS orderType,
                   COUNT(*) AS count
            FROM wms_shipment_order
            WHERE deleted = 0
              AND order_time &gt;= #{beginTime}
              AND order_time &lt; #{endTime}
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d')
            UNION ALL
            SELECT DATE_FORMAT(order_time, '%Y-%m-%d') AS date,
                   ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@MOVEMENT.type} AS orderType,
                   COUNT(*) AS count
            FROM wms_movement_order
            WHERE deleted = 0
              AND order_time &gt;= #{beginTime}
              AND order_time &lt; #{endTime}
              <if test="warehouseId != null">
                  AND (source_warehouse_id = #{warehouseId} OR target_warehouse_id = #{warehouseId})
              </if>
            GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d')
            UNION ALL
            SELECT DATE_FORMAT(order_time, '%Y-%m-%d') AS date,
                   ${@cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum@CHECK.type} AS orderType,
                   COUNT(*) AS count
            FROM wms_check_order
            WHERE deleted = 0
              AND order_time &gt;= #{beginTime}
              AND order_time &lt; #{endTime}
              <if test="warehouseId != null">
                  AND warehouse_id = #{warehouseId}
              </if>
            GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d')
        ) t
        GROUP BY date, orderType
        ORDER BY date, orderType
    </select>
 
    <!-- 统计库存总数量 -->
    <select id="selectInventoryTotalQuantity" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(quantity), 0)
        FROM wms_inventory
        WHERE deleted = 0
          <if test="warehouseId != null">
              AND warehouse_id = #{warehouseId}
          </if>
    </select>
 
    <!-- 按商品统计库存数量排行 -->
    <select id="selectInventoryItemRank" resultType="java.util.Map">
        SELECT item.id AS id,
               item.name AS name,
               IFNULL(SUM(inventory.quantity), 0) AS quantity
        FROM wms_inventory inventory
                 INNER JOIN wms_item_sku sku ON sku.id = inventory.sku_id AND sku.deleted = 0
                 INNER JOIN wms_item item ON item.id = sku.item_id AND item.deleted = 0
        WHERE inventory.deleted = 0
          AND inventory.quantity &gt; 0
          <if test="warehouseId != null">
              AND inventory.warehouse_id = #{warehouseId}
          </if>
        GROUP BY item.id, item.name
        ORDER BY quantity DESC, item.id ASC
        LIMIT #{limit}
    </select>
 
    <!-- 按仓库统计库存数量排行 -->
    <select id="selectInventoryWarehouseRank" resultType="java.util.Map">
        SELECT warehouse.id AS id,
               warehouse.name AS name,
               IFNULL(SUM(inventory.quantity), 0) AS quantity
        FROM wms_inventory inventory
                 INNER JOIN wms_warehouse warehouse ON warehouse.id = inventory.warehouse_id AND warehouse.deleted = 0
        WHERE inventory.deleted = 0
          AND inventory.quantity &gt; 0
          <if test="warehouseId != null">
              AND inventory.warehouse_id = #{warehouseId}
          </if>
        GROUP BY warehouse.id, warehouse.name
        ORDER BY quantity DESC, warehouse.id ASC
        LIMIT #{limit}
    </select>
 
</mapper>