package nc.vo.scmpub.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.vo.bd.material.MaterialVersionVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.VOStatus;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nccloud.commons.lang.StringUtils;

/**
 * @author jilush
 * @version ncc1.0
 * @description 物料未做版本化时oid、vid一致性校验
 * @date 2022/6/27 14:56
 */
public class MaterialOVidConsistentCheck {

	/**
	 * @description 校验物料ovid是否一致
	 * @param vos
	 * @param materialoidField 物料oid名称
	 * @param materialvidField 物料vid名称
	 * @param crownoField      行号名称
	 * @return
	 * @author jilush
	 * @date 2022/6/27 15:02
	 * @version ncc1.0
	 */
	public void check(AbstractBill[] vos, String materialoidField, String materialvidField, String crownoField) {
		// 1、构造没有做过版本化的物料set
		Set<String> noMulVersionMaterialSet = getIsBMulVersionMaterialMap(vos, materialoidField);
		// 2、校验非多版本
		checkMulVersion(vos, materialoidField, materialvidField, crownoField, noMulVersionMaterialSet);
	}

	/**
	 * @param vos
	 * @param pk_sourceField
	 * @param pk_materialField
	 * @param crownoField
	 * @param isBMulVersionMaterialMap
	 * @return
	 * @description 校验多版本
	 * @author jilush
	 * @date 2022/6/27 15:20
	 * @version ncc1.0
	 */
	private void checkMulVersion(AbstractBill[] vos, String pk_sourceField, String pk_materialField, String crownoField,
			Set<String> noMulVersionMaterialSet) {
		for (AbstractBill vo : vos) {
			CircularlyAccessibleValueObject[] childrenVOS = vo.getChildrenVO();
			StringBuilder errorMsg = new StringBuilder();
			List<String> crownList = new ArrayList<>();
			for (CircularlyAccessibleValueObject childrenVO : childrenVOS) {
				if (childrenVO.getStatus() != VOStatus.DELETED) {
					String pk_source = (String) childrenVO.getAttributeValue(pk_sourceField);
					// 未做过版本化时校验
					if (noMulVersionMaterialSet.contains(pk_source)) {
						String pk_material = (String) childrenVO.getAttributeValue(pk_materialField);
						if (!StringUtils.equals(pk_material, pk_source)) {
							crownList.add((String) childrenVO.getAttributeValue(crownoField));
						}
					}
				}
			}
			if (crownList.size() > 0) {
				errorMsg.append(NCLangRes4VoTransl.getNCLangRes().getStrByID("4001002_0", "04001002-0811", null,
						crownList.toArray(new String[0])));
				errorMsg.append(NCLangRes4VoTransl.getNCLangRes().getStrByID("4001002_0", "04001002-0856"));
				ExceptionUtils.wrappBusinessException(errorMsg.toString());
			}
		}
	}

	/**
	 * @description 根据vo查询物料oid信息并组装set
	 * @param vos
	 * @param pk_sourceField
	 * @return
	 * @author jilush
	 * @date 2022/6/27 15:10
	 * @version ncc1.0
	 */
	private Set<String> getIsBMulVersionMaterialMap(AbstractBill[] vos, String pk_sourceField) {
		// 1、获取物料版本主键
		Set<String> pk_sourceSet = new HashSet<String>();
		for (AbstractBill vo : vos) {
			CircularlyAccessibleValueObject[] childrenVOs = vo.getChildrenVO();
			for (CircularlyAccessibleValueObject childrenVO : childrenVOs) {
				if (childrenVO.getStatus() != VOStatus.DELETED) {
					pk_sourceSet.add((String) childrenVO.getAttributeValue(pk_sourceField));
				}
			}
		}
		if (pk_sourceSet.size() > 0) {
			// 2、查询物料版本信息
			VOQuery<MaterialVersionVO> materialVersionVOQuery = new VOQuery<>(MaterialVersionVO.class);
			MaterialVersionVO[] materialVersionVOS = materialVersionVOQuery.query(pk_sourceSet.toArray(new String[0]));
			// 3、组装set，版本号大于1表示物料做过版本化
			Set<String> noMulVersionMaterialSet = new HashSet<>();
			for (MaterialVersionVO materialVersionVO : materialVersionVOS) {
				if (materialVersionVO.getVersion() == 1) {
					noMulVersionMaterialSet.add(materialVersionVO.getPk_source());
				}
			}
			return noMulVersionMaterialSet;
		}
		return new HashSet<String>();

	}

}
