package com.kingdee.shr.recuritment.service.smartimport;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.data.SortType;
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.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.entity.SorterItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.recuritment.ISmartImportTask;
import com.kingdee.shr.recuritment.RecuritmentDemandInfo;
import com.kingdee.shr.recuritment.ResumeClipBaseInfo;
import com.kingdee.shr.recuritment.ResumeSourceInfo;
import com.kingdee.shr.recuritment.ResumeSourceType;
import com.kingdee.shr.recuritment.SmartImportTaskCollection;
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.TaskStateEnum;
import com.kingdee.shr.recuritment.TaskTypeEnum;
import com.kingdee.shr.recuritment.app.util.EncodingDetect;
import com.kingdee.shr.recuritment.app.util.db.RecDBUtils;
import com.kingdee.shr.recuritment.app.util.job.RecSyncTaskThreadPool;
import com.kingdee.shr.recuritment.service.smartimport.base.impl.ArchiveSmartImportImpl;
import com.kingdee.shr.recuritment.service.smartimport.base.impl.RecommendResumeSmartImportImpl;
import com.kingdee.shr.recuritment.service.smartimport.base.impl.ResumeSmartImportImpl;
import com.kingdee.shr.recuritment.service.smartimport.entity.SmartImportTaskEntity;
import com.kingdee.shr.recuritment.util.FileSafeUtil;
import com.kingdee.shr.recuritment.util.RecWebBaseUtils;
import com.kingdee.shr.recuritment.util.XSSDefenseUtil;
import com.kingdee.shr.recuritment.util.vo.smartimport.SmartImportTask;
import com.kingdee.shr.recuritment.utils.RecBaseUtils;

public class SmartImportServiceEx {
	private static final Logger logger = Logger.getLogger(SmartImportServiceEx.class);
	private long taskTimeoutMillis;
	private final BlockingQueue<FutureTimeTask> futureTaskQueue;
	private static final List<Object> FILETYPELIST = Arrays.asList("DOC", "DOCX", "TXT", "RTF", "PDF", "MSG", "WPS",
			"JPG", "PNG", "GIF");
	private static final String FILETYPESTR = "DOC,DOCX,TXT,RTF,PDF,MSG,WPS,JPG,PNG,GIF";
	private int resumeFileMinSize;
	private int resumeFileMaxSize;

	private SmartImportServiceEx() {
		this.taskTimeoutMillis = 1800000L;
		this.resumeFileMinSize = 1;
		this.resumeFileMaxSize = 20971520;
		this.futureTaskQueue = new LinkedBlockingQueue();
		RecSyncTaskThreadPool.getInstance().submitAsyncTask(new TimeoutTaskCheckThread());
	}

	public static SmartImportServiceEx getInstance() throws ShrWebBizException {
		return SmartImportServiceEx.SmartImportServiceHolder.service;
	}

	public void setSmartImportChooseDemand(String recuritmentDemandId) throws ShrWebBizException {
		this.setSmartImportChooseDemand(recuritmentDemandId, TaskTypeEnum.SMARTIMPORT_RESUME);
	}

	public void setSmartImportChooseDemand(String recuritmentDemandId, TaskTypeEnum typeEnum)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getUserProgressTaskOrAddNotExists(typeEnum);
		if (taskInfo != null) {
			try {
				IObjectPK recuritmentDemandPK = recuritmentDemandId == null ? null
						: new ObjectUuidPK(recuritmentDemandId);
				SmartImportTaskFactory.getRemoteInstance()
						.setSmartImportChooseDemand(new ObjectUuidPK(taskInfo.getId().toString()), recuritmentDemandPK);
			} catch (BOSException var5) {
				logger.error("******************设置智能导入所选需求失败！*****************");
				logger.error(var5.getMessage(), var5);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}

	}

	public void setSmartImportChooseResumeClip(String resumeClipId, TaskTypeEnum taskType) throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getUserProgressTaskOrAddNotExists(taskType);
		if (taskInfo != null) {
			try {
				IObjectPK resumeClipPK = resumeClipId == null ? null : new ObjectUuidPK(resumeClipId);
				SmartImportTaskFactory.getRemoteInstance()
						.setSmartImportChooseResumeClip(new ObjectUuidPK(taskInfo.getId().toString()), resumeClipPK);
			} catch (BOSException var5) {
				logger.error("******************设置智能导入所选简历夹失败！*****************");
				logger.error(var5.getMessage(), var5);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}

	}

	public void setSmartImportChooseRecommendation(String interRecommendation, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getUserProgressTaskOrAddNotExists(taskType);
		if (taskInfo != null) {
			try {
				IObjectPK interRecommendationId = interRecommendation == null ? null
						: new ObjectUuidPK(interRecommendation);
				SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseRecommendation(
						new ObjectUuidPK(taskInfo.getId().toString()), interRecommendationId);
			} catch (BOSException var5) {
				logger.error("******************设置智能导入所选简历夹失败！*****************");
				logger.error(var5.getMessage(), var5);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}

	}

	public void setSmartImportChooseResumeSource(String resumeSource, TaskTypeEnum taskType) throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getUserProgressTaskOrAddNotExists(taskType);
		if (taskInfo != null) {
			try {
				IObjectPK resumeSourceId = resumeSource == null ? null : new ObjectUuidPK(resumeSource);
				SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeSource(
						new ObjectUuidPK(taskInfo.getId().toString()), resumeSourceId);
			} catch (BOSException var5) {
				logger.error("******************设置智能导入所选简历夹失败！*****************");
				logger.error(var5.getMessage(), var5);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}

	}

	public void setSmartImportChooseSourceType(String sourceType, TaskTypeEnum taskType) throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getUserProgressTaskOrAddNotExists(taskType);
		if (taskInfo != null) {
			try {
				SmartImportTaskFactory.getRemoteInstance()
						.setSmartImportChooseSourceType(new ObjectUuidPK(taskInfo.getId().toString()), sourceType);
			} catch (BOSException var5) {
				logger.error("******************设置智能导入所选来源类型失败！*****************");
				logger.error(var5.getMessage(), var5);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}

	}

	/** @deprecated */
	@Deprecated
	public SmartImportTask uploadResume(Map<String, MultipartFile> fileMap, String taskId, String recuritmentDemandId)
			throws ShrWebBizException {
		SmartImportTask taskData = this.doUploadResume(fileMap, TaskTypeEnum.SMARTIMPORT_RESUME);
		return taskData;
	}

	/** @deprecated */
	@Deprecated
	public SmartImportTask uploadArchiveResume(Map<String, MultipartFile> fileMap, String taskId, String resumeClipId)
			throws ShrWebBizException {
		SmartImportTask taskData = this.doUploadResume(fileMap, TaskTypeEnum.SMARTIMPORT_ARCHIVE);
		return taskData;
	}

	public SmartImportTask uploadResume(Map<String, MultipartFile> fileMap, TaskTypeEnum type)
			throws ShrWebBizException {
		SmartImportTask taskData = this.doUploadResume(fileMap, type);
		return taskData;
	}

	private SmartImportTask doUploadResume(Map<String, MultipartFile> fileMap, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = null;
		SmartImportTaskDetailCollection detailColl = new SmartImportTaskDetailCollection();
		Context ctx = SHRContext.getInstance().getContext();
		Iterator var6 = fileMap.entrySet().iterator();

		while (true) {
			if (var6.hasNext()) {
				Map.Entry<String, MultipartFile> entity = (Map.Entry) var6.next();
				MultipartFile mf = (MultipartFile) entity.getValue();
				String resumeFileName = mf.getOriginalFilename();
				byte[] resumeFileData = null;

				try {
					resumeFileData = mf.getBytes();
				} catch (IOException var16) {
					logger.error(var16.getMessage(), var16);
					throw new ShrWebBizException("附件上传失败，获取附件内容异常！");
				}

				if (StringUtils.isBlank(resumeFileName)) {
					throw new ShrWebBizException("简历附件名不能为空：" + resumeFileName);
				}

				if (resumeFileData != null && resumeFileData.length >= this.resumeFileMinSize) {
					if (resumeFileData.length > this.resumeFileMaxSize) {
						throw new ShrWebBizException("简历附件大小不能超过：" + this.resumeFileMaxSize / 1024 / 1024 + "M！");
					}

					resumeFileData = FileSafeUtil.checkFileComplianceIncloudXSS(ctx, mf, FILETYPELIST,
							"DOC,DOCX,TXT,RTF,PDF,MSG,WPS,JPG,PNG,GIF");
					if (taskInfo == null) {
						taskInfo = this.getUserProgressTaskOrAddNotExists(taskType);
					}

					if (taskInfo == null) {
						if (logger.isDebugEnabled()) {
							logger.debug("上传简历失败,上传简历时获取任务失败");
						}

						throw new ShrWebBizException("上传简历失败。");
					}

					String fileType = resumeFileName.substring(resumeFileName.lastIndexOf(".") + 1);
					if (!StringUtils.equalsIgnoreCase("xls", fileType)
							&& !StringUtils.equalsIgnoreCase("xlsx", fileType) && !resumeFileName.contains("csv")) {
						String encode;
						if (StringUtils.equals("text/html", fileType) || resumeFileName.contains("html")) {
							encode = EncodingDetect.getJavaEncode(resumeFileData);

							try {
								resumeFileData = XSSDefenseUtil.cleanXSS(new String(resumeFileData, encode))
										.getBytes(encode);
							} catch (UnsupportedEncodingException var15) {
								logger.error(var15.getMessage(), var15);
								throw new ShrWebBizException("上传简历失败。");
							}
						}

						encode = null;

						SmartImportTaskDetailInfo taskDetailInfo;
						try {
							taskDetailInfo = SmartImportTaskDetailFactory.getRemoteInstance()
									.addUploadResumeFile(resumeFileName, resumeFileData, taskInfo);
						} catch (BOSException var14) {
							logger.error("上传简历失败" + var14.getMessage());
							logger.error(var14.getMessage(), var14);
							throw new ShrWebBizException("上传简历失败。");
						}

						if (taskDetailInfo != null) {
							detailColl.add(taskDetailInfo);
						}
						continue;
					}

					throw new ShrWebBizException("非法文件！");
				}

				throw new ShrWebBizException("简历附件不能为空！");
			}

			if (taskInfo != null) {
				taskInfo.put("detailColl", detailColl);
			}

			return this.getSmartImportTaskSchedule(taskInfo);
		}
	}

	private synchronized SmartImportTaskInfo getUserProgressTaskOrAddNotExists(TaskTypeEnum taskType)
			throws ShrWebBizException {
		UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
		SmartImportTaskInfo taskInfo = this.getUserProgressSmartImportTask(user, taskType);
		if (taskInfo == null) {
			taskInfo = this.addUserProgressTask(user, taskType);
		}

		return taskInfo;
	}

	private SmartImportTaskInfo getUserProgressSmartImportTask(UserInfo user, TaskTypeEnum taskType)
			throws ShrWebBizException {
		try {
			SmartImportTaskInfo taskInfo = SmartImportTaskFactory.getRemoteInstance()
					.getUserProgressTask(new ObjectUuidPK(user.getId()), taskType);
			return taskInfo;
		} catch (BOSException var4) {
			logger.error(var4.getMessage(), var4);
			throw new ShrWebBizException("获取解析任务失败！");
		}
	}

	private SmartImportTaskInfo getSmartImportTaskById(String taskId) throws ShrWebBizException {
		try {
			SmartImportTaskInfo taskInfo = SmartImportTaskFactory.getRemoteInstance()
					.getSmartImportTaskInfo(new ObjectUuidPK(taskId));
			return taskInfo;
		} catch (EASBizException var3) {
			logger.error(var3.getMessage(), var3);
			throw new ShrWebBizException("获取解析任务失败！");
		} catch (BOSException var4) {
			logger.error(var4.getMessage(), var4);
			throw new ShrWebBizException("获取解析任务失败！");
		}
	}

	private SmartImportTaskInfo addUserProgressTask(UserInfo user, TaskTypeEnum taskType) throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = null;

		try {
			taskInfo = SmartImportTaskFactory.getRemoteInstance().addUserProgressTask(user, taskType);
			return taskInfo;
		} catch (BOSException var5) {
			logger.error(var5.getMessage(), var5);
			throw new ShrWebBizException("服务器繁忙，请稍后重试！");
		}
	}

	/** @deprecated */
	@Deprecated
	public void resumeSmartImport(String recuritmentDemandId, String taskId) throws ShrWebBizException {
		if (StringUtils.isBlank(recuritmentDemandId)) {
			throw new ShrWebBizException("请选择招聘需求！");
		} else if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			logger.debug("recuritmentDemandId = " + recuritmentDemandId + ",taskId=" + taskId);
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(),
					TaskTypeEnum.SMARTIMPORT_RESUME);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo task = this.getSmartImportTaskById(taskId);
				if (task == null) {
					throw new ShrWebBizException("获取用户智能导入任务失败，请刷新重试！");
				} else {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseDemand(
								new ObjectUuidPK(task.getId()), new ObjectUuidPK(recuritmentDemandId));
						RecuritmentDemandInfo recDemandInfo = new RecuritmentDemandInfo();
						recDemandInfo.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
						task.setRecDemand(recDemandInfo);
					} catch (BOSException var9) {
						logger.error("******************设置智能导入所选需求失败！*****************");
						logger.error(var9.getMessage(), var9);
						throw new ShrWebBizException("服务器繁忙，请稍后重试！");
					}

					try {
						SmartImportTaskEntity entity = new SmartImportTaskEntity(task,
								new ResumeSmartImportImpl(recuritmentDemandId));
						this.submitTask(entity);
					} catch (BOSException var8) {
						logger.error(var8.getMessage(), var8);
						throw new ShrWebBizException();
					}
				}
			}
		}
	}

	/** @deprecated */
	@Deprecated
	public SmartImportTask resumeSmartImportAgain(String taskId) throws ShrWebBizException {
		if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(),
					TaskTypeEnum.SMARTIMPORT_RESUME);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo taskInfo = null;

				try {
					taskInfo = SmartImportTaskFactory.getRemoteInstance().getSmartImportTaskSchedule(
							new ObjectUuidPK(user.getId()), TaskTypeEnum.SMARTIMPORT_RESUME, taskId);
				} catch (BOSException var18) {
					logger.error(var18.getMessage(), var18);
					throw new ShrWebBizException("获取解析任务失败！");
				}

				String recuritmentDemandId = taskInfo.getRecDemand().getId().toString();
				SmartImportTaskInfo newTaskInfo = this.addUserProgressTask(user, TaskTypeEnum.SMARTIMPORT_RESUME);

				try {
					SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseDemand(
							new ObjectUuidPK(newTaskInfo.getId()), new ObjectUuidPK(recuritmentDemandId));
					RecuritmentDemandInfo recDemandInfo = new RecuritmentDemandInfo();
					recDemandInfo.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
					newTaskInfo.setRecDemand(recDemandInfo);
				} catch (BOSException var17) {
					logger.error("******************设置智能导入所选需求失败！*****************");
					logger.error(var17.getMessage(), var17);
					throw new ShrWebBizException("设置导入需求失败！");
				}

				SmartImportTaskDetailCollection newDetailColl = new SmartImportTaskDetailCollection();
				SmartImportTaskDetailCollection detailColl = (SmartImportTaskDetailCollection) taskInfo
						.get("detailColl");
				if (!RecBaseUtils.isEmpty(detailColl)) {
					Iterator<SmartImportTaskDetailInfo> iter = detailColl.iterator();
					SmartImportTaskDetailInfo detailInfo = null;

					while (iter.hasNext()) {
						detailInfo = (SmartImportTaskDetailInfo) iter.next();
						if (!TaskStateEnum.FINISHED.equals(detailInfo.getTaskState())
								&& !TaskStateEnum.SAVED.equals(detailInfo.getTaskState()) && !detailInfo.isIsDelete()) {
							SmartImportTaskDetailInfo newDetailInfo = new SmartImportTaskDetailInfo();
							newDetailInfo.setResumeFileName(detailInfo.getResumeFileName());
							newDetailInfo.setResumeFileType(detailInfo.getResumeFileType());
							newDetailInfo.setResumeFileData(detailInfo.getResumeFileData());
							newDetailInfo.setTaskState(TaskStateEnum.SAVED);
							newDetailInfo.setTask(newTaskInfo);
							newDetailColl.add(newDetailInfo);

							try {
								SmartImportTaskDetailFactory.getRemoteInstance().addnew(newDetailInfo);
							} catch (EASBizException var15) {
								logger.error(var15.getMessage(), var15);
								throw new ShrWebBizException("保存任务详情失败！");
							} catch (BOSException var16) {
								logger.error(var16.getMessage(), var16);
								throw new ShrWebBizException("保存任务详情失败！");
							}
						}
					}
				}

				if (newTaskInfo != null) {
					newTaskInfo.put("detailColl", newDetailColl);
				}

				try {
					SmartImportTaskEntity entity = new SmartImportTaskEntity(newTaskInfo,
							new ResumeSmartImportImpl(recuritmentDemandId));
					this.submitTask(entity);
				} catch (BOSException var14) {
					logger.error(var14.getMessage(), var14);
					throw new ShrWebBizException();
				}

				return this.getSmartImportTaskSchedule(newTaskInfo);
			}
		}
	}

	public void selfSmartImport(Context ctx, String taskId, String recuritmentDemandId, PersonInfo person,
			TaskTypeEnum taskType) throws ShrWebBizException {
		this.checkInfo(recuritmentDemandId, taskId, taskType);
		SmartImportTaskInfo runningTask = this.getRunningTask(ctx,
				ContextUtil.getCurrentUserInfo(ctx).getId().toString(), taskType);
		if (runningTask != null) {
			throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
		} else {
			SmartImportTaskInfo task = this.getSmartImportTaskById(taskId);
			if (task == null) {
				throw new ShrWebBizException("获取用户智能导入任务失败，请刷新重试！");
			} else {
				try {
					ISmartImportTask iSmartImportTask = SmartImportTaskFactory.getLocalInstance(ctx);
					task.setInterRecommendation(person);
					task.setSourceType(ResumeSourceType.SELF_UPLOAD);
					String sql;
					if (taskType.equals(TaskTypeEnum.SELF_ENTERPRISEARCHIVES)) {
						sql = "UPDATE T_REC_SmartImportTask SET FResumeClipID = '" + recuritmentDemandId
								+ "',FSourceType = 4 WHERE FID = '" + taskId + "'";
						ResumeClipBaseInfo resumeClip = new ResumeClipBaseInfo();
						resumeClip.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
						task.setResumeClip(resumeClip);
					} else {
						sql = "UPDATE T_REC_SmartImportTask SET FRecDemandID = '" + recuritmentDemandId
								+ "',FSourceType = 4 WHERE FID = '" + taskId + "'";
						iSmartImportTask.setSmartImportChooseDemand(new ObjectUuidPK(task.getId()),
								new ObjectUuidPK(recuritmentDemandId));
						RecuritmentDemandInfo recDemandInfo = new RecuritmentDemandInfo();
						recDemandInfo.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
						task.setRecDemand(recDemandInfo);
					}

					DbUtil.execute(ctx, sql);
				} catch (BOSException var12) {
					logger.error("******************员工自助上传简历，设置智能导入所选简历夹/招聘需求失败！*****************");
					logger.error(var12.getMessage(), var12);
					throw new ShrWebBizException("服务器繁忙，请稍后重试！");
				}

				try {
					SmartImportTaskEntity entity = null;
					if (taskType.equals(TaskTypeEnum.SELF_DEMAND)) {
						entity = new SmartImportTaskEntity(task,
								new ResumeSmartImportImpl(ctx, recuritmentDemandId, person, taskType));
					} else if (taskType.equals(TaskTypeEnum.SELF_ENTERPRISEARCHIVES)) {
						entity = new SmartImportTaskEntity(task,
								new ArchiveSmartImportImpl(ctx, recuritmentDemandId, person, taskType));
					}

					this.submitTask(entity);
				} catch (BOSException var11) {
					logger.error(var11.getMessage(), var11);
					throw new ShrWebBizException();
				}
			}
		}
	}

	public void resumeSmartImport(String taskId, TaskTypeEnum taskType, HashMap<String, String> paraMap)
			throws ShrWebBizException {
		String sourceType = "";
		String interRecommendationId = "";
		String resumeSourceId = "";
		String recuritmentDemandId = "";



		if(paraMap.containsKey("sourceType")) {
			sourceType = paraMap.get("sourceType");
		}
		if (paraMap.containsKey("interRecommendationId")) {
			interRecommendationId = paraMap.get("interRecommendationId");
		}
		if (paraMap.containsKey("resumeSourceId")) {
			resumeSourceId = paraMap.get("resumeSourceId");
		}
		if (paraMap.containsKey("recuritmentDemandId")) {
			recuritmentDemandId = paraMap.get("recuritmentDemandId");
		}

		if (taskType.equals(TaskTypeEnum.SMARTIMPORT_ARCHIVE)) {
			if (StringUtils.isBlank(recuritmentDemandId)) {
				throw new ShrWebBizException("请选择简历夹！");
			}
		} else if (StringUtils.isBlank(recuritmentDemandId)) {
			throw new ShrWebBizException("请选择招聘需求！");
		}

		if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			logger.debug("recuritmentDemandId = " + recuritmentDemandId + ",taskId=" + taskId);
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(), taskType);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo task = this.getSmartImportTaskById(taskId);
				if (task == null) {
					throw new ShrWebBizException("获取用户智能导入任务失败，请刷新重试！");
				} else {
					ResumeClipBaseInfo entity;
					if (taskType.equals(TaskTypeEnum.SMARTIMPORT_ARCHIVE)) {
						try {
							SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeClip(
									new ObjectUuidPK(task.getId()), new ObjectUuidPK(recuritmentDemandId));
							entity = new ResumeClipBaseInfo();
							entity.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
							task.setResumeClip(entity);
						} catch (BOSException var17) {
							logger.error("******************设置智能导入所选简历夹失败！*****************");
							logger.error(var17.getMessage(), var17);
							throw new ShrWebBizException("服务器繁忙，请稍后重试！");
						}
					} else {
						try {
							SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseDemand(
									new ObjectUuidPK(task.getId()), new ObjectUuidPK(recuritmentDemandId));
							RecuritmentDemandInfo recDemandInfo = new RecuritmentDemandInfo();
							recDemandInfo.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
							task.setRecDemand(recDemandInfo);
						} catch (BOSException var16) {
							logger.error("******************设置智能导入所选需求失败！*****************");
							logger.error(var16.getMessage(), var16);
							throw new ShrWebBizException("服务器繁忙，请稍后重试！");
						}
					}

					if (StringUtils.isNotEmpty(resumeSourceId)) {
						try {
							SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeSource(
									new ObjectUuidPK(task.getId()), new ObjectUuidPK(resumeSourceId));
							ResumeSourceInfo resumeSourceInfo = new ResumeSourceInfo();
							resumeSourceInfo.setId(BOSUuid.read(resumeSourceId));
							task.setResumeSource(resumeSourceInfo);
						} catch (BOSException var15) {
							logger.error("******************设置智能导入简历来源失败！*****************");
							logger.error(var15.getMessage(), var15);
							throw new ShrWebBizException("服务器繁忙，请稍后重试！");
						}
					}

					if (StringUtils.isNotEmpty(interRecommendationId)) {
						try {
							SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseRecommendation(
									new ObjectUuidPK(task.getId().toString()), new ObjectUuidPK(interRecommendationId));
							PersonInfo personInfo = new PersonInfo();
							personInfo.setId(BOSUuid.read(interRecommendationId));
							task.setInterRecommendation(personInfo);
						} catch (BOSException var14) {
							logger.error("******************设置智能导入所选内部推荐人失败！*****************");
							logger.error(var14.getMessage(), var14);
						}
					}

					if (StringUtils.isNotEmpty(sourceType)) {
						try {
							SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseSourceType(
									new ObjectUuidPK(task.getId().toString()), sourceType);
							task.setSourceType(ResumeSourceType.getEnum(Integer.parseInt(sourceType)));
						} catch (BOSException var13) {
							logger.error("******************设置智能导入所选来源类型失败！*****************");
							logger.error(var13.getMessage(), var13);
						}
					}

					// e二开字段
					// 是否重点人才
					if (paraMap.containsKey("isKeyTalent")) {
						if (StringUtils.equals(paraMap.get("isKeyTalent"), "true")) {
							task.put("isKeyTalent", 1);
						} else {
							task.put("isKeyTalent", 0);
						}

					}
					// 电话号码
					if (paraMap.containsKey("phoneModify")) {
						task.put("phoneModify", paraMap.get("phoneModify"));
					}
					// over
					try {
						SmartImportTaskEntity entity1;
						if (taskType.equals(TaskTypeEnum.SMARTIMPORT_RECOMMENDRESUME)) {
							entity1 = new SmartImportTaskEntity(task, new RecommendResumeSmartImportImpl(
									recuritmentDemandId, interRecommendationId, sourceType));
						} else if (taskType.equals(TaskTypeEnum.SMARTIMPORT_RESUME)) {
							entity1 = new SmartImportTaskEntity(task, new ResumeSmartImportImpl(ctx, recuritmentDemandId,
									resumeSourceId, interRecommendationId, sourceType));
						} else {
							entity1 = new SmartImportTaskEntity(task, new ArchiveSmartImportImpl(ctx,
									recuritmentDemandId, resumeSourceId, interRecommendationId, sourceType));
						}

						this.submitTask(entity1);
					} catch (BOSException var12) {
						logger.error(var12.getMessage(), var12);
						throw new ShrWebBizException();
					}
				}
			}
		}
	}

	public SmartImportTask resumeSmartImportAgain(String taskId, TaskTypeEnum taskType) throws ShrWebBizException {
		if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(), taskType);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo taskInfo = null;

				try {
					taskInfo = SmartImportTaskFactory.getRemoteInstance()
							.getSmartImportTaskSchedule(new ObjectUuidPK(user.getId()), taskType, taskId);
				} catch (BOSException var26) {
					logger.error(var26.getMessage(), var26);
					throw new ShrWebBizException("获取解析任务失败！");
				}

				String recuritmentDemandId = taskInfo.getRecDemand().getId().toString();
				String interRecommendationId = "";
				if (null != taskInfo.getInterRecommendation()) {
					interRecommendationId = taskInfo.getInterRecommendation().getId().toString();
				}

				String resumeSourceId = "";
				if (null != taskInfo.getResumeSource()) {
					resumeSourceId = taskInfo.getResumeSource().getId().toString();
				}

				String sourceType = "";
				if (null != taskInfo.getSourceType()) {
					sourceType = taskInfo.getSourceType().getValue() + "";
				}

				SmartImportTaskInfo newTaskInfo = this.addUserProgressTask(user, taskType);
				if (taskType.equals(TaskTypeEnum.SMARTIMPORT_ARCHIVE)) {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeClip(
								new ObjectUuidPK(newTaskInfo.getId()), new ObjectUuidPK(recuritmentDemandId));
						ResumeClipBaseInfo resumeClip = new ResumeClipBaseInfo();
						resumeClip.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
						newTaskInfo.setResumeClip(resumeClip);
					} catch (BOSException var25) {
						logger.error("******************设置智能导入所选简历夹失败！*****************");
						logger.error(var25.getMessage(), var25);
						throw new ShrWebBizException("服务器繁忙，请稍后重试！");
					}
				} else {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseDemand(
								new ObjectUuidPK(newTaskInfo.getId()), new ObjectUuidPK(recuritmentDemandId));
						RecuritmentDemandInfo recDemandInfo = new RecuritmentDemandInfo();
						recDemandInfo.setId((new ObjectUuidPK(recuritmentDemandId)).getKeyValue());
						newTaskInfo.setRecDemand(recDemandInfo);
					} catch (BOSException var24) {
						logger.error("******************设置智能导入所选需求失败！*****************");
						logger.error(var24.getMessage(), var24);
						throw new ShrWebBizException("服务器繁忙，请稍后重试！");
					}
				}

				if (StringUtils.isNotEmpty(resumeSourceId)) {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeSource(
								new ObjectUuidPK(newTaskInfo.getId()), new ObjectUuidPK(resumeSourceId));
						ResumeSourceInfo resumeSourceInfo = new ResumeSourceInfo();
						resumeSourceInfo.setId(BOSUuid.read(resumeSourceId));
						newTaskInfo.setResumeSource(resumeSourceInfo);
					} catch (BOSException var23) {
						logger.error("******************设置智能导入简历来源失败！*****************");
						logger.error(var23.getMessage(), var23);
						throw new ShrWebBizException("服务器繁忙，请稍后重试！");
					}
				}

				if (StringUtils.isNotEmpty(interRecommendationId)) {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseRecommendation(
								new ObjectUuidPK(newTaskInfo.getId().toString()),
								new ObjectUuidPK(interRecommendationId));
						PersonInfo personInfo = new PersonInfo();
						personInfo.setId(BOSUuid.read(interRecommendationId));
						newTaskInfo.setInterRecommendation(personInfo);
					} catch (BOSException var22) {
						logger.error("******************设置智能导入所选内部推荐人失败！*****************");
						logger.error(var22.getMessage(), var22);
					}
				}

				if (StringUtils.isNotEmpty(sourceType)) {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseSourceType(
								new ObjectUuidPK(newTaskInfo.getId().toString()), sourceType);
						newTaskInfo.setSourceType(ResumeSourceType.getEnum(Integer.parseInt(sourceType)));
					} catch (BOSException var21) {
						logger.error("******************设置智能导入所选内部推荐人失败！*****************");
						logger.error(var21.getMessage(), var21);
					}
				}

				SmartImportTaskDetailCollection newDetailColl = new SmartImportTaskDetailCollection();
				SmartImportTaskDetailCollection detailColl = (SmartImportTaskDetailCollection) taskInfo
						.get("detailColl");
				Iterator entity;
				if (!RecBaseUtils.isEmpty(detailColl)) {
					entity = detailColl.iterator();
					SmartImportTaskDetailInfo detailInfo = null;

					while (entity.hasNext()) {
						detailInfo = (SmartImportTaskDetailInfo) entity.next();
						if (!TaskStateEnum.FINISHED.equals(detailInfo.getTaskState())
								&& !TaskStateEnum.SAVED.equals(detailInfo.getTaskState()) && !detailInfo.isIsDelete()) {
							SmartImportTaskDetailInfo newDetailInfo = new SmartImportTaskDetailInfo();
							newDetailInfo.setResumeFileName(detailInfo.getResumeFileName());
							newDetailInfo.setResumeFileType(detailInfo.getResumeFileType());
							newDetailInfo.setResumeFileData(detailInfo.getResumeFileData());
							newDetailInfo.setTaskState(TaskStateEnum.SAVED);
							newDetailInfo.setTask(newTaskInfo);
							newDetailColl.add(newDetailInfo);

							try {
								SmartImportTaskDetailFactory.getRemoteInstance().addnew(newDetailInfo);
							} catch (EASBizException var19) {
								logger.error(var19.getMessage(), var19);
								throw new ShrWebBizException("保存任务详情失败！");
							} catch (BOSException var20) {
								logger.error(var20.getMessage(), var20);
								throw new ShrWebBizException("保存任务详情失败！");
							}
						}
					}
				}

				if (newTaskInfo != null) {
					newTaskInfo.put("detailColl", newDetailColl);
				}

				try {
					SmartImportTaskEntity entity1;
					if (taskType.equals(TaskTypeEnum.SMARTIMPORT_RECOMMENDRESUME)) {
						entity1 = new SmartImportTaskEntity(newTaskInfo, new RecommendResumeSmartImportImpl(
								recuritmentDemandId, interRecommendationId, sourceType));
					} else if (taskType.equals(TaskTypeEnum.SMARTIMPORT_RESUME)) {
						entity1 = new SmartImportTaskEntity(newTaskInfo, new ResumeSmartImportImpl(ctx,
								recuritmentDemandId, resumeSourceId, interRecommendationId, sourceType));
					} else {
						entity1 = new SmartImportTaskEntity(newTaskInfo, new ArchiveSmartImportImpl(ctx,
								recuritmentDemandId, resumeSourceId, interRecommendationId, sourceType));
					}

					this.submitTask(entity1);
				} catch (BOSException var18) {
					logger.error(var18.getMessage(), var18);
					throw new ShrWebBizException();
				}

				return this.getSmartImportTaskSchedule(newTaskInfo);
			}
		}
	}

	private void checkInfo(String recuritmentDemandId, String taskId, TaskTypeEnum taskType) throws ShrWebBizException {
		if (taskType.equals(TaskTypeEnum.SELF_ENTERPRISEARCHIVES)) {
			if (StringUtils.isBlank(recuritmentDemandId)) {
				throw new ShrWebBizException("请选择简历夹！");
			}
		} else if (StringUtils.isBlank(recuritmentDemandId)) {
			throw new ShrWebBizException("请选择招聘需求！");
		}

		if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		}
	}

	/** @deprecated */
	@Deprecated
	public void archiveSmartImport(String resumeClipId, String taskId) throws ShrWebBizException {
		if (StringUtils.isBlank(resumeClipId)) {
			throw new ShrWebBizException("请选择目标简历夹！");
		} else if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(),
					TaskTypeEnum.SMARTIMPORT_ARCHIVE);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo task = this.getSmartImportTaskById(taskId);
				if (task == null) {
					throw new ShrWebBizException("获取用户智能导入任务失败，请刷新重试！");
				} else {
					try {
						SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeClip(
								new ObjectUuidPK(task.getId()), new ObjectUuidPK(resumeClipId));
						ResumeClipBaseInfo resumeClip = new ResumeClipBaseInfo();
						resumeClip.setId((new ObjectUuidPK(resumeClipId)).getKeyValue());
						task.setResumeClip(resumeClip);
					} catch (BOSException var10) {
						logger.error("******************设置智能导入所选简历夹失败！*****************");
						logger.error(var10.getMessage(), var10);
						throw new ShrWebBizException("服务器繁忙，请稍后重试！");
					}

					try {
						SmartImportTaskEntity entity = new SmartImportTaskEntity(task,
								new ArchiveSmartImportImpl(resumeClipId));
						this.submitTask(entity);
					} catch (BOSException var9) {
						logger.error(var9.getMessage(), var9);
						throw new ShrWebBizException(var9.getMessage());
					}
				}
			}
		}
	}

	/** @deprecated */
	@Deprecated
	public SmartImportTask archiveSmartImportAgain(String taskId) throws ShrWebBizException {
		if (StringUtils.isBlank(taskId)) {
			throw new ShrWebBizException("解析任务ID不能为空！");
		} else {
			UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
			Context ctx = SHRContext.getInstance().getContext();
			SmartImportTaskInfo runningTask = this.getRunningTask(ctx, user.getId().toString(),
					TaskTypeEnum.SMARTIMPORT_ARCHIVE);
			if (runningTask != null) {
				throw new ShrWebBizException("您当前存在正在运行的任务，请稍后重试！");
			} else {
				SmartImportTaskInfo taskInfo = null;

				try {
					taskInfo = SmartImportTaskFactory.getRemoteInstance().getSmartImportTaskSchedule(
							new ObjectUuidPK(user.getId()), TaskTypeEnum.SMARTIMPORT_ARCHIVE, taskId);
				} catch (BOSException var19) {
					logger.error(var19.getMessage(), var19);
					throw new ShrWebBizException("获取解析任务失败！");
				}

				String resumeClipId = taskInfo.getResumeClip().getId().toString();
				SmartImportTaskInfo newTaskInfo = this.addUserProgressTask(user, TaskTypeEnum.SMARTIMPORT_ARCHIVE);

				try {
					SmartImportTaskFactory.getRemoteInstance().setSmartImportChooseResumeClip(
							new ObjectUuidPK(newTaskInfo.getId()), new ObjectUuidPK(resumeClipId));
					ResumeClipBaseInfo resumeClip = new ResumeClipBaseInfo();
					resumeClip.setId((new ObjectUuidPK(resumeClipId)).getKeyValue());
					newTaskInfo.setResumeClip(resumeClip);
				} catch (BOSException var18) {
					logger.error("******************设置智能导入所选简历夹失败！*****************");
					logger.error(var18.getMessage(), var18);
					throw new ShrWebBizException("设置导入简历夹失败！");
				}

				SmartImportTask newTask = new SmartImportTask(newTaskInfo);
				SmartImportTaskDetailCollection newDetailColl = new SmartImportTaskDetailCollection();
				SmartImportTaskDetailCollection detailColl = (SmartImportTaskDetailCollection) taskInfo
						.get("detailColl");
				if (!RecBaseUtils.isEmpty(detailColl)) {
					Iterator<SmartImportTaskDetailInfo> iter = detailColl.iterator();
					SmartImportTaskDetailInfo detailInfo = null;

					while (iter.hasNext()) {
						detailInfo = (SmartImportTaskDetailInfo) iter.next();
						if (!TaskStateEnum.FINISHED.equals(detailInfo.getTaskState())
								&& !TaskStateEnum.SAVED.equals(detailInfo.getTaskState())) {
							SmartImportTaskDetailInfo newDetailInfo = new SmartImportTaskDetailInfo();
							newDetailInfo.setResumeFileName(detailInfo.getResumeFileName());
							newDetailInfo.setResumeFileType(detailInfo.getResumeFileType());
							newDetailInfo.setResumeFileData(detailInfo.getResumeFileData());
							newDetailInfo.setTaskState(TaskStateEnum.SAVED);
							newDetailInfo.setTask(newTaskInfo);
							newDetailColl.add(newDetailInfo);

							try {
								SmartImportTaskDetailFactory.getRemoteInstance().addnew(newDetailInfo);
							} catch (EASBizException var16) {
								logger.error(var16.getMessage(), var16);
								throw new ShrWebBizException("保存任务详情失败！");
							} catch (BOSException var17) {
								logger.error(var17.getMessage(), var17);
								throw new ShrWebBizException("保存任务详情失败！");
							}

							newTask.addNonExecuteTaskDetail(newDetailInfo);
							newTask.addTaskDetail(newDetailInfo);
						}
					}
				}

				newTask.setNonExecuteAmount(newDetailColl.size());
				newTask.setDoneAmount(0);
				newTask.setFailedAmount(0);
				newTask.setSuccessedAmount(0);
				newTask.setResumeAmount(newDetailColl.size());

				try {
					SmartImportTaskEntity entity = new SmartImportTaskEntity(newTaskInfo,
							new ArchiveSmartImportImpl(resumeClipId));
					this.submitTask(entity);
					return newTask;
				} catch (BOSException var15) {
					logger.error(var15.getMessage(), var15);
					throw new ShrWebBizException();
				}
			}
		}
	}

	private void submitTask(SmartImportTaskEntity taskEntity) throws ShrWebBizException {
		SmartImportTaskInfo task = taskEntity.getTaskInfo();

		try {
			SmartImportTaskFactory.getLocalInstance(taskEntity.getCtx()).taskSubmit(task);
		} catch (BOSException var4) {
			logger.error(var4.getMessage(), var4);
			throw new ShrWebBizException("任务提交异常，请稍后重试！");
		}

		Future future = RecSyncTaskThreadPool.getInstance().submitAsyncTask(taskEntity);
		this.afterSubmitTask(taskEntity, future);
	}

	private void afterSubmitTask(SmartImportTaskEntity taskEntity, Future future) {
		FutureTimeTask futureTimeTask = new FutureTimeTask(taskEntity, future);

		try {
			this.futureTaskQueue.put(futureTimeTask);
		} catch (InterruptedException var5) {
			logger.error("****************向添加超时任务队列添加任务失败！*****************");
			logger.error(var5.getMessage(), var5);
		}

	}

	public SmartImportTask getResumeSmartImportTaskSchedule(Context ctx, TaskTypeEnum taskType)
			throws ShrWebBizException {
		UserInfo user = RecWebBaseUtils.getCurrentUserInfo();
		return this.getSmartImportTaskSchedule(ctx, user.getId().toString(), taskType);
	}

	private SmartImportTask getSmartImportTaskSchedule(SmartImportTaskInfo taskInfo) {
		SmartImportTask task = null;
		if (taskInfo != null) {
			task = new SmartImportTask(taskInfo);
			SmartImportTaskDetailCollection detailColl = (SmartImportTaskDetailCollection) taskInfo.get("detailColl");
			if (!RecBaseUtils.isEmpty(detailColl)) {
				int i = 0;

				for (int size = detailColl.size(); i < size; ++i) {
					SmartImportTaskDetailInfo detailInfo = detailColl.get(i);
					if (TaskStateEnum.FINISHED.equals(detailInfo.getTaskState())) {
						task.addSuccessTaskDetail(detailInfo);
					} else if (TaskStateEnum.SAVED.equals(detailInfo.getTaskState())) {
						task.addNonExecuteTaskDetail(detailInfo);
					} else {
						task.addFailTaskDetail(detailInfo);
					}

					task.addTaskDetail(detailInfo);
				}

				task.setNonExecuteAmount(task.getNonExecuteDetails().size());
				task.setDoneAmount(task.getSuccessDetails().size() + task.getFailDetails().size());
				task.setFailedAmount(task.getFailDetails().size());
				task.setSuccessedAmount(task.getSuccessDetails().size());
				task.setResumeAmount(task.getDetails().size());
			}
		}

		return task;
	}

	private SmartImportTask getSmartImportTaskSchedule(Context ctx, String userId, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = this.getRunningTask(ctx, userId, taskType);
		if (taskInfo != null) {
			SmartImportTaskDetailCollection detailColl = this.getTaskDetailCollection(ctx, taskInfo.getId().toString());
			taskInfo.put("detailColl", detailColl);
			return this.getSmartImportTaskSchedule(taskInfo);
		} else {
			taskInfo = this.getLastEndTask(ctx, userId, taskType);
			if (taskInfo != null) {
				SmartImportTaskInfo savedTaskInfo = this.getLastSavedTask(ctx, userId, taskType);
				SmartImportTaskDetailCollection detailColl;
				SmartImportTask task;
				if (savedTaskInfo != null) {
					detailColl = this.getTaskDetailCollection(ctx, savedTaskInfo.getId().toString());
					savedTaskInfo.put("detailColl", detailColl);
					task = this.getSmartImportTaskSchedule(savedTaskInfo);
				} else {
					task = new SmartImportTask();
				}

				task.setLastTaskInfo(taskInfo);
				detailColl = this.getTaskDetailCollection(ctx, taskInfo.getId().toString());
				int i = 0;

				for (int size = detailColl.size(); i < size; ++i) {
					SmartImportTaskDetailInfo detailInfo = detailColl.get(i);
					if (!detailInfo.isIsDelete()) {
						if (TaskStateEnum.FINISHED.equals(detailInfo.getTaskState())) {
							task.addSuccessTaskDetail(detailInfo);
						} else {
							task.addFailTaskDetail(detailInfo);
						}

						task.addTaskDetail(detailInfo);
					}
				}

				task.setFailedAmount(task.getFailDetails().size());
				task.setSuccessedAmount(task.getSuccessDetails().size());
				task.setDoneAmount(task.getSuccessDetails().size() + task.getFailDetails().size());
				task.setResumeAmount(task.getSuccessDetails().size() + task.getFailDetails().size());
				return task;
			} else {
				SmartImportTaskInfo savedTaskInfo = this.getLastSavedTask(ctx, userId, taskType);
				if (savedTaskInfo != null) {
					SmartImportTaskDetailCollection detailColl = this.getTaskDetailCollection(ctx,
							savedTaskInfo.getId().toString());
					savedTaskInfo.put("detailColl", detailColl);
					return this.getSmartImportTaskSchedule(savedTaskInfo);
				} else {
					return null;
				}
			}
		}
	}

	private SmartImportTaskInfo getLastSavedTask(Context ctx, String userId, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = null;
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(new FilterItemInfo("creator", userId, CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskType", taskType.getValue(), CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 1, CompareType.EQUALS));
		view.setFilter(filter);
		SorterItemCollection sorterColl = new SorterItemCollection();
		SorterItemInfo sorterItemInfo = new SorterItemInfo("createTime");
		sorterItemInfo.setSortType(SortType.DESCEND);
		sorterColl.add(sorterItemInfo);
		view.setSorter(sorterColl);
		SelectorItemCollection selector = new SelectorItemCollection();
		selector.add("*");
		selector.add("recDemand.*");
		selector.add("interRecommendation.*");
		selector.add("resumeSource.*");
		selector.add("recDemand.recuritPosition.*");
		selector.add("recDemand.hrJob.*");
		selector.add("resumeClip.*");
		view.setSelector(selector);

		try {
			SmartImportTaskCollection taskColl = SmartImportTaskFactory.getLocalInstance(ctx)
					.getSmartImportTaskCollection(view);
			if (!RecDBUtils.isEmpty(taskColl)) {
				taskInfo = taskColl.get(0);
			}

			return taskInfo;
		} catch (BOSException var12) {
			logger.error(var12.getMessage(), var12);
			throw new ShrWebBizException("获取解析任务失败");
		}
	}

	private SmartImportTaskInfo getRunningTask(Context ctx, String userId, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = null;
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(new FilterItemInfo("creator", userId, CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskType", taskType.getValue(), CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 1, CompareType.NOTEQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 7, CompareType.NOTEQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 6, CompareType.NOTEQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 8, CompareType.NOTEQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 9, CompareType.NOTEQUALS));
		view.setFilter(filter);
		SorterItemCollection sorterColl = new SorterItemCollection();
		SorterItemInfo sorterItemInfo = new SorterItemInfo("taskStartDate");
		sorterItemInfo.setSortType(SortType.DESCEND);
		sorterColl.add(sorterItemInfo);
		view.setSorter(sorterColl);
		SelectorItemCollection selector = new SelectorItemCollection();
		selector.add("*");
		selector.add("recDemand.*");
		selector.add("interRecommendation.*");
		selector.add("resumeSource.*");
		selector.add("recDemand.recuritPosition.*");
		selector.add("recDemand.hrJob.*");
		selector.add("resumeClip.*");
		view.setSelector(selector);

		try {
			SmartImportTaskCollection taskColl = SmartImportTaskFactory.getLocalInstance(ctx)
					.getSmartImportTaskCollection(view);
			if (!RecDBUtils.isEmpty(taskColl)) {
				taskInfo = taskColl.get(0);
			}

			return taskInfo;
		} catch (BOSException var12) {
			logger.error(var12.getMessage(), var12);
			throw new ShrWebBizException("获取正在运行中的解析任务失败");
		}
	}

	private SmartImportTaskInfo getLastEndTask(Context ctx, String userId, TaskTypeEnum taskType)
			throws ShrWebBizException {
		SmartImportTaskInfo taskInfo = null;
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(new FilterItemInfo("creator", userId, CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskType", taskType.getValue(), CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 7, CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 6, CompareType.EQUALS));
		filter.getFilterItems().add(new FilterItemInfo("taskState", 8, CompareType.EQUALS));
		filter.setMaskString("#0 and #1 and (#2 or #3 or #4)");
		view.setFilter(filter);
		SorterItemCollection sorterColl = new SorterItemCollection();
		SorterItemInfo sorterItemInfo = new SorterItemInfo("taskStartDate");
		sorterItemInfo.setSortType(SortType.DESCEND);
		sorterColl.add(sorterItemInfo);
		view.setSorter(sorterColl);
		SelectorItemCollection selector = new SelectorItemCollection();
		selector.add("*");
		selector.add("recDemand.*");
		selector.add("interRecommendation.*");
		selector.add("resumeSource.*");
		selector.add("recDemand.recuritPosition.*");
		selector.add("recDemand.hrJob.*");
		selector.add("resumeClip.*");
		view.setSelector(selector);

		try {
			SmartImportTaskCollection taskColl = SmartImportTaskFactory.getLocalInstance(ctx)
					.getSmartImportTaskCollection(view);
			if (!RecDBUtils.isEmpty(taskColl)) {
				taskInfo = taskColl.get(0);
			}

			return taskInfo;
		} catch (BOSException var12) {
			logger.error(var12.getMessage(), var12);
			throw new ShrWebBizException("获取解析任务失败");
		}
	}

	private SmartImportTaskDetailCollection getTaskDetailCollection(Context ctx, String taskId)
			throws ShrWebBizException {
		EntityViewInfo evi = new EntityViewInfo();
		FilterInfo filter01 = new FilterInfo();
		filter01.getFilterItems().add(new FilterItemInfo("task", taskId, CompareType.EQUALS));
		evi.setFilter(filter01);
		SelectorItemCollection selectorColl01 = evi.getSelector();
		selectorColl01.add(new SelectorItemInfo("id"));
		selectorColl01.add(new SelectorItemInfo("resumeFileName"));
		selectorColl01.add(new SelectorItemInfo("resumeFileType"));
		selectorColl01.add(new SelectorItemInfo("taskState"));
		selectorColl01.add(new SelectorItemInfo("info"));
		selectorColl01.add(new SelectorItemInfo("task"));
		selectorColl01.add(new SelectorItemInfo("isDelete"));
		SmartImportTaskDetailCollection detailColl = null;

		try {
			detailColl = SmartImportTaskDetailFactory.getLocalInstance(ctx).getSmartImportTaskDetailCollection(evi);
			return detailColl;
		} catch (BOSException var8) {
			logger.error(var8.getMessage(), var8);
			throw new ShrWebBizException("获取任务详情失败");
		}
	}

	public void affirmSmartImportTaskResult(String smartImportTaskId) throws ShrWebBizException {
		try {
			SmartImportTaskFactory.getRemoteInstance().affirmTask(new ObjectUuidPK(smartImportTaskId));
		} catch (BOSException var3) {
			logger.error(var3.getMessage(), var3);
			throw new ShrWebBizException("确认失败，请重试！");
		}
	}

	public void deleteResume(String taskDetailIds) throws ShrWebBizException {
		if (StringUtils.isEmpty(taskDetailIds)) {
			throw new ShrWebBizException("请选择要删除的简历！");
		} else {
			String[] detailAry = taskDetailIds.split(",");

			try {
				SmartImportTaskDetailFactory.getRemoteInstance().deleteResumes(detailAry);
			} catch (BOSException var4) {
				logger.error(var4.getMessage(), var4);
				throw new ShrWebBizException("服务器繁忙，请稍后重试！");
			}
		}
	}

	public void updateResume(Map<String, MultipartFile> fileMap, String resumeId, TaskTypeEnum taskType)
			throws ShrWebBizException {
		if (StringUtils.isEmpty(resumeId)) {
			throw new ShrWebBizException("简历更新失败，resumeId为空！");
		} else {
			Context ctx = SHRContext.getInstance().getContext();
			Iterator var5 = fileMap.entrySet().iterator();

			while (true) {
				if (var5.hasNext()) {
					Map.Entry<String, MultipartFile> entity = (Map.Entry) var5.next();
					MultipartFile mf = (MultipartFile) entity.getValue();
					FileSafeUtil.checkFileCompliance(ctx, mf, FILETYPELIST, "DOC,DOCX,TXT,RTF,PDF,MSG,WPS,JPG,PNG,GIF");
					String resumeFileName = mf.getOriginalFilename();
					byte[] resumeFileData = null;

					try {
						resumeFileData = mf.getBytes();
					} catch (IOException var12) {
						logger.error(var12.getMessage(), var12);
						throw new ShrWebBizException("附件上传失败，获取附件内容异常！");
					}

					if (StringUtils.isBlank(resumeFileName)) {
						throw new ShrWebBizException("简历附件名不能为空：" + resumeFileName);
					}

					if (resumeFileData != null && resumeFileData.length >= this.resumeFileMinSize) {
						if (resumeFileData.length > this.resumeFileMaxSize) {
							throw new ShrWebBizException("简历附件大小不能超过：" + this.resumeFileMaxSize / 1024 / 1024 + "M！");
						}

						try {
							boolean result = SmartResumeUpdateService.getInstance().smartResumeUpdate(ctx, mf, resumeId,
									taskType);
							if (!result) {
								throw new ShrWebBizException("简历更新失败，请联系系统管理员！");
							}
							continue;
						} catch (BOSException var11) {
							logger.error(var11.getMessage(), var11);
							throw new ShrWebBizException(var11.getMessage());
						}
					}

					throw new ShrWebBizException("简历附件不能为空！");
				}

				return;
			}
		}
	}

	public void recoverSmartImportTask(Context ctx) throws BOSException {
		if (logger.isDebugEnabled()) {
			logger.debug("**********启动智能导入恢复任务recoverSmartImportTask**********");
		}

		Calendar c = Calendar.getInstance();
		c.add(12, -35);
		Date timeout = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String timeoutStr = sdf.format(timeout);
		SmartImportTaskCollection taskColl = null;

		try {
			taskColl = SmartImportTaskFactory.getLocalInstance(ctx).getSmartImportTaskCollection(
					"where taskState = 9 or ((taskState = 4 or taskState = 2) and taskStartDate <= {ts'" + timeoutStr
							+ "'})");
		} catch (BOSException var11) {
			logger.error("*************************************获取超时及异常任务失败！*************************************");
			logger.error(var11.getMessage(), var11);
		}

		if (!RecBaseUtils.isEmpty(taskColl)) {
			Iterator<SmartImportTaskInfo> iter = taskColl.iterator();
			SmartImportTaskInfo taskInfo = null;

			while (iter.hasNext()) {
				taskInfo = (SmartImportTaskInfo) iter.next();

				try {
					SmartImportTaskEntity TaskEntity = null;
					if (TaskTypeEnum.SMARTIMPORT_RESUME.equals(taskInfo.getTaskType())) {
						TaskEntity = new SmartImportTaskEntity(taskInfo, new ResumeSmartImportImpl(ctx,
								taskInfo.getRecDemand().getId().toString(),
								taskInfo.getResumeSource() == null ? "" : taskInfo.getResumeSource().getId().toString(),
								taskInfo.getInterRecommendation() == null ? ""
										: taskInfo.getInterRecommendation().getId().toString(),
								taskInfo.getSourceType() == null ? "" : taskInfo.getSourceType().toString()));
					} else if (TaskTypeEnum.SMARTIMPORT_RECOMMENDRESUME.equals(taskInfo.getTaskType())) {
						TaskEntity = new SmartImportTaskEntity(taskInfo,
								new RecommendResumeSmartImportImpl(taskInfo.getRecDemand().getId().toString(),
										taskInfo.getInterRecommendation() == null ? ""
												: taskInfo.getInterRecommendation().getId().toString(),
										taskInfo.getSourceType() == null ? "" : taskInfo.getSourceType().toString()));
					} else {
						TaskEntity = new SmartImportTaskEntity(taskInfo, new ArchiveSmartImportImpl(ctx,
								taskInfo.getResumeClip().getId().toString(),
								taskInfo.getResumeSource() == null ? "" : taskInfo.getResumeSource().getId().toString(),
								taskInfo.getInterRecommendation() == null ? ""
										: taskInfo.getInterRecommendation().getId().toString(),
								taskInfo.getSourceType() == null ? "" : taskInfo.getSourceType().toString()));
					}

					if (TaskEntity != null) {
						this.submitTask(TaskEntity);
					}
				} catch (ShrWebBizException var10) {
					logger.error("*******************恢复" + taskInfo.getId() + "任务失败！*******************");
					logger.error(var10.getMessage(), var10);
				}
			}
		} else if (logger.isDebugEnabled()) {
			logger.debug("*************************************不存在超时及异常任务！*************************************");
		}

	}

	private class TimeoutTaskCheckThread implements Runnable {
		private TimeoutTaskCheckThread() {
		}

		public void run() {
			if (SmartImportServiceEx.logger.isDebugEnabled()) {
				SmartImportServiceEx.logger.debug("**************超时任务监控线程启动**************");
			}

			while (!Thread.currentThread().isInterrupted()) {
				try {
					FutureTimeTask future = (FutureTimeTask) SmartImportServiceEx.this.futureTaskQueue.take();
					if (!future.timeoutCancel()) {
						SmartImportServiceEx.this.futureTaskQueue.put(future);
					}

					Thread.sleep(100L);
				} catch (InterruptedException var2) {
					SmartImportServiceEx.logger.error("************************超时任务队列获取数据被异常中断***********************");
					SmartImportServiceEx.logger.error(var2.getMessage(), var2);
				} catch (Exception var3) {
					SmartImportServiceEx.logger.error("************************超时任务队列获取数据异常***********************");
					SmartImportServiceEx.logger.error(var3.getMessage(), var3);
				}
			}

			if (SmartImportServiceEx.logger.isDebugEnabled()) {
				SmartImportServiceEx.logger.debug("**************超时任务监控线程中断**************");
			}

		}
	}

	private class FutureTimeTask {
		private Future future;
		private SmartImportTaskEntity taskEntity;

		public FutureTimeTask(SmartImportTaskEntity taskEntity, Future future) {
			this.taskEntity = taskEntity;
			this.future = future;
		}

		public boolean timeoutCancel() {
			SmartImportTaskInfo taskInfo = this.taskEntity.getTaskInfo();
			if (taskInfo.getTaskState().equals(TaskStateEnum.RUNNING)) {
				if (!this.future.isDone()) {
					long now = System.currentTimeMillis();
					if (now - taskInfo.getTaskStartDate().getTime() <= SmartImportServiceEx.this.taskTimeoutMillis) {
						return false;
					}

					this.future.cancel(true);
				}
			} else if (taskInfo.getTaskState().equals(TaskStateEnum.SUBMITED)) {
				return false;
			}

			return true;
		}
	}

	private static class SmartImportServiceHolder {
		private static final SmartImportServiceEx service = new SmartImportServiceEx();

		private SmartImportServiceHolder() {
		}
	}
}
