package nccloud.web.hryf.regApply.action;

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

import nc.hr.utils.ResHelper;
import nc.itf.trn.regmng.IRegmngManageService;
import nc.vo.pub.BusinessException;
import nc.vo.trn.pub.TRNConst;
import nc.vo.trn.regmng.AggRegapplyVO;
import nc.vo.trn.regmng.Glbdef2VO;
import nc.vo.trn.regmng.RegapplyVO;
import nc.vo.trn.transmng.StapplyVO;
import nc.vo.uap.rbac.constant.INCSystemUserConst;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.StringUtils;
import nccloud.dto.hryf.tools.HRYFConst;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrhi.pub.PsndocTempUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 转正申请-保存/修改保存
 * @author lbt 20190314
 *
 */
public class RegApplySaveAction extends RegBaseAction {


	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		//解析请求中得参数，放入map中，并根据参数获取用户的登录上下文信息。
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		Map<String,Object> result = new HashMap<>();

		String opera_code = param.getString("opera_code", "");//操作类型 1：保存 2：修改保存
		if(StringUtils.isBlank(opera_code)){
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0038")
/*@res "opera_code 操作类型没传！"*/);
		}

		RegapplyVO regapplyVO = HRNccTemplateUtils.getVOFromForm(RegapplyVO.class, PsndocTempUtils.getModelStr(param, "formData"));
		if (regapplyVO == null) {
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0021")
/*@res "form 数据没传！"*/);
		}
		
		Glbdef2VO[] glbdef1VOs = HRNccTemplateUtils.getVOFromGrid(Glbdef2VO.class,PsndocTempUtils.getModelStr(param, "formBodyData"));
		if (glbdef1VOs == null) {
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0021")
/*@res "form 数据没传！"*/);
		}
		
		context.setPk_org(regapplyVO.getPk_org());
		AggRegapplyVO aggvo = new AggRegapplyVO();
		aggvo.setParentVO(regapplyVO);
		aggvo.setChildrenVO(glbdef1VOs);

		//Form表单非空校验
		Map saveCheck = super.batchSaveCheck(context, param, HRYFConst.APPCODE_REGAPPLY, HRYFConst.PAGECODE_REGAPPLY, regapplyVO.getProbation_type(), regapplyVO, TRNConst.TRNS_OLDINFO_TAB, TRNConst.TRNS_NEWINFO_TAB);
		result.putAll(saveCheck);

		//执行保存操作
		IRegmngManageService  regmngManageService = ServiceLocator.find(IRegmngManageService.class);

		if(opera_code.equals("1")){
			aggvo = (AggRegapplyVO) regmngManageService.insertBill(aggvo);
		}else if(opera_code.equals("2")){
			String billMaker = (String) aggvo.getParentVO().getAttributeValue(StapplyVO.BILLMAKER);
			if (billMaker == null || INCSystemUserConst.NC_USER_PK.equals(billMaker)) {
				aggvo.getParentVO().setAttributeValue(StapplyVO.BILLMAKER, context.getPk_loginUser());
			}
			aggvo = (AggRegapplyVO) regmngManageService.updateBill(aggvo, true);
		}
		result.put("formData", HRNccTemplateUtils.getFormWithArea(HRYFConst.PAGECODE_REGAPPLY, "card", regapplyVO));
		
		result.put("formBodyData", HRNccTemplateUtils.getGridWithArea(HRYFConst.PAGECODE_REGAPPLY, "card_body", (Glbdef2VO[])aggvo.getChildrenVO()));
		//返回页面
		return result;

	}





}