package cn.updev.Events.Event;

import cn.updev.Database.HibernateSessionFactory;
import cn.updev.Events.Static.IEvent;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Date;
import java.util.List;

/**
 * Created by hypo on 15-9-30.
 */
public class EventDAO {

    public EventDAO() {

    }

    public IEvent getEventById(Long eventId){

        Session session = HibernateSessionFactory.currentSession();
        Query query =session.createQuery("from Event e where e.eventId=" + eventId);

        if(query.list().size() == 0){
            return null;
        }
        IEvent rnt = (IEvent) query.list().get(0);
        session.clear();
        session.flush();
        HibernateSessionFactory.closeSession();
        return rnt;
    }

    public List<IEvent> getSingleEventByUserId(Integer userId){

        Session session = HibernateSessionFactory.currentSession();
        Query query =session.createQuery("from Event e where e.doerId=" + userId + " and e.groupId = NULL");
        List<IEvent> list = query.list();

        session.clear();
        session.flush();
        HibernateSessionFactory.closeSession();

        return list;
    }

    public List<IEvent> getEventByUserId(Integer userId){

        Session session = HibernateSessionFactory.currentSession();
        Query query =session.createQuery("from Event e where e.doerId=" + userId);
        List<IEvent> list = query.list();

        session.clear();
        session.flush();
        HibernateSessionFactory.closeSession();

        return list;
    }

    public List<IEvent> getEventByEventGroupId(Integer eventGroupId){

        Session session = HibernateSessionFactory.currentSession();
        Query query =session.createQuery("from Event e where e.groupId=" + eventGroupId);
        List<IEvent> list = query.list();
        session.clear();
        session.flush();
        HibernateSessionFactory.closeSession();

        return list;
    }

    public List<IEvent> getEventByUserGroupId(Integer userGroupId){

        Session session = HibernateSessionFactory.currentSession();
        session.clear();
        session.flush();
        HibernateSessionFactory.closeSession();
        return null;
    }

    public Boolean eventFinish(Long eventId){

        Session session = HibernateSessionFactory.currentSession();
        IEvent event = getEventById(eventId);
        if(event.isFinish()){
            event.setFinishTime(new Date());

            session.update(event);

            session.clear();
            session.flush();
            HibernateSessionFactory.closeSession();

            return true;
        }else {
            event.setFinishTime(null);

            session.update(event);

            session.clear();
            session.flush();
            HibernateSessionFactory.closeSession();

            return false;
        }

    }
}

@@ -644,58 +644,8 @@
 0);%0A
-        session.clear();%0A        session.flush();%0A
     
@@ -1004,58 +1004,8 @@
 );%0A%0A
-        session.clear();%0A        session.flush();%0A
     
@@ -1334,58 +1334,8 @@
 );%0A%0A
-        session.clear();%0A        session.flush();%0A
     
@@ -1682,58 +1682,8 @@
 ();%0A
-        session.clear();%0A        session.flush();%0A
     
@@ -1896,58 +1896,8 @@
 ();%0A
-        session.clear();%0A        session.flush();%0A
     
@@ -1960,32 +1960,1008 @@
 rn null;%0A    %7D%0A%0A
+    public Integer getAllUserEventNum(Integer userId)%7B%0A        Session session = HibernateSessionFactory.currentSession();%0A        String hql = %22select count(*) from Event e where e.ownerId=%22 + userId;%0A        Query query =session.createQuery(hql);%0A        Integer rnt = ((Number)query.uniqueResult()).intValue();%0A%0A        HibernateSessionFactory.closeSession();%0A        return rnt;%0A    %7D%0A    public Integer getUnFinishUserEventNum(Integer userId)%7B%0A%0A        Session session = HibernateSessionFactory.currentSession();%0A        String hql = %22select count(*) from Event e where e.ownerId=%22 + userId +%22 and e.finishTime=null%22;%0A        Query query =session.createQuery(hql);%0A        Integer rnt = ((Number)query.uniqueResult()).intValue();%0A%0A        HibernateSessionFactory.closeSession();%0A        return rnt;%0A    %7D%0A    public Integer getAllTeamEventNum(Integer userId)%7B%0A%0A        return 0;%0A    %7D%0A    public Integer getUnFinishteamEventNum(Integer userId)%7B%0A%0A        return 0;%0A    %7D%0A%0A
     public Boole

update event dao but not finish