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.Collections;
import java.util.List;

import org.jfw.apt.annotation.Autowrie;
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 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.util.PageQueryResult;

import com.ekexiu.portal.po.Image;
import com.ekexiu.portal.po.Professor;
import com.ekexiu.portal.po.Resource;
import com.ekexiu.portal.pojo.EditProfessor;

@DAO
public abstract class ResourceDao {
	@Autowrie
	private ImageDao imageDao;
	@Autowrie
	private ProfessorDao professorDao;
	
	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public ProfessorDao getProfessorDao() {
		return professorDao;
	}

	public void setProfessorDao(ProfessorDao professorDao) {
		this.professorDao = professorDao;
	}

	@Insert
	public abstract int insert(Connection con,Resource resource) throws SQLException;
	
	@Update
	public abstract int update(Connection con,Resource resource) throws SQLException;
	
	@UpdateWith
	@From(Resource.class)
	public abstract int updateSubject(Connection con, String resourceId, @Set String subject) throws SQLException;

	@UpdateWith
	@From(Resource.class)
	public abstract int updateIndustry(Connection con, String resourceId, @Set String industry) throws SQLException;
	
	@UpdateWith
	@From(Resource.class)
	public abstract int updateDescp(Connection con, String resourceId, @Set String descp) throws SQLException;
	
	@UpdateWith
	@From(Resource.class)
	public abstract int updateHopePayMethod(Connection con, String resourceId, @Set String hopePayMethod) throws SQLException;
	
	@UpdateWith
	@From(Resource.class)
	public abstract int updateCooperationNotes(Connection con,String resourceId,@Set String cooperationNotes) throws SQLException;
	
	public Resource query(Connection con,String resourceId) throws SQLException{
        List<Image> images = this.imageDao.queryRes(con, resourceId);
		int _m_1 = 1;
        String sql = "SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,"
        		+ " SUPPORTED_SERVICES,RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,"
        		+ " COOPERATION_NOTES,RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME "
        		+ " FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID WHERE RESOURCE_ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,resourceId);
            ResultSet rs = ps.executeQuery();
            try{
                if(rs.next()){
                    Resource _result = new Resource();
                    _result.setResourceId(rs.getString(1));
                    _result.setResourceName(rs.getString(2));
                    String _m_2 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _result.setSubject(_m_2);
                    String _m_3 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_3 = null;
                    }
                    _result.setIndustry(_m_3);
                    _result.setSupportedServices(rs.getString(5));
                    String _m_4 = rs.getString(6);
                    if(rs.wasNull()){
                        _m_4 = null;
                    }
                    _result.setDescp(_m_4);
                    Professor professor = new Professor();
                    professor.setId(rs.getString(7));
                    _result.setAscription(rs.getString(8));
                    _result.setHopePayMethod(rs.getString(9));
                    String _m_5 = rs.getString(10);
                    if(rs.wasNull()){
                        _m_5 = null;
                    }
                    _result.setCooperationNotes(_m_5);
                    _result.setCreateTime(rs.getString(11));
                    _result.setModifyTime(rs.getString(12));
                    professor.setName(rs.getString(13));
                    _result.setProfessor(professor);
                    _result.setImages(images);
                    return _result;
                }else{
                    return null;
                }
            }finally{
                try{rs.close();}catch(Exception _m_6){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_7){}
        }
    }
	
	public PageQueryResult<Resource> queryPageAll(Connection con,int pageSize,int pageNo) throws SQLException{
        int _m_1 = 0;
        PageQueryResult<Resource> _result = new PageQueryResult<Resource>();
        String sql = null;
        sql = " SELECT COUNT(1) FROM RESOURCE ";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            _result.setPageSize(pageSize);
            ResultSet _pageRs = ps.executeQuery();
            try{
                _pageRs.next();
                _m_1 = _pageRs.getInt(1);
            }finally{
                try{_pageRs.close();}catch(Exception _m_3){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_4){}
        }
        _result.setTotal(_m_1);
        if(0== _m_1){
            _result.setPageNo(1);
            _result.setData(Collections.<Resource>emptyList());
            return _result;
        }
        boolean _m_5 = (1 == pageNo);
        if(_m_5){
            _result.setPageNo(1);
            sql = " SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,"
            		+ " SUPPORTED_SERVICES,RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,"
            		+ " COOPERATION_NOTES,RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME "
            		+ " FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID ";
            sql = sql + " ORDER BY RESOURCE_NAME ";
            sql = sql + " LIMIT " + pageSize;
        }else{
            int _m_6 = _m_1 / pageSize;
            if(_m_1 % pageSize != 0){
                ++_m_6;
            }
            if(pageNo > _m_6){
                pageNo = _m_6;
            }
            _result.setPageNo(pageNo);
            --pageNo;
            int _m_7 = (pageNo * pageSize);
            sql = " SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,"
            		+ " SUPPORTED_SERVICES,RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,"
            		+ " COOPERATION_NOTES,RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME "
            		+ " FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID ";
            sql = sql + " ORDER BY RESOURCE_NAME ";
            sql = sql + " LIMIT " + pageSize+ " OFFSET "+_m_7;
        }
        ps = con.prepareStatement(sql);
        try{
            ResultSet rs = ps.executeQuery();
            try{
                List<Resource> _m_8 = new ArrayList<Resource>();
                _result.setData(_m_8);
                int _m_9 = 0;
                while((_m_9<pageSize) && rs.next()){
                    ++_m_9;
                    Resource _obj =  new Resource();
                    _obj.setResourceId(rs.getString(1));
                    _obj.setResourceName(rs.getString(2));
                    String _m_10 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_10 = null;
                    }
                    _obj.setSubject(_m_10);
                    String _m_11 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_11 = null;
                    }
                    _obj.setIndustry(_m_11);
                    _obj.setSupportedServices(rs.getString(5));
                    String _m_12 = rs.getString(6);
                    if(rs.wasNull()){
                        _m_12 = null;
                    }
                    _obj.setDescp(_m_12);
                    Professor professor = new Professor();
                    professor.setId(rs.getString(7));
                    String _m_13 = rs.getString(8);
                    if(rs.wasNull()){
                        _m_13 = null;
                    }
                    _obj.setAscription(_m_13);
                    String _m_14 = rs.getString(9);
                    if(rs.wasNull()){
                        _m_14 = null;
                    }
                    _obj.setHopePayMethod(_m_14);
                    String _m_15 = rs.getString(10);
                    if(rs.wasNull()){
                        _m_15 = null;
                    }
                    _obj.setCooperationNotes(_m_15);
                    _obj.setCreateTime(rs.getString(11));
                    _obj.setModifyTime(rs.getString(12));
                    professor.setName(rs.getString(13));
                    _obj.setProfessor(professor);
                    List<Image> images = this.imageDao.queryRes(con, _obj.getResourceId());
                    _obj.setImages(images);
                    _m_8.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_16){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_17){}
        }
    }
	
//	public List<Resource> query(Connection con) throws SQLException{
//        String sql = "SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,SUPPORTED_SERVICES,RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID ORDER BY RESOURCE_NAME ";
//        PreparedStatement ps = con.prepareStatement(sql);
//        try{
//            ResultSet rs = ps.executeQuery();
//            try{
//                List<Resource> _result = new ArrayList<Resource>();
//                while(rs.next()){
//                    Resource _obj =  new Resource();
//                    _obj.setResourceId(rs.getString(1));
//                    _obj.setResourceName(rs.getString(2));
//                    String _m_1 = rs.getString(3);
//                    if(rs.wasNull()){
//                        _m_1 = null;
//                    }
//                    _obj.setSubject(_m_1);
//                    String _m_2 = rs.getString(4);
//                    if(rs.wasNull()){
//                        _m_2 = null;
//                    }
//                    _obj.setIndustry(_m_2);
//                    _obj.setSupportedServices(rs.getString(5));
//                    String _m_3 = rs.getString(6);
//                    if(rs.wasNull()){
//                        _m_3 = null;
//                    }
//                    _obj.setDescp(_m_3);
//                    Professor professor = new Professor();
//                    professor.setId(rs.getString(7));
//                    _obj.setAscription(rs.getString(8));
//                    _obj.setHopePayMethod(rs.getString(9));
//                    String _m_4 = rs.getString(10);
//                    if(rs.wasNull()){
//                        _m_4 = null;
//                    }
//                    _obj.setCooperationNotes(_m_4);
//                    _obj.setCreateTime(rs.getString(11));
//                    _obj.setModifyTime(rs.getString(12));
//                    professor.setName(rs.getString(13));
//                    _obj.setProfessor(professor);
//                    List<Image> images = this.imageDao.queryRes(con, _obj.getResourceId());
//                    _obj.setImages(images);
//                    _result.add(_obj);
//                }
//                return _result;
//            }finally{
//                try{rs.close();}catch(Exception _m_5){}
//            }
//        }finally{
//            try{ps.close();}catch(Exception _m_6){}
//        }
//    }
	
	/**
	 * APP涓撳鎼滅储鍒楄〃閲屾樉绀轰笓瀹剁殑鎵€鏈夎祫婧愶紙鍙渶瑕佽祫婧怚D鍜屽悕绉帮級
	 * @param con 
	 * @param professorId 涓撳ID
	 * @return 杩斿洖褰撳墠涓撳鍙戝竷鐨勬墍鏈夎祫婧愶紝骞舵寜鎺掑簭
	 * @throws SQLException
	 */
	public List<Resource> queryList(Connection con,String professorId) throws SQLException{
        String sql = "SELECT RESOURCE_ID,RESOURCE_NAME FROM RESOURCE WHERE PROFESSOR_ID = ? ORDER BY CREATE_TIME DESC";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(1,professorId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Resource> resources = new ArrayList<Resource>();
                while(rs.next()){
                    Resource resource =  new Resource();
                    resource.setResourceId(rs.getString(1));
                    resource.setResourceName(rs.getString(2));
                    resources.add(resource);
                }
                return resources;
            }finally{
                try{rs.close();}catch(Exception e){}
            }
        }finally{
            try{ps.close();}catch(Exception e){}
        }
    }
	
	/**
	 * 鏌ヨ褰撳墠鐧诲綍涓撳鍙戝竷鐨勬墍鏈夎祫婧?
	 * @param con
	 * @param professorId 涓撳ID
	 * @return 杩斿洖褰撳墠涓撳鍙戝竷鐨勬墍鏈夎祫婧愰泦鍚?
	 * @throws SQLException
	 */
	public List<Resource> queryPro(Connection con,String professorId) throws SQLException{
        int _m_1 = 1;
        String sql = "SELECT RESOURCE_ID,RESOURCE_NAME,SUBJECT,INDUSTRY,SUPPORTED_SERVICES,DESCP,"
        		+ " PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,CREATE_TIME,MODIFY_TIME "
        		+ " FROM RESOURCE WHERE PROFESSOR_ID = ? ORDER BY CREATE_TIME DESC";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
            ps.setString(_m_1++,professorId);
            ResultSet rs = ps.executeQuery();
            try{
                List<Resource> _result = new ArrayList<Resource>();
                while(rs.next()){
                    Resource _obj =  new Resource();
                    _obj.setResourceId(rs.getString(1));
                    _obj.setResourceName(rs.getString(2));
                    String _m_2 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_2 = null;
                    }
                    _obj.setSubject(_m_2);
                    String _m_3 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_3 = null;
                    }
                    _obj.setIndustry(_m_3);
                    _obj.setSupportedServices(rs.getString(5));
                    String _m_4 = rs.getString(6);
                    if(rs.wasNull()){
                        _m_4 = null;
                    }
                    _obj.setDescp(_m_4);
                    _obj.setProfessorId(rs.getString(7));
                    String _m_5 = rs.getString(8);
                    if(rs.wasNull()){
                        _m_5 = null;
                    }
                    _obj.setAscription(_m_5);
                    String _m_6 = rs.getString(9);
                    if(rs.wasNull()){
                        _m_6 = null;
                    }
                    _obj.setHopePayMethod(_m_6);
                    String _m_7 = rs.getString(10);
                    if(rs.wasNull()){
                        _m_7 = null;
                    }
                    _obj.setCooperationNotes(_m_7);
                    _obj.setCreateTime(rs.getString(11));
                    _obj.setModifyTime(rs.getString(12));
                    List<Image> images = this.imageDao.queryRes(con, _obj.getResourceId());
                    _obj.setImages(images);
                    _result.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_8){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_9){}
        }
    }
	
	@DeleteWith
	@From(Resource.class)
	public abstract int delete(Connection con, String resourceId) throws SQLException;
	
	/**
	 * 鍒嗛〉鏌ユ壘鎵€鏈夎祫婧?闄勫甫璧勬簮鎵€鏈夎€呭熀鏈俊鎭?锛屾寜璧勬簮鍚嶇О鎺掑簭
	 * @param key 鏌ヨ鍐呭锛堟悳绱㈡鎼滅储鍐呭-妯＄硦鏌ヨ锛?
	 * @param subject 瀛︽湳棰嗗煙
	 * @param industry 鐮旂┒鏂瑰悜
	 * @param address 涓撳鎵€鍦ㄥ湴
	 * @param pageSize 
	 * @param pageNo
	 * @return 杩斿洖瑕佹煡鎵剧殑鎵€鏈夎祫婧?闄勫甫璧勬簮鎵€鏈夎€呭熀鏈俊鎭?
	 * @throws SQLException
	 */
	public PageQueryResult<Resource> queryPageRes(Connection con,String key,String subject,String industry,String address,int pageSize,int pageNo) throws SQLException{
        int _m_1 = 0;
        PageQueryResult<Resource> _result = new PageQueryResult<Resource>();
        int _m_3 = 1;
        boolean _m_2 = null == key;
        boolean _m_4 = null == subject;
        boolean _m_5 = null == industry;
        boolean _m6 = null == address;
        StringBuilder sql = new StringBuilder();
        boolean _m_7 = true;
        if(!_m_2){
            _m_7 =  false;
            sql.append(" WHERE ((RESOURCE_NAME LIKE ?) OR (SUPPORTED_SERVICES LIKE ?) "
            		+ " OR (RESOURCE.SUBJECT LIKE ?) OR (RESOURCE.INDUSTRY LIKE ?) "
            		+ " OR (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?))) ");
        }
        if(!_m_4){
            if(_m_7){
                _m_7 =  false;
                sql.append(" WHERE (RESOURCE.SUBJECT LIKE ?) ");
            }else{
                sql.append(" AND (RESOURCE.SUBJECT LIKE ?) ");
            }
        }
        if(!_m_5){
            if(_m_7){
                _m_7 =  false;
                sql.append(" WHERE (RESOURCE.INDUSTRY LIKE ?) ");
            }else{
                sql.append(" AND (RESOURCE.INDUSTRY LIKE ?) ");
            }
        }
        if(!_m6){
        	if(_m_7){
        		_m_7 =  false;
        		sql.append(" WHERE (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?)) ");
        	}else{
        		sql.append(" AND (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?)) ");
        	}
        }
        StringBuilder _m_6 = sql;
        sql = new StringBuilder();
        sql.append(" SELECT COUNT(1) FROM RESOURCE ");
        if(_m_6.length()>0){
            sql.append(_m_6);
        }
        PreparedStatement ps = con.prepareStatement(sql.toString());
        try{
            if(!_m_2){
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
            }
            if(!_m_4){
                ps.setString(_m_3++,subject);
            }
            if(!_m_5){
                ps.setString(_m_3++,industry);
            }
            if(!_m6){
            	ps.setString(_m_3++,address);
            }
            _result.setPageSize(pageSize);
            ResultSet _pageRs = ps.executeQuery();
            try{
                _pageRs.next();
                _m_1 = _pageRs.getInt(1);
            }finally{
                try{_pageRs.close();}catch(Exception _m_8){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_9){}
        }
        _result.setTotal(_m_1);
        if(0== _m_1){
            _result.setPageNo(1);
            _result.setData(Collections.<Resource>emptyList());
            return _result;
        }
        _m_3 = 1;
        boolean _m_10 = (1 == pageNo);
        if(_m_10){
            _result.setPageNo(1);
            sql = new StringBuilder();
            sql.append("SELECT RESOURCE_ID,RESOURCE_NAME,SUBJECT,INDUSTRY,SUPPORTED_SERVICES,DESCP,PROFESSOR_ID,"
            		+ " ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,CREATE_TIME,MODIFY_TIME FROM RESOURCE ");
            if(_m_6.length()>0){
                sql.append(_m_6);
            }
            sql.append(" ORDER BY RESOURCE_NAME");
            sql.append(" LIMIT ").append(pageSize);
        }else{
            int _m_11 = _m_1 / pageSize;
            if(_m_1 % pageSize != 0){
                ++_m_11;
            }
            if(pageNo > _m_11){
                pageNo = _m_11;
            }
            _result.setPageNo(pageNo);
            --pageNo;
            int _m_12 = (pageNo * pageSize);
            sql = new StringBuilder();
            sql.append("SELECT RESOURCE_ID,RESOURCE_NAME,SUBJECT,INDUSTRY,SUPPORTED_SERVICES,DESCP,PROFESSOR_ID,"
            		+ " ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,CREATE_TIME,MODIFY_TIME FROM RESOURCE ");
            if(_m_6.length()>0){
                sql.append(_m_6);
            }
            sql.append(" ORDER BY RESOURCE_NAME");
            sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(_m_12);
        }
        ps = con.prepareStatement(sql.toString());
        try{
            if(!_m_2){
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
            }
            if(!_m_4){
                ps.setString(_m_3++,subject);
            }
            if(!_m_5){
                ps.setString(_m_3++,industry);
            }
            if(!_m6){
            	ps.setString(_m_3++,address);
            }
            ResultSet rs = ps.executeQuery();
            try{
                List<Resource> _m_13 = new ArrayList<Resource>();
                _result.setData(_m_13);
                int _m_14 = 0;
                while((_m_14<pageSize) && rs.next()){
                    ++_m_14;
                    Resource _obj =  new Resource();
                    _obj.setResourceId(rs.getString(1));
                    _obj.setResourceName(rs.getString(2));
                    String _m_15 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_15 = null;
                    }
                    _obj.setSubject(_m_15);
                    String _m_16 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_16 = null;
                    }
                    _obj.setIndustry(_m_16);
                    _obj.setSupportedServices(rs.getString(5));
                    String _m_17 = rs.getString(6);
                    if(rs.wasNull()){
                        _m_17 = null;
                    }
                    _obj.setDescp(_m_17);
                    _obj.setProfessorId(rs.getString(7));
                    _obj.setAscription(rs.getString(8));
                    _obj.setHopePayMethod(rs.getString(9));
                    String _m_18 = rs.getString(10);
                    if(rs.wasNull()){
                        _m_18 = null;
                    }
                    _obj.setCooperationNotes(_m_18);
                    _obj.setCreateTime(rs.getString(11));
                    _obj.setModifyTime(rs.getString(12));
                    List<Image> images = this.imageDao.queryRes(con, _obj.getResourceId());
                    _obj.setImages(images);
                    EditProfessor professor = this.professorDao.queryBaseInfo(con, _obj.getProfessorId());
                    _obj.setProfessor(professor);
                    _m_13.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_19){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_20){}
        }
    }
	
	/**
	 * 鍒嗛〉鏌ユ壘鎵€鏈夎祫婧愶紝鎸夎祫婧愬悕绉版帓搴?
	 * @param key 鏌ヨ鍐呭锛堟悳绱㈡鎼滅储鍐呭-妯＄硦鏌ヨ锛?
	 * @param subject 瀛︽湳棰嗗煙
	 * @param industry 鐮旂┒鏂瑰悜
	 * @param address 涓撳鎵€鍦ㄥ湴
	 * @param pageSize 
	 * @param pageNo
	 * @return
	 * @throws SQLException
	 */
	public PageQueryResult<Resource> queryPage(Connection con,String key,String subject,String industry,String address,int pageSize,int pageNo) throws SQLException{
        int _m_1 = 0;
        PageQueryResult<Resource> _result = new PageQueryResult<Resource>();
        int _m_3 = 1;
        boolean _m_2 = null == key;
        boolean _m_4 = null == subject;
        boolean _m_5 = null == industry;
        boolean _m6 = null == address;
        StringBuilder sql = new StringBuilder();
        boolean _m_7 = true;
        if(!_m_2){
            _m_7 =  false;
            sql.append(" WHERE ((RESOURCE_NAME LIKE ?) OR (SUPPORTED_SERVICES LIKE ?) "
            		+ " OR (RESOURCE.SUBJECT LIKE ?) OR (RESOURCE.INDUSTRY LIKE ?) "
            		+ " OR (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?))) ");
        }
        if(!_m_4){
            if(_m_7){
                _m_7 =  false;
                sql.append(" WHERE (RESOURCE.SUBJECT LIKE ?) ");
            }else{
                sql.append(" AND (RESOURCE.SUBJECT LIKE ?) ");
            }
        }
        if(!_m_5){
            if(_m_7){
                _m_7 =  false;
                sql.append(" WHERE (RESOURCE.INDUSTRY LIKE ?) ");
            }else{
                sql.append(" AND (RESOURCE.INDUSTRY LIKE ?) ");
            }
        }
        if(!_m6){
        	if(_m_7){
        		_m_7 =  false;
        		sql.append(" WHERE (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?)) ");
        	}else{
        		sql.append(" AND (RESOURCE.PROFESSOR_ID IN (SELECT PROFESSOR.ID FROM PROFESSOR WHERE PROFESSOR.ADDRESS LIKE ?)) ");
        	}
        }
        StringBuilder _m_6 = sql;
        sql = new StringBuilder();
        sql.append(" SELECT COUNT(1) FROM RESOURCE ");
        if(_m_6.length()>0){
            sql.append(_m_6);
        }
        PreparedStatement ps = con.prepareStatement(sql.toString());
        try{
            if(!_m_2){
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
            }
            if(!_m_4){
                ps.setString(_m_3++,subject);
            }
            if(!_m_5){
                ps.setString(_m_3++,industry);
            }
            if(!_m6){
            	ps.setString(_m_3++,address);
            }
            _result.setPageSize(pageSize);
            ResultSet _pageRs = ps.executeQuery();
            try{
                _pageRs.next();
                _m_1 = _pageRs.getInt(1);
            }finally{
                try{_pageRs.close();}catch(Exception _m_8){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_9){}
        }
        _result.setTotal(_m_1);
        if(0== _m_1){
            _result.setPageNo(1);
            _result.setData(Collections.<Resource>emptyList());
            return _result;
        }
        _m_3 = 1;
        boolean _m_10 = (1 == pageNo);
        if(_m_10){
            _result.setPageNo(1);
            sql = new StringBuilder();
            sql.append("SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,SUPPORTED_SERVICES,"
            		+ " RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,"
            		+ " RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME,PROFESSOR.TITLE,"
            		+ " PROFESSOR.DEPARTMENT,PROFESSOR.ADDRESS,PROFESSOR.OFFICE,ORGANIZATION.NAME "
            		+ " FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID "
            		+ " LEFT JOIN ORGANIZATION ON PROFESSOR.ORG_ID = ORGANIZATION.ID");
            if(_m_6.length()>0){
                sql.append(_m_6);
            }
            sql.append(" ORDER BY RESOURCE_NAME");
            sql.append(" LIMIT ").append(pageSize);
        }else{
            int _m_11 = _m_1 / pageSize;
            if(_m_1 % pageSize != 0){
                ++_m_11;
            }
            if(pageNo > _m_11){
                pageNo = _m_11;
            }
            _result.setPageNo(pageNo);
            --pageNo;
            int _m_12 = (pageNo * pageSize);
            sql = new StringBuilder();
            sql.append("SELECT RESOURCE_ID,RESOURCE_NAME,RESOURCE.SUBJECT,RESOURCE.INDUSTRY,SUPPORTED_SERVICES,"
            		+ " RESOURCE.DESCP,PROFESSOR_ID,ASCRIPTION,HOPE_PAY_METHOD,COOPERATION_NOTES,"
            		+ " RESOURCE.CREATE_TIME,RESOURCE.MODIFY_TIME,PROFESSOR.NAME,PROFESSOR.TITLE,"
            		+ " PROFESSOR.DEPARTMENT,PROFESSOR.ADDRESS,PROFESSOR.OFFICE,ORGANIZATION.NAME "
            		+ " FROM RESOURCE LEFT JOIN PROFESSOR ON PROFESSOR_ID = PROFESSOR.ID "
            		+ " LEFT JOIN ORGANIZATION ON PROFESSOR.ORG_ID = ORGANIZATION.ID");
            if(_m_6.length()>0){
                sql.append(_m_6);
            }
            sql.append(" ORDER BY RESOURCE_NAME");
            sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(_m_12);
        }
        ps = con.prepareStatement(sql.toString());
        try{
            if(!_m_2){
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
                ps.setString(_m_3++,key);
            }
            if(!_m_4){
                ps.setString(_m_3++,subject);
            }
            if(!_m_5){
                ps.setString(_m_3++,industry);
            }
            if(!_m6){
            	ps.setString(_m_3++,address);
            }
            ResultSet rs = ps.executeQuery();
            try{
                List<Resource> _m_13 = new ArrayList<Resource>();
                _result.setData(_m_13);
                int _m_14 = 0;
                while((_m_14<pageSize) && rs.next()){
                    ++_m_14;
                    Resource _obj =  new Resource();
                    _obj.setResourceId(rs.getString(1));
                    _obj.setResourceName(rs.getString(2));
                    String _m_15 = rs.getString(3);
                    if(rs.wasNull()){
                        _m_15 = null;
                    }
                    _obj.setSubject(_m_15);
                    String _m_16 = rs.getString(4);
                    if(rs.wasNull()){
                        _m_16 = null;
                    }
                    _obj.setIndustry(_m_16);
                    _obj.setSupportedServices(rs.getString(5));
                    String _m_17 = rs.getString(6);
                    if(rs.wasNull()){
                        _m_17 = null;
                    }
                    _obj.setDescp(_m_17);
                    EditProfessor professor = new EditProfessor();
                    professor.setId(rs.getString(7));
                    _obj.setAscription(rs.getString(8));
                    _obj.setHopePayMethod(rs.getString(9));
                    String _m_18 = rs.getString(10);
                    if(rs.wasNull()){
                        _m_18 = null;
                    }
                    _obj.setCooperationNotes(_m_18);
                    _obj.setCreateTime(rs.getString(11));
                    _obj.setModifyTime(rs.getString(12));
                    professor.setName(rs.getString(13));
                    professor.setTitle(rs.getString(14));
                    professor.setDepartment(rs.getString(15));
                    professor.setAddress(rs.getString(16));
                    professor.setOffice(rs.getString(17));
                    professor.setOrgName(rs.getString(18));
                    _obj.setProfessor(professor);
                    List<Image> images = this.imageDao.queryRes(con, _obj.getResourceId());
                    _obj.setImages(images);
                    _m_13.add(_obj);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_19){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_20){}
        }
    }
	
}
