package nc.impl.wa.alert;

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.alert.PsnClassitemImpl;
import nc.vo.hi.alert.FormatMessage;
import nc.vo.hr.append.AppendableVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

/**
 * 个人薪资项目预警
 *
 * @author: liangxr
 * @date: 2010-7-23 下午01:08:46
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class PluginWaIndividual implements IPreAlertPlugin {

	AppendableVO[] poidatas = null;

	public PluginWaIndividual() {
		super();
	}

	@Override
	public PreAlertObject executeTask(PreAlertContext context) throws BusinessException {

		Logger.debug(">>个人薪资项目预警插件开始执行=" + new UFDateTime(System.currentTimeMillis()));
		// 构造预警信息
		poidatas = queryAlertData(context);

		// 构造预警消息返回对象
		PreAlertObject retObj = new PreAlertObject();
		retObj.setReturnType(PreAlertReturnType.RETURNFORMATMSG);
		retObj.setReturnObj(generateReturnObj(poidatas));
		retObj.setMsgTitle(ResHelper.getString("6013salarypmt","06013salarypmt0263")/*@res "个人薪资项目预警提示"*/);
		return retObj;
	}

	/**
	 * 查询预警提示数据
	 *
	 * @author liangxr on 2010-7-23
	 * @param context
	 * @return
	 * @throws BusinessException
	 */
	private AppendableVO[] queryAlertData(PreAlertContext context) throws BusinessException {
		String[] data = (String[]) (context.getUserData());
		if(data==null || data.length<6) { //异常判定
			return null;
		}

		String year = data[0];
		String period = data[1];
		String classid = data[2];
		String pk_org = data[4];
		//shenliangc 20140905 薪资发放节点个人薪资项目预警提示只显示界面上查询出来的人员。
		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 + ") ";
		}


		String[] pk_orgs = context.getPk_orgs();
		if (!ArrayUtils.isEmpty(pk_orgs)
				&& !ArrayUtils.contains(pk_orgs, pk_org)) {
			return null;
		}
		PsnClassitemImpl psnClassitem = new PsnClassitemImpl();
		//shenliangc 20140905 薪资发放节点个人薪资项目预警提示只显示界面上查询出来的人员。
		poidatas = psnClassitem.queryAll(year, period, classid, whereCondition);

		if ((poidatas == null) || (poidatas.length == 0))
			return null;

		return poidatas;
	}

	private Object generateReturnObj(AppendableVO[] poidatas) {
		FormatMessage formatmsg = null;
		String[] colNames = { ResHelper.getString("common","UC000-0001821")/*@res "序号"*/, ResHelper.getString("common","UC000-0001403")/*@res "姓名"*/, ResHelper.getString("common","UC000-0004171")/*@res "项目名称"*/, ResHelper.getString("6013salarypmt","06013salarypmt0257")/*@res "上限预警值"*/, ResHelper.getString("6013salarypmt","06013salarypmt0258")/*@res "下限预警值"*/, ResHelper.getString("6013salarypmt","06013salarypmt0259")/*@res "实际值"*/, ResHelper.getString("6013salarypmt","06013salarypmt0260")/*@res "提示信息"*/ };

		if (poidatas != null && poidatas.length > 0){
			Logger.debug("+++++得到行数：" + poidatas.length);
			for (int i = 0; i < colNames.length; i++) {
				Logger.debug("++++++" + colNames[i] + "====" + poidatas[0].getAttributeValue("vname"));
			}
		}else{
			Logger.debug("+++++得到行数：0" );
		}

		String[] strArrName = { "rownum", "psnname", "name", "psnceil", "psnfloor", "m", "desc" };

		int colvaluelength = 0;
		if (colNames != null && colNames.length != 0) {
			colvaluelength = colNames.length;
		}

		if (poidatas == null || poidatas.length == 0) {
			return null;
		}

		String[][] colValues = new String[poidatas.length][colvaluelength];
		for (int i = 0; i < poidatas.length; i++) {
			for (int colcnt = 0; colcnt < colvaluelength; colcnt++) {
				if (poidatas[i].getAttributeValue(strArrName[colcnt]) != null) {
					colValues[i][colcnt] = poidatas[i].getAttributeValue(strArrName[colcnt]).toString();
				} else {
					colValues[i][colcnt] = "";
				}
			}
		}
		formatmsg = new FormatMessage(ResHelper.getString("6013salarypmt","06013salarypmt0263")/*@res "个人薪资项目预警提示"*/, new String[] { ResHelper.getString("6013salarypmt","06013salarypmt0264")/*@res "以下人员的薪资项目超过上下限！"*/ }, colNames, colValues,
				new float[] { 0.4f }, new String[] { "" });

		return formatmsg;
	}
}