A generalized Koszul theory and its application