package yem.wm.cm.ht.valdiator;

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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.base.common.utils.YEM;

/**
 * 合同条款库(提交校验)
 * 1、在保存时进行校验，挖空字段是否在【条款对照中存在】
 * 2、中文、英文判断挖空字段是否长度一致、标识一致
 * @author zhangh
 *
 */
public class ContractTermsSubmitValidator extends AbstractValidator{

	@Override
	public void validate() {
		ExtendedDataEntity[] dataEntity = this.getDataEntities();
		for (ExtendedDataEntity extendedDataEntity : dataEntity) {
			//this.checkHollowing(extendedDataEntity);//原来校验挖空字段个数是否一致
			this.checkLanguageType(extendedDataEntity);
		}
	}

	/**
	 *校验合同条款库中语言类型有没有重复
	 */
	private void checkLanguageType(ExtendedDataEntity extendedDataEntity) {
		DynamicObject dataEntity = extendedDataEntity.getDataEntity();
		DynamicObjectCollection entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
		Set<String> set = new HashSet<>();
		for (int i = 0; i < entryentity.size();i++) {
			DynamicObject languageObj = entryentity.get(i).getDynamicObject("yem_languagetype");
			String language = languageObj.getString("name");
			set.add(language);
			if(set.size() != i+1){
				this.addErrorMessage(extendedDataEntity,"单据编号为" + dataEntity.getString("billno") +"的单据体，第"+ (i+1) +"行"+language+"已出现");
			}
		}
	}

	/**
	 * 校验挖空字段
	 * @param extendedDataEntity 当前页面动态对象
	 */
	private void checkHollowing(ExtendedDataEntity extendedDataEntity) {
		DynamicObject dataEntity = extendedDataEntity.getDataEntity();
		String yem_foreigntrade = dataEntity.getString("yem_foreigntrade");
		if (StringUtils.isNotEmpty(yem_foreigntrade)) {
			if (yem_foreigntrade.equals("A")) {
				this.checkChineseAndEnlishSame(dataEntity, extendedDataEntity);
			}
		}
		this.queryFieldControl(extendedDataEntity, dataEntity);//根据中文的挖空字段判断是否在【字段对照】表中存在
	}

	/**
	 * 根据中文的挖空字段判断是否在【字段对照】表中存在
	 * @param extendedDataEntity
	 * @param dataEntity 
	 */
	private void queryFieldControl(ExtendedDataEntity extendedDataEntity, DynamicObject dataEntity) {
		String yem_clauseschina = dataEntity.getString("yem_clausecontentchina");
		List<String> clauseschina = new ArrayList<>();
		if (StringUtils.isNotEmpty(yem_clauseschina)) {
			for (int i = 0; i < yem_clauseschina.length(); i++) {
				if (yem_clauseschina.contains("【")) {
					yem_clauseschina = this.cutOutString(yem_clauseschina, clauseschina);//根据挖空字段截取字符串
				}
			}
		}
		if (clauseschina.size() > 0) {
			for (String str : clauseschina) {
				//获取所有的占位符内的值，查询对应表中是否有该标识，若没有的话校验失败
				DynamicObject clausecontrast = BusinessDataServiceHelper.loadSingle("yem_bd_clausecontrast", "id", new QFilter[] {new QFilter("yem_clausekey", QCP.equals, str)});
				if (clausecontrast == null) {
					this.addErrorMessage(extendedDataEntity, String.format("挖空字段: %s在字段对照中不存在!!!", str));
				}
			}
		}
	}

	/**
	 * 贸易类型为外贸，判断中英文内容中挖空字段值，以及挖空字段数量是否一致
	 * @param dataEntity
	 * @param extendedDataEntity 
	 */
	private void checkChineseAndEnlishSame(DynamicObject dataEntity, ExtendedDataEntity extendedDataEntity) {
		DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
		String yem_clauseschina = dynamicObject.getString("yem_clausecontentchina_tag");
		String yem_clausesenglish = dynamicObject.getString("yem_clausecontentenglish_tag");
		List<String> clauseschina = new ArrayList<>();
		List<String> clausesenglish = new ArrayList<>();
		if (YEM.isNotEmpty(yem_clauseschina) && YEM.isNotEmpty(yem_clausesenglish)) {
			if (StringUtils.isNotEmpty(yem_clauseschina)) {//对中文内容进行截断
				for (int i = 0; i < yem_clauseschina.length(); i++) {
					if (yem_clauseschina.contains("【")) {
						yem_clauseschina = this.cutOutString(yem_clauseschina, clauseschina);//根据中文括号截断字符串
					}
				}
			}
			if (StringUtils.isNotEmpty(yem_clausesenglish)) {//对英文内容进行截断
				for (int i = 0; i < yem_clausesenglish.length(); i++) {
					if (yem_clausesenglish.contains("【")) {
						yem_clausesenglish = this.cutOutString(yem_clausesenglish, clausesenglish);
					}
				}
			}
			if (clauseschina.size() == clausesenglish.size()) {
				for (String str : clauseschina) {
					if (!clausesenglish.contains(str)) {
						this.addErrorMessage(extendedDataEntity, String.format("中文挖空字段: %s在英文挖空字段中不存在!!!", str));
						return;
					}
				}
				for (String str : clausesenglish) {
					if (!clauseschina.contains(str)) {
						this.addErrorMessage(extendedDataEntity, String.format("英文挖空字段: %s在中文挖空字段中不存在!!!", str));
						return;
					}
				}
			}else {
				this.addErrorMessage(extendedDataEntity, "中文内容挖空字段和英文挖空字段长度不一致!!!");
				return;
			}
		}
	}

	/**
	 * 根据挖空字段截取字符串
	 * @param yem_clauseschina
	 * @param clauseschina
	 */
	private String cutOutString(String yem_clauseschina, List<String> clauseschina) {
		String substring = yem_clauseschina.substring(yem_clauseschina.indexOf("【") + 1, yem_clauseschina.indexOf("】"));
		String str = yem_clauseschina.substring(0, yem_clauseschina.indexOf("【")) + 
				yem_clauseschina.substring(yem_clauseschina.indexOf("】") + 1);
		clauseschina.add(substring);
		return str;
	}
}
