-- 更新pro_mobile_attendance_data表的cfg_id，根据project_id从attendance_cfg中找到默认配置ID
UPDATE pro_mobile_attendance_data pmad
INNER JOIN (
    SELECT ac.project_id, ac.id AS cfg_id 
    FROM attendance_cfg ac 
    WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
SET pmad.cfg_id = ac.cfg_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0;

-- 测试查询：查看更新效果
SELECT pmad.id, pmad.project_id, pmad.cfg_id, ac.id AS new_cfg_id
FROM pro_mobile_attendance_data pmad
LEFT JOIN (
    SELECT ac.project_id, ac.id 
    FROM attendance_cfg ac 
    WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0;

-- 分批更新（可选，适用于大数据量）
UPDATE pro_mobile_attendance_data pmad
INNER JOIN (
    SELECT ac.project_id, ac.id AS cfg_id 
    FROM attendance_cfg ac 
    WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
SET pmad.cfg_id = ac.cfg_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0
LIMIT 1000;