package com.yem.wm.bc.companytitle.util;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @author xwudd
 * @date 16:30
 * @Description 抬头信息公共方法
 **/
public class TitleFuncUtil {

    /**
     * @description 取值公司抬头 赋值相关信息
     * @author xwudd
     * * @param[1] model
     * @param[2] company
     * @time 2023/7/27 16:34
     */

    public static void setCompanyInfo(IDataModel model,DynamicObject company){
        if(company != null){
            company = BusinessDataServiceHelper.loadSingle(company.getPkValue(),company.getDynamicObjectType().getName());
            model.setValue("yem_company",company);
            model.setValue("yem_companyname",company.getString("name"));
            model.setValue("yem_companyaddr",company.getString("yem_address"));
            model.setValue("yem_companylinkman",company.getString("yem_contacts"));
            model.setValue("yem_companytel",company.getString("yem_phone"));
            model.setValue("yem_companyfax",company.getString("yem_fax"));
            model.setValue("yem_companynameeng",company.getString("yem_namecn"));
            model.setValue("yem_companyaddreng",company.getString("yem_addressen"));
            model.setValue("yem_coemail", company.getString("yem_email"));
            DynamicObjectCollection bankInfos = company.getDynamicObjectCollection("yem_bankinfo");
            if(bankInfos.size() > 0){
                Boolean flag = true;
                for(DynamicObject bankInfo : bankInfos){
                    boolean defaultBank = bankInfo.getBoolean("yem_default");
                    if(defaultBank){
                        DynamicObject accountBanks = bankInfo.getDynamicObject("yem_bd_accountbanks");
                        if(accountBanks != null){
                            model.setValue("yem_companybankacc",accountBanks.getString("bankaccountnumber"));
                            model.setValue("yem_companyaccname",accountBanks.getString("name"));
                        }else{
                            model.setValue("yem_companybankacc",null);
                            model.setValue("yem_companyaccname",null);
                        }
                        DynamicObject beBank = bankInfo.getDynamicObject("yem_bd_bebank");
                        if(beBank != null){
                            beBank = BusinessDataServiceHelper.loadSingle(beBank.getPkValue(),beBank.getDynamicObjectType().getName());
                            model.setValue("yem_companyopenbank",beBank.getString("name"));
                            model.setValue("yem_openbankengaddress",beBank.getString("name_eng"));
                        }else{
                            model.setValue("yem_companyopenbank",null);
                            model.setValue("yem_openbankengaddress",null);
                        }

                        model.setValue("yem_companyswift",bankInfo.getString("yem_bankswift"));
                        model.setValue("yem_companybankaddress",bankInfo.getString("yem_bankaddress"));
                        model.setValue("yem_bankengaddress",bankInfo.getString("yem_bankaddressen"));
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    DynamicObject bankInfo = bankInfos.get(0);
                    DynamicObject accountBanks = bankInfo.getDynamicObject("yem_bd_accountbanks");
                    if(accountBanks != null){
                        model.setValue("yem_companybankacc",accountBanks.getString("bankaccountnumber"));
                        model.setValue("yem_companyaccname",accountBanks.getString("name"));
                    }else{
                        model.setValue("yem_companybankacc",null);
                        model.setValue("yem_companyaccname",null);
                    }
                    DynamicObject beBank = bankInfo.getDynamicObject("yem_bd_bebank");
                    if(beBank != null){
                        beBank = BusinessDataServiceHelper.loadSingle(beBank.getPkValue(),beBank.getDynamicObjectType().getName());
                        model.setValue("yem_companyopenbank",beBank.getString("name"));
                        model.setValue("yem_openbankengaddress",beBank.getString("name_eng"));
                    }else{
                        model.setValue("yem_companyopenbank",null);
                        model.setValue("yem_openbankengaddress",null);
                    }
                    model.setValue("yem_companyswift",bankInfo.getString("yem_bankswift"));
                    model.setValue("yem_companybankaddress",bankInfo.getString("yem_bankaddress"));
                    model.setValue("yem_bankengaddress",bankInfo.getString("yem_bankaddressen"));
                }
            }else{
                model.setValue("yem_companybankacc",null);
                model.setValue("yem_companyaccname",null);
                model.setValue("yem_companyopenbank",null);
                model.setValue("yem_openbankengaddress",null);
                model.setValue("yem_companyswift",null);
                model.setValue("yem_companybankaddress",null);
                model.setValue("yem_bankengaddress",null);
            }
        }else{
            model.setValue("yem_companyname",null);
            model.setValue("yem_companyaddr",null);
            model.setValue("yem_companylinkman",null);
            model.setValue("yem_companytel",null);
            model.setValue("yem_companyfax",null);
            model.setValue("yem_companybankacc",null);
            model.setValue("yem_companyaccname",null);
            model.setValue("yem_companyopenbank",null);
            model.setValue("yem_companyswift",null);
            model.setValue("yem_companybankaddress",null);
            model.setValue("yem_openbankengaddress",null);
            model.setValue("yem_bankengaddress",null);
            model.setValue("yem_coemail",null);
        }
    }


    /**
     * @description 取值客户 赋值相关信息
     * @author xwudd
     * * @param[1] model
     * @param[2] customer
     * @time 2023/7/27 16:34
     */

    public static void setCustomerInfo(IDataModel model,DynamicObject customer){
        if(customer != null){
            customer = BusinessDataServiceHelper.loadSingle(customer.getPkValue(),customer.getDynamicObjectType().getName());
            model.setValue("yem_customer",customer);
            model.setValue("yem_customername",customer.getString("name"));
            model.setValue("yem_customeraddr",customer.getLocaleString("bizpartner_address").getLocaleValue());
            model.setValue("yem_customerlinkman",customer.getLocaleString("linkman").getLocaleValue());
            model.setValue("yem_customertel",customer.getString("bizpartner_phone"));
            model.setValue("yem_customerfax",customer.getString("bizpartner_fax"));
            model.setValue("yem_customemail", customer.getString("postal_code"));
            DynamicObject groupcustomers = customer.getDynamicObject("yem_groupcustomers");
            if (groupcustomers != null) {
                model.setValue("yem_groupcusfields", groupcustomers.getString("name"));
            } else {
                model.setValue("yem_groupcusfields", "");
            }
            DynamicObjectCollection bankInfos = customer.getDynamicObjectCollection("entry_bank");
            if(bankInfos.size() > 0){
                Boolean flag = true;
                for(DynamicObject bankInfo : bankInfos){
                    boolean defaultBank = bankInfo.getBoolean("isdefault_bank");
                    if(defaultBank){
                        model.setValue("yem_customerbankacc",bankInfo.getString("bankaccount"));
                        model.setValue("yem_customeraccname",bankInfo.getString("accountname"));
                        DynamicObject beBank = bankInfo.getDynamicObject("bank");
                        if(beBank != null){
                            model.setValue("yem_customeropenbank",beBank.getString("name"));//开户行名称
                            model.setValue("yem_customeracccountry",beBank.getDynamicObject("country"));//开户国家
                        }else{
                            model.setValue("yem_customeropenbank",null);//开户行名称
                            model.setValue("yem_customeracccountry",null);//开户国家
                        }
                        model.setValue("yem_customeraccurrency",bankInfo.getDynamicObject("currency"));//账户币别
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    DynamicObject bankInfo = bankInfos.get(0);
                    model.setValue("yem_customerbankacc",bankInfo.getString("bankaccount"));
                    model.setValue("yem_customeraccname",bankInfo.getString("accountname"));
                    DynamicObject beBank = bankInfo.getDynamicObject("bank");
                    if(beBank != null){
                        model.setValue("yem_customeropenbank",beBank.getString("name"));//开户行名称
                        model.setValue("yem_customeracccountry",beBank.getDynamicObject("country"));//开户国家
                    }else{
                        model.setValue("yem_customeropenbank",null);//开户行名称
                        model.setValue("yem_customeracccountry",null);//开户国家
                    }
                    model.setValue("yem_customeraccurrency",bankInfo.getDynamicObject("currency"));//账户币别
                }
            }else{
                model.setValue("yem_customerbankacc",null);
                model.setValue("yem_customeraccname",null);
                model.setValue("yem_customeropenbank",null);//开户行名称
                model.setValue("yem_customeracccountry",null);//开户国家
                model.setValue("yem_customeraccurrency",null);//账户币别
            }
        }else{
            model.setValue("yem_customername",null);
            model.setValue("yem_customeraddr",null);
            model.setValue("yem_customerlinkman",null);
            model.setValue("yem_customertel",null);
            model.setValue("yem_customerfax",null);
            model.setValue("yem_customerbankacc",null);
            model.setValue("yem_customeraccname",null);
            model.setValue("yem_customeropenbank",null);
            model.setValue("yem_customeraccurrency",null);
            model.setValue("yem_customeracccountry",null);
            model.setValue("yem_customemail",null);
            model.setValue("yem_groupcusfields", null);
        }
    }
}
