package com.kingdee.shr.recuritment.web.handler.resume;

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

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.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.base.permission.util.ToolUtils;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
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.ShrWebBizException;
import com.kingdee.shr.recuritment.FocusOnSource;
import com.kingdee.shr.recuritment.ISmartImportTask;
import com.kingdee.shr.recuritment.ISmartImportTaskDetail;
import com.kingdee.shr.recuritment.RecuritmentDemandInfo;
import com.kingdee.shr.recuritment.ResumeFocusOnFactory;
import com.kingdee.shr.recuritment.ResumeSourceInfo;
import com.kingdee.shr.recuritment.SmartImportTaskDetailCollection;
import com.kingdee.shr.recuritment.SmartImportTaskDetailFactory;
import com.kingdee.shr.recuritment.SmartImportTaskDetailInfo;
import com.kingdee.shr.recuritment.SmartImportTaskFactory;
import com.kingdee.shr.recuritment.SmartImportTaskInfo;
import com.kingdee.shr.recuritment.TaskTypeEnum;
import com.kingdee.shr.recuritment.service.smartimport.SmartImportServiceEx;
import com.kingdee.shr.recuritment.util.response.ResponseUtil;


/**
 * 简历筛选拓展
 * @author coyle
 *202501
 */
public class ResumeBaseRecListHandlerEx extends ResumeBaseRecListHandler {
	private static Logger logger = Logger.getLogger(ResumeBaseRecListHandlerEx.class);
	
	public void resumeAnalysisAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		String recuritmentDemandId = request.getParameter("targetId");
		String taskId = request.getParameter("taskId");
		String resumeSourceId = request.getParameter("resumeSourceId");
		String interRecommendationId = request.getParameter("interRecommendationId");
		String sourceType = request.getParameter("sourceType");
		String isKeyTalent = request.getParameter("isKeyTalent");
		String phoneModify = request.getParameter("phoneModify");

		try {
			HashMap<String, String> paraMap = new HashMap<String, String>();
			paraMap.put("sourceType", sourceType);
			paraMap.put("interRecommendationId", interRecommendationId);
			paraMap.put("resumeSourceId", resumeSourceId);
			paraMap.put("recuritmentDemandId", recuritmentDemandId);
			// 二开字段
			paraMap.put("isKeyTalent", isKeyTalent);
			paraMap.put("phoneModify", phoneModify);
			SmartImportServiceEx.getInstance().resumeSmartImport(taskId, TaskTypeEnum.SMARTIMPORT_RESUME, paraMap);
			ResponseUtil.success(response);
		} catch (ShrWebBizException var10) {
			logger.error(var10.getMessage(), var10);
			ResponseUtil.error(response, var10.getMessage());
		}
		
	}
	

	public void  afterSmartImportTaskAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Context ctx = SHRContext.getInstance().getContext();
		// 登录人
		String userId = ctx.getCaller().toString();

		try {
			String taskIds = request.getParameter("taskIds");
			String[] split = taskIds.split(",");
			ISmartImportTask taskIns = SmartImportTaskFactory.getLocalInstance(ctx);

			ISmartImportTaskDetail detailIns = SmartImportTaskDetailFactory.getLocalInstance(ctx);
			SmartImportTaskDetailCollection detailCol = detailIns
					.getSmartImportTaskDetailCollection("where id in (" + ToolUtils.aryToStr(split, true) + ")");
	 
			for (int i = 0; i < detailCol.size(); i++) {
				SmartImportTaskDetailInfo detailInfo = detailCol.get(i);
				SmartImportTaskInfo taskInfo = detailInfo.getTask();
				if (taskInfo != null) {
					taskInfo = taskIns.getSmartImportTaskInfo(new ObjectUuidPK(taskInfo.getId()));

					//招聘需求
					RecuritmentDemandInfo recDemand = taskInfo.getRecDemand();
					String recStr = recDemand == null ? "": recDemand.getId().toString();
					PersonInfo inpInfo = taskInfo.getInterRecommendation();
					String  inPersonStr = inpInfo == null ? "":inpInfo.getId().toString();
					// 简历来源
					ResumeSourceInfo resumeSource = taskInfo.getResumeSource();
					String souStr = resumeSource == null ? "" : resumeSource.getId().toString();
					// taskInfo 手机号
					String phoneModify = com.kingdee.util.StringUtils.cnulls(taskInfo.get("phoneModify"));
					// 是否重点人才
					boolean isKeyTalent = taskInfo.getBoolean("isKeyTalent");

					// 附件名
					String resumeFileName = detailInfo.getResumeFileName();
					StringBuffer sbu = new StringBuffer();
					sbu.append(" select   TOP 1   fid , FMobilePhone ");
					sbu.append(" from  T_REC_ResumeBaseRec where ");
					sbu.append(" FCreatorID = '" + userId + "'  ");
					sbu.append(" and  FRecuritmentDemandID = '" + recStr + "'  ");
					// 附件名称 FResumeFileName
					sbu.append(" and ( fid in ( select fboid  from T_BAS_BoAttchAsso ");
					sbu.append(" where  FAttachmentID in ( select fid  from T_BAS_Attachment where fname_l2  ='"
							+ resumeFileName + "' ) )");
					sbu.append(" or  fid in ( select fboid from T_HR_SHRATTACHMENTEXT ");
					sbu.append(" where  FAttachment in ( select fid  from T_BAS_Attachment where fname_l2  ='"
							+ resumeFileName + "' ) ) )");
					//内部推荐人
					if (!StringUtils.isEmpty(inPersonStr)) {
						sbu.append("  and  FInterRecomendationID = '" + inPersonStr + "'  ");
					}
					// 简历来源
					if (!StringUtils.isEmpty(souStr)) {
						sbu.append("  and  FResSourceID = '" + souStr + "'  ");
					}
					IRowSet executeQuery = null;

					if (!StringUtils.isEmpty(phoneModify) || isKeyTalent) {
						executeQuery = DbUtil.executeQuery(ctx, sbu.toString());
						if (executeQuery.next()) {
							String resumeId = executeQuery.getString("fid");
							String FMobilePhone = executeQuery.getString("FMobilePhone");
							if (!StringUtils.equals(FMobilePhone, phoneModify) && !StringUtils.isEmpty(phoneModify)) {
								String upsql = " update T_REC_ResumeBaseRec  set  FMobilePhone =  '" + phoneModify
										+ "'  where fid = '" + resumeId + "'";
								DbUtil.execute(ctx, upsql);
							}
							// 如果是重点人才
							if (isKeyTalent && !ResumeFocusOnFactory.getLocalInstance(ctx).isTalent(resumeId,
									FocusOnSource.resume)) {
								ResumeFocusOnFactory.getLocalInstance(ctx).joinFocusOn(resumeId, FocusOnSource.resume,
										"简历筛选重点关注");
							}
						}
					}


				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (EASBizException e) {
			e.printStackTrace();
		}
			
	}
			
			
		 


}
