package com.yem.wm.es.salesorder.validator;

import com.yem.wm.im.debcrednotenew.utils.DebCredUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouc
 * @date 2024/4/24 10:20
 * @className SaleOrdeExportisVoidValidator
 * @description 外销合同【外销作废】校验器
 */
public class SaleOrdeExportisVoidValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            String billstatus = dataEntity.getString("billstatus");
            if (!billstatus.equals("C")) {
                this.addErrorMessage(dataEntitie, "当前单据未审核，禁止作废！");
            }
            //强校验：
            //1、合同是否有发运合同，如果有不能作废；
            Shippingdetails(dataEntity, dataEntitie);
            //2、合同是否有下单审批，如果有不能作废；
            Placeexamine(dataEntity, dataEntitie);
            //3、合同是否有对应贷记单，如果有不能作废；
            Debcrednote(dataEntity, dataEntitie);
            //4、合同是否有对应的款项认领，如果有不能作废；
            MoneyClaim(dataEntity, dataEntitie);
            //5、合同是否有对应的客商额度申请的额度占用并且未释放额度，如果有不能作废；
            Merchant(dataEntity, dataEntitie);
            //弱校验：
            //1、合同是否在礼品资金池有对应的礼品来源，如果有弹框点击确定，确定后删除对于礼品资金池对于合同号的礼品来源分录及其子分录；（参考反审核的逻辑）
            Giftamount(dataEntity, dataEntitie);
        }
    }

    /**
     * 合同是否有发运合同，如果有不能作废；
     * //根据核心单据id，核心单据类型获取；
     *
     * @param dataEntity 单据对象
     * @param
     */
    public void Shippingdetails(DynamicObject dataEntity, ExtendedDataEntity dataEntitie) {
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_corebilltype", QCP.equals, "yem_es_salesorder");
        qFilter.and(new QFilter("yem_corebillid", QCP.equals, pkValue));
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_shippingdetails", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addErrorMessage(dataEntitie, "当前单据【" + billno + "】已存在发运明细【" + s + "】，禁止作废！");
        }
    }

    /**
     * 合同是否有下单审批，如果有不能作废；；
     *
     * @param dataEntity 单据对象
     * @param
     */
    public void Placeexamine(DynamicObject dataEntity, ExtendedDataEntity dataEntitie) {
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_contractnumber", QCP.equals, billno);
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_placeexamine", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addErrorMessage(dataEntitie, "当前单据【" + billno + "】已存在下单审批【" + s + "】，禁止作废！");
        }
    }

    /**
     * 合同是否有对应贷记单，如果有不能作废；
     *
     * @param dataEntity 单据对象
     * @param
     */
    public void Debcrednote(DynamicObject dataEntity, ExtendedDataEntity dataEntitie) {
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_sourcebillno", QCP.equals, billno);
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_debcrednote", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addErrorMessage(dataEntitie, "当前单据【" + billno + "】已存在贷记单【" + s + "】，禁止作废！");
        }
    }

    /**
     * 合同是否有对应的款项认领，如果有不能作废；
     *
     * @param dataEntity 单据对象
     * @param
     */
    public void MoneyClaim(DynamicObject dataEntity, ExtendedDataEntity dataEntitie) {
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_receiptdetail.yem_claimdetail.yem_salcontractno.billno", QCP.equals, billno);
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_gathering", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addErrorMessage(dataEntitie, "当前单据【" + billno + "】已存在认款【" + s + "】，禁止作废！");
        }
    }

    /**
     * 合同是否有对应的客商额度申请的额度占用并且未释放额度，如果有不能作废；
     *
     * @param dataEntity 单据对象
     * @param
     */
    public void Merchant(DynamicObject dataEntity, ExtendedDataEntity dataEntitie) {
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_im_entryentity_m.yem_cn.billno", QCP.equals, billno);
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_im_partsmerchant", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addErrorMessage(dataEntitie, "当前单据【" + billno + "】已存在客商额度申请【" + s + "】，禁止作废！");
        }
    }

    /**
     *  合同是否在礼品资金池有对应的礼品来源，如果有弹框点击确定，确定后删除对于礼品资金池对于合同号的礼品来源分录及其子分录；（参考反审核的逻辑）
     * @param dataEntity 对象
     * @param dataEntitie 数据实体
     */
    public void Giftamount(DynamicObject dataEntity, ExtendedDataEntity dataEntitie){
        List list = new ArrayList<>();
        String billno = dataEntity.getString("billno");
        Object pkValue = dataEntity.getPkValue();
        QFilter qFilter = new QFilter("yem_im_giftamount_o.yem_origintype", QCP.equals, "A");
        qFilter.and(new QFilter("yem_im_giftamount_o.yem_originno", QCP.equals, billno));
        DynamicObjectCollection yem_shippingdetails = QueryServiceHelper.query("yem_im_giftamount", "id,billno", new QFilter[]{qFilter}, null);
        if (yem_shippingdetails.size() > 0) {
            for (DynamicObject shippingdetails : yem_shippingdetails) {
                String shippbillno = shippingdetails.getString("billno");
                list.add(shippbillno);
            }
        }
        if (list.size() > 0) {
            String s = DebCredUtils.dedupAndJoin(list, ";");
            this.addWarningMessage(dataEntitie, "当前单据【" + billno + "】已存在礼品资金池【" + s + "】，是否作废！");
        }
    }
}
