package nc.bs.hrwa.alert;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;

/*
 * 
 * 后台任务
 * 根据定调资信息维护子表 hi_psndoc_wadoc 定时更新 
 * 人员信息维护 bd_psndoc 的薪等薪级 jobglbdef6 字段
 * 
 * */
public class PsnWadocSendPrmSecAlert implements IBackgroundWorkPlugin{

	public PreAlertObject executeTask(BgWorkingContext bgwc) throws BusinessException {

		BaseDAO baseDao = new BaseDAO();
		
		UFDateTime time = new UFDateTime(System.currentTimeMillis());
		UFDate date = time.getDate();//系统当前日期
		
		StringBuffer psnwadocsql = new StringBuffer();
		psnwadocsql.append("select ");
		psnwadocsql.append("hi_psndoc_wadoc.pk_psndoc as pk_psndoc,");
		psnwadocsql.append("hi_psndoc_wadoc.pk_psnjob,");
		psnwadocsql.append("hi_psndoc_wadoc.negotiation_wage,");
		psnwadocsql.append("hi_psndoc_wadoc.begindate as begindate,");
		psnwadocsql.append("wa_prmlv.levelname as prmname,");
		psnwadocsql.append("wa_seclv.levelname as secname,");
		psnwadocsql.append(" doc.pk_defdoc,");
		psnwadocsql.append("doc.name,");
		psnwadocsql.append("doc.pk_defdoclist");
		psnwadocsql.append(" from hi_psndoc_wadoc ");
		psnwadocsql.append(" left join wa_grade on wa_grade.pk_wa_grd = hi_psndoc_wadoc.pk_wa_grd");
		psnwadocsql.append(" left join wa_prmlv on wa_prmlv.pk_wa_prmlv = hi_psndoc_wadoc.pk_wa_prmlv");
		psnwadocsql.append(" left join wa_seclv on wa_seclv.pk_wa_seclv = hi_psndoc_wadoc.pk_wa_seclv");
		psnwadocsql.append(" left join bd_defdoc doc");
		psnwadocsql.append(" on  doc.name = wa_prmlv.levelname||wa_seclv.levelname ");
		psnwadocsql.append(" and pk_defdoclist in (SELECT pk_defdoclist FROM bd_defdoclist where code ='HRFR01') ");
		psnwadocsql.append(" left join hi_psnjob ");
		psnwadocsql.append(" on hi_psndoc_wadoc.pk_psnjob = hi_psnjob.pk_psnjob ");
		psnwadocsql.append(" where hi_psndoc_wadoc.dr = 0 ");
		psnwadocsql.append(" and wa_grade.dr = 0");
		psnwadocsql.append(" and wa_grade.pk_wa_item = '1001N6100000000HZ7WO' ");
		psnwadocsql.append(" and hi_psndoc_wadoc.lastflag = 'Y' ");
		psnwadocsql.append(" and hi_psndoc_wadoc.negotiation_wage = 'N' ");
		psnwadocsql.append(" and doc.pk_defdoc<>nvl(hi_psnjob.jobglbdef6,'~') ");
		psnwadocsql.append(" and hi_psndoc_wadoc.begindate <='");
		psnwadocsql.append(date);
		psnwadocsql.append("'");
		
		List<Map<String, String>> list = (List<Map<String, String>>)baseDao.executeQuery(psnwadocsql.toString(), new MapListProcessor());
		if(list!=null && list.size()>0){
			Map<String,String> map = new HashMap<String,String>();

				
			list.forEach(res->{
				map.put(res.get("pk_psnjob"), res.get("pk_defdoc"));
			 
			});
	
			Set<String> set = map.keySet();
			for(String pk_psnjob : set){
				String pk_defdoc = map.get(pk_psnjob);
				
				if(pk_defdoc!=null){
					StringBuffer updatebuff = new StringBuffer();
					updatebuff.append(" update hi_psnjob");
					updatebuff.append(" set jobglbdef6 ='");
					updatebuff.append(pk_defdoc);
					updatebuff.append("' where   pk_psnjob ='");
					updatebuff.append(pk_psnjob);
					updatebuff.append("'");
					baseDao.executeUpdate(updatebuff.toString());
				}
				
			}	
				
		}
		return null;
	}

}
