package com.zhuohua.teacher.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.zhuohua.common.util.Page;
import com.zhuohua.common.util.Result;
import com.zhuohua.teacher.dao.PostHomeworkDao;
import com.zhuohua.teacher.model.ClassAssignments;


@Component("teacherPostHomeworkDao")
public class PostHomeworkDaoImpl implements PostHomeworkDao {
	private SessionFactory sessionFactory;

	@Override
	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	private Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	
	@Override
	public ClassAssignments get(int id) {
		//取到作业  将对应的老师 对应发送的班级显示出来
		List<ClassAssignments> classAssignments = this.getSession()
				.createQuery("from teacherClassAssignments s left outer join fetch s.teacher  left outer join fetch  s.SClass where s.id = :id").setInteger("id", id)
				.list();
				if(classAssignments != null && classAssignments.size() > 0){
					return classAssignments.get(0);
				}
				return null;
	}
	
	@Override
	public boolean save(ClassAssignments classAssignments) {
		Integer i=(Integer) this.getSession().save(classAssignments);
		if(i!=null)
			return true;
		return false;
	}
	@Override
	public void delete(ClassAssignments classAssignments) {
         this.getSession().delete(classAssignments);		
	}
	@Override
	public void update(ClassAssignments classAssignments) {
         this.getSession().update(classAssignments);		
	}
	
	@Override
	public Result findAll(String tno, Page page) {
		Query query=this.getSession().createQuery("from teacherClassAssignments t where t.teacher.tno=:tno");
		query.setString("tno", tno);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		Result result=new Result();
System.out.println(query.list()+"            dao"+query.list().size());
System.out.println(page+"            dao page  page.begin"+page.getBeginIndex());
		result.setList(query.list());
		result.setPage(page);
		return result;
	}
	
	@Override
	public int getAllRowCount(String tno) {
		return this.getSession().createQuery("from teacherClassAssignments t where t.teacher.tno=:tno")
				.setString("tno", tno).
				list().size();
	}
}
