feat: recycle system

This commit is contained in:
Timi
2026-11-12 17:37:05 +08:00
parent bc901e2f0b
commit 169fd257a9
70 changed files with 2085 additions and 785 deletions
@@ -41,6 +41,8 @@
</select>
<sql id="queryWhere">
1 = 1
<if test="orderAllowed != null and !orderAllowed">AND EXISTS (SELECT 1 FROM `gao_sales_order` o WHERE o.`id` = `gao_order_fulfillment`.`sales_order_id` AND o.`order_type` = 'RECYCLE')</if>
<if test="recycleAllowed != null and !recycleAllowed">AND EXISTS (SELECT 1 FROM `gao_sales_order` o WHERE o.`id` = `gao_order_fulfillment`.`sales_order_id` AND o.`order_type` != 'RECYCLE')</if>
<if test="storeId != null and storeId != ''">AND `store_id` = #{storeId}</if>
<if test="salesOrderId != null and salesOrderId != ''">AND `sales_order_id` = #{salesOrderId}</if>
<if test="customerId != null and customerId != ''">AND `customer_id` = #{customerId}</if>
@@ -60,7 +62,9 @@
SELECT COUNT(1) FROM `gao_order_fulfillment` WHERE <include refid="queryWhere"/>
</select>
<select id="selectByQuery" resultType="com.imyeyu.api.modules.gao.entity.GaoOrderFulfillment">
SELECT * FROM `gao_order_fulfillment`
SELECT `gao_order_fulfillment`.*,
(SELECT o.`order_type` FROM `gao_sales_order` o WHERE o.`id` = `gao_order_fulfillment`.`sales_order_id`) AS `orderType`
FROM `gao_order_fulfillment`
WHERE <include refid="queryWhere"/>
ORDER BY `delivered_at` DESC, `created_at` DESC
LIMIT #{offset}, #{limit}
@@ -53,6 +53,7 @@
FROM `gao_sales_order` o
<include refid="orderReminderJoins"/>
WHERE o.`deleted_at` IS NULL
AND o.`order_type` &lt;&gt; 'RECYCLE'
AND o.`status` IN ('ACTIVE', 'FULFILLED')
AND o.`payment_status` IN ('UNPAID', 'PARTIALLY_PAID')
ORDER BY o.`store_id`, c.`name`, o.`customer_id`, o.`purchased_at`, o.`order_no`
@@ -15,6 +15,7 @@
<sql id="orderProductFilter">
AND o.`deleted_at` IS NULL
AND o.`order_type` &lt;&gt; 'RECYCLE'
AND i.`deleted_at` IS NULL
AND o.`status` &lt;&gt; 'CANCELLED'
<if test="storeId != null and storeId != ''">AND o.`store_id` = #{storeId}</if>
@@ -78,6 +79,8 @@
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `directBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT'
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `packageBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'RECYCLE_ENTITLEMENT'
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `recycleBaseQuantity`,
COALESCE(SUM(CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END), 0) AS `baseQuantity`
FROM `gao_order_fulfillment` f
INNER JOIN `gao_order_fulfillment_item` fi ON fi.`fulfillment_id` = f.`id`
@@ -8,8 +8,8 @@
(SELECT COUNT(*) FROM `gao_product_stock_record` WHERE `product_id_snapshot` = #{productId}) +
(SELECT COUNT(*) FROM `gao_package_config_item` WHERE `product_id_snapshot` = #{productId}) +
(SELECT COUNT(*) FROM `gao_point_redemption` WHERE `product_id_snapshot` = #{productId}) +
(SELECT COUNT(*) FROM `gao_recycle_item` WHERE `product_id` = #{productId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange` WHERE `product_id_snapshot` = #{productId} OR `source_product_id_snapshot` = #{productId})
(SELECT COUNT(*) FROM `gao_recycle_product_yield` y JOIN `gao_product_sku` s ON s.`id` = y.`sku_id` WHERE s.`product_id` = #{productId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule_item` ri JOIN `gao_product_sku` s ON s.`id` = ri.`sku_id` WHERE s.`product_id` = #{productId})
</select>
<select id="countSkuReferences" resultType="int">
SELECT
@@ -19,9 +19,8 @@
(SELECT COUNT(*) FROM `gao_package_config_item` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_point_redeem_sku` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_point_redemption` WHERE `sku_id_snapshot` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_item` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange` WHERE `sku_id_snapshot` = #{skuId} OR `source_sku_id_snapshot` = #{skuId})
(SELECT COUNT(*) FROM `gao_recycle_product_yield` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule_item` WHERE `sku_id` = #{skuId})
</select>
<select id="countUnitReferences" resultType="int">
SELECT
@@ -31,12 +30,13 @@
(SELECT COUNT(*) FROM `gao_package_config_item` WHERE `sku_unit_id` = #{unitId}) +
(SELECT COUNT(*) FROM `gao_point_redeem_sku` WHERE `sku_unit_id` = #{unitId}) +
(SELECT COUNT(*) FROM `gao_point_redemption` WHERE `sku_unit_id_snapshot` = #{unitId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule` WHERE `sku_unit_id` = #{unitId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange` WHERE `sku_unit_id_snapshot` = #{unitId})
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule_item` WHERE `sku_unit_id` = #{unitId})
</select>
<select id="countRecycleReferences" resultType="int">
SELECT (SELECT COUNT(*) FROM `gao_recycle_exchange_rule` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange` WHERE `sku_id_snapshot` = #{skuId})
SELECT (SELECT COUNT(*) FROM `gao_recycle_product_yield` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_recycle_exchange_rule_item` WHERE `sku_id` = #{skuId}) +
(SELECT COUNT(*) FROM `gao_sales_order_item` i JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
WHERE i.`sku_id` = #{skuId} AND o.`order_type` = 'RECYCLE')
</select>
<select id="countMigrationPrecisionLoss" resultType="int">
SELECT COUNT(*) FROM (
@@ -3,6 +3,7 @@
<mapper namespace="com.imyeyu.api.modules.gao.mapper.GaoProductSkuChartMapper">
<sql id="skuOrderProductFilter">
AND o.`deleted_at` IS NULL
AND o.`order_type` &lt;&gt; 'RECYCLE'
AND i.`deleted_at` IS NULL
AND o.`status` &lt;&gt; 'CANCELLED'
AND o.`store_id` = #{storeId}
@@ -97,6 +98,8 @@
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `directBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT'
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `packageBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'RECYCLE_ENTITLEMENT'
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END ELSE 0 END), 0) AS `recycleBaseQuantity`,
COALESCE(SUM(CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`base_quantity` ELSE -fi.`base_quantity` END), 0) AS `baseQuantity`
FROM `gao_order_fulfillment` f
INNER JOIN `gao_order_fulfillment_item` fi ON fi.`fulfillment_id` = f.`id`
@@ -1,20 +0,0 @@
<?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="com.imyeyu.api.modules.gao.mapper.GaoRecycleExchangeQueryMapper">
<sql id="conditions">
`store_id` = #{storeId}
<if test="customerId != null and customerId != ''">AND `customer_id` = #{customerId}</if>
<if test="recycleItemId != null and recycleItemId != ''">AND `recycle_item_id` = #{recycleItemId}</if>
<if test="skuId != null and skuId != ''">AND `sku_id_snapshot` = #{skuId}</if>
<if test="status != null">AND `status` = #{status}</if>
<if test="beginAt != null">AND `occurred_at` &gt;= #{beginAt}</if>
<if test="endAt != null">AND `occurred_at` &lt;= #{endAt}</if>
</sql>
<select id="count" resultType="long">
SELECT COUNT(*) FROM `gao_recycle_exchange` WHERE <include refid="conditions"/>
</select>
<select id="list" resultType="com.imyeyu.api.modules.gao.entity.GaoRecycleExchange">
SELECT * FROM `gao_recycle_exchange` WHERE <include refid="conditions"/>
ORDER BY `occurred_at` DESC, `created_at` DESC, `id` DESC LIMIT #{offset}, #{limit}
</select>
</mapper>
@@ -7,16 +7,12 @@
SUM(t.returned) AS returnedQuantity, SUM(t.received - t.returned) AS netQuantity
FROM (
SELECT e.recycle_item_id, e.recycle_name_snapshot AS recycle_name,
e.recycle_unit_snapshot AS recycle_unit, e.recycle_quantity AS received, 0 AS returned
FROM gao_recycle_exchange e
WHERE e.store_id = #{storeId} AND e.occurred_at &gt;= #{startAt} AND e.occurred_at &lt; #{endAt}
UNION ALL
SELECT e.recycle_item_id, e.recycle_name_snapshot, e.recycle_unit_snapshot,
0, e.recycle_quantity
FROM gao_recycle_exchange_action a
JOIN gao_recycle_exchange e ON e.id = a.exchange_id AND e.store_id = a.store_id
WHERE a.store_id = #{storeId} AND a.action_type IN ('CANCEL', 'REVOKE')
AND a.recycle_returned = 1 AND a.occurred_at &gt;= #{startAt} AND a.occurred_at &lt; #{endAt}
e.recycle_unit_snapshot AS recycle_unit,
CASE WHEN p.direction = 'IN' THEN p.quantity ELSE 0 END AS received,
CASE WHEN p.direction = 'OUT' THEN p.quantity ELSE 0 END AS returned
FROM gao_recycle_physical_record p
JOIN gao_recycle_exchange e ON e.sales_order_id = p.order_id AND e.store_id = p.store_id
WHERE p.store_id = #{storeId} AND p.occurred_at &gt;= #{startAt} AND p.occurred_at &lt; #{endAt}
) t
GROUP BY t.recycle_item_id, t.recycle_name, t.recycle_unit
</select>
@@ -25,19 +21,25 @@
t.unit_name AS unit, SUM(t.created) AS createdQuantity,
SUM(t.verified) AS verifiedQuantity, SUM(t.revoked) AS revokedQuantity
FROM (
SELECT e.sku_id_snapshot AS sku_id, e.sku_unit_id_snapshot AS sku_unit_id,
e.product_name_snapshot AS product_name, e.unit_snapshot AS unit_name,
e.exchange_quantity AS created, 0 AS verified, 0 AS revoked
SELECT oi.sku_id, oi.sku_unit_id, oi.product_name_snapshot AS product_name,
oi.unit_snapshot AS unit_name,
oi.quantity AS created, 0 AS verified, 0 AS revoked
FROM gao_recycle_exchange e
JOIN gao_sales_order_item oi ON oi.sales_order_id = e.sales_order_id
AND oi.item_type = 'RECYCLE_ENTITLEMENT' AND oi.deleted_at IS NULL
WHERE e.store_id = #{storeId} AND e.occurred_at &gt;= #{startAt} AND e.occurred_at &lt; #{endAt}
UNION ALL
SELECT e.sku_id_snapshot, e.sku_unit_id_snapshot, e.product_name_snapshot, e.unit_snapshot,
0, CASE WHEN a.action_type = 'VERIFY' THEN e.exchange_quantity ELSE 0 END,
CASE WHEN a.action_type = 'REVOKE' THEN e.exchange_quantity ELSE 0 END
FROM gao_recycle_exchange_action a
JOIN gao_recycle_exchange e ON e.id = a.exchange_id AND e.store_id = a.store_id
WHERE a.store_id = #{storeId} AND a.action_type IN ('VERIFY', 'REVOKE')
AND a.occurred_at &gt;= #{startAt} AND a.occurred_at &lt; #{endAt}
SELECT oi.sku_id, oi.sku_unit_id, oi.product_name_snapshot, oi.unit_snapshot,
0, CASE WHEN f.source_fulfillment_id IS NULL THEN i.base_quantity / oi.base_quantity_per_unit_snapshot ELSE 0 END,
CASE WHEN f.source_fulfillment_id IS NOT NULL THEN i.base_quantity / oi.base_quantity_per_unit_snapshot ELSE 0 END
FROM gao_order_fulfillment f
JOIN gao_recycle_exchange e ON e.sales_order_id = f.sales_order_id AND e.store_id = f.store_id
JOIN gao_order_fulfillment_item i ON i.fulfillment_id = f.id
JOIN gao_sales_order_item oi ON oi.id = i.source_order_item_id AND oi.sales_order_id = f.sales_order_id
WHERE f.store_id = #{storeId}
AND ((f.source_fulfillment_id IS NULL AND f.status IN ('VERIFIED', 'REVOKED'))
OR (f.source_fulfillment_id IS NOT NULL AND f.status = 'VERIFIED'))
AND f.delivered_at &gt;= #{startAt} AND f.delivered_at &lt; #{endAt}
) t
GROUP BY t.sku_id, t.sku_unit_id, t.product_name, t.unit_name
</select>
@@ -3,6 +3,7 @@
<mapper namespace="com.imyeyu.api.modules.gao.mapper.GaoSalesOrderChartMapper">
<sql id="orderFilter">
AND o.`deleted_at` IS NULL
AND o.`order_type` &lt;&gt; 'RECYCLE'
AND o.`store_id` = #{storeId}
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</if>
<if test="orderType != null">AND o.`order_type` = #{orderType}</if>
@@ -218,6 +219,7 @@
<sql id="repeatOrderFilter">
AND o.`deleted_at` IS NULL
AND o.`order_type` &lt;&gt; 'RECYCLE'
AND o.`store_id` = #{storeId}
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</if>
<if test="orderType != null">AND o.`order_type` = #{orderType}</if>
@@ -21,6 +21,8 @@
</select>
<sql id="queryWhere">
o.`deleted_at` IS NULL
<if test="orderAllowed != null and !orderAllowed">AND o.`order_type` = 'RECYCLE'</if>
<if test="recycleAllowed != null and !recycleAllowed">AND o.`order_type` &lt;&gt; 'RECYCLE'</if>
<if test="storeId != null and storeId != ''">AND o.`store_id` = #{storeId}</if>
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</if>
<if test="fulfillableSkuId != null and fulfillableSkuId != ''">
@@ -86,7 +88,17 @@
SELECT COUNT(1) FROM `gao_sales_order` o LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id` WHERE <include refid="queryWhere"/>
</select>
<select id="selectByQuery" resultType="com.imyeyu.api.modules.gao.entity.GaoSalesOrder">
SELECT o.* FROM `gao_sales_order` o LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id` WHERE <include refid="queryWhere"/>
SELECT o.*,
(SELECT SUM(p.`quantity`) FROM `gao_recycle_payment` p
WHERE p.`order_id` = o.`id` AND p.`status` = 'PENDING') AS `recyclePendingQuantity`,
<!-- 回收物名称与单位取兑换单创建时的快照,回收物改名后历史订单仍显示当时的名称 -->
e.`id` AS `recycleExchangeId`,
e.`recycle_name_snapshot` AS `recycleItemName`,
e.`recycle_unit_snapshot` AS `recycleUnit`
FROM `gao_sales_order` o
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
LEFT JOIN `gao_recycle_exchange` e ON e.`sales_order_id` = o.`id`
WHERE <include refid="queryWhere"/>
ORDER BY o.`purchased_at` DESC, o.`created_at` DESC
LIMIT #{offset}, #{limit}
</select>