package yem.wmzh.csm.customeraccess.opplugin.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.ILocaleString;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import yem.base.common.utils.YEM;

import java.util.List;

import static yem.wmzh.csm.customeraccess.from.CustomeraccessBillPlugin.loadAdminOrgNames;

/**
 * @Description 客商准入单提交校验
 * @Author: LiuHB
 * @CreateTime: 2025-02-07  17:26
 */
public class CustomeraccessSubmitValidator extends AbstractValidator {
    /**
     * @Description: 自定义校验器
     * @function 1.【内部业务单元】=true,且字段【境内/境外】=境内时，统一社会信用代码必填
     *           2.检查 客商名称 是否在行政组织列表中
     *           3.检查客商名称 是否和准入组织相同
     * @purposes 重写系统validate方法在提交节点增加控制
     */
    @Override
    public void validate() {
        // 如果操作键不是 "submit"，直接返回
        if (!"submit".equals(this.getOperateKey())) {
            return;
        }
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        // 遍历每个数据实体并进行校验
        for (ExtendedDataEntity entity : dataEntities) {
            //【内部业务单元】=true,且字段【境内/境外】=境内时，统一社会信用代码必填
            validateCustomerInternalInfo(entity.getDataEntity(), entity);
            //客商名称校验
            Calibratecustomername(entity.getDataEntity(), entity);
        }
    }

    private void Calibratecustomername(DynamicObject dataEntity, ExtendedDataEntity entity) {
        String yemMerchantstnamed = dataEntity.getLocaleString("yem_merchantstname").getLocaleValue();
        Boolean yemInternal = (Boolean) dataEntity.getBoolean("yem_internal");//内部业务单元
        //校验必须有与客商名称匹配的组织
        matchOrganizations(yemInternal, dataEntity,yemMerchantstnamed,entity);

    }



    private void matchOrganizations(Boolean yemInternal,  DynamicObject dataEntity,String yemMerchantstnamed,  ExtendedDataEntity entity) {
        // 如果 内部业务单元 为 false 或 客商中文名称 为空，直接返回
        if (!yemInternal || YEM.isEmpty(yemMerchantstnamed)) {
            return;
        }
        //获取当前单据准入组织
        DynamicObject org = (DynamicObject)dataEntity.getDynamicObject("org");
        String name = null != org ? org.getString("name") : "";
        // 加载行政组织信息
        List<String> adminOrgNames = loadAdminOrgNames();
        // 1.检查 客商名称 是否在行政组织列表中
        //2.检查客商名称 是否和准入组织相同
        if (!adminOrgNames.contains(yemMerchantstnamed)) {

            this.addMessage(entity,"未找到与之匹配的组织，请重新检查相关信息。");
        }if(!name.equals(yemMerchantstnamed)){

            this.addMessage(entity,"该内部业务单元客商:"+yemMerchantstnamed+"与准入组织不符，请检查！");
        }
    }

    /**
     * @Description: 校验客户内部信息
     */
    private void validateCustomerInternalInfo(DynamicObject customerAccess, ExtendedDataEntity entity) {
        // 获取内部业务单元、境内/境外、统一社会信用代码
        boolean yemInternal = customerAccess.getBoolean("yem_internal");
        String domesticOrOverseas = customerAccess.getString("yem_domesticoroverseas");
        String societyCreditCode = customerAccess.getString("yem_societycreditcode");
        // 如果是内部业务单元且位于境内，检查统一社会信用代码是否为空
        if (isInternalAndDomestic(yemInternal, domesticOrOverseas) && YEM.isEmpty(societyCreditCode)) {
            this.addMessage(entity, "请填写【统一社会信用代码】");
        }
    }

    /**
     * @Description: 判断是否为内部业务单元且位于境内
     */
    private boolean isInternalAndDomestic(boolean yemInternal, String domesticOrOverseas) {
        return yemInternal && "A".equals(domesticOrOverseas);
    }
}
