package com.kingdee.eas.gyt.app;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.framework.DynamicObjectFactory;
import com.kingdee.bos.framework.IDynamicObject;
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.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.affair.EmpHireBizBillEntryCollection;
import com.kingdee.eas.hr.affair.EmpHireBizBillEntryInfo;
import com.kingdee.eas.hr.affair.EmpHireBizBillInfo;
import com.kingdee.eas.hr.affair.FlucInBizBillEntryInfo;
import com.kingdee.eas.hr.affair.FluctuationBizBillEntryCollection;
import com.kingdee.eas.hr.affair.FluctuationBizBillEntryInfo;
import com.kingdee.eas.hr.affair.FluctuationBizBillInfo;
import com.kingdee.eas.hr.affair.HRAffairBizBillEntryInfo;
import com.kingdee.eas.hr.base.EmpLaborRelationHisCollection;
import com.kingdee.eas.hr.base.EmpLaborRelationHisFactory;
import com.kingdee.eas.hr.base.EmpLaborRelationHisInfo;
import com.kingdee.eas.hr.base.EmpSalaryItemDataCollection;
import com.kingdee.eas.hr.base.EmpSalaryItemDataFactory;
import com.kingdee.eas.hr.base.EmpSalaryItemDataInfo;
import com.kingdee.eas.hr.base.EmpSalaryItemDataPartDetailCollection;
import com.kingdee.eas.hr.base.EmpSalaryItemDataPartDetailFactory;
import com.kingdee.eas.hr.base.EmpSalaryItemDataPartDetailInfo;
import com.kingdee.eas.hr.base.EmpSalaryItemDataPartFactory;
import com.kingdee.eas.hr.base.EmpSalaryItemDataPartInfo;
import com.kingdee.eas.hr.base.EmpSalaryItemTemplateCollection;
import com.kingdee.eas.hr.base.EmpSalaryItemTemplateFactory;
import com.kingdee.eas.hr.base.EmpSalaryItemTemplateInfo;
import com.kingdee.eas.hr.base.EmpSignFeeFactory;
import com.kingdee.eas.hr.base.EmpViewWay;
import com.kingdee.eas.hr.base.EmployeeTypeInfo;
import com.kingdee.eas.hr.base.IEmpLaborRelationHis;
import com.kingdee.eas.hr.base.IEmpSalaryItemDataPartDetail;
import com.kingdee.eas.hr.base.util.HRUtilExtend;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.compensation.FixAdjustSalaryCollection;
import com.kingdee.shr.compensation.FixAdjustSalaryFactory;
import com.kingdee.shr.compensation.FixAdjustSalaryInfo;
import com.kingdee.shr.recuritment.RecApprovalCollection;
import com.kingdee.shr.recuritment.RecApprovalFactory;
import com.kingdee.shr.recuritment.RecApprovalInfo;
import com.kingdee.shr.recuritment.RecBenefitCollection;
import com.kingdee.shr.recuritment.RecBenefitFactory;
import com.kingdee.shr.recuritment.RecBenefitGroupCollection;
import com.kingdee.shr.recuritment.RecBenefitGroupFactory;
import com.kingdee.shr.recuritment.RecBenefitGroupInfo;
import com.kingdee.shr.recuritment.RecBenefitInfo;
import com.kingdee.shr.recuritment.RecEntryRelationCollection;
import com.kingdee.shr.recuritment.RecEntryRelationFactory;
import com.kingdee.shr.recuritment.RecEntryRelationInfo;
import com.kingdee.util.DateTimeUtils;
import com.kingdee.util.NumericExceptionSubItem;
import com.kingdee.util.StringUtils;

public class BizHelpFacadeControllerBean extends AbstractBizHelpFacadeControllerBean {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6698078787923284884L;
	
	private static Logger logger = Logger.getLogger("com.kingdee.eas.gyt.app.BizHelpFacadeControllerBean");
	
	
	protected String _writeBack(Context ctx, String billID) throws BOSException, EASBizException {
		
		try {
			logger.info("-----BizHelpFacadeControllerBean._writeBack-----"+billID);
			
			return doWriteBack(ctx, billID);
			
		} catch (Throwable e) {
			
			setRollbackOnly();
			
			e.printStackTrace();
			
			logger.error(e.getMessage(),e);
			
			throw new EASBizException(new NumericExceptionSubItem("", e.getMessage()));
		}
	}


	protected String doWriteBack(Context ctx, String billID) throws BOSException, EASBizException {
		
		if(StringUtils.isEmpty(billID)) {
			
			return "参数错误，单据id不能为空";
		}
		
		//根据单据ID获取动态单据
		IObjectValue billInfo = getObjecValue(ctx,billID);
		
		if(null == billInfo) {
			return "没有获取到单据对象，请确认单据参数是否正确，单据ID："+billID;
		}
		
		logger.info("====单据编码===="+billInfo.getString("number")+"===单据类型======"+billInfo.getClass());
		
		//转正单
		if(billInfo instanceof EmpHireBizBillInfo) {
			
			processEmpHireBizBill(ctx,(EmpHireBizBillInfo)billInfo);
		}
		
		//调动单
		if(billInfo instanceof FluctuationBizBillInfo) {
			
			processFluctuationBizBillInfo(ctx,(FluctuationBizBillInfo)billInfo);
		}
		return "";
	}
	
	
	protected void processFluctuationBizBillInfo(Context ctx, FluctuationBizBillInfo billInfo) throws EASBizException, BOSException {
		
		FluctuationBizBillEntryCollection colls = billInfo.getEntrys();
		
		if(null == colls || colls.size() <= 0 ) {
			return;
		}
		
		for( int i=0,size=colls.size(); i<size; i++ ) {
			
			FluctuationBizBillEntryInfo entry = colls.get(i);
			
			logger.info("----调动单分录----"+entry.getId().toString());
			
			if(null == entry.getPerson()) {
				continue;
			}
			
			writeBackPerson(ctx,entry);
		}
		
	}


	/**
	 * 调动单的考察结束日期反写到员工信息.用工关系状态.预计转正日期
	 * @param ctx
	 * @param entry
	 * @throws BOSException 
	 * @throws EASBizException 
	 */
	protected void writeBackPerson(Context ctx, FluctuationBizBillEntryInfo entry) throws BOSException, EASBizException {
		
		PersonInfo person = entry.getPerson();
		
		//用工关系状态
		EmployeeTypeInfo employeeTypeInfo = entry.getEmpType();
		
		String 	personId = person.getId().toString(),
				empTypeId = employeeTypeInfo.getId().toString(),
				bizDate = DateTimeUtils.format(entry.getBizDate(),"yyyy-MM-dd HH:mm:ss");
		
		String test = HRUtilExtend.getDateStringWithoutMininuteTs(entry.getBizDate());
		
		logger.info("-----调动单分录-------"+entry.getId().toString()+"-----人员------"+personId);
		
		IEmpLaborRelationHis iEmpLaborRelationHis = EmpLaborRelationHisFactory.getLocalInstance(ctx);
		
		EmpLaborRelationHisCollection coll = iEmpLaborRelationHis.getEmpLaborRelationHisCollection(" where person.id='"+personId+"' and laborRelationState.id='"+empTypeId+"' and EFFDT='"+bizDate+"' and LEFFDT='2199-12-31 00:00:00' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return;
		}
		
		EmpLaborRelationHisInfo empLaborRelationHisInfo = coll.get(0);
		
		empLaborRelationHisInfo.setPlanFormalDate(entry.getBizDate());
		
		SelectorItemCollection sic = new SelectorItemCollection();
		sic.add(new SelectorItemInfo("planFormalDate"));
		
		iEmpLaborRelationHis.updatePartial(empLaborRelationHisInfo, sic);
		
		logger.info("----用工关系状态-----"+empLaborRelationHisInfo.getId()+"------预计转正日期-------"+entry.getBizDate()+"------调动单分录-----"+entry.getId());
	}


	/**
	 * 转正单处理
	 * 1、根据转正单的人员查询聘简历人事职单关系的历ID
	 * 2、过简历ID获取录用报批单
	 * @param ctx
	 * @param billInfo
	 * @return
	 * @throws BOSException 
	 * @throws EASBizException 
	 */
	protected void  processEmpHireBizBill(Context ctx, EmpHireBizBillInfo billInfo) throws BOSException, EASBizException {
		
		EmpHireBizBillEntryCollection colls = billInfo.getEntrys();
		
		if(null == colls || colls.size() <= 0 ) {
			return;
		}
		
		for( int i=0,size=colls.size(); i<size; i++ ) {
			
			EmpHireBizBillEntryInfo  entry = colls.get(i);
			
			logger.info("----转正单分录----"+entry.getId().toString());
			
			//录用报批单薪酬数据
			RecBenefitCollection recBenefitCollection = getRecBenefitCollection(ctx,entry,billInfo);
			
			if(recBenefitCollection == null || recBenefitCollection.size() <= 0) {
				continue;
			}
			
			//员工定调薪数据
			EmpSalaryItemDataInfo empSalaryItemDataInfo = saveSalaryData(ctx, entry);
			
			if(null == empSalaryItemDataInfo) {
				continue;
			}
			
			//员工定调薪数据分区
			EmpSalaryItemDataPartInfo empSalaryItemDataPartInfo = getEmpSalaryItemDataPartInfo(ctx,empSalaryItemDataInfo);
			
			if(null == empSalaryItemDataPartInfo) {
				continue;
			}
			
			//员工定调薪数据分区明细
			addEmpSalaryItemDataPartDetailInfo(ctx,empSalaryItemDataPartInfo,recBenefitCollection);
			
		}
		
	}
	
	protected EmpSalaryItemDataPartInfo getEmpSalaryItemDataPartInfo(Context ctx,EmpSalaryItemDataInfo empSalaryItemDataInfo) throws EASBizException, BOSException {
		
		EmpSalaryItemDataPartInfo empSalaryItemDataPartInfo = new EmpSalaryItemDataPartInfo();
		
		FilterInfo filter = new FilterInfo();
		
		filter.getFilterItems().add(new FilterItemInfo("empSalaryItemData.id", empSalaryItemDataInfo.getId().toString(), CompareType.EQUALS));
		
		if(EmpSalaryItemDataPartFactory.getLocalInstance(ctx).exists(filter)) {
			
			return EmpSalaryItemDataPartFactory.getLocalInstance(ctx).getEmpSalaryItemDataPartCollection("where empSalaryItemData.id='"+empSalaryItemDataInfo.getId().toString()+"' ").get(0);
		}
		
		empSalaryItemDataPartInfo.setEmpSalaryItemData(empSalaryItemDataInfo);
		
		empSalaryItemDataPartInfo.setIndex(0);
		
		empSalaryItemDataPartInfo.setName("月工资标准");
		
		empSalaryItemDataPartInfo.setNumber("001");
		
		empSalaryItemDataInfo.setCreator(ContextUtil.getCurrentUserInfo(ctx));
		
		Date date = new Date();
		 
		Timestamp time = new Timestamp(date.getTime());
		
		empSalaryItemDataPartInfo.setCreateTime(time);
		
		empSalaryItemDataPartInfo.setLastUpdateUser(ContextUtil.getCurrentUserInfo(ctx));
		
		empSalaryItemDataPartInfo.setLastUpdateTime(time);
		
		empSalaryItemDataPartInfo.setCU(ContextUtil.getCurrentCtrlUnit(ctx));
		
		IObjectPK pk = EmpSalaryItemDataPartFactory.getLocalInstance(ctx).addnew(empSalaryItemDataPartInfo);
		
		logger.info("----getEmpSalaryItemDataPartInfo---"+pk);
		
		return empSalaryItemDataPartInfo;
	}
	
	
	
	/**
	 * @param ctx
	 * @param empSalaryItemDataId 员工定调薪数据分区ID
	 * @return
	 * @throws BOSException
	 */
	protected Map<String,EmpSalaryItemDataPartDetailInfo> getEmpSalaryItemDataPartDetail(Context ctx,String empSalaryItemDataId) throws BOSException {
		
		IEmpSalaryItemDataPartDetail iEmpSalaryItemDataPartDetail = EmpSalaryItemDataPartDetailFactory.getLocalInstance(ctx);
		
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo selectfilter = new FilterInfo();
		selectfilter.getFilterItems().add(new FilterItemInfo("empSalaryItemDataPart.id", empSalaryItemDataId, CompareType.EQUALS));
		view.setFilter(selectfilter);
		EmpSalaryItemDataPartDetailCollection partCollection = iEmpSalaryItemDataPartDetail.getEmpSalaryItemDataPartDetailCollection(view);
		
		Map<String,EmpSalaryItemDataPartDetailInfo> map = new HashMap<String,EmpSalaryItemDataPartDetailInfo>();
		
		if(null == partCollection || partCollection.size() <= 0 ) {
			return Collections.emptyMap();
		}
		
		EmpSalaryItemDataPartDetailInfo info = null;
		
		for(int i=0,size=partCollection.size(); i<size; i++) {
			
			info = partCollection.get(i);
			
			map.put(String.valueOf(info.getEmpSalaryItemDataPart().getId())+"_"+String.valueOf(info.getSalaryItem().getId()), info);
		}
		
		return map;
	}
	
	/**
	 * 员工定调薪数据分区明细
	 * 
	 * @param ctx
	 * @param empSalaryItemDataPartInfo
	 * @param recBenefitCollection
	 * @throws EASBizException
	 * @throws BOSException
	 */
	protected void addEmpSalaryItemDataPartDetailInfo(Context ctx,EmpSalaryItemDataPartInfo empSalaryItemDataPartInfo,RecBenefitCollection recBenefitCollection) throws EASBizException, BOSException {
		
		CoreBaseCollection c = new CoreBaseCollection();
		
		//员工定调薪分区+定薪项目
		String flag = "";
		
		//返回员工定调薪数据分区明细
		Map<String,EmpSalaryItemDataPartDetailInfo> map = getEmpSalaryItemDataPartDetail(ctx,empSalaryItemDataPartInfo.getId().toString());
		
		for(int i=0,size=recBenefitCollection.size(); i<size; i++) {
			
			RecBenefitInfo recBenefitInfo = recBenefitCollection.get(i);
			
			EmpSalaryItemDataPartDetailInfo empSalaryItemDataPartDetailInfo = new EmpSalaryItemDataPartDetailInfo();
			
			empSalaryItemDataPartDetailInfo.setEmpSalaryItemDataPart(empSalaryItemDataPartInfo);
			
			empSalaryItemDataPartDetailInfo.setSalaryItem(recBenefitInfo.getSalaryItem());
			
			flag = empSalaryItemDataPartInfo.getId().toString()+"_"+recBenefitInfo.getSalaryItem().getId().toString();
			
			if(map.containsKey(flag)) {
				continue;
			}
			
			empSalaryItemDataPartDetailInfo.setCurrency(recBenefitInfo.getCurrency());
			
			empSalaryItemDataPartDetailInfo.setAmount(recBenefitInfo.getAmount());
			
			empSalaryItemDataPartDetailInfo.setDataType(recBenefitInfo.getDataType());
			
			empSalaryItemDataPartDetailInfo.setSource(0);
			
			empSalaryItemDataPartDetailInfo.setPrecision(recBenefitInfo.getPrecision());
			
			empSalaryItemDataPartDetailInfo.setDataLength(recBenefitInfo.getDataLength());
			
			empSalaryItemDataPartDetailInfo.setModulationRatio("0%");
			
			c.add(empSalaryItemDataPartDetailInfo);
		}
		
		if(null == c || c.size() <= 0 ) {
			return;
		}
		
		EmpSalaryItemDataPartDetailFactory.getLocalInstance(ctx).addnewBatchData(c);
		
		
	}
	
	/**
	 * 员工定调薪数据
	 * @param ctx
	 * @param entry
	 * @throws BOSException
	 * @throws EASBizException
	 */
	protected EmpSalaryItemDataInfo saveSalaryData(Context ctx, HRAffairBizBillEntryInfo entry) throws BOSException, EASBizException {
		
		EmpSalaryItemDataInfo empSalaryItemDataInfo = new EmpSalaryItemDataInfo();
		
		if (entry.getId() != null) {
//			deleteSalaryItemData(ctx, entry.getId());
//			empSalaryItemDataInfo.setEntryId(entry.getId().toString());
			return getEmpSalaryItemDataInfo(ctx,entry.getId());
		} else {
			BOSUuid entryId = BOSUuid.create(entry.getBOSType());
			empSalaryItemDataInfo.setEntryId(entryId.toString());
			entry.setId(entryId);
		}
		
		empSalaryItemDataInfo.setPerson(entry.getPerson());
		
		empSalaryItemDataInfo.setSalaryEffectDate(entry.getBizDate());
		
		empSalaryItemDataInfo.setIsSalaryStandard(false);
		
		empSalaryItemDataInfo.setCreator(ContextUtil.getCurrentUserInfo(ctx));
		
		Date date = new Date();
		 
		Timestamp time = new Timestamp(date.getTime());
		
		empSalaryItemDataInfo.setCreateTime(time);
		
		empSalaryItemDataInfo.setLastUpdateUser(ContextUtil.getCurrentUserInfo(ctx));
		
		empSalaryItemDataInfo.setLastUpdateTime(time);
		
		empSalaryItemDataInfo.setCU(ContextUtil.getCurrentCtrlUnit(ctx));
		
		empSalaryItemDataInfo.setEmpViewWay(EmpViewWay.NO_SHOW);
		
		empSalaryItemDataInfo.setIsShowDate(false);
		
		//这里待确认怎么取调整前的数据
		empSalaryItemDataInfo.setPreSalaryData("");
		
		FixAdjustSalaryInfo fixAdjustSalaryInfo = getFixAdjustSalaryInfo(ctx,entry.getPerson().getId().toString());
		
		if(null != fixAdjustSalaryInfo) {
			empSalaryItemDataInfo.setPreSalaryEffectDate(fixAdjustSalaryInfo.getEffectDay());
		}
		
		EmpSalaryItemTemplateInfo empSalaryItemTemplateInfo = getEmpSalaryItemTemplate(ctx);
		
		if(null != empSalaryItemTemplateInfo) {
			empSalaryItemDataInfo.setEmpSalaryItemTemplate(empSalaryItemTemplateInfo);
		}
		
		IObjectPK dataPk = EmpSalaryItemDataFactory.getLocalInstance(ctx).save(empSalaryItemDataInfo);
		
		logger.info("------saveSalaryData-----"+dataPk);
		
		return empSalaryItemDataInfo;
	}
	
	/**
	 * 定调薪
	 * @param ctx
	 * @param personID
	 * @return
	 * @throws BOSException
	 */
	protected FixAdjustSalaryInfo getFixAdjustSalaryInfo(Context ctx,String personID) throws BOSException {
		
		FixAdjustSalaryCollection coll = FixAdjustSalaryFactory.getLocalInstance(ctx).getFixAdjustSalaryCollection(" select id,effectDay,person.id where person.id='"+personID+"' order by lastUpdateTime desc");
		
		if(null == coll || coll.size() <= 0 ) {
			
			return null;
		}
		
		return coll.get(0);
		
	}
	
	/**
	 * 员工定调薪模板
	 * @param ctx
	 * @return
	 * @throws BOSException 
	 */
	protected EmpSalaryItemTemplateInfo getEmpSalaryItemTemplate(Context ctx) throws BOSException {
		
		EmpSalaryItemTemplateCollection coll = EmpSalaryItemTemplateFactory.getLocalInstance(ctx).getEmpSalaryItemTemplateCollection("select id,number,name where number='zztx' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return null;
		}
		return coll.get(0);
	}

	protected EmpSalaryItemDataInfo getEmpSalaryItemDataInfo(Context ctx ,BOSUuid entryId) throws BOSException {
		
		EmpSalaryItemDataCollection coll = EmpSalaryItemDataFactory.getLocalInstance(ctx).getEmpSalaryItemDataCollection("select id where entryId = '" + entryId.toString() + "'");
		
		if(null == coll || coll.size() <= 0 ) {
			return new EmpSalaryItemDataInfo();
		}
		
		return coll.get(0);
		
	}

	protected void deleteSalaryItemData(Context ctx, BOSUuid entryId) throws BOSException, EASBizException {
		
		EmpSalaryItemDataCollection coll = EmpSalaryItemDataFactory.getLocalInstance(ctx).getEmpSalaryItemDataCollection("select id where entryId = '" + entryId.toString() + "'");
		
		if (coll != null && coll.size() > 0) {
			
			Set<String> salaryItemDataIdSet = new HashSet<String>();

			for (int i = 0; i < coll.size(); ++i) {
				
				salaryItemDataIdSet.add(coll.get(i).getId().toString());
			}

			FilterInfo deletefilter = new FilterInfo();
			deletefilter.getFilterItems().add(new FilterItemInfo("empsalaryItemData", salaryItemDataIdSet.toString(), CompareType.INCLUDE));
			EmpSalaryItemDataPartFactory.getLocalInstance(ctx).delete(deletefilter);
			EmpSignFeeFactory.getLocalInstance(ctx).delete(deletefilter);
			deletefilter.getFilterItems().clear();
			deletefilter.getFilterItems().add(new FilterItemInfo("id", salaryItemDataIdSet.toString(), CompareType.INCLUDE));
			
			EmpSalaryItemDataFactory.getLocalInstance(ctx).delete(deletefilter);
		}

	}


	/**
	 * 根据转正单获取录用报批单的薪酬数据
	 * @param ctx
	 * @param entry
	 * @param billInfo
	 * @throws BOSException 
	 */
	protected RecBenefitCollection getRecBenefitCollection(Context ctx, EmpHireBizBillEntryInfo entry, EmpHireBizBillInfo billInfo) throws BOSException {
		
		RecBenefitCollection recBenefitCollection = null;
		
		PersonInfo person = entry.getPerson();
		
		if(null == person) {
			return recBenefitCollection;
		}
		
		String personId = person.getId().toString().trim();
		
		logger.info("------转正单分录id-------"+entry.getId().toString()+"---------员工id------"+personId);
		
		//招聘简历与人事入职单关系
		RecEntryRelationInfo recEntryRelationInfo = getRecEntryRelationInfo(ctx,personId);
		
		if(null == recEntryRelationInfo) {
			return recBenefitCollection;
		}
		
		//简历ID
		String resumeID = recEntryRelationInfo.getResumeID().trim();
		
		logger.info("-----简历ID------"+resumeID);
		
		//录用报批单
		RecApprovalInfo recApprovalInfo = getRecApproval(ctx,resumeID);
		
		if(null == recApprovalInfo) {
			return recBenefitCollection;
		}
		
		String recApprovalID = recApprovalInfo.getId().toString().trim();
		
		logger.info("----录用报批单ID------"+recApprovalID);
		
		RecBenefitGroupInfo recBenefitGroupInfo = getRecBenefitGroup(ctx,recApprovalID);
		
		if(null == recBenefitGroupInfo) {
			return recBenefitCollection;
		}
		
		String recBenefitGroupID = recBenefitGroupInfo.getId().toString().trim();
		
		logger.info("----薪酬福利分组ID------"+recBenefitGroupID);
		
		return  getRecBenefitCollection(ctx,recBenefitGroupID);
	
	}
	
	/**
	 * 录用报批薪酬福利
	 * @param ctx
	 * @param recBenefitGroupID 薪酬福利分组
	 * @return
	 * @throws BOSException
	 */
	protected RecBenefitCollection getRecBenefitCollection(Context ctx,String recBenefitGroupID) throws BOSException {
		
		RecBenefitCollection coll = RecBenefitFactory.getLocalInstance(ctx).getRecBenefitCollection(" where group.id='"+recBenefitGroupID+"' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return null;
		}
		
		return coll;
	}
	
	/**
	 * 薪酬福利分组
	 * @param ctx
	 * @param recApprovalID 录用报批单ID
	 * @return
	 * @throws BOSException
	 */
	protected RecBenefitGroupInfo getRecBenefitGroup(Context ctx,String recApprovalID) throws BOSException {
		
		RecBenefitGroupCollection coll = RecBenefitGroupFactory.getLocalInstance(ctx).getRecBenefitGroupCollection("select id where recApproval.id='"+recApprovalID+"' and number='001' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return null;
		}
		
		return coll.get(0);
	}
	
	/**
	 * 录用报批单
	 * @param ctx
	 * @param resumeID 简历ID
	 * @return
	 * @throws BOSException
	 */
	protected RecApprovalInfo getRecApproval(Context ctx,String resumeID) throws BOSException {
		
		RecApprovalCollection coll = RecApprovalFactory.getLocalInstance(ctx).getRecApprovalCollection("select id where resumeBaseRec.id='"+resumeID+"' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return null;
		}
		
		return coll.get(0);
		
	}
	
	/**
	 * 招聘简历与人事入职单关系
	 * @param ctx
	 * @param perosnID 员工
	 * @return
	 * @throws BOSException
	 */
	protected RecEntryRelationInfo getRecEntryRelationInfo(Context ctx,String perosnID) throws BOSException {
		
		RecEntryRelationCollection coll = RecEntryRelationFactory.getLocalInstance(ctx).getRecEntryRelationCollection("select entryID,resumeID where entryID='"+perosnID+"' ");
		
		if(null == coll || coll.size() <= 0 ) {
			return null;
		}
		
		return coll.get(0);
	}


	/**
	 * 单据ID对应的对象
	 * @param ctx
	 * @param billID 单据ID
	 * @return
	 * @throws BOSException
	 */
	protected IObjectValue getObjecValue(Context ctx,String billID) throws BOSException {
		
		billID = billID.trim();
		
		IDynamicObject iDynamicObject  = DynamicObjectFactory.getLocalInstance(ctx);
		
		ObjectUuidPK objpk = new ObjectUuidPK(billID);
		
		IObjectCollection objColl = iDynamicObject.getCollection(objpk.getObjectType(),"where id='"+billID+"' ");
		
		if(null == objColl || objColl.size() <= 0 ) {
			return null;
		}
		
		return objColl.getObject(0);
		
	}
	
}