package nc.bs.pub.action;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.hz.utils.BillInfoQueryUtil;
import nc.impl.fa.alter.AlterImpl;
import nc.impl.fa.alter.rule.SendARCAlterRule;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.itf.fa.byzitf.IAssetInfoToByzService;
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.alter.AlterBodyVO;
import nc.vo.fa.alter.AlterVO;
import nc.vo.fa.assetcard.AggCardVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.pub.ampub.permission.IPermission4Nccloud;

public class N_HG_APPROVE extends AMApproveAbstractCompiler2 {
	private BaseDAO baseDAO;

	public Object runComClass(PfParameterVO parameterVo) throws BusinessException {

		super.m_tmpVo = parameterVo;
		super.runComClass(parameterVo);
		if (parameterVo.isCloudEntry) {
			this.validatePermission(parameterVo, (AlterVO) this.getVo());
		}
		BillInfoQueryUtil.initSysBilltypeCache(new String[] { "HG", "H0", "HG-00", "HZ", "H3", "H4", "H5", "H6" });
		AMSagasUtils.changeCompensable(AMSagasUtils.COMPENSABLE_NOTSHOW);
		AMSagasUtils.frozenAndAddSaga(new SuperVO[] { (SuperVO) this.getVo().getParentVO() });
		Object object = (new AlterImpl()).approveBillVOs(this, parameterVo);

		// modefy by djy 20220319因为审批驳回都会走这个动作，只有审批有回调ARC
//		String approveresult = parameterVo.m_workFlow==null?null:parameterVo.m_workFlow.approveresult;
//		if ("Y".equals(approveresult)) {
			// add by dxr

			if (object instanceof AlterVO[]) {
				AlterVO[] alterVOs = (AlterVO[]) object;
				if(alterVOs!=null&&alterVOs.length>0) {
					for(AlterVO alterVO:alterVOs) {
						if(alterVO.getParentVO().getAttributeValue("bill_status")!=null
								&&(Integer)(alterVO.getParentVO().getAttributeValue("bill_status"))==3)
							new SendARCAlterRule().process(null, new AlterVO[] {alterVO});
					}
				}
			}
//		}
		// add by dxr
		String def18After = null;
		String pk_card = null;
		if (object instanceof AlterVO[]) {
			AlterVO[] alterVOs = (AlterVO[]) object;
			AlterBodyVO[] alterBodyVOs = (AlterBodyVO[]) alterVOs[0].getChildrenVO();
			for (AlterBodyVO alterBodyVO : alterBodyVOs) {
				def18After = (String) alterBodyVO.getAttributeValue("def18_after");
				pk_card = (String) alterBodyVO.getAttributeValue("pk_card");

				if (null != def18After && null != pk_card) {
					// 将资产卡片表名称字段改为def18After值
					updateFaCardName(pk_card, def18After);
				}

				// 记得置空
				def18After = null;
				pk_card = null;

				// add by djy 同步ARC变动后的卡片
				if (alterBodyVO.getPk_card() != null) {
					BillQuery<AggCardVO> query = new BillQuery<AggCardVO>(AggCardVO.class);
					AggCardVO[] aggvo = query.query(new String[] { alterBodyVO.getPk_card() });
					try {
						NCLocator.getInstance().lookup(IAssetInfoToByzService.class).sendArc(aggvo);
					} catch (Exception e) {
						ExceptionUtils.wrappBusinessException(e.getMessage());
					}
				}
			}

//			def18After = (String)alterVOs[0].getChildrenVO()[0].getAttributeValue("def18_after");
		}
		return object;
	}

	private void validatePermission(PfParameterVO vo, AlterVO aggVO) throws BusinessException {
		IPermission4Nccloud permissionService = (IPermission4Nccloud) AMProxy.lookup(IPermission4Nccloud.class);
		String transType = (String) aggVO.getParent().getAttributeValue("transi_type");
		String node_code = null;
		String resourceCode = null;
		if ("HG-01".equals(transType)) {
			node_code = "2012028015";
			resourceCode = "2012028015";
		} else if ("HG-02".equals(transType)) {
			node_code = "2012028020";
			resourceCode = "2012028020";
		} else if ("HG-04".equals(transType)) {
			node_code = "2012028030";
			resourceCode = "2012028030";
		} else if ("HG-06".equals(transType)) {
			node_code = "2012028040";
			resourceCode = "2012028040";
		} else if ("HG-13".equals(transType)) {
			node_code = "2012028075";
			resourceCode = "2012028075";
		} else if ("HG-14".equals(transType)) {
			node_code = "2012028080";
			resourceCode = "2012028080";
		} else if ("HG-16".equals(transType)) {
			node_code = "2012028090";
			resourceCode = "2012028090";
		} else if ("HG-05".equals(transType)) {
			node_code = "2012028035";
			resourceCode = "2012028035";
		} else {
			node_code = "2012028120";
			resourceCode = "2012028120";
		}

		String meta_code = "AMApprove";
		String faliure = permissionService.busiOprPermission(node_code, meta_code, resourceCode, aggVO);
		if (!StringUtils.isEmpty(faliure)) {
			throw new BusinessException(faliure);
		}
	}

	private BaseDAO getBaseDAO() {
		if (baseDAO == null) {
			baseDAO = new BaseDAO();
		}
		return baseDAO;
	}

	/**
	 * 修改资产卡片名称
	 * 
	 * @param cardPk
	 * @param afterName
	 * @throws BusinessException
	 */
	private void updateFaCardName(String cardPk, String afterName) throws BusinessException {
		String sql = "update fa_card set asset_name = '" + afterName + "' where pk_card = '" + cardPk + "'";
		Integer number = getBaseDAO().executeUpdate(sql);
		if (number == 0) {
			throw new BusinessException("更新该" + cardPk + "失败");
		}
	}
}