package nc.pubimpl.pu.m25.arap.f1;

import java.util.Map;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.ml.NCLangResOnserver;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.arap.payable.PayableBillItemVO;
import nc.vo.arap.pub.BillEnumCollection.FromSystem;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pu.pub.util.AggVOUtil;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import org.apache.commons.lang.ArrayUtils;

public class PayablebillUpdateListener implements IBusinessListener {
	public void doAction(IBusinessEvent event) throws BusinessException {
		try {
			if ("1004".equals(event.getEventType())) {
				BdUpdateEvent e = (BdUpdateEvent) event;
				AggregatedValueObject[] oldVos = (AggregatedValueObject[]) ((AggregatedValueObject[]) e.getOldObject());
				AggregatedValueObject[] newVos = (AggregatedValueObject[]) ((AggregatedValueObject[]) e.getNewObject());
				if (ArrayUtils.isEmpty(oldVos) || ArrayUtils.isEmpty(newVos)) {
					return;
				}

				AggPayableBillVO newVo = (AggPayableBillVO) newVos[0];
				AggPayableBillVO oldVo = (AggPayableBillVO) oldVos[0];
				if (!FromSystem.PO.VALUE.equals(newVo.getHeadVO().getSrc_syscode())) {
					return;
				}

				this.checkAddOrDelRows(oldVo, newVo);
				//this.checkUpdateFields(oldVo, newVo);
			}
		} catch (Exception var7) {
			ExceptionUtils.marsh(var7);
		}

	}

	private void checkAddOrDelRows(AggPayableBillVO oldVo, AggPayableBillVO newVo) {
		PayableBillItemVO[] payableBillItemVOs = (PayableBillItemVO[]) newVo.getChildrenVO();
		for (int i = 0; i < payableBillItemVOs.length; i++) {
			if(payableBillItemVOs[i].getDef12() != null) {
				if(payableBillItemVOs[i].getTop_billid() == null) {
					ExceptionUtils
					.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0037"));
				}
			}
		}
//		if (oldVo.getBodyVOs().length != newVo.getBodyVOs().length) {
//			ExceptionUtils
//					.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0037"));
//		}
//
//		Map<String, PayableBillItemVO> oldPayItemVOMap = AggVOUtil.createItemMap(new AggPayableBillVO[]{oldVo});
//		PayableBillItemVO[] arr$ = newVo.getBodyVOs();
//		int len$ = arr$.length;
//
//		for (int i$ = 0; i$ < len$; ++i$) {
//			PayableBillItemVO item = arr$[i$];
//			if (oldPayItemVOMap.get(item.getPk_payableitem()) == null) {
//				ExceptionUtils.wrappBusinessException(
//						NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0037"));
//			}
//		}

	}

	private void checkUpdateFields(AggPayableBillVO oldVo, AggPayableBillVO newVo) {
		Map<String, PayableBillItemVO> oldPayItemVOMap = AggVOUtil.createItemMap(new AggPayableBillVO[]{oldVo});
		PayableBillItemVO[] arr$ = newVo.getBodyVOs();
		int len$ = arr$.length;

		for (int i$ = 0; i$ < len$; ++i$) {
			PayableBillItemVO item = arr$[i$];

			for (int i = 0; i < this.getCheckFields().length; ++i) {
				Object newValue = item.getAttributeValue(this.getCheckFields()[i][0]);
				Object oldValue = ((PayableBillItemVO) oldPayItemVOMap.get(item.getPk_payableitem()))
						.getAttributeValue(this.getCheckFields()[i][0]);
				if ((newValue != null || oldValue != null) && (newValue != null && !newValue.equals(oldValue)
						|| oldValue != null && !oldValue.equals(newValue))) {
					ExceptionUtils.wrappBusinessException(NCLangResOnserver.getInstance().getStrByID("4004050_0",
							"04004050-0107", (String) null, new String[]{this.getCheckFields()[i][1]}));
				}
			}
		}

	}

	private String[][] getCheckFields() {
		String[][] checkFields = new String[][]{
				{"pk_currtype", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0001755")},
				{"rate", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0002837")},
				{"price", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0000741")},
				{"taxprice", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0001160")},
				{"supplier", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0000275")},
				{"material", NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0038")},
				{"taxrate", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0003078")},
				{"quantity_cr", NCLangRes4VoTransl.getNCLangRes().getStrByID("common", "UC000-0003856")},
				{"local_tax_cr", NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0039")},
				{"notax_cr", NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0040")},
				{"local_money_cr", NCLangRes4VoTransl.getNCLangRes().getStrByID("4004050_0", "04004050-0042")},
				{"taxcodeid", NCLangRes4VoTransl.getNCLangRes().getStrByID("4004030_2", "2400403002-0187")}};
		return checkFields;
	}
}