-- 回填项目目标人数
-- 口径：按项目已入库流水中去重后的员工身份证号数量回写 ccdi_project.target_count

UPDATE ccdi_project project
LEFT JOIN (
    SELECT
        project_id,
        COUNT(DISTINCT TRIM(cret_no)) AS target_count
    FROM ccdi_bank_statement
    WHERE cret_no IS NOT NULL
      AND TRIM(cret_no) <> ''
    GROUP BY 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';
