package nc.bs.pub.action;

import java.util.Hashtable;

import nc.impl.fa.reduce.rule.SendARCReduceRule;
import nc.vo.am.common.util.StringUtils;
import nc.vo.am.common.util.sagas.AMSagasUtils;
import nc.vo.am.proxy.AMProxy;
import nc.vo.fa.reduce.ReduceVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.pub.ampub.permission.IPermission4Nccloud;

public class N_HF_APPROVE extends AMApproveAbstractCompiler2 {
	private Hashtable m_methodReturnHas = new Hashtable();
	private Hashtable m_keyHas = null;

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			super.runComClass(vo);
			this.setParameter("asc", this);
			this.setParameter("parameterVo", vo);
			if (vo.isCloudEntry) {
				this.validatePermission(vo, (ReduceVO) vo.m_billEntity);
			}

			AMSagasUtils.changeCompensable(AMSagasUtils.COMPENSABLE_NOTSHOW);
			AMSagasUtils.frozenAndAddSaga(new SuperVO[] { (SuperVO) this.getVo().getParentVO() });
			this.runClass("nc.impl.fa.reduce.ReduceImpl", "approveBillVOs",
					"&asc:nc.bs.pub.compiler.AbstractCompiler2,&parameterVo:nc.vo.pub.compiler.PfParameterVO", vo,
					this.m_keyHas);
			// modefy by djy 20220319因为审批驳回都会走这个动作，只有审批有回调ARC
//			String approveresult = vo.m_workFlow==null?null:vo.m_workFlow.approveresult;
//			if ("Y".equals(approveresult)) {
				// add by dxr
				if (vo.m_preValueVos instanceof ReduceVO[]) {
					ReduceVO[] reduceVOs = (ReduceVO[]) vo.m_preValueVos;
					if(reduceVOs!=null&&reduceVOs.length>0) {
					for(ReduceVO reduceVO:reduceVOs) {
						if(reduceVO.getParentVO().getAttributeValue("bill_status")!=null
								&&(Integer)(reduceVO.getParentVO().getAttributeValue("bill_status"))==3)
							new SendARCReduceRule().process(null, new ReduceVO[] {reduceVO});
						}
					}
				}
//			}
			return this.getVos();
		} catch (Exception var3) {
			if (var3 instanceof BusinessException) {
				throw (BusinessException) var3;
			} else {
				throw new PFBusinessException(var3.getMessage(), var3);
			}
		}
	}

	public String getCodeRemark() {
		return "\tsuper.m_tmpVo = vo;\n\t\t\tsuper.m_tmpVo = vo;\n\t\t\t// 设置变量：登陆上下文\n\t\t\tsetParameter(\"asc\", this);\n\t\t\t// 设置变量：单据VO数组对象。\n\t\t\tsetParameter(\"parameterVo\", vo);\n\t\t\trunClass(\"nc.impl.fa.reduce.ReduceImpl\", \"approveBillVOs\",\n\t\t\t\t\t\"&asc:nc.bs.pub.compiler.AbstractCompiler2,\"\n\t\t\t\t\t\t\t+ \"&parameterVo:nc.vo.pub.compiler.PfParameterVO\",\n\t\t\t\t\tvo, m_keyHas);\n\t\t\treturn getVos();\n";
	}

	private void setParameter(String key, Object val) {
		if (this.m_keyHas == null) {
			this.m_keyHas = new Hashtable();
		}

		if (val != null) {
			this.m_keyHas.put(key, val);
		}

	}

	private void validatePermission(PfParameterVO vo, ReduceVO aggVO) throws BusinessException {
		IPermission4Nccloud permissionService = (IPermission4Nccloud) AMProxy.lookup(IPermission4Nccloud.class);
		String node_code = "2012056010";
		String resourceCode = "2012056010";
		String meta_code = "AMApprove";
		String faliure = permissionService.busiOprPermission(node_code, meta_code, resourceCode, aggVO);
		if (!StringUtils.isEmpty(faliure)) {
			throw new BusinessException(faliure);
		}
	}
}