A dihydropyrimidine compound and use as a medicament, especially application as a medicament used for treating and preventing hepatitis B, specifically, is a compound having Formula (I) or (Ia), or a stereisomer, a tautomer, an N-oxide, a solvate, a metabolite, a pharmaceutically acceptable salt or a prodrug thereof, wherein the variables of the formulas are as defined in the specification; and also includes the use of the compound having Formula (I) or (Ia), or an enantiomer, a diastereoisomer, a tautomer, a hydrate, a solvate, or a pharmaceutically acceptable salt thereof as a medicament, especially use as a medicament for treating and preventing hepatitis B.