package nc.impl.trn.transmng;

import java.util.ArrayList;
import java.util.List;

import nc.bs.framework.common.NCLocator;
import nc.bsutil.mmpub.dpub.SysParamUtil;
import nc.hr.frame.persistence.SimpleDocServiceTemplate;
import nc.itf.hr.frame.IPersistenceUpdate;
import nc.itf.hr.wa.IPsndocwadocManageService;
import nc.itf.trn.transmng.ITrnManageExService;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.hi.wadoc.PsndocWadocVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.trn.transmng.AggStapply;
import nc.vo.trn.transmng.StapplyVO;

/**
 * 调配业务扩展实现
 * @date 2021-1-5 20:04:14
 * @author chenzf
 *
 */
public class TrnManageExServiceImpl extends SimpleDocServiceTemplate implements ITrnManageExService {

	public TrnManageExServiceImpl() {
		super("TrnManageEx");
	}

	private IPersistenceUpdate getPersistenceUpdate() {
        return NCLocator.getInstance().lookup(IPersistenceUpdate.class);
    }
	
	private IPsndocwadocManageService getIPsndocwadocManageService() {
		return NCLocator.getInstance().lookup(IPsndocwadocManageService.class);
	}
	
	@Override
	public Boolean updatePsn(AggStapply[] vos) throws BusinessException {
        String[] updateFields = {PsnJobVO.PK_POST,"jobglbdef6"};
        List<PsnJobVO> jobList = new ArrayList();
        //是否生成定调资信息
        boolean check = SysParamUtil.getParamValueAsBoolean(((StapplyVO) vos[0].getParentVO()).getPk_org(), "TRN0099");
		List<PsndocWadocVO> wadocList = new ArrayList();
        for(AggStapply vo : vos) {
			StapplyVO stapplyVO = (StapplyVO) vo.getParentVO();
			//更新工作信息  薪等薪级jobglbdef6 pk_post岗位
			Object jobglbdef6 = stapplyVO.getAttributeValue("newjobglbdef6");
			PsnJobVO jobVO = queryByPk(PsnJobVO.class, stapplyVO.getPk_psnjob());
			jobVO.setPk_post(stapplyVO.getNewpk_post());
			jobVO.setAttributeValue("jobglbdef6", jobglbdef6);
			jobList.add(jobVO);
			//生成定调资信息hi_psndoc_wadoc
			if(check) {
				if(jobglbdef6 != null && !"".equals(jobglbdef6.toString())) {
					stapplyVO.setEffectdate(jobVO.getBegindate());
					PsndocWadocVO wadocVO = createWadocVO(stapplyVO);
					wadocList.add(wadocVO);
				}
			}
			
		}
		if(jobList.size() > 0) {
			getPersistenceUpdate().updateVOArray(null, jobList.toArray(new PsnJobVO[0]), updateFields, null);
		}
		if(wadocList.size() > 0) {
			getIPsndocwadocManageService().insertArray(wadocList.toArray(new PsndocWadocVO[0]));
		}
		
		return null;
	}
	
	public PsndocWadocVO createWadocVO(StapplyVO stapplyVO) {
		PsndocWadocVO wadocVO = new PsndocWadocVO();
		wadocVO.setPk_group(stapplyVO.getPk_group());
		wadocVO.setPk_org(stapplyVO.getPk_org());
		wadocVO.setPk_psnjob(stapplyVO.getPk_psnjob());
		wadocVO.setPk_psndoc(stapplyVO.getPk_psndoc());
		wadocVO.setAssgid(stapplyVO.getAssgid());
		wadocVO.setLastflag(UFBoolean.TRUE);
		wadocVO.setBegindate(stapplyVO.getEffectdate());
		return wadocVO;
	}

}
