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.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.OrderBy;
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.LimitQuery;
import org.jfw.apt.orm.annotation.dao.method.operator.PageSelect;
import org.jfw.apt.orm.annotation.dao.method.operator.QueryList;
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
import org.jfw.apt.orm.annotation.dao.method.operator.SelectOne;
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
import org.jfw.apt.orm.annotation.dao.param.Like;
import org.jfw.util.PageQueryResult;

import com.ekexiu.portal.basepo.LogicDict;
import com.ekexiu.portal.po.DataDict;
import com.ekexiu.portal.pojo.DataDictCode;

@DAO
public abstract class DataDictDao {

	@Insert
	public abstract int insert(Connection con, DataDict dict) throws SQLException;

	@Update
	public abstract int update(Connection con, DataDict dict) throws SQLException;

	@From(DataDict.class)
	@DeleteWith
	public abstract int delete(Connection con, String dictCode, String code) throws SQLException;

	@From(DataDict.class)
	@Nullable
	@SelectOne
	public abstract DataDict query(Connection con, String dictCode, String code) throws SQLException;

	@SelectList
	public abstract List<DataDict> query(Connection con, String dictCode) throws SQLException;
	
	/**
	 * 查询数据字典项 按下属的专家数量多少排序  取最多的前20个
	 * @param con
	 * @param dictCode 数据字典dictCode字段
	 * @return 返回数据字典caption
	 * @throws SQLException
	 */
	public List<DataDictCode> queryDictCode(Connection con,String dictCode) throws SQLException{
        String sql = "SELECT CAPTION FROM DATA_DICT INNER JOIN PROFESSOR ON POSITION(DATA_DICT.CAPTION IN "+dictCode+
        		")>0 WHERE DICT_CODE = '"+dictCode+"' GROUP BY CAPTION ORDER BY COUNT("+dictCode+") DESC LIMIT 20";
        PreparedStatement ps = con.prepareStatement(sql);
        try{
//            ps.setString(1,dictCode);
            ResultSet rs = ps.executeQuery();
            try{
                List<DataDictCode> _result = new ArrayList<DataDictCode>();
                while(rs.next()){
                	DataDictCode dataDictCode =  new DataDictCode();
//                    dataDictCode.setDictCode("SUBJECT");
//                    dataDictCode.setActived(true);
//                    dataDictCode.setCode(null);
                	dataDictCode.setCaption(rs.getString(1));
                    _result.add(dataDictCode);
                }
                return _result;
            }finally{
                try{rs.close();}catch(Exception _m_2){}
            }
        }finally{
            try{ps.close();}catch(Exception _m_3){}
        }
    }

	@PageSelect
	public abstract PageQueryResult<DataDict> query(Connection con, String dictCode, @Nullable @Like String code,
			@Nullable @Like String caption, @Nullable String parentCode, int pageSize, int pageNo) throws SQLException;

	@QueryList
	@From(DataDict.class)
	public abstract List<LogicDict> queryLogicDict(Connection con, String dictCode) throws SQLException;

	@LimitQuery
	@From(DataDict.class)
	@OrderBy("ORDER BY CODE")
	public abstract List<LogicDict> limitQuery(Connection con, String dictCode, int rows) throws SQLException;
	
	@LimitQuery
	@From(DataDict.class)
	@OrderBy("ORDER BY CODE")
	public abstract List<LogicDict> limitQuery(Connection con, String dictCode, @Nullable @Like String caption, int rows) throws SQLException;

}
