-- ============================================
-- 同步 F_BeautyType 字段数据
-- ============================================
-- 说明：从 lq_xmzl 表的 F_BeautyType 字段同步数据到其他相关表
-- 
-- 关联关系：
-- 1. lq_kd_pxmx: 通过 px 字段关联到 lq_xmzl.F_Id
-- 2. lq_kd_jksyj: 通过 F_kdpxid 关联到 lq_kd_pxmx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 3. lq_kd_kjbsyj: 通过 F_kdpxid 关联到 lq_kd_pxmx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 4. lq_hytk_mx: 通过 px 字段关联到 lq_xmzl.F_Id
-- 5. lq_hytk_jksyj: 通过 F_tkpxid 关联到 lq_hytk_mx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 6. lq_hytk_kjbsyj: 通过 F_tkpxid 关联到 lq_hytk_mx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 7. lq_xh_pxmx: 通过 px 字段关联到 lq_xmzl.F_Id
-- 8. lq_xh_jksyj: 通过 F_kdpxid 关联到 lq_xh_pxmx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 9. lq_xh_kjbsyj: 通过 F_hkpxid 关联到 lq_xh_pxmx，或通过 F_ItemId 直接关联到 lq_xmzl
-- 
-- 注意事项：
-- - 优先使用 F_ItemId 直接关联（如果存在且有效）
-- - 如果 F_ItemId 为空，则通过关联表间接关联
-- - 只更新 F_BeautyType 为 NULL 或空字符串的记录

-- ============================================
-- 1. 同步 lq_kd_pxmx（开单品项明细表）
-- ============================================
UPDATE lq_kd_pxmx t1
INNER JOIN lq_xmzl t2 ON t1.px = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 2. 同步 lq_kd_jksyj（开单健康师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_kd_jksyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_kdpxid 关联到 lq_kd_pxmx，再关联到 lq_xmzl
UPDATE lq_kd_jksyj t1
INNER JOIN lq_kd_pxmx t2 ON t1.F_kdpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_kdpxid IS NOT NULL
  AND t1.F_kdpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 3. 同步 lq_kd_kjbsyj（开单科技部老师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_kd_kjbsyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_kdpxid 关联到 lq_kd_pxmx，再关联到 lq_xmzl
UPDATE lq_kd_kjbsyj t1
INNER JOIN lq_kd_pxmx t2 ON t1.F_kdpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_kdpxid IS NOT NULL
  AND t1.F_kdpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 4. 同步 lq_hytk_mx（退卡品项明细表）
-- ============================================
UPDATE lq_hytk_mx t1
INNER JOIN lq_xmzl t2 ON t1.px = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 5. 同步 lq_hytk_jksyj（退卡健康师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_hytk_jksyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_tkpxid 关联到 lq_hytk_mx，再关联到 lq_xmzl
UPDATE lq_hytk_jksyj t1
INNER JOIN lq_hytk_mx t2 ON t1.F_tkpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_tkpxid IS NOT NULL
  AND t1.F_tkpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 6. 同步 lq_hytk_kjbsyj（退卡科技部老师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_hytk_kjbsyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_tkpxid 关联到 lq_hytk_mx，再关联到 lq_xmzl
UPDATE lq_hytk_kjbsyj t1
INNER JOIN lq_hytk_mx t2 ON t1.F_tkpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_tkpxid IS NOT NULL
  AND t1.F_tkpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 7. 同步 lq_xh_pxmx（耗卡品项明细表）
-- ============================================
UPDATE lq_xh_pxmx t1
INNER JOIN lq_xmzl t2 ON t1.px = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 8. 同步 lq_xh_jksyj（耗卡健康师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_xh_jksyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_kdpxid 关联到 lq_xh_pxmx，再关联到 lq_xmzl
UPDATE lq_xh_jksyj t1
INNER JOIN lq_xh_pxmx t2 ON t1.F_kdpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_kdpxid IS NOT NULL
  AND t1.F_kdpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 9. 同步 lq_xh_kjbsyj（耗卡科技部老师业绩表）
-- ============================================
-- 方式1：通过 F_ItemId 直接关联（优先）
UPDATE lq_xh_kjbsyj t1
INNER JOIN lq_xmzl t2 ON t1.F_ItemId = t2.F_Id
SET t1.F_BeautyType = t2.F_BeautyType
WHERE t2.F_BeautyType IS NOT NULL 
  AND t2.F_BeautyType != ''
  AND t1.F_ItemId IS NOT NULL
  AND t1.F_ItemId != ''
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- 方式2：通过 F_hkpxid 关联到 lq_xh_pxmx，再关联到 lq_xmzl
UPDATE lq_xh_kjbsyj t1
INNER JOIN lq_xh_pxmx t2 ON t1.F_hkpxid = t2.F_Id
INNER JOIN lq_xmzl t3 ON t2.px = t3.F_Id
SET t1.F_BeautyType = t3.F_BeautyType
WHERE t3.F_BeautyType IS NOT NULL 
  AND t3.F_BeautyType != ''
  AND t1.F_hkpxid IS NOT NULL
  AND t1.F_hkpxid != ''
  AND (t1.F_ItemId IS NULL OR t1.F_ItemId = '')
  AND (t1.F_BeautyType IS NULL OR t1.F_BeautyType = '');

-- ============================================
-- 10. 验证同步结果
-- ============================================
-- 查看各表同步的数据统计
-- SELECT 
--     'lq_kd_pxmx' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_kd_pxmx
-- UNION ALL
-- SELECT 
--     'lq_kd_jksyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_kd_jksyj
-- UNION ALL
-- SELECT 
--     'lq_kd_kjbsyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_kd_kjbsyj
-- UNION ALL
-- SELECT 
--     'lq_hytk_mx' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_hytk_mx
-- UNION ALL
-- SELECT 
--     'lq_hytk_jksyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_hytk_jksyj
-- UNION ALL
-- SELECT 
--     'lq_hytk_kjbsyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_hytk_kjbsyj
-- UNION ALL
-- SELECT 
--     'lq_xh_pxmx' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_xh_pxmx
-- UNION ALL
-- SELECT 
--     'lq_xh_jksyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_xh_jksyj
-- UNION ALL
-- SELECT 
--     'lq_xh_kjbsyj' AS table_name,
--     COUNT(*) AS total_count,
--     COUNT(F_BeautyType) AS beauty_type_count,
--     COUNT(*) - COUNT(F_BeautyType) AS null_count
-- FROM lq_xh_kjbsyj;

