package nc.impl.fa.asset.rule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import nc.bs.am.framework.action.ActionContext;
import nc.bs.am.framework.action.IRule;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.fa.assetcard.AggCardVO;
import nc.vo.fa.assetcard.CardVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class BeforeCheckDownstream implements IRule<AggCardVO> {

	@Override
	public void process(ActionContext<AggCardVO> context, AggCardVO... aggVos) throws BusinessException {
		if (aggVos == null || aggVos.length == 0) {
			throw new BusinessException("VO为空");
		}

		for (AggCardVO aggvo : aggVos) {
			// 保存检验，资产卡片是否有下游单据，或者有折旧记录，或者当月已结账，不允许修改名称
			checkCardName(aggvo);
		}
	}

	public void checkCardName(AggCardVO aggvo) throws BusinessException {
		IUAPQueryBS query = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		String asset_name = (String) aggvo.getParentVO().getAttributeValue("asset_name");
		// 1.是否存在下游单据，变动、减少、调入、调出、拆分、合并
		CardVO headvo = (CardVO) query.retrieveByPK(CardVO.class, aggvo.getPrimaryKey());

		if (headvo != null) {
			String name = null;
			String namesql = "select asset_name from fa_card where nvl(dr,0)=0 and pk_card='" + aggvo.getPrimaryKey()
					+ "'";
			name = (String) query.executeQuery(namesql, new ColumnProcessor());
			if (!asset_name.equals(name) && name != null) {
				String before = "select count(1) from ";
				String after = " where nvl(dr,0)=0 and pk_card='" + aggvo.getPrimaryKey() + "'";
				int count = 0;
				String HG = before + "fa_alter_b" + after;
				count = (int) query.executeQuery(HG, new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游变动单不允许修改资产名称");
				String HF = before + "fa_reduce_b" + after;
				count = (int) query.executeQuery(HF, new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游减少单不允许修改资产名称");
				String HO = before + "fa_assetcombin" + after;
				count = (int) query.executeQuery(HO, new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游合并单不允许修改资产名称");
				String HM = before + "fa_assetsplit_b" + after;
				count = (int) query.executeQuery(HM, new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游拆分单不允许修改资产名称");			
				String HD = before + "fa_deployin_b";
				count = (int) query.executeQuery(HD+" where nvl(dr,0)=0 and pk_card_in ='" + aggvo.getPrimaryKey() + "'", new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游调入单不允许修改资产名称");
				String HC = before + "fa_deployout_b" + after;
				count = (int) query.executeQuery(HC+"", new ColumnProcessor());
				if(count>0)
					throw new BusinessException("存在下游调出单不允许修改资产名称");
				
//				 2.是否有折旧记录 不控
//				String History = "select count(1) from fa_cardhistory where nvl(dr,0)=0 and newasset_flag!=0 and laststate_flag!='Y'and pk_card='"
//						+ aggvo.getPrimaryKey() + "'";
//				count = (int) query.executeQuery(History, new ColumnProcessor());
//				if (count > 0)
//					throw new BusinessException("资产已折旧不允许修改资产名称");

				// 3.当月是否已结账
				String mapsql = "select accyear,period,pk_accbook  from fa_cardhistory where nvl(dr,0)=0 and laststate_flag='Y' and pk_card='"
						+ aggvo.getPrimaryKey() + "'";
				Map<String, String> map = (Map<String, String>) query.executeQuery(mapsql, new MapProcessor());
				if (map != null && map.get("accyear") != null && map.get("period") != null
						&& map.get("pk_accbook") != null) {
					String bookclose = "select count(1) from fa_closebook where nvl(dr,0)=0 and pk_accbook='"
							+ map.get("pk_accbook")+"' and accyear='"+map.get("accyear")
							+ "' and period='"+map.get("period")+"'";
					int num = (int) query.executeQuery(bookclose, new ColumnProcessor());
					if (num > 0) {
						try {
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
							String strdate = ((UFDate) aggvo.getParentVO().getAttributeValue("business_date"))+"";
							 Date carddate = sdf.parse(strdate.substring(0, 7));
							Date peridate = sdf.parse(map.get("accyear") +"-"+ map.get("period"));
							if (carddate.compareTo(peridate) <= 0)
								throw new BusinessException("当月资产已经结账不允许修改资产名称");
						} catch (Exception e) {
							ExceptionUtils.wrappBusinessException(e.getMessage());
						}
					}
				}
			}
		}
	}
}
