-- 医疗商城首批商品分类映射
-- 用途：为 seed_medical_homepage_data.sql 中的首批医疗商品写入正式分类关联
-- 前提：已执行 20260521_medical_mall_categories_v1.sql

BEGIN;

-- 1. 主分类映射
INSERT INTO public.medical_mall_product_categories (
    product_id,
    category_id,
    is_primary,
    sort_order,
    created_at
)
SELECT p.id, mapping.category_id, TRUE, mapping.sort_order, NOW()
FROM public.ml_products p
JOIN (
    VALUES
        ('家庭退热护理贴', 'cold_fever', 10),
        ('草本暖胃调理贴', 'tcm_patch', 20),
        ('智能血压监测仪', 'blood_pressure_monitor', 30),
        ('静音雾化护理机', 'nebulizer', 40),
        ('高钙维D营养片', 'calcium', 50),
        ('乳清蛋白营养粉', 'protein', 60),
        ('术后支撑恢复带', 'postop_rehab', 70),
        ('无菌伤口护理包', 'wound_dressing', 80),
        ('家用艾灸温灸盒', 'moxibustion', 90),
        ('肩颈草本热敷贴', 'tcm_patch', 100),
        ('折叠助行辅助器', 'wheelchair_crutch', 110),
        ('浴室防滑扶手套装', 'elderly_bathroom', 120)
) AS mapping(product_name, category_id, sort_order)
    ON p.name = mapping.product_name
WHERE p.status = '1'
ON CONFLICT (product_id, category_id) DO UPDATE SET
    is_primary = EXCLUDED.is_primary,
    sort_order = EXCLUDED.sort_order,
    deleted_at = NULL;

-- 2. 扩展分类映射
INSERT INTO public.medical_mall_product_categories (
    product_id,
    category_id,
    is_primary,
    sort_order,
    created_at
)
SELECT p.id, mapping.category_id, FALSE, mapping.sort_order, NOW()
FROM public.ml_products p
JOIN (
    VALUES
        ('家庭退热护理贴', 'child_medicine', 210),
        ('草本暖胃调理贴', 'physiotherapy_hot', 220),
        ('智能血压监测仪', 'hypertension', 230),
        ('静音雾化护理机', 'respiratory_health', 240),
        ('高钙维D营养片', 'vitamin', 250),
        ('乳清蛋白营养粉', 'immune_support', 260),
        ('术后支撑恢复带', 'brace_support', 270),
        ('无菌伤口护理包', 'wound_care', 280),
        ('家用艾灸温灸盒', 'physiotherapy_hot', 290),
        ('肩颈草本热敷贴', 'physiotherapy_hot', 300),
        ('折叠助行辅助器', 'daily_living_aid', 310),
        ('浴室防滑扶手套装', 'anti_slip', 320)
) AS mapping(product_name, category_id, sort_order)
    ON p.name = mapping.product_name
WHERE p.status = '1'
ON CONFLICT (product_id, category_id) DO UPDATE SET
    is_primary = EXCLUDED.is_primary,
    sort_order = EXCLUDED.sort_order,
    deleted_at = NULL;

-- 3. 清理这些商品可能存在的重复主分类
WITH duplicate_primary AS (
    SELECT
        mmc.id,
        ROW_NUMBER() OVER (
            PARTITION BY mmc.product_id
            ORDER BY mmc.sort_order ASC, mmc.created_at ASC, mmc.id ASC
        ) AS row_num
    FROM public.medical_mall_product_categories mmc
    JOIN public.ml_products p ON p.id = mmc.product_id
    WHERE mmc.deleted_at IS NULL
      AND mmc.is_primary = TRUE
      AND p.name IN (
          '家庭退热护理贴',
          '草本暖胃调理贴',
          '智能血压监测仪',
          '静音雾化护理机',
          '高钙维D营养片',
          '乳清蛋白营养粉',
          '术后支撑恢复带',
          '无菌伤口护理包',
          '家用艾灸温灸盒',
          '肩颈草本热敷贴',
          '折叠助行辅助器',
          '浴室防滑扶手套装'
      )
)
UPDATE public.medical_mall_product_categories mmc
SET deleted_at = NOW()
FROM duplicate_primary dp
WHERE mmc.id = dp.id
  AND dp.row_num > 1;

-- 4. 校验结果
SELECT
    p.product_code,
    p.name,
    STRING_AGG(mmc.category_id || CASE WHEN mmc.is_primary THEN ' [P]' ELSE '' END, ', ' ORDER BY mmc.is_primary DESC, mmc.sort_order ASC) AS mapped_categories
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.name IN (
    '家庭退热护理贴',
    '草本暖胃调理贴',
    '智能血压监测仪',
    '静音雾化护理机',
    '高钙维D营养片',
    '乳清蛋白营养粉',
    '术后支撑恢复带',
    '无菌伤口护理包',
    '家用艾灸温灸盒',
    '肩颈草本热敷贴',
    '折叠助行辅助器',
    '浴室防滑扶手套装'
)
GROUP BY p.product_code, p.name
ORDER BY p.product_code ASC;

COMMIT;