package nccloud.api.impl.pu.m23;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import nc.bs.framework.common.NCLocator;
import nc.bs.pf.pub.PfDataCache;
import nc.itf.scmpub.reference.uap.pf.PfServiceScmUtil;
import nc.itf.uap.pf.IPfExchangeService;
import nc.pubimpl.pu.m23.api.ProduceDateSetForArrival;
import nc.ui.pub.print.version55.util.DeepClone;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pu.m23.entity.ArriveHeaderVO;
import nc.vo.pu.m23.entity.ArriveItemVO;
import nc.vo.pu.m23.entity.ArriveVO;
import nc.vo.pu.m23.rule.api.check.PUArriveVONullValidate;
import nc.vo.pub.BusinessException;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MathTool;
import nc.vo.scmpub.check.billvalidate.BillVOsCheckRule;
import nccloud.api.impl.pu.m23.fill.ArriveSaveFromOrder;
import nccloud.api.impl.pu.m23.util.queryFromOrder;
import nccloud.api.pu.m23.IAPIPuArriveMaitain;
import nccloud.openapi.scmpub.pub.TransferCodeToPKTool;
import nccloud.openapi.scmpub.pub.TransferMapToVOTool;

/**
 * zhangxinah 解决参照采购订单生成到货单校验过早的问题
 * 2312
 */
public class APIPuArriveMaitainImpl implements IAPIPuArriveMaitain {
	public ArriveVO[] saveFromOrder(List<Map<String, Object>> paramList) throws BusinessException {
		ArriveVO[] arriveVOs = null;
		ArriveVO[] saveVOs = null;

		try {
			List<ArriveVO> aggVOList = TransferMapToVOTool.transferMapToAggVO(paramList, ArriveVO.class);

			ArriveVO[] vos = (ArriveVO[]) aggVOList.toArray(new ArriveVO[aggVOList.size()]);

			aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList);

			HashSet<String> pk_order = new HashSet<String>();
			for (ArriveVO vo : vos) {
				for (ArriveItemVO itemVO : vo.getBVO()) {
					pk_order.add(itemVO.getCsourceid());
				}
			}

			OrderVO[] orderVOs = (new queryFromOrder()).query((String[]) pk_order.toArray(new String[0]));
			calNumAndMny(orderVOs);

			IPfExchangeService service = (IPfExchangeService) NCLocator.getInstance().lookup(IPfExchangeService.class);
			arriveVOs = (ArriveVO[]) service.runChangeDataAry("21", "23", orderVOs, null);
			
			BillVOsCheckRule checker = new BillVOsCheckRule(new PUArriveVONullValidate());
			checker.check(arriveVOs);

			Map<String, ArriveHeaderVO> destiHMap = new HashMap<String, ArriveHeaderVO>();
			for (int i = 0; i < arriveVOs.length; i++) {
				destiHMap.put(arriveVOs[i].getBVO()[0].getCsourceid(), arriveVOs[i].getHVO());
			}

			Map<String, ArriveItemVO> destiItemMap = new HashMap<String, ArriveItemVO>();
			for (ArriveVO arriveVO : arriveVOs) {
				for (ArriveItemVO arriveItemVO : arriveVO.getBVO()) {
					destiItemMap.put(arriveItemVO.getCsourcebid(), arriveItemVO);
				}
			}

			saveVOs = new ArriveVO[vos.length];
			for (int i = 0; i < saveVOs.length; i++) {
				saveVOs[i] = new ArriveVO();
				saveVOs[i].setHVO((ArriveHeaderVO) destiHMap.get(arriveVOs[i].getBVO()[0].getCsourceid()));
				ArriveItemVO[] itemvos = new ArriveItemVO[vos[i].getBVO().length];
				for (int j = 0; j < itemvos.length; j++) {

					itemvos[j] = (ArriveItemVO) DeepClone.clone(destiItemMap.get(vos[i].getBVO()[j].getCsourcebid()));
				}
				saveVOs[i].setBVO(itemvos);
			}

			for (int i = 0; i < vos.length; i++) {

				ArriveHeaderVO HVO = vos[i].getHVO();

				ArriveItemVO[] BVO = vos[i].getBVO();

				for (String key : HVO.getAttributeNames()) {

					if (null != HVO.getAttributeValue(key)) {
						saveVOs[i].getHVO().setAttributeValue(key, HVO.getAttributeValue(key));
					}
				}

				for (int j = 0; j < BVO.length; j++) {
					for (String key : BVO[j].getAttributeNames()) {

						if (null != BVO[j].getAttributeValue(key)) {
							saveVOs[i].getBVO()[j].setAttributeValue(key, BVO[j].getAttributeValue(key));
						}
					}
				}
			}

			for (ArriveVO saveVO : saveVOs) {

				int rowNum = 0;
				ArriveItemVO[] bvos = saveVO.getBVO();
				for (int i = 0; i < bvos.length; i++) {
					rowNum++;
					bvos[i].setCrowno("" + rowNum * 10);
				}

				BilltypeVO trantypeVO = PfDataCache.getBillTypeInfo(AppContext.getInstance().getPkGroup(),
						saveVO.getHVO().getVtrantypecode());
				saveVO.getHVO().setCtrantypeid(trantypeVO.getPk_billtypeid());
			}

			(new ArriveSaveFromOrder()).setDefaultValue(saveVOs);

			(new ProduceDateSetForArrival()).setProduceDate(saveVOs);

			saveVOs = (ArriveVO[]) PfServiceScmUtil.processBatch("SAVEBASE", "23", saveVOs, null, null);
		} catch (Exception e) {
			ExceptionUtils.wrappException(e);
		}
		return saveVOs;
	}

	private void calNumAndMny(OrderVO[] vos) {
		for (OrderVO vo : vos) {
			OrderItemVO[] bvos = vo.getBVO();
			for (OrderItemVO bvo : bvos) {

				if (bvo.getNnopayorgmny() == null) {
					bvo.setNnopayorgmny(MathTool.sub(bvo.getNtaxmny(), bvo.getNacccancelinvmny()));
				}

				if (bvo.getNcanarrivenum() == null) {
					bvo.setNcanarrivenum(MathTool.sub(bvo.getNnum(), bvo.getNaccumarrvnum()));
				}

				if (bvo.getNcaninnum() == null) {
					bvo.setNcaninnum(MathTool.sub(bvo.getNnum(), bvo.getNaccumstorenum()));
				}

				if (bvo.getNcaninvoicenum() == null)
					bvo.setNcaninvoicenum(MathTool.sub(bvo.getNnum(), bvo.getNaccuminvoicenum()));
			}
		}
	}
}
