224 lines
13 KiB
XML
224 lines
13 KiB
XML
<?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.GaoProductChartMapper">
|
|
<sql id="productFilter">
|
|
AND p.`deleted_at` IS NULL
|
|
<if test="storeId != null and storeId != ''">AND p.`store_id` = #{storeId}</if>
|
|
<if test="productId != null and productId != ''">AND p.`id` = #{productId}</if>
|
|
<if test="skuId != null and skuId != ''">AND EXISTS (
|
|
SELECT 1 FROM `gao_product_sku` k_filter
|
|
WHERE k_filter.`id` = #{skuId} AND k_filter.`product_id` = p.`id` AND k_filter.`deleted_at` IS NULL
|
|
)</if>
|
|
<if test="categoryId != null and categoryId != ''">AND p.`category_id` = #{categoryId}</if>
|
|
<if test="productType != null">AND p.`product_type` = #{productType}</if>
|
|
</sql>
|
|
|
|
<sql id="orderProductFilter">
|
|
AND o.`deleted_at` IS NULL
|
|
AND i.`deleted_at` IS NULL
|
|
AND o.`status` <> 'CANCELLED'
|
|
<if test="storeId != null and storeId != ''">AND o.`store_id` = #{storeId}</if>
|
|
<if test="productId != null and productId != ''">AND i.`product_id_snapshot` = #{productId}</if>
|
|
<if test="skuId != null and skuId != ''">AND i.`sku_id` = #{skuId}</if>
|
|
<if test="categoryId != null and categoryId != ''">AND p.`category_id` = #{categoryId}</if>
|
|
<if test="productType != null">AND p.`product_type` = #{productType}</if>
|
|
AND o.`purchased_at` >= #{beginAt}
|
|
AND o.`purchased_at` <= #{endAt}
|
|
</sql>
|
|
|
|
<select id="selectSalesDaily" resultType="com.imyeyu.api.modules.gao.vo.GaoProductSalesDailyStatView">
|
|
SELECT
|
|
CAST(DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
|
|
COUNT(DISTINCT o.`id`) AS `orderCount`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` ELSE 0 END), 0) AS `directQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` ELSE 0 END), 0) AS `packageQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END), 0) AS `directBaseQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END), 0) AS `packageBaseQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN COALESCE(i.`line_amount`, 0) ELSE 0 END), 0) AS `directSaleAmount`
|
|
FROM `gao_sales_order_item` i
|
|
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
|
|
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
|
|
WHERE 1 = 1
|
|
<include refid="orderProductFilter"/>
|
|
GROUP BY DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d')
|
|
ORDER BY `dateValue` ASC
|
|
</select>
|
|
|
|
<select id="selectSalesRank" resultType="com.imyeyu.api.modules.gao.vo.GaoProductSalesRankView">
|
|
SELECT
|
|
i.`product_id_snapshot` AS `productId`,
|
|
MAX(i.`product_name_snapshot`) AS `productName`,
|
|
COUNT(DISTINCT o.`id`) AS `orderCount`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` ELSE 0 END), 0) AS `directQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` ELSE 0 END), 0) AS `packageQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END), 0) AS `directBaseQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END), 0) AS `packageBaseQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN COALESCE(i.`line_amount`, 0) ELSE 0 END), 0) AS `directSaleAmount`
|
|
FROM `gao_sales_order_item` i
|
|
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
|
|
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
|
|
WHERE 1 = 1
|
|
<include refid="orderProductFilter"/>
|
|
GROUP BY i.`product_id_snapshot`
|
|
ORDER BY (SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END)
|
|
+ SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END)) DESC,
|
|
directSaleAmount DESC,
|
|
i.`product_id_snapshot` ASC
|
|
LIMIT #{topN}
|
|
</select>
|
|
|
|
<select id="selectFulfillmentDaily" resultType="com.imyeyu.api.modules.gao.vo.GaoProductFulfillmentDailyStatView">
|
|
SELECT
|
|
CAST(DATE_FORMAT(FROM_UNIXTIME(f.`delivered_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT'
|
|
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`quantity` ELSE -fi.`quantity` END ELSE 0 END), 0) AS `directQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT'
|
|
THEN CASE WHEN f.`source_fulfillment_id` IS NULL THEN fi.`quantity` ELSE -fi.`quantity` END ELSE 0 END), 0) AS `packageQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT'
|
|
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 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`
|
|
INNER JOIN `gao_sales_order_item` i ON i.`id` = fi.`source_order_item_id`
|
|
INNER JOIN `gao_sales_order` o ON o.`id` = f.`sales_order_id` AND o.`deleted_at` IS NULL
|
|
INNER JOIN `gao_product` p ON p.`id` = fi.`product_id_snapshot` AND p.`store_id` = f.`store_id`
|
|
WHERE f.`store_id` = #{storeId}
|
|
AND f.`status` = 'VERIFIED'
|
|
AND f.`delivered_at` >= #{beginAt}
|
|
AND f.`delivered_at` <= #{endAt}
|
|
<if test="productId != null and productId != ''">AND fi.`product_id_snapshot` = #{productId}</if>
|
|
<if test="skuId != null and skuId != ''">AND fi.`sku_id` = #{skuId}</if>
|
|
<if test="categoryId != null and categoryId != ''">AND p.`category_id` = #{categoryId}</if>
|
|
<if test="productType != null">AND p.`product_type` = #{productType}</if>
|
|
GROUP BY DATE_FORMAT(FROM_UNIXTIME(f.`delivered_at` / 1000), '%Y%m%d')
|
|
ORDER BY `dateValue` ASC
|
|
</select>
|
|
|
|
<select id="selectFulfillmentRank" resultType="com.imyeyu.api.modules.gao.vo.GaoProductFulfillmentRankView">
|
|
SELECT
|
|
i.`product_id_snapshot` AS `productId`,
|
|
MAX(i.`product_name_snapshot`) AS `productName`,
|
|
COUNT(DISTINCT o.`id`) AS `orderCount`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` ELSE 0 END), 0) AS `directQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` ELSE 0 END), 0) AS `packageQuantity`,
|
|
COALESCE(SUM(i.`reserved_base_quantity`), 0) AS `reservedQuantity`,
|
|
COALESCE(SUM(i.`fulfilled_base_quantity`), 0) AS `fulfilledQuantity`,
|
|
COALESCE(SUM(i.`base_quantity` - i.`reserved_base_quantity` - i.`fulfilled_base_quantity`), 0) AS `availableQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`base_quantity` ELSE 0 END), 0) AS `directBaseQuantity`,
|
|
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`base_quantity` ELSE 0 END), 0) AS `packageBaseQuantity`,
|
|
COALESCE(SUM(i.`reserved_base_quantity`), 0) AS `reservedBaseQuantity`,
|
|
COALESCE(SUM(i.`fulfilled_base_quantity`), 0) AS `fulfilledBaseQuantity`,
|
|
COALESCE(SUM(i.`base_quantity` - i.`reserved_base_quantity` - i.`fulfilled_base_quantity`), 0) AS `availableBaseQuantity`
|
|
FROM `gao_sales_order_item` i
|
|
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
|
|
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
|
|
WHERE 1 = 1
|
|
<include refid="orderProductFilter"/>
|
|
GROUP BY i.`product_id_snapshot`
|
|
ORDER BY `availableBaseQuantity` DESC, `fulfilledBaseQuantity` DESC, i.`product_id_snapshot` ASC
|
|
LIMIT #{topN}
|
|
</select>
|
|
|
|
<select id="selectStockMovementDaily" resultType="com.imyeyu.api.modules.gao.vo.GaoProductStockMovementDailyStatView">
|
|
SELECT
|
|
CAST(DATE_FORMAT(FROM_UNIXTIME(r.`occurred_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
|
|
COALESCE(SUM(CASE WHEN r.`on_hand_delta` > 0 THEN r.`on_hand_delta` ELSE 0 END), 0) AS `inQuantity`,
|
|
COALESCE(SUM(CASE WHEN r.`on_hand_delta` < 0 THEN -r.`on_hand_delta` ELSE 0 END), 0) AS `outQuantity`,
|
|
COALESCE(SUM(r.`on_hand_delta`), 0) AS `netOnHandQuantity`,
|
|
COALESCE(SUM(CASE WHEN r.`reserved_delta` > 0 THEN r.`reserved_delta` ELSE 0 END), 0) AS `reserveQuantity`,
|
|
COALESCE(SUM(CASE WHEN r.`reserved_delta` < 0 THEN -r.`reserved_delta` ELSE 0 END), 0) AS `releaseQuantity`
|
|
FROM `gao_product_stock_record` r
|
|
INNER JOIN `gao_product` p ON p.`id` = r.`product_id_snapshot` AND p.`store_id` = r.`store_id`
|
|
WHERE r.`store_id` = #{storeId}
|
|
AND r.`occurred_at` >= #{beginAt}
|
|
AND r.`occurred_at` <= #{endAt}
|
|
<if test="productId != null and productId != ''">AND r.`product_id_snapshot` = #{productId}</if>
|
|
<if test="skuId != null and skuId != ''">AND r.`sku_id` = #{skuId}</if>
|
|
<if test="categoryId != null and categoryId != ''">AND p.`category_id` = #{categoryId}</if>
|
|
<if test="productType != null">AND p.`product_type` = #{productType}</if>
|
|
GROUP BY DATE_FORMAT(FROM_UNIXTIME(r.`occurred_at` / 1000), '%Y%m%d')
|
|
ORDER BY `dateValue` ASC
|
|
</select>
|
|
|
|
<select id="selectStockHealth" resultType="com.imyeyu.api.modules.gao.vo.GaoProductStockHealthView">
|
|
SELECT
|
|
p.`id` AS `productId`,
|
|
p.`name` AS `productName`,
|
|
p.`category_id` AS `categoryId`,
|
|
c.`name` AS `categoryName`,
|
|
p.`product_type` AS `productType`,
|
|
COALESCE(SUM(s.`on_hand_quantity`), 0) AS `onHandQuantity`,
|
|
COALESCE(SUM(s.`reserved_quantity`), 0) AS `reservedQuantity`,
|
|
COALESCE(SUM(s.`on_hand_quantity` - s.`reserved_quantity`), 0) AS `availableQuantity`,
|
|
CASE WHEN COUNT(k.`min_stock_quantity`) = 0 THEN NULL ELSE SUM(k.`min_stock_quantity`) END AS `minStockQuantity`,
|
|
MAX(CASE WHEN k.`min_stock_quantity` IS NOT NULL
|
|
AND COALESCE(s.`on_hand_quantity`, 0) - COALESCE(s.`reserved_quantity`, 0) < k.`min_stock_quantity`
|
|
THEN 1 ELSE 0 END) AS `lowStock`
|
|
, COUNT(k.`id`) AS `skuCount`
|
|
, SUM(CASE WHEN k.`min_stock_quantity` IS NOT NULL
|
|
AND COALESCE(s.`on_hand_quantity`, 0) - COALESCE(s.`reserved_quantity`, 0) < k.`min_stock_quantity`
|
|
THEN 1 ELSE 0 END) AS `lowStockSkuCount`
|
|
FROM `gao_product` p
|
|
LEFT JOIN `gao_product_category` c ON c.`id` = p.`category_id` AND c.`store_id` = p.`store_id` AND c.`deleted_at` IS NULL
|
|
INNER JOIN `gao_product_sku` k ON k.`product_id` = p.`id` AND k.`deleted_at` IS NULL
|
|
LEFT JOIN `gao_product_stock` s ON s.`sku_id` = k.`id` AND s.`store_id` = p.`store_id`
|
|
WHERE p.`store_id` = #{storeId}
|
|
AND k.`stock_enabled` = 1
|
|
<include refid="productFilter"/>
|
|
GROUP BY p.`id`, p.`name`, p.`category_id`, c.`name`, p.`product_type`, p.`sort`
|
|
ORDER BY `lowStock` DESC, `availableQuantity` ASC, p.`sort` ASC, p.`name` ASC
|
|
LIMIT #{topN}
|
|
</select>
|
|
|
|
<select id="selectType" resultType="com.imyeyu.api.modules.gao.vo.GaoProductTypeStatView">
|
|
SELECT
|
|
p.`product_type` AS `productType`,
|
|
COUNT(1) AS `productCount`
|
|
FROM `gao_product` p
|
|
WHERE p.`store_id` = #{storeId}
|
|
<include refid="productFilter"/>
|
|
GROUP BY p.`product_type`
|
|
ORDER BY p.`product_type` ASC
|
|
</select>
|
|
|
|
<select id="selectCategory" resultType="com.imyeyu.api.modules.gao.vo.GaoProductCategoryStatView">
|
|
SELECT
|
|
p.`category_id` AS `categoryId`,
|
|
COALESCE(c.`name`, '未分类') AS `categoryName`,
|
|
COUNT(1) AS `productCount`
|
|
FROM `gao_product` p
|
|
LEFT JOIN `gao_product_category` c ON c.`id` = p.`category_id` AND c.`store_id` = p.`store_id` AND c.`deleted_at` IS NULL
|
|
WHERE p.`store_id` = #{storeId}
|
|
<include refid="productFilter"/>
|
|
GROUP BY p.`category_id`, c.`name`
|
|
ORDER BY `productCount` DESC, `categoryName` ASC
|
|
</select>
|
|
|
|
<select id="selectUnitSalesRank" resultType="com.imyeyu.api.modules.gao.vo.GaoProductUnitSalesRankView">
|
|
SELECT
|
|
i.`product_id_snapshot` AS `productId`,
|
|
MAX(i.`product_name_snapshot`) AS `productName`,
|
|
i.`sku_id` AS `skuId`,
|
|
MAX(i.`sku_code_snapshot`) AS `skuCode`,
|
|
MAX(i.`sku_name_snapshot`) AS `skuName`,
|
|
MAX(i.`spec_summary_snapshot`) AS `specSummary`,
|
|
i.`sku_unit_id` AS `productUnitId`,
|
|
i.`sku_unit_id` AS `skuUnitId`,
|
|
MAX(i.`unit_snapshot`) AS `unitName`,
|
|
COUNT(DISTINCT o.`id`) AS `orderCount`,
|
|
COALESCE(SUM(i.`quantity`), 0) AS `quantity`,
|
|
COALESCE(SUM(i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1)), 0) AS `baseQuantity`,
|
|
COALESCE(SUM(i.`line_amount`), 0) AS `saleAmount`
|
|
FROM `gao_sales_order_item` i
|
|
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
|
|
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
|
|
WHERE i.`item_type` = 'DIRECT_PRODUCT'
|
|
<include refid="orderProductFilter"/>
|
|
GROUP BY i.`product_id_snapshot`, i.`sku_id`, i.`sku_unit_id`
|
|
ORDER BY `baseQuantity` DESC, `saleAmount` DESC, i.`product_id_snapshot` ASC, i.`sku_id` ASC, i.`sku_unit_id` ASC
|
|
LIMIT #{topN}
|
|
</select>
|
|
</mapper>
|