Provided herein are a dihydropyrimidine compound and use as a medicament, especially application as a medicament used for treating and preventing hepatitis B. Specifically, provided herein 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. Also provided herein is 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.