A unified total energy approach to high-temperature superconductivity and long-range ordering in solids