-- 医疗商城商品分类手工映射模板
-- 用途：批量补齐 public.medical_mall_product_categories 的真实数据
-- 前提：已执行 20260521_medical_mall_categories_v1.sql 或拆分版 schema + RLS + seed

BEGIN;

-- 1. 查看当前未映射商品
SELECT
    p.id,
    p.name,
    p.product_code,
    p.subtitle,
    p.category_id,
    p.status,
    p.created_at
FROM public.ml_products p
LEFT JOIN public.medical_mall_product_categories mmc
    ON mmc.product_id = p.id
   AND mmc.deleted_at IS NULL
WHERE p.status = '1'
  AND mmc.product_id IS NULL
ORDER BY p.created_at DESC;

-- 1.1 按商品名/编码查 UUID
-- 用途：先定位商品 UUID，再回填到下面的手工映射段。
SELECT
    p.id,
    p.product_code,
    p.name,
    p.subtitle,
    p.sale_count,
    p.created_at
FROM public.ml_products p
WHERE p.status = '1'
  AND FALSE
  -- 示例：AND (p.name ILIKE '%血压计%' OR p.product_code = 'BP001')
ORDER BY p.sale_count DESC, p.created_at DESC;

-- 2. 插入主分类映射
-- 建议：每个商品只保留 1 条 is_primary = true
-- 默认使用 WHERE FALSE 保持模板可执行；填入真实数据后，删除 WHERE FALSE 即可。
INSERT INTO public.medical_mall_product_categories (
    product_id,
    category_id,
    is_primary,
    sort_order,
    created_at
)
SELECT *
FROM (
  VALUES
    ('00000000-0000-0000-0000-000000000000'::uuid, 'blood_pressure_monitor', TRUE, 10, NOW())
    -- 示例：, ('商品UUID'::uuid, 'nebulizer', TRUE, 10, NOW())
    -- 示例：, ('商品UUID'::uuid, 'wound_care', TRUE, 20, NOW())
) AS seed_rows(product_id, category_id, is_primary, sort_order, created_at)
WHERE FALSE
ON CONFLICT (product_id, category_id) DO UPDATE SET
    is_primary = EXCLUDED.is_primary,
    sort_order = EXCLUDED.sort_order,
    deleted_at = NULL;

-- 2.1 直接按商品名/编码插入主分类映射
-- 用途：不手填 UUID，直接从 ml_products 查出商品后写入映射。
INSERT INTO public.medical_mall_product_categories (
  product_id,
  category_id,
  is_primary,
  sort_order,
  created_at
)
SELECT
  p.id,
  'blood_pressure_monitor',
  TRUE,
  10,
  NOW()
FROM public.ml_products p
WHERE p.status = '1'
  AND FALSE
  -- 示例：AND (p.name ILIKE '%血压计%' OR p.product_code = 'BP001')
ON CONFLICT (product_id, category_id) DO UPDATE SET
  is_primary = EXCLUDED.is_primary,
  sort_order = EXCLUDED.sort_order,
  deleted_at = NULL;

-- 3. 插入扩展分类映射
-- 默认使用 WHERE FALSE 保持模板可执行；填入真实数据后，删除 WHERE FALSE 即可。
INSERT INTO public.medical_mall_product_categories (
    product_id,
    category_id,
    is_primary,
    sort_order,
    created_at
)
SELECT *
FROM (
  VALUES
    ('00000000-0000-0000-0000-000000000000'::uuid, 'hypertension', FALSE, 30, NOW())
    -- 示例：, ('商品UUID'::uuid, 'elderly_bathroom', FALSE, 30, NOW())
) AS extra_rows(product_id, category_id, is_primary, sort_order, created_at)
WHERE FALSE
ON CONFLICT (product_id, category_id) DO UPDATE SET
    is_primary = EXCLUDED.is_primary,
    sort_order = EXCLUDED.sort_order,
    deleted_at = NULL;

-- 3.1 直接按商品名/编码插入扩展分类映射
INSERT INTO public.medical_mall_product_categories (
  product_id,
  category_id,
  is_primary,
  sort_order,
  created_at
)
SELECT
  p.id,
  'hypertension',
  FALSE,
  30,
  NOW()
FROM public.ml_products p
WHERE p.status = '1'
  AND FALSE
  -- 示例：AND (p.name ILIKE '%电子血压计%' OR p.product_code = 'BP001')
ON CONFLICT (product_id, category_id) DO UPDATE SET
  is_primary = EXCLUDED.is_primary,
  sort_order = EXCLUDED.sort_order,
  deleted_at = NULL;

-- 4. 软删除错误映射
UPDATE public.medical_mall_product_categories
SET deleted_at = NOW()
WHERE deleted_at IS NULL
  AND (
    -- 示例：product_id = '商品UUID' AND category_id = '错误分类ID'
    FALSE
  );

-- 5. 检查是否存在多个主分类
SELECT
    product_id,
    COUNT(*) AS primary_count
FROM public.medical_mall_product_categories
WHERE deleted_at IS NULL
  AND is_primary = TRUE
GROUP BY product_id
HAVING COUNT(*) > 1;

-- 6. 查看分类覆盖情况
SELECT
    c.id AS category_id,
    c.name AS category_name,
    COUNT(mmc.product_id) AS mapped_products
FROM public.medical_mall_categories c
LEFT JOIN public.medical_mall_product_categories mmc
    ON mmc.category_id = c.id
   AND mmc.deleted_at IS NULL
WHERE c.deleted_at IS NULL
GROUP BY c.id, c.name, c.sort_order
ORDER BY c.sort_order ASC, c.id ASC;

-- 7. 查看仍依赖 fallback 风险较高的商品
SELECT
    p.id,
    p.name,
    p.subtitle,
    p.sale_count,
    p.created_at
FROM public.ml_products p
LEFT JOIN public.medical_mall_product_categories mmc
    ON mmc.product_id = p.id
   AND mmc.deleted_at IS NULL
WHERE p.status = '1'
  AND mmc.product_id IS NULL
  AND (
    p.name ILIKE '%血压%' OR
    p.name ILIKE '%血糖%' OR
    p.name ILIKE '%血氧%' OR
    p.name ILIKE '%体温%' OR
    p.name ILIKE '%雾化%' OR
    p.name ILIKE '%制氧%' OR
    p.name ILIKE '%轮椅%' OR
    p.name ILIKE '%拐杖%' OR
    p.name ILIKE '%护理床%' OR
    p.name ILIKE '%感冒%' OR
    p.name ILIKE '%退热%' OR
    p.name ILIKE '%创可贴%' OR
    p.name ILIKE '%纱布%' OR
    p.name ILIKE '%钙片%' OR
    p.name ILIKE '%维生素%' OR
    p.name ILIKE '%蛋白%' OR
    p.name ILIKE '%艾灸%' OR
    p.name ILIKE '%热敷%'
  )
ORDER BY p.sale_count DESC, p.created_at DESC;

COMMIT;