package nc.bs.pub.action;

import java.util.Hashtable;

import nc.bs.dao.BaseDAO;
import nc.bs.hz.utils.BillInfoQueryUtil;
import nc.itf.fa.service.IUseDeptScaleService;
import nc.pub.fa.common.util.StringUtils;
import nc.vo.am.common.util.sagas.AMSagasUtils;
import nc.vo.am.proxy.AMProxy;
import nc.vo.fa.asset.AssetVO;
import nc.vo.fa.deployin.DeployInBodyVO;
import nc.vo.fa.deployin.DeployInHeadVO;
import nc.vo.fa.deployin.DeployInVO;
import nc.vo.fa.deptscale.DeptScaleVO;
import nc.vo.pub.AggregatedValueObject;
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.web.byz.action.ARCApproveAction;

public class N_HD_APPROVE extends AMApproveAbstractCompiler2 {
	private Hashtable m_methodReturnHas = new Hashtable<>();

	private Hashtable m_keyHas = null;

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			Object retObject = null;
			this.m_tmpVo = vo;
			super.runComClass(vo);
			this.m_tmpVo = vo;
			if (vo.isCloudEntry) {
				validatePermission(vo, (DeployInVO) vo.m_billEntity);
				AMSagasUtils.frozenAndAddSaga(new AggregatedValueObject[] { (AggregatedValueObject) vo.m_billEntity });
			}
			setParameter("asc", this);
			setParameter("parameterVo", vo);
			retObject = runClass("nc.impl.fa.deployin.DeployInImpl", "approveBillVOs",
					"&asc:nc.bs.pub.compiler.AbstractCompiler2,&parameterVo:nc.vo.pub.compiler.PfParameterVO", vo,
					this.m_keyHas);
			
			BaseDAO baseDAO=new BaseDAO();
			if (retObject.getClass().isArray()) {
				if (retObject instanceof DeployInVO[]) {
					DeployInVO[] deployInVOs = (DeployInVO[]) retObject;
					for (DeployInVO deployInVO : deployInVOs) {
						String link_keys = (String) deployInVO.getChildrenVO()[0].getAttributeValue("pk_usedept");
						if (StringUtils.isNotEmpty(link_keys)) {
							DeptScaleVO[] deptScaleVOs = ((IUseDeptScaleService) AMProxy
									.lookup(IUseDeptScaleService.class)).queryUseDeptByLink(new String[] { link_keys });
							if (null != deptScaleVOs && deptScaleVOs.length > 0 && null != deptScaleVOs[0])
								deployInVO.getChildrenVO()[0].setAttributeValue("pk_usedept",
										deptScaleVOs[0].getPk_dept());
						}

						// add by djy 20220216 增加对ARC系统的对接
						// modefy by djy 20220305 不在审批动作类同步ARC
//						DeployInHeadVO head = (DeployInHeadVO) deployInVO.getParentVO();
//						DeployInBodyVO[] body = (DeployInBodyVO[]) deployInVO.getChildrenVO();
//						if ("碧优资".equals(head.getDef1())) {
//							// 推送给ARC
//							new ARCApproveAction().sendApprove(body[0].getAttributeValue("pk_card") + "",
//									head.getDef2(), 4);
//						}
						
						//补充异常丢失的卡片主键、资产编码 add by helen 2022-03-08
						DeployInBodyVO[] body = (DeployInBodyVO[]) deployInVO.getChildrenVO();
						for(DeployInBodyVO bvo:body) {
							AssetVO assetVO=BillInfoQueryUtil.selectCard(bvo.getPk_card_in());
							if(assetVO!=null&&assetVO.getAsset_code()!=null&&bvo.getPk_card_in()!=null) {
								String sql="update fa_deployin_b set pk_card_in='"+bvo.getPk_card_in()+"',asset_code='"+assetVO.getAsset_code()+"' where pk_deployin_b='"+bvo.getPrimaryKey()+"'";
								String cardsql="update fa_cardsub set def21='N' where pk_card='"+bvo.getPk_card_in()+"'";
								baseDAO.executeUpdate(sql);
								baseDAO.executeUpdate(cardsql);
							}
						}
					}
				} else if (retObject instanceof DeployInVO) {
					DeployInVO deployInVO = (DeployInVO) retObject;
					String link_keys = (String) deployInVO.getChildrenVO()[0].getAttributeValue("pk_usedept");
					if (StringUtils.isNotEmpty(link_keys)) {
						DeptScaleVO[] deptScaleVOs = ((IUseDeptScaleService) AMProxy.lookup(IUseDeptScaleService.class))
								.queryUseDeptByLink(new String[] { link_keys });
						if (null != deptScaleVOs && deptScaleVOs.length > 0 && null != deptScaleVOs[0])
							deployInVO.getChildrenVO()[0].setAttributeValue("pk_usedept", deptScaleVOs[0].getPk_dept());
					}

					// add by djy 20220216 增加对ARC系统的对接
					// modefy by djy 20220305 不在审批动作类同步ARC
//					DeployInHeadVO head = (DeployInHeadVO) deployInVO.getParentVO();
//					DeployInBodyVO[] body = (DeployInBodyVO[]) deployInVO.getChildrenVO();
//					if ("碧优资".equals(head.getDef1())) {
//						// 推送给ARC
//						new ARCApproveAction().sendApprove(body[0].getAttributeValue("pk_card") + "", head.getDef2(),
//								4);
//					}
					
					//补充异常丢失的卡片主键、资产编码 add by helen 2022-03-08
					DeployInBodyVO[] body = (DeployInBodyVO[]) deployInVO.getChildrenVO();
					for(DeployInBodyVO bvo:body) {
						AssetVO assetVO=BillInfoQueryUtil.selectCard(bvo.getPk_card_in());
						if(assetVO!=null&&assetVO.getAsset_code()!=null&&bvo.getPk_card_in()!=null) {
							String sql="update fa_deployin_b set pk_card_in='"+bvo.getPk_card_in()+"',asset_code='"+assetVO.getAsset_code()+"' where pk_deployin_b='"+bvo.getPrimaryKey()+"'";
							String cardsql="update fa_cardsub set def21='N' where pk_card='"+bvo.getPk_card_in()+"'";
							baseDAO.executeUpdate(sql);
							baseDAO.executeUpdate(cardsql);
						}
					}
				}
			}
			return retObject;
		} catch (Exception ex) {
			if (ex instanceof BusinessException)
				throw (BusinessException) ex;
			throw new PFBusinessException(ex.getMessage(), ex);
		}
	}

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

	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);
	}
}
