package yem.wm.bd.companyinfo.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 公司抬头保存校验
 *              校验{银行信息}、{联系人信息}有且有唯一的默认值
 * @Author: zhangh
 * @CreateTime: 2025-02-08  17:09
 */
public class CompanyInfoSaveValidator extends AbstractValidator {
    @Override
    public void validate() {
        for (ExtendedDataEntity extendedDataEntity : this.getDataEntities()) {
            checkDefault(extendedDataEntity);
        }
    }

    /**
     * @Description: 校验逻辑
     * @function 校验{银行信息}、{联系人信息}有且仅有唯一的默认值
     * @purposes 用途
     * @param extendedDataEntity
     * @return void
     */

    public void checkDefault(ExtendedDataEntity extendedDataEntity) {
        DynamicObject dataEntity = extendedDataEntity.getDataEntity();//获取单据的实体

        DynamicObjectCollection banksentry = dataEntity.getDynamicObjectCollection("yem_banksentry");//获取《公司抬头》--{银行信息}
        List<DynamicObject> isdefaultbank = banksentry.stream().filter(x -> x.getBoolean("yem_isdefaultbank") == true).collect(Collectors.toList());//获取《公司抬头》--{银行信息}中【默认】为是的集合
        if (banksentry.size() > 0 && (isdefaultbank.size() > 1 || isdefaultbank.size() == 0)) {
            this.addErrorMessage(extendedDataEntity, "银行信息中需要有一个默认。");
        }

        DynamicObjectCollection linkmanentry = dataEntity.getDynamicObjectCollection("yem_linkmanentry");//获取《公司抬头》--{联系人信息}
        List<DynamicObject> isdefaultlinkman = linkmanentry.stream().filter(x -> x.getBoolean("yem_isdefaultlinkman") == true).collect(Collectors.toList());//获取《公司抬头》--{联系人信息}中【默认】为是的集合
        if (linkmanentry.size() > 0 && (isdefaultlinkman.size() > 1 || isdefaultlinkman.size() == 0)) {
            this.addErrorMessage(extendedDataEntity, "联系人信息中需要有一个默认的联系人。");
        }
    }
}
