package nc.bs.pub.action;

import java.util.Hashtable;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.fa.prv.IAssetSplit;
import nc.vo.am.common.util.StringUtils;
import nc.vo.am.proxy.AMProxy;
import nc.vo.fa.assetsplit.AssetSplitVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.pub.ampub.permission.IPermission4Nccloud;
import nccloud.pubitf.fa.maintain.assetsplit.IAssetSplit4Cloud;

public class N_HM_DELETE extends AbstractCompiler2 {
  private Hashtable m_methodReturnHas = new Hashtable<>();
  
  private Hashtable m_keyHas = null;
  
  public Object runComClass(PfParameterVO vo) throws BusinessException {
    try {
      this.m_tmpVo = vo;
      setParameter("context", getUserObj());
      setParameter("vo", getVo());
      AssetSplitVO billVO=(AssetSplitVO) getVo();
      if("Y".equals(billVO.getParentVO().getAttributeValue("def10")))
			throw new BusinessException("已经同步ARC单据不能删除！");
      
      if (vo.isCloudEntry) {
        validatePermission(vo, (AssetSplitVO)vo.m_billEntity);
        ((IAssetSplit4Cloud)NCLocator.getInstance().lookup(IAssetSplit4Cloud.class)).delete((AssetSplitVO)getVo());
      } else {
        ((IAssetSplit)NCLocator.getInstance().lookup(IAssetSplit.class)).delete((AssetSplitVO)getVo());
      } 
      Object retVal = null;
      return retVal;
    } catch (Exception ex) {
      if (ex instanceof BusinessException)
        throw (BusinessException)ex; 
      throw new PFBusinessException(ex.getMessage(), ex);
    } 
  }
  
  private void validatePermission(PfParameterVO vo, AssetSplitVO aggVO) throws BusinessException {
    IPermission4Nccloud permissionService = (IPermission4Nccloud)AMProxy.lookup(IPermission4Nccloud.class);
    String node_code = "2012040010";
    String resourceCode = "2012040010";
    String meta_code = "Delete";
    String faliure = permissionService.busiOprPermission(node_code, meta_code, resourceCode, aggVO);
    if (!StringUtils.isEmpty(faliure))
      throw new BusinessException(faliure); 
  }
  
  public String getCodeRemark() {
    return "    super.m_tmpVo = vo;\n// \nsetParameter(\"context\", getUserObj());\n// \nsetParameter(\"vo\", getVo());\n// object\nrunClass(\"nc.impl.fa.assetsplit.AssetSplitImpl\", \"delete\", \"&vo:nc.vo.fa.assetsplit.AssetSplitVO\", vo, m_keyHas);\nreturn 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); 
  }
}
