package com.tarena.oss.selfservice.dao;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tarena.oss.dms.Detailmonths;
import com.tarena.oss.userservice.pojo.UserService;
import com.tarena.oss.util.PageParameter;

public class UserSelfserviceMgmtByHibernate extends HibernateDaoSupport implements UserSelfserviceMgmtDAO {

	public UserService queryBill() {
		 List<UserService> userself = this.getHibernateTemplate().find("from UserService us ");
		 return userself.get(0);
		
	}
//??
	public Collection<UserService> queryBillByLoginname(String loginname) {
		return this.getHibernateTemplate().find("from UserService us where us.loginname='"+loginname+"'");
	    
		
	}

	public UserService queryBillByUserid(Integer userid) {
		List<UserService> userself = this.getHibernateTemplate().find("from UserService us where 1=1 and us.price.id=" +userid);
		return userself.get(0);
	}

	public Collection<UserService> queryBills() {
		List<UserService> list = this.getHibernateTemplate().find("from UserService us ");
		if(list.size() == 0)
			return null;
		return list;
	}

	public Collection<UserService> queryBills(Integer userid) {
		List<UserService> list = this.getHibernateTemplate().find("from UserService us where 1=1 and us.user.id="+userid);
		return list;
	}

	public Collection<UserService> queryBills(String labip) {
		// TODO Auto-generated method stub
		return null;
	}

	public void queryBill(Integer year, Integer month, Integer day) {
		this.getHibernateTemplate().find("from Detailmonths dm");
		
	}

//	public Detaildays queryDetaildays_intByDay(int year, int month, int day) {
//		return (Detaildays) this.getHibernateTemplate().find("from Detaildays dd ").get(0);
//	}

	public Collection<Detailmonths> queryDetailmonths_intByMonth(final int year,final int month,int currentPage) {
//		return this.getHibernateTemplate().find("from Detailmonths dm where logouttime like '"+year+"-"+month+"%'");
		
		final int startRow = (currentPage - 1) * PageParameter.CNT_EACH_PAGE;
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
				Query qry = arg0.createQuery("from Detailmonths dm where logouttime like '"+year+"-"+month+"%'");
				qry.setFirstResult(startRow);
				qry.setMaxResults(PageParameter.CNT_EACH_PAGE);
				return qry.list();
			}
			
		});
	}

//	public Detailyears queryDetailyears_intByYear(int year) {
//		return (Detailyears) this.getHibernateTemplate().find("from Detailyears dy where 1=1 and year="+year).get(0);
//	}

	public int queryUserServicesCnts() {
		Integer value = (Integer) this.getHibernateTemplate().find("select count(*) from Detailmonths dm").get(0);
		return value;
	}
	public int queryUserServicesCnts(String loginName) {
		Integer value = (Integer) this.getHibernateTemplate().find("select count(*) " +
				"from UserService us where us.loginname='"+loginName+"'").get(0);
		return value;
	}
	public Collection<UserService> queryUserServiceList(String cond,int currentPage) {
		final String qryStr = cond; 
		final int startRow = (currentPage - 1) * PageParameter.CNT_EACH_PAGE;
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
				Query qry = arg0.createQuery("from UserService us where us.loginname='"+qryStr+"'");
				qry.setFirstResult(startRow);
				qry.setMaxResults(PageParameter.CNT_EACH_PAGE);
				return qry.list();
			}
			
		});
	}
	public Collection<Detailmonths> queryDetailmonths_intByMonth(int year, int month) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find("from Detailmonths dm where logouttime like '"+year+"-"+month+"%'");
	}

}
