package nccloud.web.hrwa.wagrade.action;

import nc.hr.utils.CommonUtils;
import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IWaGradeQueryService;
import nc.itf.hr.wa.IWaGradeService;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.uif2.LoginContext;
import nc.vo.util.ManageModeUtil;
import nc.vo.wa.grade.AggWaGradeVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrwa.wagrade.action.util.WaGradeCommon;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 
 * 下午4:20:50 
 * msw： 
 * 薪资规则点增加按钮时进行校验,若校验通过，返回true,不通过，直接抛错
 *
 */
public class WaGradeBeforeDoAction 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);// 前端必须传入操作运算符，例如add,copy,edit
		if (opType == null) {
			return null;
		}
		if (WaGradeCommon.OPTYPE_LIST.contains(opType)) {
			validata(context, opType, null);
		} else if ("edit".equals(opType) || "delete".equals(opType)) {
			String pkWaGrd = param.getString("pk_wa_grd", null);
			AggWaGradeVO gradeVo = (AggWaGradeVO) ServiceLocator.find(IWaGradeQueryService.class)
					.queryAggWagradeByGrdPK(pkWaGrd);
			validata(context, opType, gradeVo);
		}
		return true;
	}

	/**
	 * 
	 * 2019年6月26日下午3:52:31
	 * msw
	 * @param context
	 * @param opType
	 * @param ob
	 * @throws BusinessException：
	 *
	 */
	public void validata(LoginContext context, String opType, Object ob) throws BusinessException {
		// 检查是否已经选择了人力资源组织人力资源组织
		try {
			if (context.getNodeType() != NODE_TYPE.GROUP_NODE && context.getNodeType() != NODE_TYPE.GLOBE_NODE) {
				CommonUtils.checkOrg(context);
			}
		} catch (BusinessException ex) {
			throw new BusinessException(ResHelper.getString("60130payrule",
					"060130payrule0005")/* @res "请选择人力资源组织！" */);
		}
		// 增加的场合,没有选中数据的场合不进行管控模式的校验
		if ("add".equals(opType) || ob == null) {
			return;
		}
		// 管控模式的校验
		boolean blnIsCanOperator = ManageModeUtil.manageable(ob, context);
		if (!blnIsCanOperator) {
			throw new BusinessException(ManageModeUtil.getDisManageableMsg(context.getNodeType()));
		}
        if("edit".equals(opType) || ob == null){
        	AggWaGradeVO gradeVo=(AggWaGradeVO)ob;
			boolean editable = ServiceLocator.find(IWaGradeService.class).validateEffectVersion(gradeVo.getParentVO().getPk_wa_grd());
			if (editable) {
				String sbErrMessage = ResHelper.getString("60130paystd","060130paystd0235")/*@res "该薪资标准类别已经生效不能修改！"*/;
				throw new BusinessException(sbErrMessage);
			}
			
			UFBoolean isgroup = gradeVo.getParentVO().getIsgroup();
			UFBoolean isscope = gradeVo.getParentVO().getIsscope();
			if((isgroup != null && isgroup.booleanValue()) && (isscope == null || !isscope.booleanValue())) {
				throw new BusinessException("集团分配数据并可确定级别金额范围并允许公司增加子级别为否，不能修改");
			}
        }
	}
}
