package com.kingdee.eas.hr.emp.web.handler;

import java.sql.SQLException;

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

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.util.BOSUuid;
import com.kingdee.eas.basedata.hraux.DiplomaModalityCollection;
import com.kingdee.eas.basedata.hraux.DiplomaModalityFactory;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.emp.PersonDegreeInfo;
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.preentry.PEPersonDegreeCollection;
import com.kingdee.shr.preentry.PEPersonDegreeFactory;

/**
 * 我的档案-教育经历
 * @author issuser
 *
 */
public class PersonDegreeReSumeEditHandlerEx extends EmployeeMultiRowHandler {
	
	@Override
	protected void beforeSave(HttpServletRequest request,
			HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
    	PersonDegreeInfo info = (PersonDegreeInfo) model;
    	System.out.println("Ա��������Ϣ��" + info);
    	String personId = info.getPerson().getId().toString();
    	String fid =  model.getString("id");
    	System.out.print("教育经历单据ID的============"+fid);
        String diplomaModality = info.getDiplomaModality().getId().toString();
        String sql = "select fid,FDiplomaModality,FIsHighest from T_HR_PersonDegree where FPERSONID  = '"+personId+"'";
        try{
        	System.out.println("sql��" + sql);
            IRowSet rs = DbUtil.executeQuery(ctx, sql);
            //是否最高学历
            boolean isHighest  = true;
            //是否全日制统招
            boolean isDiplomaModality = false;
            while (rs.next()){
            	System.out.print(fid+"-----"+rs.getString("fid").equals(fid)+"-------"+rs.getString("fid"));
            	System.out.print(diplomaModality+"-----"+diplomaModality.equals(rs.getString("FDiplomaModality"))+"-------"+rs.getString("FDiplomaModality"));
                
            	if( !rs.getString("fid").equals(fid)
                        && diplomaModality.equals(rs.getString("FDiplomaModality"))){
                    isDiplomaModality = true;
                }
                if(rs.getInt("FIsHighest") == 1){
                	isHighest = false;
                }
            }
            //修改审核校验
            String auditSql = "select * from T_HR_EmployeeAudit where FEntity = 'com.kingdee.eas.hr.emp.app.PersonDegree' and FSTATUS  = '1'" +
            		" and FPERSONID  = '"+personId+"' and FPk != 'fid'";
            IRowSet auditRs = DbUtil.executeQuery(ctx, auditSql);
            while(auditRs.next()){
            	String diplomaModalityName = null;
            	String[] deacAfter = rs.getString("FDescAfter").split("；");
                for(String s : deacAfter){
                    if(s.contains("学历形式ʽ") || s.contains("Academic Qualification Form")){
                        diplomaModalityName = s.substring(s.indexOf("学历形式")+1,s.length());
                        continue ;
                    }
                }
                String param = " where fname_l2 = '" + diplomaModalityName + "' or fname_l1 = '" + diplomaModalityName + "' ";
                DiplomaModalityCollection diplomCollection = DiplomaModalityFactory.getLocalInstance(ctx).getDiplomaModalityCollection(param);
                if(diplomaModality.equals(diplomCollection.get(0).getId())){
                	isDiplomaModality = true;
                	break;
                }              
            }
            
            if(isHighest){
            	model.put("isHighest", true);
            	info.setIsHighest(true);
            }
            if(isDiplomaModality){
            	throw new ShrWebBizException("Employee qualifications are not allowed to be repeated !"); 
            }

        } catch (SQLException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		} 
		super.beforeSave(request, response, model);
	}
	

}
