package nccloud.web.hrwa.waruleex.action;

import java.util.HashMap;
import java.util.Map;

import nc.hr.utils.CommonUtils;
import nc.hr.utils.StringPiecer;
import nc.itf.hr.wa.IWaRuleExQueryService;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.rule.AggWageformVO;
import nc.vo.wa.rule.WageformVO;
import nccloud.commons.lang.ArrayUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.model.PageInfo;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccQueryHelper;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrwa.pub.IWATempletConst;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 
 * 下午4:20:50 
 * msw： 
 * 薪资规则点增加按钮时进行校验,若校验通过，返回true,不通过，直接抛错
 *
 */
public class WaRuleExRefreshAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		String areaCode = param.getString("area_code", null);
		String appCode = NODE_TYPE.ORG_NODE.equals(context.getNodeType()) ? IWATempletConst.APPCODE_WARULE_ORG
				: IWATempletConst.APPCODE_WARULE_GRP;
		if (areaCode == null)
			return null;
		AggWageformExVO[] aggVOs = (AggWageformExVO[]) ServiceLocator.find(IWaRuleExQueryService.class).queryWageFormVO(context, "pk_org='"+context.getPk_org()+"'");
		WageformExVO[] vos = CommonUtils.getParentVOArrayFromAggVOs(WageformExVO.class, aggVOs);
		if (ArrayUtils.isEmpty(vos))
			return null;
		HRNccQueryHelper queryHelper = new HRNccQueryHelper(request);
		PageInfo pageInfo = queryHelper.getPageInfo(vos.length);
		WageformExVO[] currVOs = queryHelper.getCurrPageVOs(pageInfo, vos);
		Grid grid = HRNccTemplateUtils.getGridWithArea(appCode, areaCode, currVOs);
		grid.getModel().setPageinfo(pageInfo);
		grid.getModel().setAllpks(StringPiecer.getStrArray(currVOs, WageformExVO.PK_WA_WAGEFORMEX));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Parent", grid);
		map.put("Child", vos);

		return map;
	}
}
