The complex chemistry of hot cores in Sgr B2(N): influence of cosmic-ray ionization and thermal history