package nc.impl.hr.train;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.uap.lock.PKLock;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.hi.psndoc.TrainVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;

/**
 * 定时同步培训结果到人员培训信息子集中
 * @author chenzf
 * @date 2022-4-21 16:08:58
 */
public class TrainSubImportPlugin implements IBackgroundWorkPlugin {

	@Override
	public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
		PreAlertObject alertObj = new PreAlertObject();
		if(!PKLock.getInstance().addDynamicLock("TrainSubImportPlugin")) {
			alertObj.setReturnObj("任务执行中，请勿重复执行！");
			return alertObj;
	    }
		List<TrainVO> recordList = new ArrayList();
		BaseDAO dao = new BaseDAO();
		Map map = arg0.getKeyMap();
		Date date = new Date();
		int dyear = date.getYear() + 1900;
		int qyear = date.getYear() + 1900;
		if(map.get("nums") != null && !"".equals(map.get("nums"))) {
			int nums = Integer.valueOf(arg0.getKeyMap().get("nums").toString());
			if(nums == 0) {
				qyear = 2019;
			} else {
				qyear = dyear - Integer.valueOf(map.get("nums").toString());
			}
		} else {
			
		}
		String sql = 
			"select bd_psndoc.pk_psndoc,\n" +
			"       bd_psndoc.pk_group,\n" + 
			"       hi_psnjob.pk_psnjob,\n" + 
			"       hi_psnjob.pk_psnorg,\n" + 
			"       hi_psnjob.pk_org,\n" + 
			"       hi_psndoc_train.pk_psndoc_sub,\n" + 
			"       hi_psndoc_train.lastflag,\n" + 
			"       hi_psndoc_train.recordnum,\n" + 
			"       '正气云学堂' || substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '年度学习情况' act_name,\n" + 
			"       substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-01-01' begindate,\n" + 
			"       substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-12-31' enddate,\n" + 
			"       sum(nvl(actualStudyHours,0)) tra_time\n" + 
			"        from cloud_onlineuserknowledge\n" + 
			"       left join cloud_psndoc\n" + 
			"            on cloud_psndoc.id = cloud_onlineuserknowledge.userid\n" + 
			"       inner join bd_psndoc\n" + 
			"            on bd_psndoc.mobile = cloud_psndoc.mobile or bd_psndoc.mobile = cloud_psndoc.userName\n" + 
			"       inner join hi_psnorg\n" + 
			"            on hi_psnorg.pk_psndoc = bd_psndoc.pk_psndoc and hi_psnorg.indocflag = 'Y' and hi_psnorg.lastflag = 'Y'\n" + 
			"            and hi_psnorg.endflag = 'N'\n" + 
			"       inner join hi_psnjob\n" + 
			"            on hi_psnjob.pk_psndoc = bd_psndoc.pk_psndoc and hi_psnjob.lastflag = 'Y' and hi_psnjob.ismainjob = 'Y'\n" + 
			"            and hi_psnorg.pk_psnorg = hi_psnjob.pk_psnorg and hi_psnjob.endflag = 'N'\n" + 
			"       left join hi_psndoc_train\n" + 
			"            on hi_psndoc_train.pk_psndoc = hi_psndoc_train.pk_psndoc\n" + 
			"            and substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-01-01' = hi_psndoc_train.begindate\n" + 
			"            and substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-12-31' = hi_psndoc_train.enddate\n" + 
			"      where cloud_onlineuserknowledge.actualstudyfinishdate is not null and nvl(bd_psndoc.dr,0) = 0\n" + 
			"      and substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) > '"+qyear+"' and substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) <= '"+dyear+"'\n" + 
			"      group by bd_psndoc.pk_psndoc,\n" + 
			"            bd_psndoc.pk_group,\n" + 
			"            hi_psnjob.pk_psnjob,\n" + 
			"            hi_psnjob.pk_psnorg,\n" + 
			"            hi_psnjob.pk_org,\n" + 
			"            hi_psndoc_train.pk_psndoc_sub,\n" + 
			"            hi_psndoc_train.lastflag,\n" + 
			"            hi_psndoc_train.recordnum,\n" + 
			"            '正气云学堂' || substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '年度学习情况',\n" + 
			"            substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-01-01',\n" + 
			"            substr(cloud_onlineuserknowledge.actualStudyFinishDate,0,4) || '-12-31'";

		List<TrainVO> list = (List<TrainVO>) dao.executeQuery(sql, new BeanListProcessor(TrainVO.class));
		
		//培训类别
		String defdocSql = "select pk_defdoc from bd_defdoc where code = '99' and pk_defdoclist = (select pk_defdoclist from bd_defdoclist where code = 'HR021_0xx')";
		String pk_defdoc = (String) dao.executeQuery(defdocSql, new ColumnProcessor());
		
		List<TrainVO> insertList = new ArrayList();//新增数据list
		List<TrainVO> updList = new ArrayList();//更新数据list

		if(list != null && list.size() > 0) {
			for(TrainVO vo : list) {
				vo.setTra_type(pk_defdoc);
				if(StringUtils.isBlank(vo.getPk_psndoc_sub())) {
					insertList.add(vo);
					vo.setRecordnum(0);
					vo.setLastflag(UFBoolean.TRUE);
				} else {
					updList.add(vo);
				}
			}
		}
		alertObj.setMsgTitle("");
		if(insertList.size() > 0) {
			String[] pks = dao.insertVOList(insertList);
			alertObj.setReturnObj(pks);
			alertObj.setMsgTitle("新增培训记录数据"+pks.length+"条数据");
		}
		if(updList.size() > 0) {
			dao.updateVOList(updList);
			alertObj.setMsgTitle(alertObj.getMsgTitle() + "更新培训记录数据"+updList.size()+"条数据");
		}
		
		alertObj.setReturnType(PreAlertReturnType.RETURNMESSAGE);
		return alertObj;
	}
	

}
