package nccloud.web.hrwa.waruleex.action;

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

import nc.hr.utils.ResHelper;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.rule.AggWageformVO;
import nc.vo.wa.rule.WageformVO;
import nc.vo.wa.rule.WageformdetVO;
import nccloud.commons.collections.MapUtils;
import nccloud.commons.lang.ArrayUtils;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.hr.pub.HRCommonAction;
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： 
 * 返回复制或编辑时的数据
 *
 */
public class WaRuleExGetHandleDataAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		String opType = param.getString("op_type", null);
		String pkWaWageform = param.getString("pk_wa_wageformex", null);
		WaRuleExCommon common = new WaRuleExCommon();
		AggWageformExVO aggWageformVO = common.listHandleData(context, pkWaWageform);
		if ("copy".equals(opType)) {// 复制需要清空部分已选中行记录的数据
			aggWageformVO = getNeedCopiedData(aggWageformVO);
		}
		if (aggWageformVO == null) {
			return null;
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		setResultMap(resultMap, param, context, aggWageformVO);
		return MapUtils.isEmpty(resultMap) ? null : resultMap;
	}
/**
 * 
 * 2019年6月5日下午7:25:49
 * msw
 * @param resultMap
 * @param param
 * @param context
 * @param aggWageformVO：
 *
 */
	private void setResultMap(Map<String, Object> resultMap, ParamUtils param, LoginContext context,
			AggWageformExVO aggWageformVO) {
		String areaCodeParent = param.getString("area_parent_code", null);
		String areaCodeChild = param.getString("area_child_code", null);
		String appCode = NODE_TYPE.ORG_NODE.equals(context.getNodeType()) ? IWATempletConst.APPCODE_WARULE_ORG
				: IWATempletConst.APPCODE_WARULE_GRP;
		WageformExVO selectedParentVo = (WageformExVO) aggWageformVO.getParentVO();
		Form parentForm = (selectedParentVo != null)
				? HRNccTemplateUtils.getFormWithArea(appCode, areaCodeParent, selectedParentVo) : null;
		WageformdetExVO[] selectedChildVOs = (WageformdetExVO[]) aggWageformVO.getChildrenVO();
		Grid childGrid = !ArrayUtils.isEmpty(selectedChildVOs)
				? HRNccTemplateUtils.getGridWithArea(appCode, areaCodeChild, selectedChildVOs) : null;
		resultMap.put("Parent", parentForm);
		resultMap.put("Child", childGrid);
	}
	
	/**
	 * 
	 * 2019年6月27日下午1:54:53
	 * msw
	 * @param selectedAggVO
	 * @return
	 * @throws BusinessException：
	 * 返回需要复制的数据给前端
	 */
	private AggWageformExVO getNeedCopiedData(AggWageformExVO selectedAggVO) throws BusinessException {
		if (selectedAggVO == null) {
			throw new BusinessException(ResHelper.getString("60130payrule",
					"060130payrule0000")/* @res "请先选中欲复制的薪资规则！" */);
		}
		// 前端用于展示的复制后的数据
		AggWageformExVO copiedData = new AggWageformExVO();
		WageformExVO wageVo = (WageformExVO) selectedAggVO.getParentVO().clone();
		setWageformVO(copiedData, wageVo);
		setWageformdetVO(copiedData, selectedAggVO);
		return copiedData;
	}
/**
 * 
 * 2019年6月27日下午2:15:06
 * msw
 * @param copiedData:重新封装的复制后的数据
 * @param wageVo：
 *
 */
	private void setWageformVO(AggWageformExVO copiedData, WageformExVO wageVo) {
		wageVo.setCreator(null);
		wageVo.setCreationtime(null);
		wageVo.setModifier(null);
		wageVo.setModifiedtime(null);
		// 复制时清空编码和名称
		wageVo.setCode(null);
		wageVo.setName(null);
		wageVo.setName2(null);
		wageVo.setName3(null);
		wageVo.setName4(null);
		wageVo.setName5(null);
		wageVo.setName6(null);
		wageVo.setStatus(VOStatus.NEW);
		copiedData.setParentVO(wageVo);
	}
/**
 * 
 * 2019年6月27日下午2:15:02
 * msw
 * @param copiedData:重新封装的复制后的数据
 * @param selectedAggVO：
 *
 */
	private void setWageformdetVO(AggWageformExVO copiedData, AggWageformExVO selectedAggVO) {
		WageformdetExVO[] selectedChildVOs = (WageformdetExVO[]) selectedAggVO.getChildrenVO();
		WageformdetExVO[] childVOs = null;
		if (!ArrayUtils.isEmpty(selectedChildVOs)) {
			childVOs = new WageformdetExVO[selectedChildVOs.length];
			for (int i = 0; i < childVOs.length; i++) {
				childVOs[i] = (WageformdetExVO) selectedChildVOs[i].clone();
				childVOs[i].setPk_wa_wageformex(null);
				childVOs[i].setPk_wa_wageformdetex(null);
				childVOs[i].setStatus(VOStatus.NEW);
			}
		}
		copiedData.setChildrenVO(childVOs);
	}
}
