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

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.ILocaleString;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import yem.base.common.utils.DynamicObjectUtil;
import yem.base.common.utils.YEM;

import java.sql.Array;
import java.util.*;

/**
 * @Description 客商准入单操作：生成客户供应商仓库
 * @Author: LiuHB
 * @CreateTime: 2025-02-14  09:33
 */
public class CreatMerchantRepositorOpPlugin extends AbstractOperationServicePlugIn {

    /**
     * @Description: 自定义校验器
     * @function 1.【客户分类】【供应商分类】为空且【客户】【供应商】不为空
     *           2.【客户分类】为空且 客户不为空
     *           3.【供应商分类】为空且 供应商不为空
     * @purposes 重写系统validate方法在生成基础资料时增加控制
     * @param e
     * @return void
     */

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                String operateKey = this.getOperateKey();
                ExtendedDataEntity[] dataEntities = this.getDataEntities();
                for (ExtendedDataEntity extendedDataEntity : dataEntities) {
                    DynamicObject merchantApply = extendedDataEntity.getDataEntity();
                    String merchantstname = merchantApply.getLocaleString("yem_merchantstname").getLocaleValue();
                    //客商准入单id
                    Long merchantApplyid = (Long) merchantApply.get("id");
                    //客户基础资料
                    DynamicObject customer = BusinessDataServiceHelper.loadSingle("bd_customer", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals, merchantApplyid)});
                    //供应商基础资料
                    DynamicObject supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals, merchantApplyid)});
                    //客户分类
                    DynamicObject yemClassifycustomer = merchantApply.getDynamicObject("yem_classifycustomer");
                    //供应商分类
                    DynamicObject yemClassifysupplier = merchantApply.getDynamicObject("yem_classifysupplier");
                    switch (operateKey) {
                        case "merchantrepository":
                            if((null==yemClassifycustomer&&null==yemClassifysupplier)&&(null!=customer&&null!=supplier)) {
                                this.addWarningMessage(extendedDataEntity,"由于供应商分类和客户分类为空，该操作会删除"+merchantstname+"生成的客户和供应商,请确认是否删除。");
                            }else if(null==yemClassifycustomer && null!= customer){
                                this.addWarningMessage(extendedDataEntity,"由于客户分类为空，该操作会删除"+merchantstname+"生成的客户,请确认是否删除。");
                            }else if(null==yemClassifysupplier && null!=supplier){
                                this.addWarningMessage(extendedDataEntity,"由于供应商分类为空，该操作会删除"+merchantstname+"生成的供应商,请确认是否删除。");
                            }
                        break;
                    }
                }
            }
        });
    }

    /**
     * @Description: 对操作结果进行整理，或者执行其他无需事务保护的逻辑。
     * @function 客户、供应商、仓库的 生成更新逻辑
     * @purposes 操作执行完毕，事务提交之后触发
     */

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        processCustomerAccessEntities(e,e.getDataEntities());
    }

    /**
     * @Description: 客户、供应商、仓库的 生成更新主方法
     * @param Customeraccess   客商准入选中数据集
     * @return void
     */

    private void processCustomerAccessEntities(BeforeOperationArgs e,DynamicObject[] Customeraccess) {
        for (DynamicObject merchantApply : Customeraccess) {
            //客商准入单id
            Long merchantApplyid = (Long) merchantApply.get("id");
            //客户基础资料
            DynamicObject customer = BusinessDataServiceHelper.loadSingle("bd_customer", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals, merchantApplyid)});
            //供应商基础资料
            DynamicObject supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals, merchantApplyid)});
            //仓库基础资料
            String selectfields = DynamicObjectUtil.getSelectfields("bd_warehouse", false);
            DynamicObject[] warehouse = BusinessDataServiceHelper.load("bd_warehouse",selectfields,new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals, merchantApplyid)});
            //当前登陆人id
            long userId = UserServiceHelper.getCurrentUserId();
            //客户状态(正常)
            DynamicObject customerstatus = BusinessDataServiceHelper.loadSingle("bd_customerstatus", new QFilter[]{new QFilter("number", QCP.equals, "ZSKH")});//查询客户状态
            //供应商状态(正常)
            DynamicObject supplierstatus = BusinessDataServiceHelper.loadSingle("bd_supplierstatus", new QFilter[]{new QFilter("number", QCP.equals, "ZCGYS")});//查询客户状态
            //联系人分录
            DynamicObjectCollection linkmaninfo = merchantApply.getDynamicObjectCollection("yem_linkmaninfo_e");
            //银行信息分录
            DynamicObjectCollection bankinfo = merchantApply.getDynamicObjectCollection("yem_bankinfo_e");
            //仓库信息分录
            DynamicObjectCollection warehouseinfo = merchantApply.getDynamicObjectCollection("yem_warehouseinfo_e");
            //联系人分录默认行信息
            Map<Object, Object> linkmanMap = getDefaultLinkManentry(linkmaninfo);
            //联系人分录信息(以id为主键)
            Map<Object, Map<String, Object>> linkmanentryMap = getLinkManentry(linkmaninfo, false);
            //获取银行信息默认行币别
            DynamicObject bankCurrency = getBankCurrency(bankinfo);
            //银行分录信息(以id为主键)
            Map<Object, Map<String, Object>> bankentryMap = getLinkManentry(bankinfo, false);//银行分录信息(以行id为主键)
            //客户分类
            DynamicObject yemClassifycustomer = merchantApply.getDynamicObject("yem_classifycustomer");
            //供应商分类
            DynamicObject yemClassifysupplier = merchantApply.getDynamicObject("yem_classifysupplier");
            //删除地址基础资料和客户，供应商的关联信息
            delRelatedInfo(merchantApply,yemClassifycustomer,yemClassifysupplier,customer,supplier);
            //生成/更新客户
            if(null!=yemClassifycustomer){
                creatOrUpdateCustomer(customer, merchantApply,customerstatus, userId, linkmanMap, linkmanentryMap, bankCurrency, bankentryMap);
            } else if(null!=customer){
                //客户分类为空删除已生成的客户
                OperationServiceHelper.executeOperate("delete", "bd_customer", new DynamicObject[]{customer}, OperateOption.create());
            }
            //生成/更新供应商
            if(null!=yemClassifysupplier){
                creatOrUpdateSupplier(supplier, merchantApply,supplierstatus,userId,linkmanMap,linkmanentryMap,bankCurrency,bankentryMap);
            }  else if(null!=supplier){
                //供应商分类为空删除已生成的客户
                OperationServiceHelper.executeOperate("delete", "bd_supplier", new DynamicObject[]{supplier}, OperateOption.create());
            }
            //生成/更新仓库
            creatOrUpdateWarehouse(warehouse, merchantApply,warehouseinfo);
        }
    }
/**
 * @Description: 生成/更新仓库
 * @param merchantApply  客商准入单
 * @param warehouse      客商准入对应的仓库集合
 * @param warehouseinfo  客商准入仓库信息分录
 * @return void
 */

    private void creatOrUpdateWarehouse(DynamicObject[] warehouse, DynamicObject merchantApply, DynamicObjectCollection warehouseinfo) {

            if(null!=warehouse){
                //更新
                updateWarehouse(merchantApply,warehouse,warehouseinfo);

            }else {
                //新增
                creatwarehouse( merchantApply, warehouseinfo);

            }

    }



    /**
     * @Description: 生成/更新供应商基础资料
     * @function 1.存在客商准入id与客商准入单相同的客户则更新不存在新增
     * @param supplier  供应商基础资料
     * @param merchantApply  客商准入单
     * @param supplierstatus  供应商状态
     * @param userId          当前登录人id
     * @param linkmanMap      联系人分录默认行信息
     * @param linkmanentryMap 联系人分录信息(以准入单行id为key)
     * @param bankCurrency    银行信息默认行币别
     * @param bankentryMap    银行分录信息(以准入单行id为key)
     * @return void
     */

    private void creatOrUpdateSupplier(DynamicObject supplier, DynamicObject merchantApply,DynamicObject supplierstatus,long userId,Map<Object, Object> linkmanMap, Map<Object,
            Map<String, Object>> linkmanentryMap,DynamicObject bankCurrency, Map<Object, Map<String, Object>> bankentryMap) {
        if(null!=supplier){
            //更新供应商
            updatesupplier(supplier,linkmanentryMap,merchantApply,bankCurrency,supplierstatus,linkmanMap,bankentryMap);
        }
        else{
            //新建供应商
            createsupplier(userId,merchantApply,bankCurrency,supplierstatus,linkmanMap,linkmanentryMap,bankentryMap);
        }
    }




    /**
     * @Description: 生成/更新客户基础资料
     * @function 1.存在客商准入id与客商准入单相同的客户则更新不存在新增
     * @param customer        客户基础资料
     * @param merchantApply   客商准入单
     * @param customerstatus  客户状态
     * @param userId          当前登录人id
     * @param linkmanMap      联系人分录默认行信息
     * @param linkmanentryMap 联系人分录信息(以准入单行id为key)
     * @param bankCurrency    银行信息默认行币别
     * @param bankentryMap    银行分录信息(以准入单行id为key)
     * @return void
     */

    private void creatOrUpdateCustomer(DynamicObject customer, DynamicObject merchantApply, DynamicObject customerstatus, long userId, Map<Object, Object> linkmanMap, Map<Object,
            Map<String, Object>> linkmanentryMap, DynamicObject bankCurrency, Map<Object, Map<String, Object>> bankentryMap) {
        if (null != customer) {
            //更新客户
            updateCustomer(customer,linkmanentryMap,merchantApply,bankCurrency,customerstatus,linkmanMap,bankentryMap);
        }
        else {
            //新建客户
            createCustomer(userId,merchantApply,bankCurrency,customerstatus,linkmanMap,linkmanentryMap,bankentryMap);
        }
    }
    /**
     * @Description: 新建仓库
     * @param merchantApply  客商准入单
     * @param warehouseinfo  客商准入联系人分类
     * @return void
     */
    private void creatwarehouse(DynamicObject merchantApply, DynamicObjectCollection warehouseinfo) {
        List<DynamicObject> warehouseentry = new ArrayList<>();
        for (DynamicObject warehouseinforow : warehouseinfo) {
            DynamicObject bdwarehouse = BusinessDataServiceHelper.newDynamicObject("bd_warehouse");
            bdwarehouse.set("yem_customeraccessid",merchantApply.get("id"));
            bdwarehouse.set("createorg", merchantApply.get("org"));
            bdwarehouse.set("ctrlstrategy","5");
            bdwarehouse.set("enable","1");
            bdwarehouse.set("status","A");
            bdwarehouse.set("name",warehouseinforow.get("yem_warehousename"));
            bdwarehouse.set("group",warehouseinforow.get("yem_warehousegroup"));
            bdwarehouse.set("useorg",merchantApply.get("org"));
            if(!YEM.isEmpty(warehouseinforow.get("yem_citycounty"))){
                bdwarehouse.set("address",warehouseinforow.get("yem_citycounty"));
            }else{
                bdwarehouse.set("address",null);
            }
            bdwarehouse.set("detailaddress",warehouseinforow.get("yem_address"));
            bdwarehouse.set("principal",warehouseinforow.get("yem_contact"));
            bdwarehouse.set("telephone",warehouseinforow.get("yem_warehousetel"));
            warehouseentry.add(bdwarehouse);
            OperationServiceHelper.executeOperate("save", "bd_warehouse", new DynamicObject[]{bdwarehouse}, OperateOption.create());
            warehouseinforow.set("yem_warehouse",bdwarehouse);
        }
        SaveServiceHelper.save(new DynamicObject[]{merchantApply});
    }



    /**
     * @Description: 删除地址基础资料和客户，供应商的关联信息
     * @function 根据客户，供应商分类删除对应基础资料的地址关联信息
     * @purposes 方便之后逻辑删除地址数据
     * @param merchantApply  客商准入单
     * @param yemClassifycustomer  客户分类
     * @param yemClassifysupplier  供应商分类
     * @param customer  客户
     * @param supplier  供应商
     * @return void
     */

    private void delRelatedInfo(DynamicObject merchantApply, DynamicObject yemClassifycustomer, DynamicObject yemClassifysupplier, DynamicObject customer, DynamicObject supplier) {
        //客商准入联系人分录
        List<DynamicObject> bd_address  = new ArrayList<>();
        DynamicObjectCollection yem_linkmaninfo_e = merchantApply.getDynamicObjectCollection("yem_linkmaninfo_e");
        for (DynamicObject yem_linkmaninf : yem_linkmaninfo_e) {
            String addressnumber = (String) yem_linkmaninf.get("yem_addressnumber");
            DynamicObject address = BusinessDataServiceHelper.loadSingle("bd_address", new QFilter[]{new QFilter("number", QCP.equals, addressnumber)});
            if((null==yemClassifycustomer&&null==yemClassifysupplier)&&(null!=customer&&null!=supplier)) {
                address.set("supplierid", null);
                address.set("customerid", null);
                address.set("iscustomeradd", null);
                address.set("issupplieradd", null);
                address.set("supplier", null);
                address.set("customer", null);
                bd_address.add(address);
            }else if(null==yemClassifycustomer && null!= customer){
                address.set("iscustomeradd", null);
                address.set("customer", null);
                address.set("customerid", null);
                bd_address.add(address);
            }else if(null==yemClassifysupplier && null!=supplier){
                address.set("supplier", null);
                address.set("issupplieradd", null);
                address.set("supplierid", null);
                bd_address.add(address);
            }
        }
        SaveServiceHelper.save(bd_address.toArray(new DynamicObject[0]));
    }



    /**
     * @Description: 新增供应商基础资料
     * @param userId  当前登录人id
     * @param merchantApply  客商准入单
     * @param bankCurrency  银行信息默认行币别
     * @param supplierstatus  供应商状态
     * @param linkmanMap  联系人分录默认行信息
     * @param linkmanentryMap  联系人分录信息(以准入单行id为key)
     * @param bankentryMap   银行分录信息(以准入单行id为key)
     * @return void
     */
    private void createsupplier(long userId, DynamicObject merchantApply, DynamicObject bankCurrency, DynamicObject supplierstatus, Map<Object, Object> linkmanMap,
                                Map<Object, Map<String, Object>> linkmanentryMap, Map<Object, Map<String, Object>> bankentryMap) {
        //获取联系人默认行信息、银行信息分录默认行信息及单据体数据，构建客户基础资料表头
        DynamicObject bdSupplier = BusinessDataServiceHelper.newDynamicObject("bd_supplier");//新建客户基础资料
        bdSupplier.set("status", "A");
        bdSupplier.set("enable", "1");
        bdSupplier.set("creator", userId);
        bdSupplier.set("yem_customeraccessid", merchantApply.get("id"));
        bdSupplier.set("ctrlstrategy","5");
        bdSupplier.set("createorg", merchantApply.get("org"));
        addCustomerInfo(bdSupplier,merchantApply,bankCurrency,supplierstatus,linkmanMap,false);
        OperationServiceHelper.executeOperate("save", "bd_customer", new DynamicObject[]{bdSupplier}, OperateOption.create());
        //从客商准入联系人分录获取地址信息新建地址基础资料
        for (Map.Entry<Object, Map<String, Object>> outerEntry : linkmanentryMap.entrySet()) {
            Map<String, Object> innerMap = outerEntry.getValue();
            String yemAddressnumber = (String) innerMap.get("yem_addressnumber");
            DynamicObject bdaddress = null;
            if (!YEM.isEmpty(yemAddressnumber)) {
                bdaddress = creatAddressr(bdSupplier,innerMap,yemAddressnumber,false);
            }
            //新建地址信息后从联系人分录获取数据赋值
            DynamicObjectCollection entryAddress = bdSupplier.getDynamicObjectCollection("entry_linkman");
            DynamicObject creatlinkman  = entryAddress.addNew();
            //新增客户分录行数据
            creatCustomerEntry(innerMap,creatlinkman,bdaddress);
        }
        //从客商准入联系人分录获取银行信息新建银行信息分录
        creatBankInfo(bankentryMap,bdSupplier);

        SaveServiceHelper.save(new DynamicObject[]{bdSupplier});
    }


    /**
     * @Description: 更新仓库
     * @function 1.删除客商准入单不存在但是在仓库基础资料中存在的仓库
     *           2.判断客户的【仓库主数据】是否为空
     *              为空新增仓库
     *              不为空获取仓库实体更新数据
     * @param merchantApply  客商准入单
     * @param warehouse      客商准入对应的仓库集合
     * @param warehouseinfo  客商准入仓库信息分录
     * @return void
     */

    private void updateWarehouse(DynamicObject merchantApply,DynamicObject[] warehouse, DynamicObjectCollection warehouseinfo) {
        deleteWarehouse(warehouse,warehouseinfo);
        List<DynamicObject> warehouseentry = new ArrayList<>();
        for (DynamicObject warehouserowid : warehouseinfo) {
            DynamicObject yemWarehouse = warehouserowid.getDynamicObject("yem_warehouse");
            if (null != yemWarehouse) {
                yemWarehouse.set("name", warehouserowid.get("yem_warehousename"));
                yemWarehouse.set("group", warehouserowid.get("yem_warehousegroup"));
                if (!YEM.isEmpty(warehouserowid.get("yem_citycounty"))) {
                    yemWarehouse.set("address", warehouserowid.get("yem_citycounty"));
                } else {
                    yemWarehouse.set("address", null);
                }
                yemWarehouse.set("detailaddress", warehouserowid.get("yem_address"));
                yemWarehouse.set("principal", warehouserowid.get("yem_contact"));
                yemWarehouse.set("telephone", warehouserowid.get("yem_warehousetel"));
                warehouseentry.add(yemWarehouse);
            }else {
                //新增仓库
                creatwarehouse(merchantApply,warehouseinfo);
            }

        }
        SaveServiceHelper.save(new DynamicObject[]{merchantApply});
        SaveServiceHelper.save(warehouseentry.toArray(new DynamicObject[0]));
    }

    /**
     * @Description: 删除仓库
     * @function 功能
     * @purposes 用途
     * @param warehouse  客商准入生成仓库集合
     * @param warehouseinfo   客商准入仓库分录
     * @return void
     */
    private void deleteWarehouse(DynamicObject[] warehouse, DynamicObjectCollection warehouseinfo) {
        Map<Long,DynamicObject> warehouseMap = new HashMap<>();//仓库id集合
        List<Long> merchantWarehouserowid = new ArrayList();//客商准入仓库id集合
        for (DynamicObject depot : warehouse) {
            warehouseMap.put((Long)depot.get("id"),depot);
        }
        for (DynamicObject warehouserowid : warehouseinfo) {
            DynamicObject yemWarehouse = warehouserowid.getDynamicObject("yem_warehouse");
            long id = null != yemWarehouse ?  yemWarehouse.getLong("id") : 0L;
            merchantWarehouserowid.add(id);
        }
        //warehouselist存在 bdaddressnumberList 不存在的值
        List<DynamicObject> result = new ArrayList<>();//
        for (Long number : warehouseMap.keySet()) {
            if (!merchantWarehouserowid.contains(number)) { // 检查是否不存在
                result.add(warehouseMap.get(number));
            }
        }
        OperationServiceHelper.executeOperate("delete", "bd_warehouse", result.toArray(new DynamicObject[0]), OperateOption.create());
    }




    /**
     * @Description: 新增客户基础资料
     * @param userId  当前登录人id
     * @param merchantApply  客商准入单
     * @param bankCurrency  银行信息默认行币别
     * @param customerstatus  客户状态
     * @param linkmanMap  联系人分录默认行信息
     * @param linkmanentryMap  联系人分录信息(以准入单行id为key)
     * @param bankentryMap   银行分录信息(以准入单行id为key)
     * @return void
     */
    private void createCustomer(long userId,DynamicObject merchantApply,DynamicObject bankCurrency,DynamicObject customerstatus,Map<Object, Object> linkmanMap, Map<Object,
            Map<String, Object>> linkmanentryMap,Map<Object, Map<String, Object>> bankentryMap ) {
        /**
         *特殊字段
         * 注意处理付款币别（取银行分录默认行）√
         * 客户状态需查询客户状态(bd_customerstatus)基础资料，赋默认值“正常” √
         * 工商信息中客户信息字段取默认行 √
         */
        //获取联系人默认行信息、银行信息分录默认行信息及单据体数据，构建客户基础资料表头
        DynamicObject bdCustomer = BusinessDataServiceHelper.newDynamicObject("bd_customer");//新建客户基础资料
        bdCustomer.set("status", "A");
        bdCustomer.set("enable", "1");
        bdCustomer.set("creator", userId);
        bdCustomer.set("yem_customeraccessid", merchantApply.get("id"));
        bdCustomer.set("ctrlstrategy","5");
        bdCustomer.set("createorg", merchantApply.get("org"));
        addCustomerInfo(bdCustomer,merchantApply,bankCurrency,customerstatus,linkmanMap,true);
        OperationResult bd_customer = OperationServiceHelper.executeOperate("save", "bd_customer", new DynamicObject[]{bdCustomer}, OperateOption.create());

        //从客商准入联系人分录获取地址信息新建地址基础资料
        for (Map.Entry<Object, Map<String, Object>> outerEntry : linkmanentryMap.entrySet()) {
            Map<String, Object> innerMap = outerEntry.getValue();
            String yemAddressnumber = (String) innerMap.get("yem_addressnumber");
            DynamicObject bdaddress = null;
            if (!YEM.isEmpty(yemAddressnumber)) {
                bdaddress = creatAddressr(bdCustomer,innerMap,yemAddressnumber,true);
            }
            //新建地址信息后从联系人分录获取数据赋值
            DynamicObjectCollection entryAddress = bdCustomer.getDynamicObjectCollection("entry_linkman");
            DynamicObject creatlinkman  = entryAddress.addNew();
            //新增客户分录行数据
            creatCustomerEntry(innerMap,creatlinkman,bdaddress);
        }
        //从客商准入联系人分录获取银行信息新建银行信息分录
        creatBankInfo(bankentryMap,bdCustomer);

        SaveServiceHelper.save(new DynamicObject[]{bdCustomer});
    }


    /**
     * @Description: 更新供应商数据
     * @function 同客户更新方案
     * @purposes 客商准入单做修改时同步更新供应商基础班资料数据
     * @param supplier 供应商
     * @param linkmanentryMap 联系人分录信息(以准入单行id为key)
     * @param merchantApply 客商准入单
     * @param bankCurrency 银行信息默认行币别
     * @param supplierstatus  供应商状态
     * @param linkmanMap 联系人分录默认行信息
     * @param bankentryMap 银行分录信息(以准入单行id为key)
     * @return void
     */

    private void updatesupplier(DynamicObject supplier, Map<Object, Map<String, Object>> linkmanentryMap, DynamicObject merchantApply,
                                DynamicObject bankCurrency, DynamicObject supplierstatus, Map<Object, Object> linkmanMap, Map<Object, Map<String, Object>> bankentryMap) {
        DynamicObjectCollection entryLinkmans = supplier.getDynamicObjectCollection("entry_linkman");
        Map<Object, Map<String, Object>> linkManentry = getLinkManentry(entryLinkmans, true);
        DynamicObjectCollection entryBanks = supplier.getDynamicObjectCollection("entry_bank");
        deleteEntryLinkmans(linkmanentryMap, entryLinkmans, linkManentry);
        entryBanks.clear();
        SaveServiceHelper.save(new DynamicObject[]{supplier});

        //重新获取供应商基础资料及分录
        DynamicObject suppliers = BusinessDataServiceHelper.loadSingle("bd_supplier", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals,  (Long) merchantApply.get("id"))});
        DynamicObjectCollection entryLinkmanentrys = suppliers.getDynamicObjectCollection("entry_linkman");
        //获取供应商分录数据集合（以来源单据行id为key）
        Map<Object, Map<String, Object>> entryLinkmaneninfo = getLinkManentry(entryLinkmanentrys, true);
        Set<Object> entrylistid = entryLinkmaneninfo.keySet();//客户分录来源单据id
        //更新供应商户单据头数据
        addCustomerInfo(suppliers,merchantApply,bankCurrency,supplierstatus,linkmanMap,false);

        //更新供应商联系人分录数据
        for (Map.Entry<Object, Map<String, Object>> outerEntry : linkmanentryMap.entrySet()) {
            Map<String, Object> innerMap = outerEntry.getValue();//客商准入联系人分录行数据
            Long customeraccessid = (Long) innerMap.get("id"); //客商准入联系人分录id
            String yemAddressnumber = (String) innerMap.get("yem_addressnumber");//客商准入联系人分录地址信息编码
            List<String> deladdress = new ArrayList<>(); //需要删除的地址信息编号
            DynamicObject bd_address = null;

            if (entryLinkmanentrys.size() < linkmanentryMap.size()) {
                DynamicObject bdaddress = null;
                if (!YEM.isEmpty(yemAddressnumber) && !entrylistid.contains(customeraccessid)) {
                    DynamicObject creatlinkman = entryLinkmanentrys.addNew();
                    bdaddress = creatAddressr(suppliers,innerMap,yemAddressnumber,false);
                    creatCustomerEntry(innerMap,creatlinkman,bdaddress);
                }
            }
            else {
                for (DynamicObject entryLinkmanentry : entryLinkmanentrys) {
                    Long linkmaninfoid = (Long) entryLinkmanentry.get("yem_linkmaninfoid");//客户联系人分录来源单据分录id
                    String number = "";
                    DynamicObject bdaddress = null;
                    if(customeraccessid.equals(linkmaninfoid)){
                        //匹配后优先更新基础数据
                        entryLinkmanentry.set("contactperson",innerMap.get("yem_name"));
                        entryLinkmanentry.set("contactpersonpost",innerMap.get("yem_office"));
                        DynamicObject departments = (DynamicObject) innerMap.get("yem_departments");
                        ILocaleString depname = null != departments ? departments.getLocaleString("name") : null;
                        entryLinkmanentry.set("dept",depname);
                        entryLinkmanentry.set("phone",innerMap.get("yem_phone"));
                        entryLinkmanentry.set("fax",innerMap.get("yem_fax"));
                        entryLinkmanentry.set("email",innerMap.get("yem_email"));
                        entryLinkmanentry.set("isdefault_linkman",innerMap.get("yem_isdefault_linkman"));
                        entryLinkmanentry.set("yem_linkmaninfoid",innerMap.get("id"));
                        bdaddress = entryLinkmanentry.getDynamicObject("associatedaddress");
                        number = null != bdaddress ? (String) bdaddress.get("number") : ""; //客户联系人分录关联地址编码
                        if(null!=bdaddress) {
                            bdaddress = BusinessDataServiceHelper.loadSingleFromCache(bdaddress.getPkValue(), "bd_address");
                        }
                        if(!YEM.isEmpty(number)) {
                            if (yemAddressnumber.equals(number)) {
                                bdaddress.set("name", innerMap.get("yem_addressname"));
                                bdaddress.set("hihn_tradeterms", innerMap.get("yem_tradeterms"));
                                bdaddress.set("hihn_clearanceco", innerMap.get("yem_clearanceco"));
                                bdaddress.set("hihn_forwarderco", innerMap.get("yem_forwarderco"));
                                bdaddress.set("customeraddrsspurpose", innerMap.get("yem_customeraddrsspur"));
                                bdaddress.set("admindivision", innerMap.get("yem_admindivision"));
                                bdaddress.set("detailaddress", innerMap.get("yem_detailaddress"));
                                bdaddress.set("linkman", innerMap.get("yem_linkman"));
                                bdaddress.set("yem_suppliervx", innerMap.get("yem_wechat"));
                                bdaddress.set("yem_supplierqq", innerMap.get("yem_qq"));
                                bdaddress.set("phone", innerMap.get("yem_phone"));
                                bdaddress.set("addemail", innerMap.get("yem_email"));
                                bdaddress.set("zipcode", innerMap.get("yem_zipcode"));
                                bdaddress.set("timezone", innerMap.get("yem_timezone"));
                                bdaddress.set("default", innerMap.get("yem_addressdefault"));
                                bdaddress.set("admindivisiondata", innerMap.get("yem_admindivision"));
                                SaveServiceHelper.save(new DynamicObject[]{bdaddress});
                            }else{
                                deladdress.add((String)bdaddress.get("number"));
                                bd_address = creatAddressr(suppliers,innerMap,yemAddressnumber,false);
                                entryLinkmanentry.set("associatedaddress",bd_address);
                            }
                        }
                        if(!YEM.isEmpty(yemAddressnumber)&&YEM.isEmpty(number)){
                            bd_address = creatAddressr(suppliers,innerMap,yemAddressnumber,false);
                            entryLinkmanentry.set("associatedaddress", bd_address);
                        }
                    }
                    if(YEM.isEmpty(yemAddressnumber)&&!YEM.isEmpty(number)){
                        if(null!=bdaddress) {
                            deladdress.add((String)bdaddress.get("number"));
                            entryLinkmanentry.set("associatedaddress", null);
                        }
                    }
                }
            }
            DeleteServiceHelper.delete("bd_address",new QFilter[]{new QFilter("number",QCP.in,deladdress.toArray())});
        }
        //从客商准入联系人分录获取银行信息新建银行信息分录
        creatBankInfo(bankentryMap,suppliers);
        SaveServiceHelper.save(new DynamicObject[]{suppliers});
    }

    /**
     * @Description: 更新客户数据
     * @function
     *              客商准入-客户：客商准入id  联系人分录： 客商准入联系人分录id - 客户联系人分录来源单据分录id
     *               1.根据【联系人分录id】获取到【客商准入不存在的分录id】但在【客户分录存在的分录id】的分录行DynamicObject并删除，从获取到的需要删除的DynamicObject中获取关联地值基础资料的DynamicObject，判断不为空删除
     *               2.清空银行信息分录
     *               3，更新客户单据头数据
     *               4.遍历获取到的客商准入联系人分录数据更新联系人分录基础数据，获取当前行的id以及地址信息编码
     *               判断【客户联系人分录】的size()是否大于【客商准入联系人分录】的size()，
     *                  小于则判断 （【客商准入联系人分录地址信息编码】且【客户分录来源单据id】不包含当前行的【客商准入联系人分录id】）则新增   [处理客商准入分录行大于客户分录行数，进行新增地址信息和联系人分录]
     *                  大于遍历客户分录判断【客商准入id】是否匹配，
     *                      匹配：判断客户当前行【关联地址编码】不为空后判断客商准入的地址编码和客户【关联地址编码】是否相同，
     *                           相同： 更新【地址基础资料】数据。
     *                          不相同：记录【关联地址】到  List<String> deladdress（需要删除的地址基础资料集合）后根据客商准入分录信息新增地址基础资料 [处理客商准入分录修改地址信息后同步修改客户关联地址]
     *                      不匹配：判断【客商准入的地址信息编码】不为空且【客户基础资料关联地址】为空，新增地址信息 [ 处理第一次生成客户未录入地址信息，之后补充]
     *                      判断【客商准入的地址编码为空】且【客户关联地址编码不为空】 将记录客户该行的关联地址到  List<String> deladdress（需要删除的地址基础资料集合） [客商准入已清空地址信息但是客户仍存在]
     *                   遍历完成后删除list中的地址基础资料
     *               5.新增银行信息分录并保存客户基础资料
     *
     * @purposes 客商准入单做修改时同步更新客户基础班资料数据
     * @param customer 客户实体
     * @param linkmanentryMap 联系人分录信息(以准入单行id为key)
     * @param merchantApply 客商准入单
     * @param bankCurrency 银行信息默认行币别
     * @param customerstatus 客户状态
     * @param linkmanMap 联系人分录默认行信息
     * @param bankentryMap 银行分录信息(以准入单行id为key)
     * @return void
     */
    private void updateCustomer(DynamicObject customer, Map<Object, Map<String, Object>> linkmanentryMap,DynamicObject merchantApply,
                                DynamicObject bankCurrency,DynamicObject customerstatus,Map<Object, Object> linkmanMap,Map<Object, Map<String, Object>> bankentryMap) {
        DynamicObjectCollection entryLinkmans = customer.getDynamicObjectCollection("entry_linkman");
        Map<Object, Map<String, Object>> linkManentry = getLinkManentry(entryLinkmans, true);
        DynamicObjectCollection entryBanks = customer.getDynamicObjectCollection("entry_bank");
        deleteEntryLinkmans(linkmanentryMap, entryLinkmans, linkManentry);
        entryBanks.clear();
        SaveServiceHelper.save(new DynamicObject[]{customer});

        //重新获取客户基础资料及分录
        DynamicObject customers = BusinessDataServiceHelper.loadSingle("bd_customer", new QFilter[]{new QFilter("yem_customeraccessid", QCP.equals,  (Long) merchantApply.get("id"))});
        DynamicObjectCollection entryLinkmanentrys = customers.getDynamicObjectCollection("entry_linkman");
        //获取客户分录数据集合（以来源单据行id为key）
        Map<Object, Map<String, Object>> entryLinkmaneninfo = getLinkManentry(entryLinkmanentrys, true);
        Set<Object> entrylistid = entryLinkmaneninfo.keySet();//客户分录来源单据id
        //更新客户单据头数据
        addCustomerInfo(customers,merchantApply,bankCurrency,customerstatus,linkmanMap,true);
        //更新客户联系人分录数据
        for (Map.Entry<Object, Map<String, Object>> outerEntry : linkmanentryMap.entrySet()) {
            Map<String, Object> innerMap = outerEntry.getValue();//客商准入联系人分录行数据
            Long customeraccessid = (Long) innerMap.get("id"); //客商准入联系人分录id
            String yemAddressnumber = (String) innerMap.get("yem_addressnumber");//客商准入联系人分录地址信息编码
            List<String> deladdress = new ArrayList<>(); //需要删除的地址信息编号
            DynamicObject bd_address = null;

            if (entryLinkmanentrys.size() < linkmanentryMap.size()) {
                DynamicObject bdaddress = null;
                if (!YEM.isEmpty(yemAddressnumber) && !entrylistid.contains(customeraccessid)) {
                    DynamicObject creatlinkman = entryLinkmanentrys.addNew();
                    bdaddress = creatAddressr(customers,innerMap,yemAddressnumber,true);
                    creatCustomerEntry(innerMap,creatlinkman,bdaddress);
                }
            }
            else {
                //遍历客户联系人分录
                for (DynamicObject entryLinkmanentry : entryLinkmanentrys) {
                    Long linkmaninfoid = (Long)entryLinkmanentry.get("yem_linkmaninfoid");//客户联系人分录来源单据分录id
                    String number = "";
                    DynamicObject bdaddress = null;
                    if(customeraccessid.equals(linkmaninfoid)){
                        //匹配后优先更新基础数据
                        entryLinkmanentry.set("contactperson",innerMap.get("yem_name"));
                        entryLinkmanentry.set("contactpersonpost",innerMap.get("yem_office"));
                        DynamicObject departments = (DynamicObject) innerMap.get("yem_departments");
                        ILocaleString depname = null != departments ? departments.getLocaleString("name") : null;
                        entryLinkmanentry.set("dept",depname);
                        entryLinkmanentry.set("phone",innerMap.get("yem_phone"));
                        entryLinkmanentry.set("fax",innerMap.get("yem_fax"));
                        entryLinkmanentry.set("email",innerMap.get("yem_email"));
                        entryLinkmanentry.set("isdefault_linkman",innerMap.get("yem_isdefault_linkman"));
                        entryLinkmanentry.set("yem_linkmaninfoid",innerMap.get("id"));

                        bdaddress = entryLinkmanentry.getDynamicObject("associatedaddress");
                        number = null != bdaddress ? (String) bdaddress.get("number") : ""; //客户联系人分录关联地址编码
                        if(null!=bdaddress) {
                            bdaddress = BusinessDataServiceHelper.loadSingleFromCache(bdaddress.getPkValue(), "bd_address");
                        }
                        if(!YEM.isEmpty(number)) {
                            if (yemAddressnumber.equals(number)) {
                                bdaddress.set("name", innerMap.get("yem_addressname"));
                                bdaddress.set("hihn_tradeterms", innerMap.get("yem_tradeterms"));
                                bdaddress.set("hihn_clearanceco", innerMap.get("yem_clearanceco"));
                                bdaddress.set("hihn_forwarderco", innerMap.get("yem_forwarderco"));
                                bdaddress.set("customeraddrsspurpose", innerMap.get("yem_customeraddrsspur"));
                                bdaddress.set("admindivision", innerMap.get("yem_admindivision"));
                                bdaddress.set("detailaddress", innerMap.get("yem_detailaddress"));
                                bdaddress.set("linkman", innerMap.get("yem_linkman"));
                                bdaddress.set("yem_suppliervx", innerMap.get("yem_wechat"));
                                bdaddress.set("yem_supplierqq", innerMap.get("yem_qq"));
                                bdaddress.set("phone", innerMap.get("yem_phone"));
                                bdaddress.set("addemail", innerMap.get("yem_email"));
                                bdaddress.set("zipcode", innerMap.get("yem_zipcode"));
                                bdaddress.set("timezone", innerMap.get("yem_timezone"));
                                bdaddress.set("default", innerMap.get("yem_addressdefault"));
                                bdaddress.set("admindivisiondata", innerMap.get("yem_admindivision"));
                                SaveServiceHelper.save(new DynamicObject[]{bdaddress});
                            }else{
                                deladdress.add((String)bdaddress.get("number"));
                                bd_address = creatAddressr(customers,innerMap,yemAddressnumber,true);
                                entryLinkmanentry.set("associatedaddress",bd_address);
                            }
                        }
                        if(!YEM.isEmpty(yemAddressnumber)&&YEM.isEmpty(number)){
                            bd_address = creatAddressr(customers,innerMap,yemAddressnumber,true);
                            entryLinkmanentry.set("associatedaddress", bd_address);
                        }
                    }
                    if(YEM.isEmpty(yemAddressnumber)&&!YEM.isEmpty(number)){
                        if(null!=bdaddress) {
                            deladdress.add((String)bdaddress.get("number"));
                            entryLinkmanentry.set("associatedaddress", null);
                        }
                    }
                }
            }
            DeleteServiceHelper.delete("bd_address",new QFilter[]{new QFilter("number",QCP.in,deladdress.toArray())});
        }
        //从客商准入联系人分录获取银行信息新建银行信息分录
        creatBankInfo(bankentryMap,customers);
        SaveServiceHelper.save(new DynamicObject[]{customers});

    }


    /**
     * @Description: 新建联系人单据体部分数据
     * @return void
     */
   private void addCustomerInfo(DynamicObject bdCustomer,DynamicObject merchantApply,DynamicObject bankCurrency,DynamicObject customerstatus,Map<Object, Object> linkmanMap,Boolean info){
       Boolean flag = (Boolean)merchantApply.get("yem_internal");
       if(flag){
           bdCustomer.set("internal_company", merchantApply.get("org"));
       }
       bdCustomer.set("name", merchantApply.get("yem_merchantstname"));
       bdCustomer.set("group", merchantApply.get("yem_classifycustomer"));
       bdCustomer.set("yem_customeraccessid", merchantApply.get("id"));
       bdCustomer.set("country", merchantApply.get("yem_country"));
       bdCustomer.set("societycreditcode", merchantApply.get("yem_societycreditcode"));
       bdCustomer.set("tx_register_no", merchantApply.get("yem_registerno"));
       bdCustomer.set("artificialperson", merchantApply.get("yem_artificialperson"));
       bdCustomer.set("bizpartner_address", merchantApply.get("yem_businessaddress"));
       bdCustomer.set("businessscope", merchantApply.get("yem_businessscope"));
       bdCustomer.set("paymentcurrency", bankCurrency);
       if(info){
           bdCustomer.set("customerstatus", customerstatus);
       }else{
           bdCustomer.set("supplier_status", customerstatus);
       }
       bdCustomer.set("linkman", linkmanMap.get("yem_name"));
       bdCustomer.set("bizpartner_phone", linkmanMap.get("yem_phone"));
       bdCustomer.set("bizpartner_fax", linkmanMap.get("yem_fax"));
       bdCustomer.set("postal_code", linkmanMap.get("yem_email"));
       bdCustomer.set("admindivision", linkmanMap.get("yem_admindivision"));
       bdCustomer.set("bizpartner_address", linkmanMap.get("yem_detailaddress"));
   }



        /**
        * @Description: 新增客户分录数据
        * @return void
        */

    private void creatCustomerEntry(Map<String, Object> innerMap,DynamicObject creatlinkman,DynamicObject bdaddress){
        creatlinkman.set("contactperson",innerMap.get("yem_name"));
        creatlinkman.set("contactpersonpost",innerMap.get("yem_office"));
        DynamicObject departments = (DynamicObject) innerMap.get("yem_departments");
        ILocaleString depname = null != departments ? departments.getLocaleString("name") : null;
        creatlinkman.set("dept",depname);
        creatlinkman.set("phone",innerMap.get("yem_phone"));
        creatlinkman.set("fax",innerMap.get("yem_fax"));
        creatlinkman.set("email",innerMap.get("yem_email"));
        creatlinkman.set("isdefault_linkman",innerMap.get("yem_isdefault_linkman"));
        creatlinkman.set("associatedaddress",bdaddress);
        creatlinkman.set("yem_linkmaninfoid",innerMap.get("id"));
    }


    /**
     * @Description: 新增银行信息分录数据
     * @return void
     */
  private void creatBankInfo(Map<Object, Map<String, Object>> bankentryMap,DynamicObject bdCustomer){
      for (Map.Entry<Object, Map<String, Object>> outerEntry : bankentryMap.entrySet()) {
          DynamicObjectCollection entrybank = bdCustomer.getDynamicObjectCollection("entry_bank");
          DynamicObject creatbank  = entrybank.addNew();
          Map<String, Object> bankMap = outerEntry.getValue();  // 获取内层 Map
          creatbank.set("bankaccount",bankMap.get("yem_bankaccount"));
          creatbank.set("accountname",bankMap.get("yem_accountname"));
          creatbank.set("iban",bankMap.get("yem_iban"));
          creatbank.set("bank",bankMap.get("yem_bank"));
          creatbank.set("currency",bankMap.get("yem_currency"));
          creatbank.set("isdefault_bank",bankMap.get("yem_isdefault_bank"));
      }
  }


    /**
     * @Description: 新增地址基础资料
     * @param customers  客户
     * @param innerMap   客商准入分录行数据
     * @return kd.bos.dataentity.entity.DynamicObject
     */
    private DynamicObject creatAddressr(DynamicObject customers,Map<String, Object> innerMap, String yemAddressnumber,Boolean flag){
        DynamicObject address = BusinessDataServiceHelper.loadSingle("bd_address", new QFilter[]{new QFilter("number", QCP.equals, yemAddressnumber)});
        DynamicObject bdaddress = BusinessDataServiceHelper.newDynamicObject("bd_address");
        if(null!=address) {
            if(flag){
                address.set("customer", customers);
                address.set("iscustomeradd", true);
                address.set("customerid", customers.get("id"));

            }else {
                address.set("supplier", customers);
                address.set("issupplieradd",true);
                address.set("supplierid", customers.get("id"));
            }
            OperationServiceHelper.executeOperate("save", "bd_address", new DynamicObject[]{address}, OperateOption.create());
            return address;
        }
        if(flag){
            bdaddress.set("customer", customers);
            bdaddress.set("iscustomeradd", true);
            bdaddress.set("customerid", customers.get("id"));
        }else {
            bdaddress.set("supplier", customers);
            bdaddress.set("issupplieradd",true);
            bdaddress.set("supplierid", customers.get("id"));
        }
        bdaddress.set("number", innerMap.get("yem_addressnumber"));
        bdaddress.set("name", innerMap.get("yem_addressname"));
        bdaddress.set("hihn_tradeterms", innerMap.get("yem_tradeterms"));
        bdaddress.set("hihn_clearanceco", innerMap.get("yem_clearanceco"));
        bdaddress.set("hihn_forwarderco", innerMap.get("yem_forwarderco"));
        bdaddress.set("customeraddrsspurpose", innerMap.get("yem_customeraddrsspur"));
        bdaddress.set("admindivision", innerMap.get("yem_admindivision"));
        bdaddress.set("detailaddress", innerMap.get("yem_detailaddress"));
        bdaddress.set("linkman", innerMap.get("yem_linkman"));
        bdaddress.set("yem_suppliervx", innerMap.get("yem_wechat"));
        bdaddress.set("yem_supplierqq", innerMap.get("yem_qq"));
        bdaddress.set("phone", innerMap.get("yem_phone"));
        bdaddress.set("addemail", innerMap.get("yem_email"));
        bdaddress.set("zipcode", innerMap.get("yem_zipcode"));
        bdaddress.set("timezone", innerMap.get("yem_timezone"));
        bdaddress.set("default", innerMap.get("yem_addressdefault"));

        bdaddress.set("status", "C");
        if(!YEM.isEmpty(innerMap.get("yem_admindivision"))){
            bdaddress.set("admindivisiondata",innerMap.get("yem_admindivision"));
        }else{
            bdaddress.set("admindivisiondata",null);
        }
        bdaddress.set("createorg", innerMap.get("org"));
        bdaddress.set("enable", "1");


        OperationServiceHelper.executeOperate("save", "bd_address", new DynamicObject[]{bdaddress}, OperateOption.create());
        return bdaddress;
    }

    /**
     * @Description:  根据联系人分录id获取到客商准入不存在的分录id但在客户分录存在的分录id的分录行DynamicObject同时获取关联地值基础资料的DynamicObject，判断不为空删除
     * @param linkmanentryMap 联系人分录信息(以准入单行id为key)
     * @param entryLinkmans  客户/供应商基础资料联系人分录数据集合
     * @return void
     */
    private void deleteEntryLinkmans(Map<Object, Map<String, Object>> linkmanentryMap, DynamicObjectCollection entryLinkmans , Map<Object, Map<String, Object>> linkManentry) {
        OperateOption create = OperateOption.create();
        //客商准入联系人分录id集合
        List<Long> addressnumberList = getEntryInfo(linkmanentryMap,false);
        //客户联系人分录id
        List<Long> bdaddressnumberList = getEntryInfo(linkManentry,true);

        //addressnumberList bdaddressnumberList 不存在的值
        List<Long> result = new ArrayList<>();
        for (Long number : bdaddressnumberList) {
            if (!addressnumberList.contains(number)) { // 检查是否不存在
                result.add(number);
            }
        }
        //删除联系人分录对应行
        List<DynamicObject> deleterow= new ArrayList<>();
        List<String> deleteassociatedaddress= new ArrayList<>();
        for (DynamicObject entryLinkman : entryLinkmans) {
            Long yem_linkmaninfoid = (Long)entryLinkman.get("yem_linkmaninfoid");
            if(result.contains(yem_linkmaninfoid)){
                deleterow.add(entryLinkman);
                DynamicObject associatedaddress = (DynamicObject)entryLinkman.get("associatedaddress");
                if(null!=associatedaddress){
                    DynamicObject address = BusinessDataServiceHelper.loadSingleFromCache(associatedaddress.getPkValue(), "bd_address");
                    if(null!=address) {
                        deleteassociatedaddress.add((String) address.get("number"));
                    }
                }

            }
        }
        //删除地址基础资料
        DeleteServiceHelper.delete("bd_address",new QFilter[]{new QFilter("number",QCP.in,deleteassociatedaddress.toArray())});
        //删除联系人分录对应行
        entryLinkmans.removeAll(deleterow);
    }


    /**
     * @Description: 获取分录id集合
     * @return List<Long>
     */
    /**
     * @Description: 获取分录id集合
     * @param linkmanentryMap 分录数据集合
     * @param flag   true: 客户或供应商基础资料 false:客商准入单
     * @return java.util.List<java.lang.Long>
     */

    private  List<Long> getEntryInfo(Map<Object, Map<String, Object>> linkmanentryMap,boolean flag){
        List<Long> addressnumberMap = new ArrayList<>();
        for (Map.Entry<Object, Map<String, Object>> outerEntry : linkmanentryMap.entrySet()) {
            Map<String, Object> linkManMap = outerEntry.getValue();
            if(flag){
                Long linkmaninfoid = (Long) linkManMap.get("yem_linkmaninfoid");
                addressnumberMap.add(linkmaninfoid);

            }else{
                Long id = (Long) linkManMap.get("id");
                addressnumberMap.add(id);

            }
        }
        return addressnumberMap;
    }


    /**
     * @Description: 获取银行信息默认行币别
     * @param bankinfo   银行信息分录
     * @return kd.bos.dataentity.entity.DynamicObject
     */
    private DynamicObject getBankCurrency(DynamicObjectCollection bankinfo){
        DynamicObject yemCurrency = null;//银行分录默认行币别
        for (DynamicObject bank : bankinfo) {
            boolean yemIsdefaultBank = bank.getBoolean("yem_isdefault_bank");
            if (yemIsdefaultBank) {
                yemCurrency = bank.getDynamicObject("yem_currency");
            }
        }
        return yemCurrency;
    }


    /**
     * @Description: 获取分录默认行信息
     * @param linkmaninfo   分录信息
     * @return java.util.Map<java.lang.Object,java.lang.Object>
     */
    private Map<Object, Object> getDefaultLinkManentry(DynamicObjectCollection linkmaninfo){
        Map<Object, Object> linkmanMap = new HashMap<>();
        for (DynamicObject linkman : linkmaninfo) {
            Boolean yemIsdefaultLinkman = (Boolean) linkman.get("yem_isdefault_linkman");
            if (yemIsdefaultLinkman) {
                DynamicObjectType dynamicObjectType = linkman.getDynamicObjectType();
                DataEntityPropertyCollection properties = dynamicObjectType.getProperties();
                for (IDataEntityProperty property : properties) {
                    String name = property.getName();
                    Object value = linkman.get(name);
                    linkmanMap.put(name, value);
                }
            }
        }
        return linkmanMap;
    }


    /**
     * @Description: 获取分录所有信息(以准入单行id为主键)
     * @param linkmaninfo   分录信息
     * @param flag   true: 客户或供应商基础资料 false:客商准入单
     * @return java.util.Map<java.lang.Object,java.util.Map<java.lang.String,java.lang.Object>>
     */
    private  Map<Object, Map<String,Object>> getLinkManentry(DynamicObjectCollection linkmaninfo,Boolean flag){
        Map<Object, Map<String,Object>> linkmanentryMap = new HashMap<>();
        for (DynamicObject linkman : linkmaninfo) {
            Map<String, Object> map = new HashMap<>();
            Object id = null;
            if(flag) {
                id = (Object) linkman.get("yem_linkmaninfoid");
            }else{
                id = (Object) linkman.get("id");
            }
            DynamicObjectType dynamicObjectType = linkman.getDynamicObjectType();
            DataEntityPropertyCollection properties = dynamicObjectType.getProperties();
            for (IDataEntityProperty property : properties) {
                String name = property.getName();
                Object value = linkman.get(name);
                map.put(name, value);
            }
            linkmanentryMap.put(id,map);
        }
        return linkmanentryMap;
    }

}
