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.Accessory;
import com.ekexiu.portal.po.Professor;


@DAO
public abstract class AccessoryDao {
	@Insert
	public abstract int insert(Connection con, Accessory accessory) throws SQLException;
	
	public List<Accessory> queryOper(Connection con, String operationId) throws SQLException {
        int _m_1 = 1;
        String sql = "SELECT ACCESSORY_ID,ACCESSORY_SRC,ACCESSORY.OPERATION_ID,"
        		+ " ACCESSORY.PROFESSOR_ID,ACCESSORY_NAME,ACCESSORY.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM ACCESSORY LEFT JOIN PROFESSOR ON ACCESSORY.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE ACCESSORY.OPERATION_ID = ? ORDER BY ACCESSORY.CREATE_TIME";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,operationId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Accessory> _result = new ArrayList<Accessory>();
                while(rs.next()){
                    Accessory _obj =  new Accessory();
                    _obj.setAccessoryId(rs.getString(1));
                    _obj.setAccessorySrc(rs.getString(2));
                    _obj.setOperationId(rs.getString(3));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(4));
                    String _m_2 = rs.getString(5);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _obj.setAccessoryName(_m_2);
                    _obj.setCreateTime(rs.getString(6));
                    professor.setName(rs.getString(7));
                    _obj.setProfessor(professor);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_3){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_4){}
        }
    }
	
	public List<Accessory> queryPro(Connection con, String professorId) throws SQLException {
        int _m_1 = 1;
        String sql = "SELECT ACCESSORY_ID,ACCESSORY_SRC,ACCESSORY.OPERATION_ID,"
        		+ " ACCESSORY.PROFESSOR_ID,ACCESSORY_NAME,ACCESSORY.CREATE_TIME,PROFESSOR.NAME "
        		+ " FROM ACCESSORY LEFT JOIN PROFESSOR ON ACCESSORY.PROFESSOR_ID = PROFESSOR.ID "
        		+ " WHERE ACCESSORY.PROFESSOR_ID = ? ORDER BY ACCESSORY.CREATE_TIME";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,professorId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Accessory> _result = new ArrayList<Accessory>();
                while(rs.next()){
                    Accessory _obj =  new Accessory();
                    _obj.setAccessoryId(rs.getString(1));
                    _obj.setAccessorySrc(rs.getString(2));
                    _obj.setOperationId(rs.getString(3));
                    Professor professor = new Professor();
                    professor.setId(rs.getString(4));
                    String _m_2 = rs.getString(5);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _obj.setAccessoryName(_m_2);
                    _obj.setCreateTime(rs.getString(6));
                    professor.setName(rs.getString(7));
                    _obj.setProfessor(professor);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_3){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_4){}
        }
    }
	
	@DeleteWith
	@From(Accessory.class)
	public abstract int delete(Connection con, String accessoryId) throws SQLException;
}
