-- 同步2025年12月开单、耗卡、退卡相关数据的科美分类
-- 根据品项信息表(lq_xmzl)的F_BeautyType字段更新各表的F_BeautyType字段

-- ============================================
-- 1. 更新开单品项明细表(lq_kd_pxmx)的F_BeautyType
-- ============================================
UPDATE lq_kd_pxmx pxmx
JOIN lq_kd_kdjlb kd ON pxmx.glkdbh = kd.F_Id
JOIN lq_xmzl xmzl ON pxmx.px = xmzl.F_Id
SET pxmx.F_BeautyType = xmzl.F_BeautyType
WHERE pxmx.F_IsEffective = 1
  AND kd.F_IsEffective = 1
  AND DATE_FORMAT(kd.kdrq, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''
  AND (pxmx.F_BeautyType IS NULL OR pxmx.F_BeautyType != xmzl.F_BeautyType);

-- ============================================
-- 2. 更新开单健康师业绩表(lq_kd_jksyj)的F_BeautyType
-- ============================================
UPDATE lq_kd_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
SET jksyj.F_BeautyType = xmzl.F_BeautyType
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.yjsj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''
  AND (jksyj.F_BeautyType IS NULL OR jksyj.F_BeautyType != xmzl.F_BeautyType);

-- ============================================
-- 3. 更新耗卡健康师业绩表(lq_xh_jksyj)的F_BeautyType
-- ============================================
UPDATE lq_xh_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
SET jksyj.F_BeautyType = xmzl.F_BeautyType
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.yjsj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''
  AND (jksyj.F_BeautyType IS NULL OR jksyj.F_BeautyType != xmzl.F_BeautyType);

-- ============================================
-- 4. 更新退卡健康师业绩表(lq_hytk_jksyj)的F_BeautyType
-- ============================================
UPDATE lq_hytk_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
SET jksyj.F_BeautyType = xmzl.F_BeautyType
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.tksj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''
  AND (jksyj.F_BeautyType IS NULL OR jksyj.F_BeautyType != xmzl.F_BeautyType);

-- ============================================
-- 验证更新结果（可选执行）
-- ============================================
-- 查看开单品项明细表更新统计
SELECT 
    '开单品项明细表' AS table_name,
    COUNT(*) AS total_records,
    SUM(CASE WHEN pxmx.F_BeautyType = xmzl.F_BeautyType THEN 1 ELSE 0 END) AS matched_records,
    SUM(CASE WHEN pxmx.F_BeautyType != xmzl.F_BeautyType THEN 1 ELSE 0 END) AS unmatched_records
FROM lq_kd_pxmx pxmx
JOIN lq_kd_kdjlb kd ON pxmx.glkdbh = kd.F_Id
JOIN lq_xmzl xmzl ON pxmx.px = xmzl.F_Id
WHERE pxmx.F_IsEffective = 1
  AND kd.F_IsEffective = 1
  AND DATE_FORMAT(kd.kdrq, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''

UNION ALL

-- 查看开单健康师业绩表更新统计
SELECT 
    '开单健康师业绩表' AS table_name,
    COUNT(*) AS total_records,
    SUM(CASE WHEN jksyj.F_BeautyType = xmzl.F_BeautyType THEN 1 ELSE 0 END) AS matched_records,
    SUM(CASE WHEN jksyj.F_BeautyType != xmzl.F_BeautyType THEN 1 ELSE 0 END) AS unmatched_records
FROM lq_kd_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.yjsj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''

UNION ALL

-- 查看耗卡健康师业绩表更新统计
SELECT 
    '耗卡健康师业绩表' AS table_name,
    COUNT(*) AS total_records,
    SUM(CASE WHEN jksyj.F_BeautyType = xmzl.F_BeautyType THEN 1 ELSE 0 END) AS matched_records,
    SUM(CASE WHEN jksyj.F_BeautyType != xmzl.F_BeautyType THEN 1 ELSE 0 END) AS unmatched_records
FROM lq_xh_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.yjsj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != ''

UNION ALL

-- 查看退卡健康师业绩表更新统计
SELECT 
    '退卡健康师业绩表' AS table_name,
    COUNT(*) AS total_records,
    SUM(CASE WHEN jksyj.F_BeautyType = xmzl.F_BeautyType THEN 1 ELSE 0 END) AS matched_records,
    SUM(CASE WHEN jksyj.F_BeautyType != xmzl.F_BeautyType THEN 1 ELSE 0 END) AS unmatched_records
FROM lq_hytk_jksyj jksyj
JOIN lq_xmzl xmzl ON jksyj.F_ItemId = xmzl.F_Id
WHERE jksyj.F_IsEffective = 1
  AND DATE_FORMAT(jksyj.tksj, '%Y-%m') = '2025-12'
  AND xmzl.F_BeautyType IS NOT NULL
  AND xmzl.F_BeautyType != '';

