package com.kingdee.shr.affair.web.handler.hrman;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

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

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.hr.base.AffairActionReason;
import com.kingdee.eas.hr.base.AffairActionReasonCollection;
import com.kingdee.eas.hr.base.AffairActionReasonFactory;
import com.kingdee.eas.hr.base.AffairActionReasonInfo;
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.web.json.JSONUtils;
import com.kingdee.util.LocaleUtils;

/**
 * 调动单Handler扩展
 * @author issuser
 *
 */
public class FluctuationBizBillHrManEditHandlerEx extends FluctuationBizBillHrManEditHandler {

	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.affair.web.handler.hrman.FluctuationBizBillHrManEditHandlerEx");
	
	public Map<String, Object> getEmpOrgRelationAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        String personId = request.getParameter("personId");
        String positionId = request.getParameter("positionId");
        logger.error("转正单获取主要任职参数，人员id:"+personId+",职位id："+positionId);
        Map<String, Object> map = this.getEmpOrgRelation(personId, positionId, "1");
        logger.error("转正单获取主要任职相关字段："+map.toString());
        JSONUtils.writeJson(response, map);
        return null;
    }
	
	public Map<String, Object> getPartEmpOrgRelationAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        String personId = request.getParameter("personId");
        String positionId = request.getParameter("positionId");
        logger.error("获取兼职参数，人员id:"+personId+",职位id："+positionId);
        Map<String, Object> map = this.getEmpOrgRelation(personId, positionId, "0");
        logger.error("获取兼职相关字段："+map.toString());
        JSONUtils.writeJson(response, map);
        return null;
    }
	
	public Map<String, Object> getEmpOrgRelation(String personId,String positionId,String assignType) throws SHRWebException {
		Map<String, Object> map = new HashMap<String, Object>();
        Context ctx = SHRContext.getInstance().getContext();
      //202504  coyle
        String selSql = "select top 1 " +
				" a.fleffdt  as leftDate, \n" +
				" b.fid as leaderId,b.FNAME_l1 as leaderName, \n" +
                " c.fid as personTypeId,c.fname_l1 as personTypeName, \n" +
                " d.fid as academicId,d.fname_l1 as academicName, \n" +
                " f.fid as jobTwoId,f.fname_l1 as jobTwoName, \n" +
                " a.CFHourlywage,a.CFAdmintitle \n" +
                " from T_HR_EmpOrgRelation a \n" +
                " left join t_bd_person  as b on a.CFLineManagerNameI = b.fid \n" +
                " left join CT_MP_WorkerCategory as c on a.CFWorkercategoryID = c.fid \n" +
                " left join CT_MP_AcademicTitle as d on a.CFAcademicTitleID = d.fid \n" +
                " left join CT_HR_Job2 as f on a.CFJobsID = f.fid \n" +
                " where a.fpersonid = '" + personId + "' and a.fassignType = '"+assignType+"' \n" +
                " and a.fpositionid = '" + positionId + "' \n" +
                " order by a.fleffdt desc";
    	System.out.println("变动记录查询："+selSql);
        try {
            IRowSet rs = DbUtil.executeQuery(ctx, selSql);
           
            while (rs.next()) {
            	//202504
				map.put("leftDate",rs.getString("leftDate"));
                map.put("leaderId",rs.getString("leaderId"));
                map.put("leaderName",rs.getString("leaderName"));
                map.put("personTypeId",rs.getString("personTypeId"));
                map.put("personTypeName",rs.getString("personTypeName"));
                map.put("academicId",rs.getString("academicId"));
                map.put("academicName",rs.getString("academicName"));
                map.put("jobTwoId",rs.getString("jobTwoId"));
                map.put("jobTwoName",rs.getString("jobTwoName"));
                map.put("hourlywage",rs.getString("CFHourlywage"));
                map.put("admint",rs.getString("CFAdmintitle"));
            }
        } catch (SQLException e) {
			e.printStackTrace();
			logger.error("转正单获取主要任职相关字段错误："+e.getMessage());
		} catch (BOSException e) {
			e.printStackTrace();
			logger.error("转正单获取主要任职相关字段错误："+e.getMessage());
		}
        return map;
    }
	
	/**
	 * 根据调动类型编码获取调动类型信息
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws SHRWebException
	 */
	public Map<String, Object> getDefaultChangeTypeAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		Map<String, Object> map = new HashMap<String, Object>();
		Context ctx = SHRContext.getInstance().getContext();
		String changeTypeNumber = request.getParameter("changeTypeNumber");
		try {
			AffairActionReasonCollection affairActionReasonCollection = AffairActionReasonFactory.getLocalInstance(ctx).getAffairActionReasonCollection("where number = '"+changeTypeNumber+"'");
			if(affairActionReasonCollection != null && affairActionReasonCollection.size()>0) {
				AffairActionReasonInfo affairActionReasonInfo = affairActionReasonCollection.get(0);
				map.put("changeTypeId", affairActionReasonInfo.getId().toString());
				map.put("changeTypeName", affairActionReasonInfo.getName(LocaleUtils.locale_l1));
			}
		} catch (BOSException e) {
			e.printStackTrace();
		}
		JSONUtils.writeJson(response, map);
		return null;
	}
}
