-- ============================================================
-- 快手电商 - 报表系统初始化数据
-- 基于 seed_data_kuaishou.sql 中实际表结构生成
-- 覆盖报表: 订单 / 商品 / 售后 / 分销订单 / 代发订单
-- ============================================================

-- =============================================
-- 1. 业务配置 (report_business_config)
-- =============================================
INSERT INTO report_business_config (tenant_id, business_code, business_name, description, status, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', '快手电商', '快手电商平台订单、商品、售后、分销数据分析', 'ACTIVE',
 '{"platform":"kuaishou","timezone":"Asia/Shanghai"}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code) DO NOTHING;


-- =============================================
-- 2. 报表配置 (report_report_config)
-- =============================================

-- 2.1 订单分析
INSERT INTO report_report_config (tenant_id, business_code, report_code, report_name, description, status, stat_table_name, stat_table_comment, date_field, primary_keys, conflict_keys, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'order_analysis', '订单分析', '基于 kuaishou_order_list 的订单维度聚合分析，支持按日期/状态/地区/支付方式/渠道等维度', 'ACTIVE',
 'kuaishou_order_stat', '快手订单统计宽表（聚合自 kuaishou_order_list）',
 'stat_date',
 '["id"]'::jsonb,
 '["stat_date","oid"]'::jsonb,
 '{"defaultTimeRange":"last_30_days","defaultDimensions":["stat_date"],"defaultIndicators":[{"field":"order_count","aggregate":"COUNT"},{"field":"total_fee","aggregate":"SUM"}]}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code) DO NOTHING;

-- 2.2 商品分析
INSERT INTO report_report_config (tenant_id, business_code, report_code, report_name, description, status, stat_table_name, stat_table_comment, date_field, primary_keys, conflict_keys, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'item_analysis', '商品分析', '基于 kuaishou_item_list 的商品维度销量、销售额分析，支持按类目/类型/上下架状态等维度', 'ACTIVE',
 'kuaishou_item_stat', '快手商品统计宽表（聚合自 kuaishou_item_list）',
 'stat_date',
 '["id"]'::jsonb,
 '["stat_date","kwai_item_id","category_id"]'::jsonb,
 '{"defaultTimeRange":"last_30_days","defaultDimensions":["stat_date","category_name"],"defaultIndicators":[{"field":"sales_volume","aggregate":"SUM"},{"field":"sales_amount","aggregate":"SUM"}]}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code) DO NOTHING;

-- 2.3 售后分析
INSERT INTO report_report_config (tenant_id, business_code, report_code, report_name, description, status, stat_table_name, stat_table_comment, date_field, primary_keys, conflict_keys, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'refund_analysis', '售后分析', '基于 kuaishou_refund_list 的售后维度分析，退款金额/退款率等核心指标', 'ACTIVE',
 'kuaishou_refund_stat', '快手售后统计宽表（聚合自 kuaishou_refund_list）',
 'stat_date',
 '["id"]'::jsonb,
 '["stat_date","refund_id"]'::jsonb,
 '{"defaultTimeRange":"last_30_days","defaultDimensions":["stat_date","refund_type"],"defaultIndicators":[{"field":"refund_count","aggregate":"COUNT"},{"field":"refund_fee","aggregate":"SUM"}]}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code) DO NOTHING;

-- 2.4 分销订单分析
INSERT INTO report_report_config (tenant_id, business_code, report_code, report_name, description, status, stat_table_name, stat_table_comment, date_field, primary_keys, conflict_keys, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'cps_order_analysis', '分销订单分析', '基于 kuaishou_cps_order_list 的分销订单维度分析，含分销收入/佣金等指标', 'ACTIVE',
 'kuaishou_cps_order_stat', '快手分销订单统计宽表（聚合自 kuaishou_cps_order_list）',
 'stat_date',
 '["id"]'::jsonb,
 '["stat_date","oid"]'::jsonb,
 '{"defaultTimeRange":"last_30_days","defaultDimensions":["stat_date","status"],"defaultIndicators":[{"field":"order_count","aggregate":"COUNT"},{"field":"estimated_income","aggregate":"SUM"}]}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code) DO NOTHING;

-- 2.5 代发订单分析
INSERT INTO report_report_config (tenant_id, business_code, report_code, report_name, description, status, stat_table_name, stat_table_comment, date_field, primary_keys, conflict_keys, config, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', '代发订单分析', '基于 kuaishou_dropshipping_order_list 的代发订单维度分析，按厂家/物流/地区等维度', 'ACTIVE',
 'kuaishou_dropshipping_stat', '快手代发订单统计宽表（聚合自 kuaishou_dropshipping_order_list）',
 'stat_date',
 '["id"]'::jsonb,
 '["stat_date","dropshipping_order_code"]'::jsonb,
 '{"defaultTimeRange":"last_30_days","defaultDimensions":["stat_date","dropshipping_status"],"defaultIndicators":[{"field":"order_count","aggregate":"COUNT"}]}'::jsonb,
 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code) DO NOTHING;


-- =============================================
-- 3. 字段配置 (report_field_config)
-- =============================================

-- ----------------------------------------------------------
-- 3.1 订单分析 - 字段配置
-- 源表: kuaishou_order_list
-- 实际列: oid, status, createTime, updateTime, payTime, sendTime, recvTime,
--         refundTime, totalFee, expressFee, discountFee, originalPrice,
--         buyerOpenId, sellerOpenId, buyerNick, sellerNick, remark, itemTitle,
--         num, price, activityType, cpsType, payType, payChannel, channel,
--         commentStatus, priorityDelivery, carrierType, carrierId,
--         province, city, district, provinceCode, cityCode, districtCode
-- ----------------------------------------------------------

-- 维度字段
INSERT INTO report_field_config (tenant_id, business_code, report_code, field_code, field_name, field_type, data_type, field_role, is_aggregatable, is_filterable, is_queryable, is_sortable, default_aggregate, valid_aggregates, filter_operators, expression, expression_type, format_pattern, unit, dict_code, sort_order, group_name, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'order_analysis', 'stat_date', '统计日期', 'DATE', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', '', 'yyyy-MM-dd', '', '', 1, '时间维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'oid', '订单ID', 'BIGINT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 2, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'status', '订单状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'order_status_dict', 3, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'province', '省份', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 4, '地域维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'city', '城市', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 5, '地域维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'district', '区县', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 6, '地域维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'pay_type', '支付类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'pay_type_dict', 7, '支付维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'pay_channel', '支付渠道', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 8, '支付维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'channel', '分销渠道', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 9, '渠道维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'activity_type', '活动类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 10, '活动维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'buyer_nick', '买家昵称', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE"]'::jsonb, '', '', '', '', '', 11, '用户维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'item_title', '商品标题', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE"]'::jsonb, '', '', '', '', '', 12, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),

-- 指标字段
(1, 'kuaishou_ecommerce', 'order_analysis', 'order_count', '订单数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT","SUM"]'::jsonb, '[]'::jsonb, '1', 'CALCULATED', '#,##0', '单', '', 21, '订单指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'total_fee', '订单金额(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', 'ORIGINAL', '', '分', '', 22, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'total_fee_yuan', '订单金额(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, 'total_fee / 100.0', 'CALCULATED', '#,##0.00', '元', '', 23, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'express_fee', '运费(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 24, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'express_fee_yuan', '运费(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, 'express_fee / 100.0', 'CALCULATED', '#,##0.00', '元', '', 25, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'discount_fee', '优惠金额(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 26, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'discount_fee_yuan', '优惠金额(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, 'discount_fee / 100.0', 'CALCULATED', '#,##0.00', '元', '', 27, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'original_price', '原价(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 28, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'item_num', '商品件数', 'INT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '#,##0', '件', '', 29, '商品指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'avg_order_amount', '客单价(元)', 'FLOAT', 'STRING', 'INDICATOR', true, false, true, true, 'AVG', '["AVG"]'::jsonb, '[]'::jsonb, 'total_fee / 100.0 / NULLIF(order_count, 0)', 'CALCULATED', '#,##0.00', '元', '', 30, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'order_analysis', 'discount_rate', '优惠率(%)', 'FLOAT', 'STRING', 'INDICATOR', true, false, true, true, 'AVG', '["AVG"]'::jsonb, '[]'::jsonb, 'CASE WHEN SUM(total_fee) > 0 THEN SUM(discount_fee) * 100.0 / (SUM(total_fee) + SUM(discount_fee)) ELSE 0 END', 'CALCULATED', '#,##0.00', '%', '', 31, '比率指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, field_code) DO NOTHING;


-- ----------------------------------------------------------
-- 3.2 商品分析 - 字段配置
-- 源表: kuaishou_item_list
-- 实际列: kwaiItemId, relItemId, title, details, categoryId, categoryName,
--         price, volume, status, auditStatus, auditReason, shelfStatus,
--         itemType, createTime, updateTime, expressTemplateId, linkUrl
-- ----------------------------------------------------------

-- 维度字段
INSERT INTO report_field_config (tenant_id, business_code, report_code, field_code, field_name, field_type, data_type, field_role, is_aggregatable, is_filterable, is_queryable, is_sortable, default_aggregate, valid_aggregates, filter_operators, expression, expression_type, format_pattern, unit, dict_code, sort_order, group_name, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'item_analysis', 'stat_date', '统计日期', 'DATE', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', '', 'yyyy-MM-dd', '', '', 1, '时间维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'kwai_item_id', '商品ID', 'BIGINT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 2, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'title', '商品标题', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE","IN"]'::jsonb, '', '', '', '', '', 3, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'category_id', '类目ID', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 4, '类目维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'category_name', '类目名称', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 5, '类目维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'item_type', '商品类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'item_type_dict', 6, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'shelf_status', '上下架状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'shelf_status_dict', 7, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'audit_status', '审核状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 8, '商品维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),

-- 指标字段
(1, 'kuaishou_ecommerce', 'item_analysis', 'sales_volume', '销量', 'INT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', 'ORIGINAL', '#,##0', '件', '', 21, '销售指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'price', '商品价格(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'AVG', '["AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 22, '价格指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'price_yuan', '商品价格(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'AVG', '["AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, 'price / 100.0', 'CALCULATED', '#,##0.00', '元', '', 23, '价格指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'sales_amount', '销售额(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 24, '销售指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'sales_amount_yuan', '销售额(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, 'sales_amount / 100.0', 'CALCULATED', '#,##0.00', '元', '', 25, '销售指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'item_count', '商品数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT"]'::jsonb, '[]'::jsonb, 'COUNT(DISTINCT kwai_item_id)', 'CALCULATED', '#,##0', '个', '', 26, '商品指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'item_analysis', 'avg_price_per_unit', '件均价(元)', 'FLOAT', 'STRING', 'INDICATOR', true, false, true, true, 'AVG', '["AVG"]'::jsonb, '[]'::jsonb, 'SUM(sales_amount) / 100.0 / NULLIF(SUM(sales_volume), 0)', 'CALCULATED', '#,##0.00', '元', '', 27, '价格指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, field_code) DO NOTHING;


-- ----------------------------------------------------------
-- 3.3 售后分析 - 字段配置
-- 源表: kuaishou_refund_list
-- 实际列: refundId, oid, itemId, skuId, relSkuId, skuNick, handlingWay,
--         negotiateStatus, refundFee, refundReason, refundReasonDesc,
--         refundDesc, refundType, status, receiptStatus, buyerId, sellerId,
--         logisticsId, relItemId, submitTime, createTime, updateTime,
--         negotiateUpdateTime, endTime, expireTime
-- ----------------------------------------------------------

-- 维度字段
INSERT INTO report_field_config (tenant_id, business_code, report_code, field_code, field_name, field_type, data_type, field_role, is_aggregatable, is_filterable, is_queryable, is_sortable, default_aggregate, valid_aggregates, filter_operators, expression, expression_type, format_pattern, unit, dict_code, sort_order, group_name, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'refund_analysis', 'stat_date', '统计日期', 'DATE', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', '', 'yyyy-MM-dd', '', '', 1, '时间维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_id', '售后单ID', 'BIGINT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 2, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_type', '售后类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'refund_type_dict', 3, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_reason_desc', '售后原因', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 4, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'status', '售后状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'refund_status_dict', 5, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'handling_way', '处理方式', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 6, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'negotiate_status', '协商状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 7, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),

-- 指标字段
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_count', '售后单数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT","SUM"]'::jsonb, '[]'::jsonb, '1', 'CALCULATED', '#,##0', '单', '', 21, '售后指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_fee', '退款金额(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 22, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_fee_yuan', '退款金额(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, 'refund_fee / 100.0', 'CALCULATED', '#,##0.00', '元', '', 23, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'avg_refund_amount', '笔均退款(元)', 'FLOAT', 'STRING', 'INDICATOR', true, false, true, true, 'AVG', '["AVG"]'::jsonb, '[]'::jsonb, 'SUM(refund_fee) / 100.0 / NULLIF(SUM(refund_count), 0)', 'CALCULATED', '#,##0.00', '元', '', 24, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'refund_analysis', 'negotiate_count', '协商中数量', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'SUM', '["SUM"]'::jsonb, '[]'::jsonb, 'CASE WHEN negotiate_status = 1 THEN 1 ELSE 0 END', 'CALCULATED', '#,##0', '单', '', 25, '售后指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, field_code) DO NOTHING;


-- ----------------------------------------------------------
-- 3.4 分销订单分析 - 字段配置
-- 源表: kuaishou_cps_order_list
-- 实际列: oid, distributorId, distributorName, sellerId, status,
--         settlementTime, settlementSuccessTime, refundTime, payTime,
--         expressFee, totalFee, commissionRate, estimatedIncome,
--         createTime, updateTime, platformDpRate, activityUserId,
--         activityUserNickname, investmentPromotionRate,
--         investmentPromotionAmount, buyerOpenId, settlementBizType,
--         promoterServiceInCome, promoterExcitationInCome,
--         investmentServiceInCome, investmentExcitationInCome,
--         orderChannel, activityId, itemId
-- ----------------------------------------------------------

-- 维度字段
INSERT INTO report_field_config (tenant_id, business_code, report_code, field_code, field_name, field_type, data_type, field_role, is_aggregatable, is_filterable, is_queryable, is_sortable, default_aggregate, valid_aggregates, filter_operators, expression, expression_type, format_pattern, unit, dict_code, sort_order, group_name, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'stat_date', '统计日期', 'DATE', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', '', 'yyyy-MM-dd', '', '', 1, '时间维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'oid', '订单ID', 'BIGINT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 2, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'status', '分销单状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'cps_status_dict', 3, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'distributor_name', '分销者', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE"]'::jsonb, '', '', '', '', '', 4, '分销维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'order_channel', '出单渠道', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 5, '渠道维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'settlement_biz_type', '业务类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 6, '业务维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),

-- 指标字段
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'order_count', '订单数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT","SUM"]'::jsonb, '[]'::jsonb, '1', 'CALCULATED', '#,##0', '单', '', 21, '订单指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'total_fee', '实付金额(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 22, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'total_fee_yuan', '实付金额(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, 'total_fee / 100.0', 'CALCULATED', '#,##0.00', '元', '', 23, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'express_fee', '运费(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 24, '金额指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'estimated_income', '分销预估收入(分)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX"]'::jsonb, '["=",">=","<="]'::jsonb, '', 'ORIGINAL', '', '分', '', 25, '收入指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'estimated_income_yuan', '分销预估收入(元)', 'FLOAT', 'STRING', 'INDICATOR', true, true, true, true, 'SUM', '["SUM","AVG","MAX"]'::jsonb, '["=",">=","<="]'::jsonb, 'estimated_income / 100.0', 'CALCULATED', '#,##0.00', '元', '', 26, '收入指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'commission_rate', '分销佣金率(%)', 'BIGINT', 'STRING', 'INDICATOR', true, true, true, true, 'AVG', '["AVG","MAX","MIN"]'::jsonb, '["=",">=","<="]'::jsonb, 'commission_rate / 10.0', 'CALCULATED', '#,##0.0', '%', '', 27, '佣金指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, field_code) DO NOTHING;


-- ----------------------------------------------------------
-- 3.5 代发订单分析 - 字段配置
-- 源表: kuaishou_dropshipping_order_list
-- 实际列: oid, dropshippingOrderCode, payTime, buyerOpenId, buyerNick,
--         sellerOpenId, sellerNick, orderStatus, orderStatusDesc,
--         refundStatus, refundStatusDesc, orderType, orderTypeDesc,
--         deliveryTime, createTime, updateTime, waybillCode,
--         expressCompanyCode, expressCompanyName, factoryCode, factoryName,
--         allocateTime, dropshippingStatus, dropshippingStatusDesc,
--         cancelAllocateTime, cancelAllocateReason, name, mobile,
--         provinceName, cityName, districtName, detailAddress
-- ----------------------------------------------------------

-- 维度字段
INSERT INTO report_field_config (tenant_id, business_code, report_code, field_code, field_name, field_type, data_type, field_role, is_aggregatable, is_filterable, is_queryable, is_sortable, default_aggregate, valid_aggregates, filter_operators, expression, expression_type, format_pattern, unit, dict_code, sort_order, group_name, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'stat_date', '统计日期', 'DATE', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=",">=","<=","BETWEEN"]'::jsonb, '', '', 'yyyy-MM-dd', '', '', 1, '时间维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'dropshipping_order_code', '代发订单编码', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 2, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'order_status', '订单状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', 'dropshipping_status_dict', 3, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'dropshipping_status', '代发状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 4, '代发维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'factory_name', '代发厂家', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE"]'::jsonb, '', '', '', '', '', 5, '厂家维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'express_company_name', '物流公司', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","LIKE"]'::jsonb, '', '', '', '', '', 6, '物流维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'province_name', '省份', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 7, '地域维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'city_name', '城市', 'VARCHAR', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN","LIKE"]'::jsonb, '', '', '', '', '', 8, '地域维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'order_type', '订单类型', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 9, '订单维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'refund_status', '售后状态', 'INT', 'STRING', 'DIMENSION', false, true, true, true, '', '[]'::jsonb, '["=","IN"]'::jsonb, '', '', '', '', '', 10, '售后维度', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),

-- 指标字段
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'order_count', '订单数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT","SUM"]'::jsonb, '[]'::jsonb, '1', 'CALCULATED', '#,##0', '单', '', 21, '订单指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'factory_count', '厂家数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'COUNT', '["COUNT"]'::jsonb, '[]'::jsonb, 'COUNT(DISTINCT factory_code)', 'CALCULATED', '#,##0', '个', '', 22, '厂家指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW()),
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'pending_allocate_count', '待分配数', 'INT', 'STRING', 'INDICATOR', true, false, true, true, 'SUM', '["SUM"]'::jsonb, '[]'::jsonb, 'CASE WHEN dropshipping_status = 1 THEN 1 ELSE 0 END', 'CALCULATED', '#,##0', '单', '', 23, '订单指标', 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, field_code) DO NOTHING;


-- =============================================
-- 4. 抽取配置 (report_extract_config)
-- =============================================

-- ----------------------------------------------------------
-- 4.1 订单分析 - AGGREGATE 聚合抽取
-- 源: kuaishou_order_list → 目标: kuaishou_order_stat
-- 按 createTime 转 stat_date，GROUP BY 维度字段，聚合指标
-- ----------------------------------------------------------
INSERT INTO report_extract_config (tenant_id, business_code, report_code, extract_code, extract_name, source_table_name, source_table_alias, target_table_name, is_enabled, extract_type, extract_mode, extract_key_field, extract_key_format, group_by_fields, filter_expression, join_configs, field_mappings, transform_rules, batch_size, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'order_analysis', 'order_daily_stat', '订单每日统计抽取',
 'kuaishou_order_list', 'o',
 'kuaishou_order_stat', true, 'INCREMENTAL', 'AGGREGATE',
 'updateTime', 'yyyy-MM-dd',
 '["stat_date","oid","status","province","city","district","pay_type","pay_channel","channel","activity_type","buyer_nick","item_title"]'::jsonb,
 '',
 '[]'::jsonb,
 '[
   {"sourceField":"stat_date","targetField":"stat_date","fieldType":"DATE"},
   {"sourceField":"oid","targetField":"oid","fieldType":"BIGINT","defaultValue":0},
   {"sourceField":"status","targetField":"status","fieldType":"INT","defaultValue":0},
   {"sourceField":"province","targetField":"province","fieldType":"VARCHAR"},
   {"sourceField":"city","targetField":"city","fieldType":"VARCHAR"},
   {"sourceField":"district","targetField":"district","fieldType":"VARCHAR"},
   {"sourceField":"payType","targetField":"pay_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"payChannel","targetField":"pay_channel","fieldType":"VARCHAR"},
   {"sourceField":"channel","targetField":"channel","fieldType":"VARCHAR"},
   {"sourceField":"activityType","targetField":"activity_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"buyerNick","targetField":"buyer_nick","fieldType":"VARCHAR"},
   {"sourceField":"itemTitle","targetField":"item_title","fieldType":"VARCHAR"},
   {"sourceField":"oid","targetField":"order_count","fieldType":"INT","aggregateFunction":"COUNT","defaultValue":0},
   {"sourceField":"totalFee","targetField":"total_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"expressFee","targetField":"express_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"discountFee","targetField":"discount_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"originalPrice","targetField":"original_price","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"num","targetField":"item_num","fieldType":"INT","aggregateFunction":"SUM","defaultValue":0}
 ]'::jsonb,
 '[
   {"ruleCode":"ts_to_date","ruleType":"FORMAT","expression":"","format":"yyyy-MM-dd","sourceField":"createTime","targetField":"stat_date"}
 ]'::jsonb,
 500, 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, extract_code) DO NOTHING;


-- ----------------------------------------------------------
-- 4.2 商品分析 - AGGREGATE 聚合抽取
-- 源: kuaishou_item_list → 目标: kuaishou_item_stat
-- ----------------------------------------------------------
INSERT INTO report_extract_config (tenant_id, business_code, report_code, extract_code, extract_name, source_table_name, source_table_alias, target_table_name, is_enabled, extract_type, extract_mode, extract_key_field, extract_key_format, group_by_fields, filter_expression, join_configs, field_mappings, transform_rules, batch_size, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'item_analysis', 'item_daily_stat', '商品每日统计抽取',
 'kuaishou_item_list', 'i',
 'kuaishou_item_stat', true, 'INCREMENTAL', 'AGGREGATE',
 'updateTime', 'yyyy-MM-dd',
 '["stat_date","kwai_item_id","title","category_id","category_name","item_type","shelf_status","audit_status"]'::jsonb,
 '',
 '[]'::jsonb,
 '[
   {"sourceField":"stat_date","targetField":"stat_date","fieldType":"DATE"},
   {"sourceField":"kwaiItemId","targetField":"kwai_item_id","fieldType":"BIGINT","defaultValue":0},
   {"sourceField":"title","targetField":"title","fieldType":"VARCHAR"},
   {"sourceField":"categoryId","targetField":"category_id","fieldType":"INT","defaultValue":0},
   {"sourceField":"categoryName","targetField":"category_name","fieldType":"VARCHAR"},
   {"sourceField":"itemType","targetField":"item_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"shelfStatus","targetField":"shelf_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"auditStatus","targetField":"audit_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"volume","targetField":"sales_volume","fieldType":"INT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"price","targetField":"price","fieldType":"BIGINT","aggregateFunction":"AVG","defaultValue":0},
   {"sourceField":"kwaiItemId","targetField":"sales_amount","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0,
     "transformRule":{"ruleCode":"volume_times_price","ruleType":"CALCULATE","expression":"volume * price"}}
 ]'::jsonb,
 '[
   {"ruleCode":"ts_to_date","ruleType":"FORMAT","expression":"","format":"yyyy-MM-dd","sourceField":"createTime","targetField":"stat_date"}
 ]'::jsonb,
 500, 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, extract_code) DO NOTHING;


-- ----------------------------------------------------------
-- 4.3 售后分析 - AGGREGATE 聚合抽取
-- 源: kuaishou_refund_list → 目标: kuaishou_refund_stat
-- ----------------------------------------------------------
INSERT INTO report_extract_config (tenant_id, business_code, report_code, extract_code, extract_name, source_table_name, source_table_alias, target_table_name, is_enabled, extract_type, extract_mode, extract_key_field, extract_key_format, group_by_fields, filter_expression, join_configs, field_mappings, transform_rules, batch_size, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'refund_analysis', 'refund_daily_stat', '售后每日统计抽取',
 'kuaishou_refund_list', 'r',
 'kuaishou_refund_stat', true, 'INCREMENTAL', 'AGGREGATE',
 'updateTime', 'yyyy-MM-dd',
 '["stat_date","refund_id","refund_type","refund_reason_desc","status","handling_way","negotiate_status"]'::jsonb,
 '',
 '[]'::jsonb,
 '[
   {"sourceField":"stat_date","targetField":"stat_date","fieldType":"DATE"},
   {"sourceField":"refundId","targetField":"refund_id","fieldType":"BIGINT","defaultValue":0},
   {"sourceField":"refundType","targetField":"refund_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"refundReasonDesc","targetField":"refund_reason_desc","fieldType":"VARCHAR"},
   {"sourceField":"status","targetField":"status","fieldType":"INT","defaultValue":0},
   {"sourceField":"handlingWay","targetField":"handling_way","fieldType":"INT","defaultValue":0},
   {"sourceField":"negotiateStatus","targetField":"negotiate_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"refundId","targetField":"refund_count","fieldType":"INT","aggregateFunction":"COUNT","defaultValue":0},
   {"sourceField":"refundFee","targetField":"refund_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0}
 ]'::jsonb,
 '[
   {"ruleCode":"ts_to_date","ruleType":"FORMAT","expression":"","format":"yyyy-MM-dd","sourceField":"createTime","targetField":"stat_date"}
 ]'::jsonb,
 500, 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, extract_code) DO NOTHING;


-- ----------------------------------------------------------
-- 4.4 分销订单分析 - AGGREGATE 聚合抽取
-- 源: kuaishou_cps_order_list → 目标: kuaishou_cps_order_stat
-- ----------------------------------------------------------
INSERT INTO report_extract_config (tenant_id, business_code, report_code, extract_code, extract_name, source_table_name, source_table_alias, target_table_name, is_enabled, extract_type, extract_mode, extract_key_field, extract_key_format, group_by_fields, filter_expression, join_configs, field_mappings, transform_rules, batch_size, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'cps_order_analysis', 'cps_order_daily_stat', '分销订单每日统计抽取',
 'kuaishou_cps_order_list', 'c',
 'kuaishou_cps_order_stat', true, 'INCREMENTAL', 'AGGREGATE',
 'updateTime', 'yyyy-MM-dd',
 '["stat_date","oid","status","distributor_name","order_channel","settlement_biz_type"]'::jsonb,
 '',
 '[]'::jsonb,
 '[
   {"sourceField":"stat_date","targetField":"stat_date","fieldType":"DATE"},
   {"sourceField":"oid","targetField":"oid","fieldType":"BIGINT","defaultValue":0},
   {"sourceField":"status","targetField":"status","fieldType":"INT","defaultValue":0},
   {"sourceField":"distributorName","targetField":"distributor_name","fieldType":"VARCHAR"},
   {"sourceField":"orderChannel","targetField":"order_channel","fieldType":"VARCHAR"},
   {"sourceField":"settlementBizType","targetField":"settlement_biz_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"oid","targetField":"order_count","fieldType":"INT","aggregateFunction":"COUNT","defaultValue":0},
   {"sourceField":"totalFee","targetField":"total_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"expressFee","targetField":"express_fee","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"estimatedIncome","targetField":"estimated_income","fieldType":"BIGINT","aggregateFunction":"SUM","defaultValue":0},
   {"sourceField":"commissionRate","targetField":"commission_rate","fieldType":"BIGINT","aggregateFunction":"AVG","defaultValue":0}
 ]'::jsonb,
 '[
   {"ruleCode":"ts_to_date","ruleType":"FORMAT","expression":"","format":"yyyy-MM-dd","sourceField":"createTime","targetField":"stat_date"}
 ]'::jsonb,
 500, 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, extract_code) DO NOTHING;


-- ----------------------------------------------------------
-- 4.5 代发订单分析 - AGGREGATE 聚合抽取
-- 源: kuaishou_dropshipping_order_list → 目标: kuaishou_dropshipping_stat
-- ----------------------------------------------------------
INSERT INTO report_extract_config (tenant_id, business_code, report_code, extract_code, extract_name, source_table_name, source_table_alias, target_table_name, is_enabled, extract_type, extract_mode, extract_key_field, extract_key_format, group_by_fields, filter_expression, join_configs, field_mappings, transform_rules, batch_size, status, creator, created_at, updater, updated_at) VALUES
(1, 'kuaishou_ecommerce', 'dropshipping_analysis', 'dropshipping_daily_stat', '代发订单每日统计抽取',
 'kuaishou_dropshipping_order_list', 'd',
 'kuaishou_dropshipping_stat', true, 'INCREMENTAL', 'AGGREGATE',
 'updateTime', 'yyyy-MM-dd',
 '["stat_date","dropshipping_order_code","order_status","dropshipping_status","factory_name","express_company_name","province_name","city_name","order_type","refund_status"]'::jsonb,
 '',
 '[]'::jsonb,
 '[
   {"sourceField":"stat_date","targetField":"stat_date","fieldType":"DATE"},
   {"sourceField":"dropshippingOrderCode","targetField":"dropshipping_order_code","fieldType":"VARCHAR"},
   {"sourceField":"orderStatus","targetField":"order_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"dropshippingStatus","targetField":"dropshipping_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"factoryName","targetField":"factory_name","fieldType":"VARCHAR"},
   {"sourceField":"factoryCode","targetField":"factory_code","fieldType":"VARCHAR"},
   {"sourceField":"expressCompanyName","targetField":"express_company_name","fieldType":"VARCHAR"},
   {"sourceField":"provinceName","targetField":"province_name","fieldType":"VARCHAR"},
   {"sourceField":"cityName","targetField":"city_name","fieldType":"VARCHAR"},
   {"sourceField":"orderType","targetField":"order_type","fieldType":"INT","defaultValue":0},
   {"sourceField":"refundStatus","targetField":"refund_status","fieldType":"INT","defaultValue":0},
   {"sourceField":"dropshippingOrderCode","targetField":"order_count","fieldType":"INT","aggregateFunction":"COUNT","defaultValue":0}
 ]'::jsonb,
 '[
   {"ruleCode":"ts_to_date","ruleType":"FORMAT","expression":"","format":"yyyy-MM-dd","sourceField":"createTime","targetField":"stat_date"}
 ]'::jsonb,
 500, 'ACTIVE', 'admin', NOW(), 'admin', NOW())
ON CONFLICT (tenant_id, business_code, report_code, extract_code) DO NOTHING;


-- =============================================
-- 5. 使用说明
-- =============================================
-- 初始化后，按以下步骤使用报表系统：
-- 1. 确保已执行 sql/seed_data_kuaishou.sql 同步源表数据
-- 2. 在管理后台「业务管理」确认快手电商业务已加载
-- 3. 在「报表配置」点击「自动建表」为每个报表创建统计宽表
-- 4. 在「抽取配置」点击「执行抽取」指定 stat_date，从源表聚合数据到宽表
-- 5. 在「数据查询」选择维度/指标/筛选条件进行查询分析
--
-- 关键设计原则：
-- - sourceField 严格对齐 kuaishou_xxx_list 表的实际列名（camelCase）
-- - targetField / field_code 使用 snake_case 保持项目内部命名一致
-- - 金额字段源表存分(BIGINT)，报表提供衍生元字段(CALCULATED expression / 100.0)
-- - 抽取增量字段统一用 updateTime（毫秒时间戳 BIGINT）
-- - AGGREGATE 模式按 GROUP BY 维度聚合，非聚合字段取第一条
