package nc.bs.hrtrn.common;
import nc.bs.framework.common.NCLocator;
import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.itf.uap.rbac.IUserManageQuery;
import nc.vo.er.exception.ExceptionHandler;
import nc.vo.hi.entrymng.AggEntryapplyVO;
import nc.vo.hi.entrymng.EntryapplyVO;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.om.hrdept.HRDeptVO;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.pf.plugin.IPfMsgCustomReceiver;
import nc.vo.pub.pf.plugin.ReceiverVO;
import nc.vo.sm.UserVO;
import nc.vo.trn.transmng.AggStapply;
import nc.vo.trn.transmng.StapplyVO;

public class LeaderChargeReceiver implements IPfMsgCustomReceiver {

	@Override
	public ReceiverVO[] createReceivers() {
		ReceiverVO lrr = new ReceiverVO();
		lrr.setName("部门分管领导");
		lrr.setCode("leaderCharge");
		return new ReceiverVO[] {lrr};
	}

	@Override
	public UserVO[] queryUsers(ReceiverVO arg0, PfParameterVO paravo) {
		return queryOperatorByPsn(paravo);
	}

	private UserVO[] queryOperatorByPsn(PfParameterVO paravo) {
		try {
			String applyMan = null;
			IPersistenceRetrieve service = NCLocator.getInstance().lookup(IPersistenceRetrieve.class);
			String pk_psnjob = null;
			//入职申请
			if (paravo.m_preValueVo != null && paravo.m_preValueVo instanceof AggEntryapplyVO) {
				EntryapplyVO entryapplyVO = ((EntryapplyVO)((AggEntryapplyVO)paravo.m_preValueVo).getParentVO());
				pk_psnjob = entryapplyVO.getPk_psnjob();
				
			}else if (paravo.m_preValueVo != null && paravo.m_preValueVo instanceof AggStapply) {
				//离职申请
				StapplyVO stapplyVO = ((StapplyVO)((AggStapply)paravo.m_preValueVo).getParentVO());
				pk_psnjob = stapplyVO.getPk_psnjob();
			}
			if(pk_psnjob != null) {
				PsnJobVO psnjob = (PsnJobVO) service.retrieveByPk(null, PsnJobVO.class, pk_psnjob);
				HRDeptVO deptVO = (HRDeptVO) service.retrieveByPk(null, HRDeptVO.class, psnjob.getPk_dept());
				if(deptVO != null) {
					applyMan = deptVO.getAttributeValue("glbdef2") == null ? null : deptVO.getAttributeValue("glbdef2").toString();
				}
			}
			if (applyMan != null) {
				IUserManageQuery userManageQuery = (IUserManageQuery)NCLocator.getInstance().lookup(IUserManageQuery.class.getName());
				return userManageQuery.queryUserVOsByPsnDocID(applyMan);
			}
		} catch (Exception e) {
			ExceptionHandler.handleExceptionRuntime(e);
		}
		return new UserVO[0];
	}
}
