package nc.impl.wa.alert;

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

import nc.bs.logging.Logger;
import nc.bs.pub.pa.IPreAlertPlugin;
import nc.bs.pub.pa.PreAlertContext;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.hr.utils.InSQLCreator;
import nc.hr.utils.ResHelper;
import nc.impl.wa.paydata.PaydataServiceImpl;
import nc.impl.wa.rule.WaRuleExQueryImpl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import nc.vo.wa.paydata.DataVO;
import nc.vo.wa.pub.WaLoginContext;
import nccloud.commons.lang.StringUtils;

/**
 * 薪资发放项目不符合条件预警
 * @date 2021-3-18 17:38:38
 * @author chenzf
 *
 */
public class WaItemControlPlugin implements IPreAlertPlugin {

	@Override
	public PreAlertObject executeTask(PreAlertContext context) throws BusinessException {
		Logger.debug(">>薪资发放项目不符合条件预警插件开始执行=" + new UFDateTime(System.currentTimeMillis()));
		
		PreAlertObject retObj = new PreAlertObject();
		retObj.setReturnType(PreAlertReturnType.RETURNFORMATMSG);
		retObj.setReturnObj(toMsg(context));//数据组装
		retObj.setMsgTitle("薪资发放项目不符合条件");
		return retObj;
	}

	public Object toMsg(PreAlertContext context) throws BusinessException{
		String message = "";
		String[] data = (String[])context.getUserData();
		
		String year = data[0];
		String period = data[1];
		String classid = data[2];
		String pk_org = data[4];
		String pk_psndoclist = data[5];
		String whereCondition = null;
		
		if(StringUtils.isNotBlank(pk_psndoclist)){
			String[] split = pk_psndoclist.split(",");
			InSQLCreator insql = new InSQLCreator();
			String inSQL = insql.getInSQL(split);
			whereCondition = " pk_psndoc in( " + inSQL + ") ";
		}
		PaydataServiceImpl paydataServiceImpl = new PaydataServiceImpl();
		WaLoginContext waLoginContext = new WaLoginContext();
		waLoginContext.getWaLoginVO().getPeriodVO().setCyear(year);
		waLoginContext.getWaLoginVO().getPeriodVO().setCperiod(period);
		waLoginContext.getWaLoginVO().setPk_wa_class(classid);
		waLoginContext.getWaLoginVO().setPk_org(pk_org);
		waLoginContext.setPk_org(pk_org);
		waLoginContext.setPk_group(context.getGroupId());
		
		DataVO[] dataVOs = paydataServiceImpl.queryDataVOsByCond(waLoginContext, whereCondition, null);
		
		WaRuleExQueryImpl waRuleExQueryImpl = new WaRuleExQueryImpl();
		List<Map<String, Object>> ruleMap = waRuleExQueryImpl.queryWageformExContral(waLoginContext);
		
		Map<String,String> tipMap = new HashMap();
		boolean controlFlag = false;
		String pk_wa_data = dataVOs[0].getPk_wa_data();
		Map<String,String> msgMap = paydataServiceImpl.queryMapByPkWaData(pk_wa_data);
		if(ruleMap != null && ruleMap.size() > 0) {
			Map<String,List> itemMap = new HashMap();
			for(int i = 0; i < ruleMap.size(); i++) {
				List list = new ArrayList();
				Map<String, Object> map = ruleMap.get(i);
				//例子f_1
				String itemkey = (String) map.get("itemkey");
				//bd_psndoc.edu = "@@@@Z710000000005L5U"
				String vformula = (String)map.get("vformula");
				//薪资项目名称
				String name = (String) map.get("name");
				//发放控制
				String iscontrol = (String) map.get("iscontrol");
				//上限金额
				UFDouble upperlimit = map.get("upperlimit") == null ? UFDouble.ZERO_DBL : new UFDouble(map.get("upperlimit").toString());
				if(!upperlimit.isTrimZero()) {
					list.add(name);
					list.add(upperlimit);
					list.add(vformula);
					list.add(iscontrol);
					itemMap.put(itemkey, list);
				}
			}
			for(DataVO dataVO : dataVOs) {
				String psncode = dataVO.getPsncode();
				String psnname = dataVO.getPsnname();
				String msg = "";
				for(String key : itemMap.keySet()) {
					UFDouble value = (UFDouble) dataVO.getAttributeValue(key);
					UFDouble upperlimit = (UFDouble) itemMap.get(key).get(1);
					if(value.compareTo(upperlimit) > 0) {
						//薪资项目itemMap.get(key)，发放金额value,原限制条件：upperlimit
//						msg = msg + itemMap.get(key).get(0)+",";
						msg = msg + "薪资项目["+itemMap.get(key)+"],发放金额[" +value+"],原限制条件["+upperlimit+"],";
						if(!controlFlag) {
							controlFlag = "Y".equals(itemMap.get(key).get(3)) ? true : false;
						}
					}
				}
				if(msg.length() > 0) {
					tipMap.put(psncode+","+psnname, msg.substring(0, msg.length() - 1));
				}
			}
		}
		//【公司】【薪资类别】【薪资年度】【薪资期间】发放金额不符合控制条件：【薪资项目】【姓名】【发放金额】，原限制条件：【原限制条件】
		if(tipMap.size() > 0) {
			if(msgMap.size() > 0) {
				//公司
				String orgname = msgMap.get("orgname");
				//薪资类别
				String classname = msgMap.get("classname");
				//薪资年度
				String cyear = msgMap.get("cyear");
				//薪资期间
				String cperiod = msgMap.get("cperiod");
				
				message = "["+orgname+"]"+"["+classname+"]"+"["+cyear+"]"+"["+cperiod+"]发放金额不符合控制条件：";
			}
			for(String key : tipMap.keySet()) {
				message = message + key + tipMap.get(key) + ",";
			}
		} 
		
		if(message.length() > 0) {
			message = message.substring(0, message.length() - 1);
		}
		
		return message;
	}
}
