package com.yem.wm.syn.eas.submit.op;

import com.yem.wm.syn.eas.submit.utils.FilterUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.entity.validate.ValidationErrorInfo;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;

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

/**
 * @author 涛
 * @date 2024/1/23
 * 销售出库-服务插件-提交同步EAS时校验 客户供应商物料
 */
public class ESDelivernoticePlugIn extends AbstractOperationServicePlugIn {
    private final static Log log = LogFactory.getLog(ESDelivernoticePlugIn.class);
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_companyorg");
        fieldKeys.add("yem_materiel");
        fieldKeys.add("yem_customer1");
        fieldKeys.add("yem_es_materialinfo");
    }

    @Override

    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        List<ExtendedDataEntity> selectedRows = e.getSelectedRows();
        ArrayList<ExtendedDataEntity> ArrayList = new ArrayList<>();
        for (ExtendedDataEntity selectedRow: selectedRows) {
            DynamicObject dataEntity = selectedRow.getDataEntity();
            Object pk = dataEntity.getPkValue();


            //财务组织
            DynamicObject yemCompanyorg = dataEntity.getDynamicObject("yem_companyorg");
            String orgname = yemCompanyorg.getString("name");
            //单据编码
            String billno = dataEntity.getString("billno");
            ArrayList<String> list = new ArrayList<>();
            //客户
            DynamicObject customer = dataEntity.getDynamicObject("yem_customer1");
            Boolean customers = FilterUtils.setCustomer(yemCompanyorg, customer);
            StringBuilder msg = new StringBuilder();
            if (customers){
                String customerNum = customer.getString("number");
                msg.append("财务组织"+orgname+"客户编码为:"+customerNum+"不存在,请在EAS核准!\n");
                list.add(customerNum);
            }
            //物料
            DynamicObjectCollection entry = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject entryEntity:entry) {
                DynamicObject material = entryEntity.getDynamicObject("yem_materiel");
                Boolean materials = FilterUtils.setMaterial(yemCompanyorg, material);
                if (materials){
                    String materialNum = material.getString("number");
                    msg.append("财务组织"+orgname+"物料编码为:"+materialNum+"不存在,请在EAS核准!\n");
                    list.add(materialNum);
                }
            }

            if (list.size()==0){
                ArrayList.add(selectedRow);
            }else{
                this.operationResult.addErrorInfo(new ValidationErrorInfo("",pk, 0,0,"err","提交失败","单据编码:"+billno+"提交失败,"+msg, ErrorLevel.Error));
            }
        }

        e.getSelectedRows().clear();
        if (ArrayList.size()==0){
            e.setCancel(true);
        }else {
            e.getSelectedRows().addAll(ArrayList);
        }

    }
}
