package nccloud.web.hrwa.waruleex.action.formula.variable;

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

import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IItemQueryService;
import nc.itf.hr.wa.IWaGradeQueryService;
import nc.vo.ml.Language;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.MultiLangContext;
import nc.vo.pub.BusinessException;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaPrmlvVO;
import nc.vo.wa.grade.WaSeclvVO;
import nc.vo.wa.item.WaItemVO;
import nc.vo.wa.pub.WaLoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.web.hr.formula.HRFormulaItem;
import nccloud.web.hr.formula.itf.IVariableFactory;
import nccloud.web.hr.formula.variable.AbstractVaribleCreator;

/**
 * 薪资发放项目创建器
 * 
 * @author Administrator
 *
 */
public class WaRuleExVariableCreator extends AbstractVaribleCreator {

	WaLoginContext context = null;

	public WaRuleExVariableCreator(WaLoginContext context) {
		this.context = context;
	}

	@Override
	public List<HRFormulaItem> createFormulaItems(Object... params) throws BusinessException {
		List<HRFormulaItem> itemList = new ArrayList<HRFormulaItem>();
		String tablename=(params==null) ? null :params[0].toString();
		if(tablename != null && ((tablename.endsWith(IVariableFactory.WA_PRMLV)
				|| (tablename.endsWith(IVariableFactory.WA_SECLV))))){
			return createFormulaGrade(tablename);
		}
		// 公共薪资项目
		WaItemVO[] itemVOs = ServiceLocator.find(IItemQueryService.class).queryWaItemVOsByCondition(context, "");
		if (ArrayUtils.isEmpty(itemVOs))
			return itemList;
		for (WaItemVO waClassItemVO : itemVOs) {
			HRFormulaItem itemVO = new HRFormulaItem(
					IVariableFactory.WA_DATA + IVariableFactory.ITEM_SEPERATOR + waClassItemVO.getItemkey(),
					waClassItemVO.getMultilangName(),
					getStdDes(ResHelper.getString("6013commonbasic", "06013commonbasic0016")/* @res"公共薪资项目" */,
							waClassItemVO.getMultilangName()),
					getStdDes(ResHelper.getString("6013commonbasic", "06013commonbasic0016")/* @res"公共薪资项目" */,
							waClassItemVO.getMultilangName()),
					waClassItemVO.getIitemtype());
			itemList.add(itemVO);
		}
		return itemList;
	}
/**
 * 
 * 下午5:41:19
 * msw
 * @param tablename
 * @return
 * @throws BusinessException：
 * TODO：
 *
 */
	public List<HRFormulaItem> createFormulaGrade(String tablename) throws BusinessException {

		List<HRFormulaItem> fieldItems = new ArrayList<HRFormulaItem>();
		if (tablename != null && ((tablename.endsWith(IVariableFactory.WA_PRMLV)
				|| (tablename.endsWith(IVariableFactory.WA_SECLV))))) {
			AggWaGradeVO[] aggWaGradeVos =(AggWaGradeVO[])ServiceLocator.find(IWaGradeQueryService.class).queryWaGradeVO(context, null);
			fieldItems = processGrade4Formular(aggWaGradeVos, fieldItems, tablename);
		}
		return fieldItems;
	}
/**
 * 
 * 下午5:41:24
 * msw
 * @param aggWaGradeVos
 * @param fieldItems
 * @param tablename
 * @return
 * @throws BusinessException：
 * TODO： 
 *
 */
	private List<HRFormulaItem> processGrade4Formular(AggWaGradeVO[] aggWaGradeVos, List<HRFormulaItem> fieldItems,
			String tablename) throws BusinessException {
		if (aggWaGradeVos == null) {
			return fieldItems;
		}
		// 显示名称
		String strDisplayName = null;
		// 薪资级别
		WaPrmlvVO[] prmlvvos = null;
		// 薪资档别
		WaSeclvVO[] seclvvos = null;
		for (AggWaGradeVO aggWaGradeVO : aggWaGradeVos) {
			strDisplayName = getDisPlayName((WaGradeVO) aggWaGradeVO.getParentVO());
			prmlvvos = (WaPrmlvVO[]) aggWaGradeVO.getTableVO(IVariableFactory.WA_PRMLV);
			seclvvos = (WaSeclvVO[]) aggWaGradeVO.getTableVO(IVariableFactory.WA_SECLV);
			
			if (tablename.endsWith(IVariableFactory.WA_PRMLV)) {
				if (!ArrayUtils.isEmpty(prmlvvos)) {
					String strPrmDisplayName = strDisplayName + IVariableFactory.WA_PRMLV_DES;
					HRFormulaItem item = new HRFormulaItem(
							IVariableFactory.LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + IVariableFactory.RIGHT_SEPERATOR + IVariableFactory.WA_CRITERION + IVariableFactory.ITEM_SEPERATOR + IVariableFactory.WA_PRMLV_TABLE,
							strPrmDisplayName, getStdDes(IVariableFactory.GRADENAME, strPrmDisplayName),
							getStdDes(IVariableFactory.GRADENAME, strPrmDisplayName));
					item.setDataType(204);
					item.setRefModelName("薪资级别");
					item.setRefModelPath("hrwa/refer/basic/WaPrmlvGridRef/index");
					item.setPara3((prmlvvos != null && prmlvvos.length > 0) ? prmlvvos[0].getPk_wa_grd() : "");
					fieldItems.add(item);

				}
			} else {
				if (!ArrayUtils.isEmpty(seclvvos)) {
					String strSecDisplayName = strDisplayName + IVariableFactory.WA_SECLV_DES;	
					HRFormulaItem item = new HRFormulaItem(
							IVariableFactory.LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + IVariableFactory.RIGHT_SEPERATOR + IVariableFactory.WA_CRITERION + IVariableFactory.ITEM_SEPERATOR + IVariableFactory.WA_SECLV_TABLE,
							strSecDisplayName, getStdDes(IVariableFactory.GRADENAME, strSecDisplayName),
							getStdDes(IVariableFactory.GRADENAME, strSecDisplayName));
					item.setDataType(204);
					item.setRefModelName("薪资档别");
					item.setRefModelPath("hrwa/refer/basic/WaSeclvGridRef/index");
					item.setPara3((seclvvos != null  &&  seclvvos.length>0) ? seclvvos[0].getPk_wa_grd() : "");
					fieldItems.add(item);

				}
			}
		}
		return fieldItems;
	}
/**
 * 
 * 下午5:41:30
 * msw
 * @param gradevo
 * @return：
 * TODO：
 *
 */
	private String getDisPlayName(WaGradeVO gradevo) {
		LanguageVO langVO = MultiLangContext.getInstance().getCurrentLangVO();
		if (Language.TRAD_CHINESE_CODE.equals(langVO.getLangcode())) {
			if (!StringUtils.isBlank(gradevo.getName2())) {
				return gradevo.getName2();
			}
		} else if (Language.ENGLISH_CODE.equals(langVO.getLangcode())) {
			if (!StringUtils.isBlank(gradevo.getName3())) {
				return gradevo.getName3();
			}
		} else {
			return gradevo.getName();
		}

		return gradevo.getName();
	}
}
