-- 修复项目总人数统计口径：仅按项目流水 cret_no 直接匹配员工主数据后的去重人数回写。
UPDATE ccdi_project project
LEFT JOIN (
    SELECT
        bs.project_id,
        COUNT(DISTINCT TRIM(bs.cret_no)) AS target_count
    FROM ccdi_bank_statement bs
    INNER JOIN ccdi_base_staff staff
        ON staff.id_card = TRIM(bs.cret_no)
    WHERE bs.cret_no IS NOT NULL
      AND TRIM(bs.cret_no) != ''
    GROUP BY bs.project_id
) stats ON stats.project_id = project.project_id
SET project.target_count = COALESCE(stats.target_count, 0),
    project.update_by = 'system',
    project.update_time = NOW()
WHERE project.del_flag = '0';
