package com.ekexiu.portal.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jfw.apt.orm.annotation.dao.DAO;
import org.jfw.apt.orm.annotation.dao.method.From;
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;

import com.ekexiu.portal.po.Messages;
import com.ekexiu.portal.po.Professor;

@DAO
public abstract class MessagesDao {
	@Insert
	public abstract int insert(Connection con, Messages messages) throws SQLException;
	
	public Messages query(Connection con, String messageId) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT MESSAGE_ID,MESSAGES.OPERATION_ID,MESSAGES.PROFESSOR_ID,"
        		+ " LEAVE_WORD,MESSAGES.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM MESSAGES LEFT JOIN PROFESSOR ON MESSAGES.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE MESSAGE_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,messageId);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                    Messages _result = new Messages();
                    _result.setMessageId(rs.getString(1));
                    _result.setOperationId(rs.getString(2));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(3));
                    _result.setLeaveWord(rs.getString(4));
                    _result.setCreateTime(rs.getString(5));
                    professor.setName(rs.getString(6));
                    _result.setProfessor(professor);
                    return _result;
                }else{
                    return null;
                }
            }finally{
                try{rs.close();}catch(Exception _m_2){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_3){}
        }
    }
	
	public List<Messages> queryOper(Connection con, String operationId) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT MESSAGE_ID,MESSAGES.OPERATION_ID,MESSAGES.PROFESSOR_ID,"
        		+ " LEAVE_WORD,MESSAGES.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM MESSAGES LEFT JOIN PROFESSOR ON MESSAGES.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE MESSAGES.OPERATION_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,operationId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Messages> _result = new ArrayList<Messages>();
                while(rs.next()){
                    Messages _obj =  new Messages();
                    _obj.setMessageId(rs.getString(1));
                    _obj.setOperationId(rs.getString(2));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(3));
                    _obj.setLeaveWord(rs.getString(4));
                    _obj.setCreateTime(rs.getString(5));
                    professor.setName(rs.getString(6));
                    _obj.setProfessor(professor);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_2){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_3){}
        }
    }
	
	public List<Messages> quetyPro(Connection con,String operationId, String professorId) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT MESSAGE_ID,MESSAGES.OPERATION_ID,MESSAGES.PROFESSOR_ID,"
        		+ " LEAVE_WORD,MESSAGES.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM MESSAGES LEFT JOIN PROFESSOR ON MESSAGES.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE MESSAGES.OPERATION_ID = ? AND MESSAGES.PROFESSOR_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
        	ps.setString(_m_1++, operationId);
            ps.setString(_m_1++,professorId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Messages> _result = new ArrayList<Messages>();
                while(rs.next()){
                    Messages _obj =  new Messages();
                    _obj.setMessageId(rs.getString(1));
                    _obj.setOperationId(rs.getString(2));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(3));
                    _obj.setLeaveWord(rs.getString(4));
                    _obj.setCreateTime(rs.getString(5));
                    professor.setName(rs.getString(6));
                    _obj.setProfessor(professor);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_2){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_3){}
        }
    }
	
	@DeleteWith
	@From(Messages.class)
	public abstract int delete(Connection con, String messageId) throws SQLException;
	
}
