feat(admin): implement order management and statistics with real database integration
This commit is contained in:
98
docs/sql/30_rpc/order/rpc_admin_order_list_v1.sql
Normal file
98
docs/sql/30_rpc/order/rpc_admin_order_list_v1.sql
Normal file
@@ -0,0 +1,98 @@
|
||||
-- =====================================================================================
|
||||
-- Admin 订单管理 - 主订单列表分页查询 RPC
|
||||
-- 位置:docs/sql/30_rpc/order/
|
||||
-- 对象类型:RPC 函数(SECURITY DEFINER)
|
||||
-- 版本:v1
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.rpc_admin_order_list(
|
||||
p_page INTEGER DEFAULT 1,
|
||||
p_page_size INTEGER DEFAULT 15,
|
||||
p_order_status INTEGER DEFAULT NULL,
|
||||
p_search TEXT DEFAULT NULL,
|
||||
p_start_time TIMESTAMPTZ DEFAULT NULL,
|
||||
p_end_time TIMESTAMPTZ DEFAULT NULL
|
||||
)
|
||||
RETURNS JSONB
|
||||
SECURITY DEFINER
|
||||
SET search_path = public
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_offset INTEGER;
|
||||
v_total BIGINT;
|
||||
v_items JSONB;
|
||||
BEGIN
|
||||
-- 1. 权限检查 (依赖 public.ak_users.role)
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM public.ak_users
|
||||
WHERE auth_id = auth.uid() AND role IN ('admin', 'analytics')
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Permission denied';
|
||||
END IF;
|
||||
|
||||
v_offset := (p_page - 1) * p_page_size;
|
||||
|
||||
-- 2. 获取总数
|
||||
SELECT COUNT(*) INTO v_total
|
||||
FROM public.ml_orders o
|
||||
LEFT JOIN public.ak_users u ON o.user_id = u.id
|
||||
WHERE (p_order_status IS NULL OR o.order_status = p_order_status)
|
||||
AND (p_start_time IS NULL OR o.created_at >= p_start_time)
|
||||
AND (p_end_time IS NULL OR o.created_at <= p_end_time)
|
||||
AND (
|
||||
p_search IS NULL
|
||||
OR o.order_no ILIKE '%' || p_search || '%'
|
||||
OR u.username ILIKE '%' || p_search || '%'
|
||||
OR u.phone ILIKE '%' || p_search || '%'
|
||||
);
|
||||
|
||||
-- 3. 获取明细列表
|
||||
SELECT jsonb_agg(t) INTO v_items
|
||||
FROM (
|
||||
SELECT
|
||||
o.id,
|
||||
o.order_no,
|
||||
o.total_amount,
|
||||
o.paid_amount,
|
||||
o.discount_amount,
|
||||
o.order_status,
|
||||
o.payment_status,
|
||||
o.shipping_status,
|
||||
o.paid_at,
|
||||
o.created_at,
|
||||
u.username as buyer_name,
|
||||
u.phone as buyer_phone,
|
||||
(
|
||||
SELECT jsonb_build_object(
|
||||
'product_name', oi.product_name,
|
||||
'image_url', oi.image_url,
|
||||
'quantity', oi.quantity
|
||||
)
|
||||
FROM public.ml_order_items oi
|
||||
WHERE oi.order_id = o.id
|
||||
ORDER BY oi.created_at ASC
|
||||
LIMIT 1
|
||||
) as first_item_summary
|
||||
FROM public.ml_orders o
|
||||
LEFT JOIN public.ak_users u ON o.user_id = u.id
|
||||
WHERE (p_order_status IS NULL OR o.order_status = p_order_status)
|
||||
AND (p_start_time IS NULL OR o.created_at >= p_start_time)
|
||||
AND (p_end_time IS NULL OR o.created_at <= p_end_time)
|
||||
AND (
|
||||
p_search IS NULL
|
||||
OR o.order_no ILIKE '%' || p_search || '%'
|
||||
OR u.username ILIKE '%' || p_search || '%'
|
||||
OR u.phone ILIKE '%' || p_search || '%'
|
||||
)
|
||||
ORDER BY o.created_at DESC
|
||||
LIMIT p_page_size
|
||||
OFFSET v_offset
|
||||
) t;
|
||||
|
||||
RETURN jsonb_build_object(
|
||||
'total', v_total,
|
||||
'items', COALESCE(v_items, '[]'::jsonb)
|
||||
);
|
||||
END;
|
||||
$$;
|
||||
45
docs/sql/30_rpc/order/rpc_admin_order_source_stats_v1.sql
Normal file
45
docs/sql/30_rpc/order/rpc_admin_order_source_stats_v1.sql
Normal file
@@ -0,0 +1,45 @@
|
||||
-- =====================================================================================
|
||||
-- Admin 订单统计 - 订单来源分布 RPC
|
||||
-- 位置:docs/sql/30_rpc/order/
|
||||
-- 对象类型:RPC 函数(SECURITY DEFINER)
|
||||
-- 版本:v1
|
||||
-- 说明:统计订单来源分布。
|
||||
-- 注意:当前 ml_orders DDL 未包含来源/渠道字段,本函数提供最小可用兜底:统一返回 "unknown" 汇总。
|
||||
-- 若后续新增 channel/payment_method 等字段,可在此函数中替换为按渠道分组统计。
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.rpc_admin_order_source_stats(
|
||||
p_start_time TIMESTAMPTZ,
|
||||
p_end_time TIMESTAMPTZ
|
||||
)
|
||||
RETURNS JSONB
|
||||
SECURITY DEFINER
|
||||
SET search_path = public
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_items JSONB;
|
||||
BEGIN
|
||||
-- 1. 权限检查
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM public.ak_users
|
||||
WHERE auth_id = auth.uid() AND role IN ('admin', 'analytics')
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Permission denied';
|
||||
END IF;
|
||||
|
||||
-- 2. 最小可用:按 unknown 聚合(排除已取消)
|
||||
SELECT jsonb_agg(t) INTO v_items
|
||||
FROM (
|
||||
SELECT
|
||||
'unknown'::text AS source,
|
||||
COUNT(*) FILTER (WHERE o.order_status != 5) AS order_count,
|
||||
COALESCE(SUM(o.total_amount) FILTER (WHERE o.order_status != 5), 0) AS total_amount
|
||||
FROM public.ml_orders o
|
||||
WHERE o.created_at >= p_start_time
|
||||
AND o.created_at <= p_end_time
|
||||
) t;
|
||||
|
||||
RETURN COALESCE(v_items, '[]'::jsonb);
|
||||
END;
|
||||
$$;
|
||||
60
docs/sql/30_rpc/order/rpc_admin_order_stats_v1.sql
Normal file
60
docs/sql/30_rpc/order/rpc_admin_order_stats_v1.sql
Normal file
@@ -0,0 +1,60 @@
|
||||
-- =====================================================================================
|
||||
-- Admin 订单统计 - 核心 KPI 汇总 RPC
|
||||
-- 位置:docs/sql/30_rpc/order/
|
||||
-- 对象类型:RPC 函数(SECURITY DEFINER)
|
||||
-- 版本:v1
|
||||
-- 说明:获取指定时间段内的订单量、销售额、退款数及退款金额
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.rpc_admin_order_stats(
|
||||
p_start_time TIMESTAMPTZ,
|
||||
p_end_time TIMESTAMPTZ
|
||||
)
|
||||
RETURNS JSONB
|
||||
SECURITY DEFINER
|
||||
SET search_path = public
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_order_count BIGINT;
|
||||
v_total_amount DECIMAL(12,2);
|
||||
v_refund_count BIGINT;
|
||||
v_refund_amount DECIMAL(12,2);
|
||||
BEGIN
|
||||
-- 1. 权限检查
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM public.ak_users
|
||||
WHERE auth_id = auth.uid() AND role IN ('admin', 'analytics')
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Permission denied';
|
||||
END IF;
|
||||
|
||||
-- 2. 统计订单汇总(不含已取消)
|
||||
SELECT
|
||||
COUNT(*),
|
||||
COALESCE(SUM(total_amount), 0)
|
||||
INTO v_order_count, v_total_amount
|
||||
FROM public.ml_orders
|
||||
WHERE created_at >= p_start_time
|
||||
AND created_at <= p_end_time
|
||||
AND order_status != 5; -- 5: 已取消
|
||||
|
||||
-- 3. 统计退款汇总
|
||||
-- 注意:这里基于 ml_orders 的 payment_status 或 order_status 判断已退款
|
||||
SELECT
|
||||
COUNT(*),
|
||||
COALESCE(SUM(discount_amount), 0) -- 暂时用这个,若有真实退款金额字段请替换
|
||||
INTO v_refund_count, v_refund_amount
|
||||
FROM public.ml_orders
|
||||
WHERE created_at >= p_start_time
|
||||
AND created_at <= p_end_time
|
||||
AND order_status IN (6, 7); -- 6: 退款中, 7: 已退款
|
||||
|
||||
RETURN jsonb_build_object(
|
||||
'order_count', v_order_count,
|
||||
'total_amount', v_total_amount,
|
||||
'refund_count', v_refund_count,
|
||||
'refund_amount', v_refund_amount
|
||||
);
|
||||
END;
|
||||
$$;
|
||||
52
docs/sql/30_rpc/order/rpc_admin_order_trend_v1.sql
Normal file
52
docs/sql/30_rpc/order/rpc_admin_order_trend_v1.sql
Normal file
@@ -0,0 +1,52 @@
|
||||
-- =====================================================================================
|
||||
-- Admin 订单统计 - 趋势统计 RPC
|
||||
-- 位置:docs/sql/30_rpc/order/
|
||||
-- 对象类型:RPC 函数(SECURITY DEFINER)
|
||||
-- 版本:v1
|
||||
-- 说明:按天聚合指定时间范围内的订单量/销售额/退款量/退款金额
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.rpc_admin_order_trend(
|
||||
p_start_time TIMESTAMPTZ,
|
||||
p_end_time TIMESTAMPTZ,
|
||||
p_group_by TEXT DEFAULT 'day'
|
||||
)
|
||||
RETURNS JSONB
|
||||
SECURITY DEFINER
|
||||
SET search_path = public
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_items JSONB;
|
||||
BEGIN
|
||||
-- 1. 权限检查
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM public.ak_users
|
||||
WHERE auth_id = auth.uid() AND role IN ('admin', 'analytics')
|
||||
) THEN
|
||||
RAISE EXCEPTION 'Permission denied';
|
||||
END IF;
|
||||
|
||||
-- 2. 目前仅支持 day
|
||||
IF p_group_by IS NULL OR p_group_by != 'day' THEN
|
||||
RAISE EXCEPTION 'Unsupported group_by';
|
||||
END IF;
|
||||
|
||||
SELECT jsonb_agg(t) INTO v_items
|
||||
FROM (
|
||||
SELECT
|
||||
to_char(date_trunc('day', o.created_at), 'YYYY-MM-DD') AS date_group,
|
||||
COUNT(*) FILTER (WHERE o.order_status != 5) AS order_count,
|
||||
COALESCE(SUM(o.total_amount) FILTER (WHERE o.order_status != 5), 0) AS total_amount,
|
||||
COUNT(*) FILTER (WHERE o.order_status IN (6, 7)) AS refund_count,
|
||||
COALESCE(SUM(o.discount_amount) FILTER (WHERE o.order_status IN (6, 7)), 0) AS refund_amount
|
||||
FROM public.ml_orders o
|
||||
WHERE o.created_at >= p_start_time
|
||||
AND o.created_at <= p_end_time
|
||||
GROUP BY date_trunc('day', o.created_at)
|
||||
ORDER BY date_trunc('day', o.created_at) ASC
|
||||
) t;
|
||||
|
||||
RETURN COALESCE(v_items, '[]'::jsonb);
|
||||
END;
|
||||
$$;
|
||||
Reference in New Issue
Block a user