package com.kingdee.shr.customer.gtiit.handler;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.preentry.PEInfoMultiPageInfo;
import com.kingdee.shr.preentry.PEPersonDegreeCollection;
import com.kingdee.shr.preentry.PEPersonDegreeFactory;
import com.kingdee.shr.preentry.web.handler.PEDegreeMultiRowHandler;

public class PEDegreeMultiRowHandlerEx extends PEDegreeMultiRowHandler{

	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.customer.gtiit.handler.PEDegreeMultiRowHandlerEx");
	
	public void isHighestAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String talentId = request.getParameter("talentId");
		System.out.println("预入职人员id："+talentId);
		EntityViewInfo view = new EntityViewInfo();
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.appendFilterItem("talent.id", talentId);
        filterInfo.appendFilterItem("isHighest", true);
        view.setFilter(filterInfo);
		try {
			PEPersonDegreeCollection pedColl = PEPersonDegreeFactory.getLocalInstance(ctx).getPEPersonDegreeCollection(view);
			if (pedColl.size() == 0) {
				modelMap.put("flag", true);
	        }
		} catch (BOSException e) {
			e.printStackTrace();
		}
		modelMap.put("flag", false);
		JSONUtils.writeJson(response, modelMap);
	}
	
	public void checkDiplomaAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String talentId = request.getParameter("talentId")==null?"":request.getParameter("talentId");
		String diplomaModality = request.getParameter("diplomaModality")==null?"":request.getParameter("diplomaModality");
		String fid = request.getParameter("fid")==null?"":request.getParameter("fid");
		logger.error("预入职人员："+talentId+",学历形式："+diplomaModality+",id:"+fid);
        String sql = "select fid,FDiplomaModality,FIsHighest from T_HR_PEPersonDegree where FTALENTID  = '"+talentId+"'";
        try{
            IRowSet rs = DbUtil.executeQuery(ctx, sql);
            boolean isDiplomaModality = false;
            while (rs.next()){
            	if( !rs.getString("fid").equals(fid)
                        && diplomaModality.equals(rs.getString("FDiplomaModality"))){
                    isDiplomaModality = true;
                }
            }
            modelMap.put("isDiplomaModality", isDiplomaModality);
            JSONUtils.writeJson(response, modelMap);
        } catch (SQLException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		}
	}
	
	
	@Override
	protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response,
			CoreBaseInfo coreBaseInfo) throws SHRWebException {
		super.afterCreateNewModel(request, response, coreBaseInfo);
		PEInfoMultiPageInfo info = null;
		String talentId = null;
		if (coreBaseInfo instanceof PEInfoMultiPageInfo) {
			info = (PEInfoMultiPageInfo) coreBaseInfo;
			talentId = request.getParameter("relatedFieldId");
		}

		if (null != info && null != talentId) {
			EntityViewInfo view = new EntityViewInfo();
			FilterInfo filter = new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("talent.id", talentId, CompareType.EQUALS));
			view.setFilter(filter);
			CoreBaseCollection infoCollection = null;

			try {
				infoCollection = this.getBizInterface(request).getCollection(view);
			} catch (BOSException var10) {
				var10.printStackTrace();
			}

			if (null == infoCollection || infoCollection.size() < 1) {
				coreBaseInfo.put("isSingle", true);
			}
			
			Context ctx = SHRContext.getInstance().getContext();
			System.out.println("预入职人员id："+talentId);
			EntityViewInfo highestView = new EntityViewInfo();
	        FilterInfo filterInfo = new FilterInfo();
	        filterInfo.appendFilterItem("talent.id", talentId);
	        filterInfo.appendFilterItem("isHighest", true);
	        highestView.setFilter(filterInfo);
			try {
				PEPersonDegreeCollection pedColl = PEPersonDegreeFactory.getLocalInstance(ctx).getPEPersonDegreeCollection(highestView);
				if (pedColl.size() == 0) {
					coreBaseInfo.put("isHighest", true);
		        }
			} catch (BOSException e) {
				e.printStackTrace();
			}
		}
	}
	
}
