/*
 * @(#)N_36K4_SAVEBASE.java 2012-3-9
 * Copyright 2010 UFIDA Software CO.LTD. All rights reserved.
 */
package nc.bs.pub.action;

import java.util.Hashtable;

import nc.bs.pub.compiler.AbstractCompiler2;
import nc.bs.sf.delivery.DeliveryBR;
import nc.bs.sf.delivery.DeliveryBankAccBookBR;
import nc.bs.sf.delivery.DeliveryInnerAccBookBR;
import nc.bs.sf.delivery.busilog.DeliveryEditBusiLog;
import nc.bs.sf.delivery.util.DeliveryUtil;
import nc.bs.sf.delivery.util.GXDeliveryEventDispatcher;
import nc.itf.sf.delivery.proxy.DeliveryServiceProxy;
import nc.pubitf.tmpub.util.SagasBizPubUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.pf.IPfRetCheckInfo;
import nc.vo.sf.allocate.AllocateVO;
import nc.vo.sf.delivery.AggDeliveryVO;
import nc.vo.sf.delivery.DeliveryBVO;
import nc.vo.sf.delivery.DeliveryEncryptVO;
import nc.vo.sf.delivery.DeliveryVO;
import nc.vo.sf.delivery.IDeliveryConst;
import nc.vo.sf.delivery.util.DeliveryPubUtil;
import nc.vo.sf.delivery.util.DeliveryVOUtil;
import nc.vo.tmpub.proxy.TMPubServiceProxy;
import nc.vo.tmpub.util.ArrayUtil;
import nc.vo.tmpub.util.DateUtil;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.pubitf.sf.delivery.DeliverySagaInfoAdapter;

/**
 * 上收保存
 *
 * @author  chengfei
 * @version 1.0 2012-3-9
 * @since   NC6.1
 */
public class N_36K4_SAVEBASE extends AbstractCompiler2 {
	private final java.util.Hashtable m_methodReturnHas = new java.util.Hashtable();
	private Hashtable m_keyHas = null;

	/**
	 * N_36K4_SAVEBASE 构造子注解。
	 */
	public N_36K4_SAVEBASE() {
		super();
	}

	/*
	 * 备注：平台编写规则类 接口执行类
	 */
	@Override
	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			AggDeliveryVO aggvo = (AggDeliveryVO) this.getVo();
			// 提交操作需要自己设置审批状态为提交态（或者审批进行中）
			DeliveryVO deliveryVO = (DeliveryVO) (aggvo).getParentVO();
			// begin tm yangjn 单据冻结校验
			if(deliveryVO.getPk_delivery_h() != null){
				DeliverySagaInfoAdapter adapter = new DeliverySagaInfoAdapter();
				SagasBizPubUtil.checkFrozen(adapter, deliveryVO.getPk_delivery_h());
			}
			//end
			deliveryVO.setVbillstatus(IPfRetCheckInfo.NOSTATE);
			// 设置单据状态为待审批
			deliveryVO
					.setBillstatus(IDeliveryConst.BILLSTATUS_WAITCOMMIT);
			//对子表中组织、集团字段进行赋值   hanlua  2013-07-02
			DeliveryBVO[] deliveryBVOS = (aggvo).getItem();
			for(DeliveryBVO deliveryBVO :deliveryBVOS){
				deliveryBVO.setPk_org(deliveryVO.getPk_org());
				deliveryBVO.setPk_group(deliveryVO.getPk_group());
			}
			DeliveryBR br = new DeliveryBR();
			br.beforeSave(aggvo);
			if (aggvo.getParentVO().getStatus() != VOStatus.NEW) {
				//xug 修改前
				GXDeliveryEventDispatcher.getInstantiation("74d29f86-4334-4fc9-a7d3-085cf5dd7845").fireEvent("1003",aggvo);
				DeliveryVOUtil.deleteEbankWrite(aggvo);
				new DeliveryInnerAccBookBR().deleteInnerAccRealBalance(aggvo);
				new DeliveryBankAccBookBR().deleteRecRealBankAcc(aggvo);
				// 更新时，先进行验证签名
				TMPubServiceProxy
						.getCaLogRecordService()
						.checkPerOperationCASignatureByVO(
								br.findDelivery(aggvo), DeliveryEncryptVO.class);

				// 添加数字签名
				aggvo = br.addBillCaSignRecord(aggvo);
				DeliveryUtil.setBankCodeAndCheckSingleForEdit(aggvo);
				aggvo = new DeliveryBR().saveDelivery(aggvo);
				//记录修改的日志
				new DeliveryEditBusiLog().writeBusiLog(aggvo);
				//xug 修改后
				GXDeliveryEventDispatcher.getInstantiation("74d29f86-4334-4fc9-a7d3-085cf5dd7845").fireEvent("1004",aggvo);
			} else {
				//xug 新增前
				GXDeliveryEventDispatcher.getInstantiation("74d29f86-4334-4fc9-a7d3-085cf5dd7845").fireEvent("1001",aggvo);
//				AllocatePubUtil.setBillDateTimeByBusiDate(aggAllocateVO,AllocateVO.ACCEPTDATE, new String[] { AllocateVO.DBILLTIME,AllocateVO.CREATIONTIME });
				String[] vls = new String[] { AllocateVO.DBILLTIME,AllocateVO.CREATIONTIME };
				if (!ArrayUtil.isNull(vls)) {
					UFDateTime currentTime = DateUtil.getCurrTime();
					for (String timeField : vls) {
						aggvo.getHead().setAttributeValue(timeField, currentTime);
					}
				}
				aggvo = DeliveryServiceProxy.getDeliveryManageService()
						.addAggDeliveryVO(aggvo);
				//xug 新增后
				GXDeliveryEventDispatcher.getInstantiation("74d29f86-4334-4fc9-a7d3-085cf5dd7845").fireEvent("1002",aggvo);
			}
			// 冲销业务要处理内部账户账
			new DeliveryInnerAccBookBR().writeInnerRealBalance(aggvo);
			// 冲销业务要预占上收银行账号实时账
			new DeliveryBankAccBookBR().writeRecBankAccRealBalance(aggvo, true);
			// 非冲销业务要预占单位实时账
			// new DeliveryBankAccBookBR().writePayBankAccRealBalance(aggvo,
			// true);

			// ####本脚本必须含有返回值,返回DLG和PNL的组件不允许有返回值####
			return aggvo;
		} catch (Exception ex) {
			if (ex instanceof BusinessException)
				throw (BusinessException) ex;
			else
				throw new PFBusinessException(ex.getMessage(), ex);
		}
	}

	/*
	 * 备注：平台编写原始脚本
	 */
	@Override
	public String getCodeRemark() {
		return nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("3632delivery_0","03632delivery-0115")/*@res "	//####本脚本必须含有返回值,返回DLG和PNL的组件不允许有返回值####\n	Object retObj=null;\n"*/;
	}

	/*
	 * 备注：设置脚本变量的HAS
	 */
	private void setParameter(String key, Object val) {
		if (m_keyHas == null) {
			m_keyHas = new Hashtable();
		}
		if (val != null) {
			m_keyHas.put(key, val);
		}
	}

}