package com.ekexiu.portal.dao;

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

import org.jfw.apt.annotation.Nullable;
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.Or;
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
import org.jfw.apt.orm.annotation.dao.method.Select;
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
import org.jfw.apt.orm.annotation.dao.method.operator.PageSelect;
import org.jfw.apt.orm.annotation.dao.method.operator.QueryOne;
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
import org.jfw.apt.orm.annotation.dao.param.Set;
import org.jfw.apt.orm.annotation.dao.param.SqlColumn;
import org.jfw.apt.orm.core.defaultImpl.StringHandler;
import org.jfw.util.PageQueryResult;

import com.ekexiu.portal.po.Organization;
import com.ekexiu.portal.po.Professor;
import com.ekexiu.portal.pojo.EditProfessor;
import com.ekexiu.portal.pojo.ProfessorInfo;

@DAO
public abstract class ProfessorDao {
	@Insert
	public abstract int insert(Connection con, Professor professor) throws SQLException;

	@Update
	public abstract int update(Connection con, Professor professor) throws SQLException;
	
	@UpdateWith
	@From(Professor.class)
	public abstract int updateStarLevel(Connection con, String id, @Set BigDecimal starLevel) throws SQLException;
	
	@UpdateWith
	@From(Professor.class)
	public abstract int updateConsultCount(Connection con, String id, @Set int consultCount) throws SQLException;

	@UpdateWith
	@From(Professor.class)
	public abstract int updateSubject(Connection con, String id, @Set String subject) throws SQLException;

	@UpdateWith
	@From(Professor.class)
	public abstract int updateDescp(Connection con, String id, @Set String descp) throws SQLException;

	@UpdateWith
	@From(Professor.class)
	public abstract int updateIndustry(Connection con, String id, @Set String industry) throws SQLException;
	
	@UpdateWith
	@From(Professor.class)
	public abstract int updateAddress(Connection con, String id, @Set String address) throws SQLException;
	
	@UpdateWith
	@From(Professor.class)
	public abstract int updateProvince(Connection con, String id, @Set String province) throws SQLException;
	
	@UpdateWith
	@From(Professor.class)
	public abstract int updateStarAvg(Connection con, String id, @Set BigDecimal starAvg) throws SQLException;
	
	/**
	 * 查询专家基本信息接口
	 * @param con
	 * @param id 专家ID
	 * @return 返回专家基本信息
	 * @throws SQLException
	 */
	public EditProfessor queryBaseInfo(Connection con, String id) throws SQLException {
		int _m_1 = 1;
        String sql = "SELECT P.OFFICE,P.DEPARTMENT,P.TITLE,AUTHENTICATION,P.ID,P.NAME,P.ADDRESS,ORGANIZATION.NAME "
        		+ " FROM PROFESSOR P LEFT JOIN ORGANIZATION ON P.ORG_ID = ORGANIZATION.ID WHERE P.ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,id);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                	EditProfessor professor = new EditProfessor();
                    professor.setOffice(rs.getString(1));
                    professor.setDepartment(rs.getString(2));
                    professor.setTitle(rs.getString(3));
                    professor.setAuthentication("1".equals(rs.getString(4)));
                    professor.setId(rs.getString(5));
                    professor.setName(rs.getString(6));
                    professor.setAddress(rs.getString(7));
                    professor.setOrgName(rs.getString(8));
                    return professor;
                }else{
                    return null;
                }
            }finally{
                try{rs.close();}catch(Exception _m_9){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_10){}
        }
	}
	
	/**
	 * 查询专家基本信息+星级+咨询次数的接口
	 * @param con
	 * @param id 专家ID
	 * @return 返回专家基本信息+星级+咨询次数
	 * @throws SQLException
	 */
	public EditProfessor queryEditBaseInfo(Connection con, String id) throws SQLException {
		int _m_1 = 1;
        String sql = "SELECT P.OFFICE,P.DEPARTMENT,P.TITLE,AUTHENTICATION,P.ID,P.NAME,"
        		+ " P.ADDRESS,P.STAR_LEVEL,P.CONSULT_COUNT,ORGANIZATION.NAME "
        		+ " FROM PROFESSOR P LEFT JOIN ORGANIZATION ON P.ORG_ID = ORGANIZATION.ID WHERE P.ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,id);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                	EditProfessor professor = new EditProfessor();
                    professor.setOffice(rs.getString(1));
                    professor.setDepartment(rs.getString(2));
                    professor.setTitle(rs.getString(3));
                    professor.setAuthentication("1".equals(rs.getString(4)));
                    professor.setId(rs.getString(5));
                    professor.setName(rs.getString(6));
                    professor.setAddress(rs.getString(7));
                    BigDecimal starLevel = rs.getBigDecimal(8);
                    if(rs.wasNull()){
                    	starLevel = null;
                    }
                    professor.setStarLevel(starLevel);
                    professor.setConsultCount(rs.getInt(9));
                    professor.setOrgName(rs.getString(10));
                    return professor;
                }else{
                    return null;
                }
            }finally{
                try{rs.close();}catch(Exception _m_9){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_10){}
        }
	}
	
	public Professor query(Connection con, String id) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT P.OFFICE,P.SUBJECT,P.INDUSTRY,P.ADDRESS,P.DEPARTMENT,ORG_ID,P.TITLE,AUTHENTICATION,P.ID,P.NAME,P.DESCP,P.CREATE_TIME,P.MODIFY_TIME,ORGANIZATION.NAME FROM PROFESSOR P LEFT JOIN ORGANIZATION ON P.ORG_ID = ORGANIZATION.ID WHERE P.ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,id);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                    Professor _result = new Professor();
                    String _m_2 = rs.getString(1);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _result.setOffice(_m_2);
                    String _m_3 = rs.getString(2);
                    if(rs.wasNull()){
                        _m_3 = null;
                    }
                    _result.setSubject(_m_3);
                    String _m_4 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_4 = null;
                    }
                    _result.setIndustry(_m_4);
                    String _m_5 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_5 = null;
                    }
                    _result.setAddress(_m_5);
                    String _m_6 = rs.getString(5);
                    if(rs.wasNull()){
                        _m_6 = null;
                    }
                    _result.setDepartment(_m_6);
                    Organization organization = new Organization();
                    organization.setId(rs.getString(6));
                    String _m_7 = rs.getString(7);
                    if(rs.wasNull()){
                        _m_7 = null;
                    }
                    _result.setTitle(_m_7);
                    _result.setAuthentication("1".equals(rs.getString(8)));
                    _result.setId(rs.getString(9));
                    _result.setName(rs.getString(10));
                    String _m_8 = rs.getString(11);
                    if(rs.wasNull()){
                        _m_8 = null;
                    }
                    _result.setDescp(_m_8);
                    _result.setCreateTime(rs.getString(12));
                    _result.setModifyTime(rs.getString(13));
                    organization.setName(rs.getString(14));
                    _result.setOrganization(organization);
                    return _result;
                }else{
                    return null;
                }
            }finally{
                try{rs.close();}catch(Exception _m_9){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_10){}
        }
    }

//	@SelectList
//	public abstract List<Professor> query(Connection con) throws SQLException;
	
	public List<Professor> query(Connection con) throws SQLException{
        String sql = "SELECT OFFICE,SUBJECT,INDUSTRY,ADDRESS,DEPARTMENT,ORG_ID,organization.NAME AS ONAME,"
        		+ " TITLE,AUTHENTICATION,professor.ID,professor.NAME AS PNAME,professor.DESCP,"
        		+ " professor.CREATE_TIME,professor.MODIFY_TIME FROM PROFESSOR "
        		+ " LEFT JOIN ORGANIZATION ON professor.ORG_ID=organization.ID ORDER BY ONAME";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            try{
                List<Professor> _result = new ArrayList<Professor>();
                while(rs.next()){
                    Professor _obj =  new Professor();
                    String _m_1 = rs.getString(1);
                    if(rs.wasNull()){
                        _m_1 = null;
                    }
                    _obj.setOffice(_m_1);
                    String _m_2 = rs.getString(2);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _obj.setSubject(_m_2);
                    String _m_3 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_3 = null;
                    }
                    _obj.setIndustry(_m_3);
                    _obj.setAddress(rs.getString(4));
                    _obj.setDepartment(rs.getString(5));
                    Organization organization = new Organization();
                    organization.setId(rs.getString(6));
                    organization.setName(rs.getString(7));
                    _obj.setOrganization(organization);
                    _obj.setTitle(rs.getString(8));
                    _obj.setAuthentication("1".equals(rs.getString(9)));
                    _obj.setId(rs.getString(10));
                    _obj.setName(rs.getString(11));
                    String _m_4 = rs.getString(12);
                    if(rs.wasNull()){
                        _m_4 = null;
                    }
                    _obj.setDescp(_m_4);
                    _obj.setCreateTime(rs.getString(13));
                    _obj.setModifyTime(rs.getString(14));
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_5){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_6){}
        }
    }
	
	/**
	 * 查询所有专家的咨询星级和咨询完成次数
	 * @param con
	 * @return
	 * @throws SQLException
	 */
	public List<Professor> queryStar(Connection con) throws SQLException{
		String sql = "SELECT ID,STAR_LEVEL,CONSULT_COUNT FROM PROFESSOR ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            try{
                List<Professor> _result = new ArrayList<Professor>();
                while(rs.next()){
                    Professor professor =  new Professor();
                    professor.setId(rs.getString(1));
                    professor.setStarLevel(rs.getBigDecimal(2));
                    professor.setConsultCount(rs.getInt(3));
                    _result.add(professor);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_5){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_6){}
        }
	}

	/**
	 * 查询所有专家咨询星级的最大值
	 * @param con
	 * @return
	 * @throws SQLException
	 */
	public BigDecimal queryBigStar(Connection con) throws SQLException{
		String sql = "SELECT MAX(STAR_LEVEL) FROM PROFESSOR ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            rs.next();
            try{
            	return rs.getBigDecimal(1);
            }finally{
                try{rs.close();}catch(Exception e1){}
            }
        }finally{
            try{ps.close();}catch(Exception e2){}
        }
	}
	
	/**
	 * 查询所有专家咨询星级的最小值
	 * @param con
	 * @return
	 * @throws SQLException
	 */
	public BigDecimal querySmallStar(Connection con) throws SQLException{
		String sql = "SELECT MIN(STAR_LEVEL) FROM PROFESSOR ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            rs.next();
            try{
            	return rs.getBigDecimal(1);
            }finally{
                try{rs.close();}catch(Exception e1){}
            }
        }finally{
            try{ps.close();}catch(Exception e2){}
        }
	}
	
	/**
	 * 查询所有专家咨询次数的最大值
	 * @param con
	 * @return
	 * @throws SQLException
	 */
	public BigDecimal queryBigConsult(Connection con) throws SQLException{
		String sql = "SELECT MAX(CONSULT_COUNT) FROM PROFESSOR ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            rs.next();
            try{
            	return new BigDecimal(rs.getInt(1));
            }finally{
                try{rs.close();}catch(Exception e1){}
            }
        }finally{
            try{ps.close();}catch(Exception e2){}
        }
	}
	
	/**
	 * 查询所有专家咨询次数的最小值
	 * @param con
	 * @return
	 * @throws SQLException
	 */
	public BigDecimal querySmallConsult(Connection con) throws SQLException{
		String sql = "SELECT MIN(CONSULT_COUNT) FROM PROFESSOR ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            rs.next();
            try{
            	return new BigDecimal(rs.getInt(1));
            }finally{
                try{rs.close();}catch(Exception e1){}
            }
        }finally{
            try{ps.close();}catch(Exception e2){}
        }
	}
	
	@Nullable
	@QueryOne
	@From(Professor.class)
	@Select(Professor.class)
	public abstract ProfessorInfo queryInfo(Connection con, String id) throws SQLException;

	@DeleteWith
	@From(Professor.class)
	public abstract int delete(Connection con, String id) throws SQLException;

	@PageSelect
	@Or
	@OrderBy(" ORDER BY ID ")
	public abstract PageQueryResult<Professor> query(Connection con,
			@Nullable @SqlColumn(value = { "ID IN (SELECT PROFESSOR_ID FROM RESEARCH_AREA WHERE CAPTION LIKE ?)",
					"" + "ORG_ID IN(SELECT ID FROM ORGANIZATION WHERE NAME LIKE ?)", "NAME LIKE ?" }, handlerClass = StringHandler.class) String key,
			int pageSize, int pageNo) throws SQLException;

	/**
	 * APP专家搜索列表分页多条件查询
	 * @param con
	 * @param key
	 * @param subject 学术领域
	 * @param industry 应用行业
	 * @param province 所在省份
	 * @param address 所在城市
	 * @param authentication 是否认证
	 * @param pageSize 
	 * @param pageNo
	 * @return 按搜索条件返回专家列表
	 * @throws SQLException
	 */
	public PageQueryResult<EditProfessor> queryAPP(Connection con, @Nullable String key, String subject, String industry, 
			String province, String address, @Nullable Integer authentication, int pageSize, int pageNo) throws SQLException {
		PageQueryResult<EditProfessor> _result = new PageQueryResult<EditProfessor>();
		StringBuilder sql = new StringBuilder();
		boolean hasKey = null != key;
		if (hasKey) {
			sql.append(" WHERE (( PROFESSOR.ID IN (SELECT PROFESSOR_ID FROM RESEARCH_AREA WHERE CAPTION LIKE ?)) "
					+ " OR (ORG_ID IN(SELECT ORGANIZATION.ID FROM ORGANIZATION WHERE ORGANIZATION.NAME LIKE ?)) "
					+ " OR ( PROFESSOR.NAME LIKE ?) OR (SUBJECT LIKE ?) OR (INDUSTRY LIKE ?) OR (PROVINCE LIKE ?) OR (ADDRESS LIKE ?) "
					+ " OR PROFESSOR.ID IN (SELECT PROFESSOR_ID FROM RESOURCE WHERE RESOURCE_NAME LIKE ?))");
		}
		boolean hasSubject = null != subject;
		boolean hasIndustry = null != industry;
		boolean hasProvince = null != province;
		boolean hasAddress = null != address;
		boolean hasAuth = null != authentication;
		if (hasSubject) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(SUBJECT LIKE ?)");
		}
		if (hasIndustry) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(INDUSTRY LIKE ?)");
		}
		if (hasProvince) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(PROVINCE LIKE ?)");
		}
		if (hasAddress) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(ADDRESS LIKE ?)");
		}
		if (hasAuth) {
			if(0 == authentication){
				sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(AUTHENTICATION = '0')");
			}else if(1 == authentication){
				sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(AUTHENTICATION = '1')");
			}
		}
		StringBuilder whereSql = sql;
		sql = new StringBuilder();
		sql.append("SELECT COUNT(1) FROM PROFESSOR");
		if (whereSql.length() > 0) {
			sql.append(whereSql);
		}
		PreparedStatement ps = con.prepareStatement(sql.toString());
		int paramIndex = 1;
		int totalSize = 0;
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasProvince) {
				ps.setString(paramIndex++, province);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			_result.setPageSize(pageSize);
			ResultSet _pageRs = ps.executeQuery();
			try {
				_pageRs.next();
				totalSize = _pageRs.getInt(1);
			} finally {
				try {
					_pageRs.close();
				} catch (Exception _m_6) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception _m_7) {
			}
		}
		_result.setTotal(totalSize);
		if (0 == totalSize) {
			_result.setPageNo(1);
			_result.setData(Collections.<EditProfessor> emptyList());
			return _result;
		}
		paramIndex = 1;
		if (1 == pageNo) {
			_result.setPageNo(1);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,ADDRESS,TITLE,AUTHENTICATION,PROFESSOR.ID,PROFESSOR.NAME,ORG_ID,ORGANIZATION.NAME"
	        		+ " FROM PROFESSOR LEFT JOIN ORGANIZATION ON PROFESSOR.ORG_ID=ORGANIZATION.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY STAR_LEVEL DESC ");
			sql.append(" LIMIT ").append(pageSize);
		} else {
			int _pageSize = totalSize / pageSize;
			if (totalSize % pageSize != 0) {
				++_pageSize;
			}
			if (pageNo > _pageSize) {
				pageNo = _pageSize;
			}
			_result.setPageNo(pageNo);
			--pageNo;
			int _m_10 = (pageNo * pageSize);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,ADDRESS,TITLE,AUTHENTICATION,PROFESSOR.ID,PROFESSOR.NAME,ORG_ID,ORGANIZATION.NAME"
	        		+ " FROM PROFESSOR LEFT JOIN ORGANIZATION ON PROFESSOR.ORG_ID=ORGANIZATION.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY STAR_LEVEL DESC ");
			sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(_m_10);
		}
		ps = con.prepareStatement(sql.toString());
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasProvince) {
				ps.setString(paramIndex++, province);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			java.sql.ResultSet rs = ps.executeQuery();
			try {
				List<EditProfessor> _m_11 = new ArrayList<EditProfessor>();
				_result.setData(_m_11);
				while (rs.next()) {
					EditProfessor professor =  new EditProfessor();
                    String _m_1 = rs.getString(1);
                    if(rs.wasNull()){
                        _m_1 = null;
                    }
                    professor.setOffice(_m_1);
                    professor.setAddress(rs.getString(2));
                    professor.setTitle(rs.getString(3));
                    professor.setAuthentication("1".equals(rs.getString(4)));
                    professor.setId(rs.getString(5));
                    professor.setName(rs.getString(6));
                    professor.setOrgId(rs.getString(7));
                    professor.setOrgName(rs.getString(8));
					_m_11.add(professor);
				}
				return _result;
			} finally {
				try {
					rs.close();
				} catch (Exception e1) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception e2) {
			}
		}
	}
	
	/**
	 * 分页查询专家基础信息，多条件查询专家列表。
	 * @param con
	 * @param key 搜索信息
	 * @param subject 学术领域
	 * @param industry 应用行业
	 * @param address 所在城市
	 * @param pageSize 
	 * @param pageNo
	 * @return 返回专家信息列表
	 * @throws SQLException
	 */
	public PageQueryResult<EditProfessor> queryEditBaseInfo(Connection con, @Nullable String key, String subject, 
			String industry, String address,@Nullable Integer authentication, int pageSize, int pageNo) throws SQLException {
		PageQueryResult<EditProfessor> _result = new PageQueryResult<EditProfessor>();
		StringBuilder sql = new StringBuilder();
		boolean hasKey = null != key;
		if (hasKey) {
			sql.append(" WHERE (( professor.ID IN (SELECT PROFESSOR_ID FROM RESEARCH_AREA WHERE CAPTION LIKE ?)) "
					+ " OR (ORG_ID IN(SELECT organization.ID FROM ORGANIZATION WHERE organization.NAME LIKE ?)) "
					+ " OR ( professor.NAME LIKE ?) OR (professor.SUBJECT LIKE ?) "
					+ " OR (professor.INDUSTRY LIKE ?) OR (professor.ADDRESS LIKE ?))");
		}
		boolean hasSubject = null != subject;
		boolean hasIndustry = null != industry;
		boolean hasAddress = null != address;
		boolean hasAuth = null != authentication;
		if (hasSubject) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(SUBJECT LIKE ?)");
		}
		if (hasIndustry) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(INDUSTRY LIKE ?)");
		}
		if (hasAddress) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(ADDRESS LIKE ?)");
		}
		if (hasAuth) {
			if(0 == authentication){
				sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(AUTHENTICATION = '0')");
			}else if(1 == authentication){
				sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(AUTHENTICATION = '1')");
			}
		}

		StringBuilder whereSql = sql;
		sql = new StringBuilder();
		sql.append("SELECT COUNT(1) FROM PROFESSOR");
		if (whereSql.length() > 0) {
			sql.append(whereSql);
		}
		PreparedStatement ps = con.prepareStatement(sql.toString());
		int paramIndex = 1;
		int totalSize = 0;
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			_result.setPageSize(pageSize);
			ResultSet _pageRs = ps.executeQuery();
			try {
				_pageRs.next();
				totalSize = _pageRs.getInt(1);
			} finally {
				try {
					_pageRs.close();
				} catch (Exception _m_6) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception _m_7) {
			}
		}
		_result.setTotal(totalSize);
		if (0 == totalSize) {
			_result.setPageNo(1);
			_result.setData(Collections.<EditProfessor> emptyList());
			return _result;
		}
		paramIndex = 1;
		if (1 == pageNo) {
			_result.setPageNo(1);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,SUBJECT,INDUSTRY,ADDRESS,DEPARTMENT,organization.NAME AS ONAME,TITLE,"
					+ " AUTHENTICATION,professor.ID,professor.NAME AS PNAME,STAR_LEVEL,CONSULT_COUNT "
					+ " FROM PROFESSOR LEFT JOIN ORGANIZATION ON ORG_ID=organization.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY ONAME ");
			sql.append(" LIMIT ").append(pageSize);
		} else {
			int _pageSize = totalSize / pageSize;
			if (totalSize % pageSize != 0) {
				++_pageSize;
			}
			if (pageNo > _pageSize) {
				pageNo = _pageSize;
			}
			_result.setPageNo(pageNo);
			--pageNo;
			int _m_10 = (pageNo * pageSize);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,SUBJECT,INDUSTRY,ADDRESS,DEPARTMENT,organization.NAME AS ONAME,TITLE,"
					+ " AUTHENTICATION,professor.ID,professor.NAME AS PNAME,STAR_LEVEL,CONSULT_COUNT "
					+ " FROM PROFESSOR LEFT JOIN ORGANIZATION ON ORG_ID=organization.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY ONAME ");
			sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(_m_10);
		}
		ps = con.prepareStatement(sql.toString());
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			java.sql.ResultSet rs = ps.executeQuery();
			try {
				List<EditProfessor> _m_11 = new ArrayList<EditProfessor>();
				_result.setData(_m_11);
				while (rs.next()) {
					EditProfessor professor = new EditProfessor();
					professor.setOffice(rs.getString(1));
					professor.setSubject(rs.getString(2));
					professor.setIndustry(rs.getString(3));
					professor.setAddress(rs.getString(4));
					professor.setDepartment(rs.getString(5));
					professor.setOrgName(rs.getString(6));
					professor.setTitle(rs.getString(7));
					professor.setAuthentication("1".equals(rs.getString(8)));
					professor.setId(rs.getString(9));
					professor.setName(rs.getString(10));
					professor.setStarLevel(rs.getBigDecimal(11));
					professor.setConsultCount(rs.getInt(12));
					_m_11.add(professor);
				}
				return _result;
			} finally {
				try {
					rs.close();
				} catch (Exception _m_17) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception _m_18) {
			}
		}
	}
	
	public PageQueryResult<Professor> query(Connection con, @Nullable String key, String subject, String industry, String address, int pageSize, int pageNo)
			throws SQLException {

		org.jfw.util.PageQueryResult<com.ekexiu.portal.po.Professor> _result = new org.jfw.util.PageQueryResult<com.ekexiu.portal.po.Professor>();

		StringBuilder sql = new StringBuilder();
		boolean hasKey = null != key;
		if (hasKey) {
			sql.append(
					" WHERE (( professor.ID IN (SELECT PROFESSOR_ID FROM RESEARCH_AREA WHERE CAPTION LIKE ?)) OR (ORG_ID IN(SELECT organization.ID FROM ORGANIZATION WHERE organization.NAME LIKE ?)) OR ( professor.NAME LIKE ?) OR (professor.SUBJECT LIKE ?) OR (professor.INDUSTRY LIKE ?) OR (professor.ADDRESS LIKE ?))");
		}
		boolean hasSubject = null != subject;
		boolean hasIndustry = null != industry;
		boolean hasAddress = null != address;
		if (hasSubject) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(SUBJECT LIKE ?)");
		}
		if (hasIndustry) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(INDUSTRY LIKE ?)");
		}
		if (hasAddress) {
			sql.append(sql.length() > 0 ? " AND " : " WHERE ").append("(ADDRESS LIKE ?)");
		}

		StringBuilder whereSql = sql;
		sql = new StringBuilder();
		sql.append("SELECT COUNT(1) FROM PROFESSOR");
		if (whereSql.length() > 0) {
			sql.append(whereSql);
		}
		java.sql.PreparedStatement ps = con.prepareStatement(sql.toString());
		int paramIndex = 1;
		int totalSize = 0;
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			_result.setPageSize(pageSize);
			java.sql.ResultSet _pageRs = ps.executeQuery();
			try {
				_pageRs.next();
				totalSize = _pageRs.getInt(1);
			} finally {
				try {
					_pageRs.close();
				} catch (Exception _m_6) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception _m_7) {
			}
		}
		_result.setTotal(totalSize);
		if (0 == totalSize) {
			_result.setPageNo(1);
			_result.setData(java.util.Collections.<com.ekexiu.portal.po.Professor> emptyList());
			return _result;
		}
		paramIndex = 1;
		if (1 == pageNo) {
			_result.setPageNo(1);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,SUBJECT,INDUSTRY,ADDRESS,DEPARTMENT,ORG_ID,organization.NAME AS ONAME,TITLE,AUTHENTICATION,professor.ID,professor.NAME AS PNAME,professor.DESCP,professor.CREATE_TIME,professor.MODIFY_TIME FROM PROFESSOR LEFT JOIN ORGANIZATION ON professor.ORG_ID=organization.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY ONAME ");
			sql.append(" LIMIT ").append(pageSize);
		} else {
			int _pageSize = totalSize / pageSize;
			if (totalSize % pageSize != 0) {
				++_pageSize;
			}
			if (pageNo > _pageSize) {
				pageNo = _pageSize;
			}
			_result.setPageNo(pageNo);
			--pageNo;
			int _m_10 = (pageNo * pageSize);
			sql = new StringBuilder();
			sql.append("SELECT OFFICE,SUBJECT,INDUSTRY,ADDRESS,DEPARTMENT,ORG_ID,organization.NAME AS ONAME,TITLE,AUTHENTICATION,professor.ID,professor.NAME AS PNAME,professor.DESCP,professor.CREATE_TIME,professor.MODIFY_TIME FROM PROFESSOR LEFT JOIN ORGANIZATION ON professor.ORG_ID=organization.ID ");
			if (whereSql.length() > 0) {
				sql.append(whereSql);
			}
			sql.append(" ORDER BY ONAME ");
			sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(_m_10);
		}
		ps = con.prepareStatement(sql.toString());
		try {
			if (hasKey) {
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
				ps.setString(paramIndex++, key);
			}
			if (hasSubject) {
				ps.setString(paramIndex++, subject);
			}
			if (hasIndustry) {
				ps.setString(paramIndex++, industry);
			}
			if (hasAddress) {
				ps.setString(paramIndex++, address);
			}
			java.sql.ResultSet rs = ps.executeQuery();
			try {
				java.util.List<com.ekexiu.portal.po.Professor> _m_11 = new java.util.ArrayList<com.ekexiu.portal.po.Professor>();
				_result.setData(_m_11);
				while (rs.next()) {
					com.ekexiu.portal.po.Professor _obj = new com.ekexiu.portal.po.Professor();
					java.lang.String _m_13 = rs.getString(1);
					if (rs.wasNull()) {
						_m_13 = null;
					}
					_obj.setOffice(_m_13);
					java.lang.String _m_14 = rs.getString(2);
					if (rs.wasNull()) {
						_m_14 = null;
					}
					_obj.setSubject(_m_14);
					java.lang.String _m_15 = rs.getString(3);
					if (rs.wasNull()) {
						_m_15 = null;
					}
					_obj.setIndustry(_m_15);
					_obj.setAddress(rs.getString(4));
					_obj.setDepartment(rs.getString(5));
					Organization organization = new Organization();
					organization.setId(rs.getString(6));
					organization.setName(rs.getString(7));
					_obj.setOrganization(organization);
					_obj.setTitle(rs.getString(8));
					_obj.setAuthentication("1".equals(rs.getString(9)));
					_obj.setId(rs.getString(10));
					_obj.setName(rs.getString(11));
					java.lang.String _m_16 = rs.getString(12);
					if (rs.wasNull()) {
						_m_16 = null;
					}
					_obj.setDescp(_m_16);
					_obj.setCreateTime(rs.getString(13));
					_obj.setModifyTime(rs.getString(14));
					_m_11.add(_obj);
				}
				return _result;
			} finally {
				try {
					rs.close();
				} catch (Exception _m_17) {
				}
			}
		} finally {
			try {
				ps.close();
			} catch (Exception _m_18) {
			}
		}
	}

}
