package nc.bs.mmpac.wr.bp;

//import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.mmpac.wr.rule.prodin.WrProdInSetInStockOrgRule;
import nc.bs.mmpac.wr.rule.prodin.WrProdInSetWarehouseRule;
import nc.bs.uif2.validation.ValidationException;
import nc.bs.uif2.validation.ValidationFailure;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.util.mmf.framework.base.MMNumberUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.wr.consts.WrptLangConst;
import nc.vo.mmpac.wr.entity.AggWrVO;
import nc.vo.mmpac.wr.entity.WrItemVO;
import nc.vo.mmpac.wr.entity.WrQualityVO;
import nc.vo.mmpac.wr.entity.WrTransTypeVO;
import nc.vo.mmpac.wr.enumeration.WrBillStatusEnum;
import nc.vo.mmpac.wr.enumeration.WrQualityProcessMethodEnum;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;

public class WrAutoProdInFilterBP {

	
	public AggWrVO[] filter(AggWrVO[] aggWrVOs) {
		//过滤交易类型是否勾选了自动入库
		List<String> transIDs = new ArrayList<String>();
		for (AggWrVO aggvo : aggWrVOs) {
			if (!WrBillStatusEnum.COMMITE.equalsValue(aggvo.getParentVO().getFbillstatus())) {
				continue;
			}
			if (MMValueCheck.isEmpty(transIDs) || !transIDs.contains(aggvo.getParentVO().getVtrantypeid())) {
				transIDs.add(aggvo.getParentVO().getVtrantypeid());
			}
		}
		Map<String, Boolean> isAutoInMap = this.getIsAutoProdIn(transIDs);
		if (MMValueCheck.isEmpty(isAutoInMap)) {
			return null;
		}
		// 入库过滤规则
		List<AggWrVO> inVOs = new ArrayList<AggWrVO>();
		for (AggWrVO aggvo : aggWrVOs) {
			//审批通过
			if (!WrBillStatusEnum.COMMITE.equalsValue(aggvo.getParentVO().getFbillstatus())) {
				continue;
			}
			//交易类型：自动入库
			String transID = aggvo.getParentVO().getVtrantypeid();
			if (!isAutoInMap.containsKey(transID) ||! MMValueCheck.isTrue(isAutoInMap.get(transID))) {
				continue;
			}
			//过滤类型为合格入库的
			if(MMValueCheck.isEmpty(aggvo.getChildrenVO())) {
				continue;
			}
			List<WrItemVO> initems=new ArrayList<WrItemVO>();
			for(WrItemVO item:aggvo.getChildrenVO()) {
				if(MMValueCheck.isEmpty(item.getQualityvos())){
					continue;
				}
				List<WrQualityVO> inqvos=new ArrayList<WrQualityVO>();
				for(WrQualityVO qvo:item.getQualityvos()) {
					if(WrQualityProcessMethodEnum.PRODIN.equalsValue( qvo.getFgprocessmethod())&&MMNumberUtil.isGtZero(qvo.getNgnum())) {
						WrQualityVO inqvo=(WrQualityVO)qvo.clone();
						inqvo.setNgtoinnum(inqvo.getNgnum());
						inqvo.setNgtoinastnum(inqvo.getNgastnum());
						inqvos.add(inqvo);
					}
				}
				if(MMValueCheck.isEmpty(inqvos)) {
					continue;
				}
				WrItemVO initem=(WrItemVO)item.clone();
				initem.setQualityvos(inqvos.toArray(new WrQualityVO[0]));
				initems.add(initem);
			}
			if(MMValueCheck.isEmpty(initems)) {
				continue;
			}
			AggWrVO agg=(AggWrVO)aggvo.clone();
			agg.setChildren(WrItemVO.class, initems.toArray(new WrItemVO[0]));
			inVOs.add(agg);
			
		}
		if (MMValueCheck.isEmpty(inVOs)) {
			return null;
		}
		AggWrVO[] filterVOs = inVOs.toArray(new AggWrVO[0]);		
		// 设置库存组织和仓库
		IRule<AggWrVO> wrProdInSetInStockOrgRule = new WrProdInSetInStockOrgRule();
		wrProdInSetInStockOrgRule.process(filterVOs);
		//2025年5月30日08点45分 -- 外部系统推送接口时填了仓库，此处不清空
		String userCode = InvocationInfoProxy.getInstance().getUserCode();
		if("rzmes".equals(userCode)||"MES".equals(userCode)){

		}else{
			// 设置入库仓库规则
			IRule<AggWrVO> wrProdInSetWarehouseRule = new WrProdInSetWarehouseRule();
			wrProdInSetWarehouseRule.process(filterVOs);
		}
		//过滤掉入库仓库为空的数据
		List<AggWrVO>result=new ArrayList<AggWrVO>();
		for(AggWrVO agg:filterVOs) {
			List<WrItemVO> items=new ArrayList<WrItemVO>();
			for(WrItemVO item:agg.getChildrenVO()) {
				List<WrQualityVO> qvos=new ArrayList<WrQualityVO>();
				for(WrQualityVO qvo:item.getQualityvos()) {
					if(MMValueCheck.isNotEmpty(qvo.getCgwarehouseid())) {
						qvos.add(qvo);
					}
				}
				if(MMValueCheck.isNotEmpty(qvos)) {
					item.setQualityvos(qvos.toArray(new WrQualityVO[0]));
					items.add(item);
				}
			}
			if(MMValueCheck.isNotEmpty(items)) {
				agg.setChildren(WrItemVO.class, items.toArray(new WrItemVO[0]));
				result.add(agg);
			}
		}
		if(MMValueCheck.isEmpty(result)) {
			/** 生产报告审批成功，倒冲成功，自动入库失败！**/
			ValidationFailure validationFailure = new ValidationFailure(WrptLangConst.ERR_Audit_ProdIn());
			ExceptionUtils.wrappException(new ValidationException(Arrays.asList(validationFailure)));
		}
		return result.toArray(new AggWrVO[0]);
		
	}

	
	private Map<String, Boolean> getIsAutoProdIn(List<String> ctrantypeids) {
		if (MMValueCheck.isEmpty(ctrantypeids)) {
			return null;
		}
		SqlBuilder whereSql = new SqlBuilder();
		whereSql.append(" and ");
		whereSql.append(WrTransTypeVO.CTRANTYPEID, ctrantypeids.toArray(new String[0]));
		VOQuery<WrTransTypeVO> voQuery = new VOQuery<WrTransTypeVO>(WrTransTypeVO.class,
				new String[] { WrTransTypeVO.CTRANTYPEID, WrTransTypeVO.BCREATEPRODIN });
		WrTransTypeVO[] queryResults = voQuery.query(whereSql.toString(), null);
		if (MMValueCheck.isEmpty(queryResults)) {
			return null;
		}
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		for (WrTransTypeVO queryResult : queryResults) {
			result.put(queryResult.getCtrantypeid(), MMValueCheck.isTrue(queryResult.getBcreateprodin()));
		}
		return result;

	}
}
