-- 修正项目目标人数口径
-- 正确口径：银行流水中的身份证号在员工主数据中存在时，按去重身份证号数量回写 ccdi_project.target_count

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 = 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';
