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

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.framework.CoreBaseInfo;
import com.kingdee.eas.hr.emp.EmployeeBizException;
import com.kingdee.eas.hr.emp.PersonDegreeCollection;
import com.kingdee.eas.hr.emp.PersonDegreeFactory;
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.base.syssetting.web.json.JSONUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工教育经历
 */
public class PersonDegreeEditHandlerEx extends PersonDegreeEditHandler{

    
    /*public Map<String, Object> customerCheckAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException  {
        Map<String, Object> map = new HashMap<String, Object>();
        String personId = request.getParameter("personId");
        String fid = request.getParameter("fid");
        String diplomaModality = request.getParameter("diplomaModality");
        Context ctx = SHRContext.getInstance().getContext();
        String sql = "select fid,FDiplomaModality,FIsHighest from T_HR_PersonDegree where FPERSONID  = '"+personId+"'";
        try{
            IRowSet rs = DbUtil.executeQuery(ctx, sql);
            //�ж����ѧ���Ƿ����
            map.put("isHighest",false);
            //�ж�ѧ���Ƿ��ظ�
            map.put("hasData",false);
            while (rs.next()){
            	System.out.print(fid+"-----"+rs.getString("fid").equals(fid)+"-------"+rs.getString("fid"));
            	System.out.print(diplomaModality+"-----"+rs.getString("FDiplomaModality").equals(diplomaModality)+"-------"+rs.getString("FDiplomaModality"));
                
            	if( !rs.getString("fid").equals(fid)
                        && diplomaModality.equals(rs.getString("FDiplomaModality"))){
                    map.put("hasData",true);
                }
                if(rs.getInt("FIsHighest") == 1){
                	 map.put("isHighest",true);
                }
            }
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BOSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        JSONUtils.writeJson(response, map);
        return null;
    }*/
    
    @Override
    protected void beforeSave(HttpServletRequest request,
    		HttpServletResponse response, CoreBaseInfo model)
    		throws SHRWebException {
    	Context ctx = SHRContext.getInstance().getContext();
    	PersonDegreeInfo info = (PersonDegreeInfo) model;
    	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{
            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+"-----"+rs.getString("FDiplomaModality").equals(fid)+"-------"+rs.getString("FDiplomaModality"));
                
            	if( !rs.getString("fid").equals(fid)
                        && diplomaModality.equals(rs.getString("FDiplomaModality"))){
                    isDiplomaModality = true;
                }
                if(rs.getInt("FIsHighest") == 1){
                	isHighest = false;
                }
            }
            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);
		if ((!(info.isIsHighest())) && (info.getId() == null)) {
			EntityViewInfo view = new EntityViewInfo();
			FilterInfo filterInfo = new FilterInfo();
			filterInfo.appendFilterItem("person", personId);
			filterInfo.appendFilterItem("isHighest", Boolean.valueOf(true));
			view.setFilter(filterInfo);
			try {
				PersonDegreeCollection pdColl = PersonDegreeFactory
						.getLocalInstance(ctx).getPersonDegreeCollection(view);
				if (pdColl.size() == 0)
					throw new ShrWebBizException(new EmployeeBizException(
							EmployeeBizException.NEED_HIGH_EDU));
			} catch (BOSException e) {
				e.printStackTrace();
				throw new SHRWebException(e);
			}
		}
    }

}
