package nc.nc2nc.ncimpl.pub.plgin.v65;

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

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.nc2nc.mid.pub.AbstractNcDataImporter;
import nc.nc2nc.mid.pub.DataClassWraper;
import nc.nc2nc.mid.pub.ISynObject;
import nc.nc2nc.mid.pub.OrgVO;
import nc.nc2nc.mid.pub.ParameterDef;
import nc.nc2nc.mid.pub.SynObjType;
import nc.nc2nc.mid.pub.SynUtil;
import nc.nc2nc.mid.pub.itf.ISyn2ncPubService;
import nc.nc2nc.mid.vo.SynPsnJobVO;
import nc.nc2nc.ncimpl.pub.plgin.v61.cvter.PsnJobVOConvert;
import nc.nc2nc.ncimpl.pub.plgin.v61.cvter.SynOperateType;
import nc.nc2nc.ncimpl.pub.plgin.v61.cvter.SynWriteDataParam;
import nc.nc2nc.ncimpl.pub.plgin.v61.itf.IImpPsnJobService;
import nc.nc2nc.ncimpl.vo.bak.SynPsnJobBakVO;
import nc.nc2nc.ncimpl.vo.cfg.context.SynContextCfgVO;
import nc.nc2nc.ncimpl.vo.log.SynLogBVO;
import nc.vo.bd.psn.PsnjobVO;
import nc.vo.pub.BusinessException;

public class PsnJobDataImporter extends AbstractNcDataImporter
{

	@Override
	public SynLogBVO[] impData2NC(DataClassWraper dataWraper) throws Exception
	{

		if (dataWraper == null)
		{
			return null;
		}
		
		List<SynLogBVO> synlogList = new ArrayList<SynLogBVO>();
		if (!doSynContextValidition(synlogList))
		{
			return synlogList.toArray(new SynLogBVO[synlogList.size()]);
		}
		
		SynContextCfgVO synContextCfg = this.getSynContext().getSynContextCfg();
		String to_org = synContextCfg.getTo_org();
		OrgVO orgvo = getSynContext().getTargetOrgVO();
		String pk_group = orgvo.getPk_group();

		ISyn2ncPubService syn2ncPubService = NCLocator.getInstance().lookup(ISyn2ncPubService.class);
		IImpPsnJobService psnService = NCLocator.getInstance().lookup(IImpPsnJobService.class);
		SynWriteDataParam objSynParam = new SynWriteDataParam();
		objSynParam.setTo_org(to_org);
		objSynParam.setTo_pk_group(pk_group);
		objSynParam.setTo_Pk_AccountingBook(synContextCfg.getTo_glbook());

		if (getStep().getDo_new_action().booleanValue() && dataWraper.getNewObjects() != null && dataWraper.getNewObjects().size() > 0)
		{
			boolean isCheckPsnRepeate = "true".equalsIgnoreCase(getPlgSelfParamValue("is_check_repeat"));
			List<ISynObject> addObjs = dataWraper.getNewObjects();
			for (ISynObject addObj : addObjs)
			{
				doAddAction(addObj, dataWraper, objSynParam,orgvo, to_org, pk_group, isCheckPsnRepeate, psnService, syn2ncPubService, synlogList);
			}
		}
		else
		{
			Logger.debug(">>>>>PsnJobDataImporter:插入人员工作记录数据时，数据为空！");
			SynLogBVO log = new SynLogBVO();
			log.setMessage(">>>>>PsnJobDataImporter:插入人员工作记录数据时，数据为空！");
			log.setObj_type(getEntityType());
			log.setSuccess(true);
			log.setPk_context(synContextCfg.getPk_context_cfg());
			log.setContext_code(synContextCfg.getCfg_code());
			log.setObj_name("<人员工作记录新增>");
			synlogList.add(log);
		}
		
		if (getStep().getDo_update_action().booleanValue() && dataWraper.getUpdateObjects() != null && dataWraper.getUpdateObjects().size() > 0)
		{
			boolean isCheckPsnRepeate = "true".equalsIgnoreCase(getPlgSelfParamValue("is_check_repeat"));
			List<ISynObject> updateObjs = dataWraper.getUpdateObjects();
			synlogList = new ArrayList<SynLogBVO>();
			for (ISynObject updateObj : updateObjs)
			{
				doUpdateAction(updateObj, dataWraper, objSynParam,orgvo, to_org, pk_group, isCheckPsnRepeate, psnService, syn2ncPubService, synlogList);
			}
		}
		else
		{
			Logger.debug(">>>>>PsnJobDataImporter:更新人员工作记录数据时，数据为空！");
			SynLogBVO log = new SynLogBVO();
			log.setMessage(">>>>>PsnJobDataImporter:更新人员工作记录数据时，数据为空！");
			log.setObj_type(getEntityType());
			log.setSuccess(true);
			log.setPk_context(synContextCfg.getPk_context_cfg());
			log.setContext_code(synContextCfg.getCfg_code());
			log.setObj_name("<人员工作记录更新>");
			synlogList.add(log);
		}
		
		return synlogList.toArray(new SynLogBVO[synlogList.size()]);
	}

	protected void doAddAction(ISynObject addObj, DataClassWraper dataWraper, SynWriteDataParam objSynParam,OrgVO orgvo, String to_org, String pk_group, boolean isCheckPsnRepeate, IImpPsnJobService psnService, ISyn2ncPubService syn2ncPubService, List<SynLogBVO> synlogList)
	{
		try
		{
			SynPsnJobVO svo = (SynPsnJobVO) addObj;

			ISynObject originobj = dataWraper.getOriginExtSystemSynObjById(addObj.getObject_id());
			SynPsnJobBakVO synbakvo = svo.toSynObjectBak();
			synbakvo.setSynObj(originobj);
			PsnJobVOConvert psnjobVOConvert = new PsnJobVOConvert();
			PsnjobVO psnjob = psnjobVOConvert.convert(svo, objSynParam,null,orgvo, to_org, pk_group, SynOperateType.syn_type_insert,synlogList);
			PsnjobVO addVo = psnService.insertPsnjob_RequiresNew(psnjob, isCheckPsnRepeate, synbakvo);
			if (null == addVo)
			{
				throw new BusinessException("插入人员工作记录数据失败，结果返回为空！编号为：" + svo.getObject_code() + ",名称为：" + svo.getObject_name());
			}
			
			svo.setPk_nc_object(addVo.getPk_psnjob());
			svo.setSuccess2nc(true);

			SynLogBVO log = new SynLogBVO(svo, " 插入成功！");
			log.setObj_type(getEntityType());
			synlogList.add(log);
		}
		catch (Exception e)
		{
			Logger.error(">>>>>PsnJobDataImporter:插入数据时出错" + e.getMessage(), e);
			SynLogBVO log = new SynLogBVO(addObj, "插入人员工作记录数据出错！错误信息：" + SynUtil.getExceptionStackMsg(e));
			log.setObj_type(getEntityType());
			log.setSuccess(false);
			synlogList.add(log);
		}
	}
	
	protected void doUpdateAction(ISynObject updateObj, DataClassWraper dataWraper, SynWriteDataParam objSynParam,OrgVO orgvo, String to_org, String pk_group, boolean isCheckPsnRepeate, IImpPsnJobService psnService, ISyn2ncPubService syn2ncPubService, List<SynLogBVO> synlogList)
	{
		try
		{
			SynPsnJobVO svo = (SynPsnJobVO) updateObj;

			ISynObject originobj = dataWraper.getOriginExtSystemSynObjById(updateObj.getObject_id());
			SynPsnJobBakVO synbakvo = svo.toSynObjectBak();
			synbakvo.setSynObj(originobj);

			PsnjobVO psnOld = psnService.queryPsnjob(svo.getPk_nc_object());
			if (null == psnOld)
			{
				try
				{
					// 删除映射关系
					syn2ncPubService.deleteByVOClassAndPk_RequiresNew(getEntityBackupVOClass(), updateObj.getPk_bak());
					
					// 需要执行新增
					doAddAction(updateObj, dataWraper, objSynParam,orgvo, to_org, pk_group, isCheckPsnRepeate, psnService, syn2ncPubService, synlogList);
				}
				catch(Exception e)
				{
					Logger.error("由【更新】转【新增】发生异常:" + e.getMessage(), e);
					updateObj.setSuccess2nc(false);
					synlogList.add(getSynLogBVO(updateObj, SynUtil.getExceptionStackMsg(e)));
				}
				
				return;
			}
			PsnJobVOConvert psnjobVOConvert = new PsnJobVOConvert();
			PsnjobVO psnjobvo = psnjobVOConvert.convert(svo, objSynParam,psnOld,orgvo, to_org, pk_group, SynOperateType.syn_type_update,synlogList);
			PsnjobVO updateVo = psnService.updatePsnjob_RequiresNew(psnjobvo, isCheckPsnRepeate, synbakvo);
			if (null == updateVo)
			{
				throw new BusinessException("更新人员工作记录失败，结果返回为空！编号为：" + svo.getObject_code() + ",名称为：" + svo.getObject_name());
			}
			svo.setSuccess2nc(true);

			SynLogBVO log = new SynLogBVO(svo, " 更新成功！");
			log.setObj_type(getEntityType());
			synlogList.add(log);
		}
		catch (Exception e)
		{
			Logger.error(">>>>>PsnJobDataImporter:更新数据时出错" + e.getMessage(), e);
			SynLogBVO log = new SynLogBVO(updateObj, "更新人员工作记录出错！错误信息：" + SynUtil.getExceptionStackMsg(e));
			log.setObj_type(getEntityType());
			log.setSuccess(false);
			synlogList.add(log);
		}
	}

	@Override
	public String getEntityType()
	{
		return SynObjType.PsnJob;
	}

	@Override
	public ParameterDef[] getPlginSelfParamDefs()
	{
		ParameterDef paramDef = new ParameterDef();
		paramDef.setCode("is_check_repeat");
		paramDef.setDescription("请输入true/false");
		paramDef.setName("是否对人员工作记录的重复进行检查");
		return new ParameterDef[] { paramDef };
	}

	@Override
	public Class<SynPsnJobBakVO> getEntityBackupVOClass()
	{
		return SynPsnJobBakVO.class;
	}

	@Override
	public Class<SynPsnJobVO> getEntityVOClass()
	{
		return SynPsnJobVO.class;
	}
	
	public SynLogBVO getSynLogBVO(ISynObject synObj, String msg)
	{
		SynLogBVO logBVO = new SynLogBVO(synObj, msg);
		logBVO.setObj_type(getEntityType());
		return logBVO;
	}

	@Override
	public boolean isNeedDataClassifyService()
	{
		return true;
	}
}
