package com.ecep.contract.cloud.u8.ctx;

import static com.ecep.contract.SpringApp.getBean;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.hibernate.Hibernate;
import org.springframework.beans.BeansException;
import org.springframework.util.StringUtils;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.cloud.AbstractCtx;
import com.ecep.contract.cloud.u8.YongYouU8Repository;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.customer.service.CompanyCustomerEntityService;
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.vendor.service.CompanyVendorEntityService;
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CompanyCustomerEntity;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.CompanyVendorEntity;
import com.ecep.contract.model.Employee;

import lombok.Getter;
import lombok.Setter;

public class AbstractYongYouU8Ctx extends AbstractCtx {
    @Getter
    @Setter
    YongYouU8Repository repository;
    @Setter
    CompanyService companyService;
    @Setter
    EmployeeService employeeService;
    @Setter
    CompanyCustomerService companyCustomerService;
    @Setter
    CompanyCustomerEntityService companyCustomerEntityService;
    @Setter
    CompanyVendorService companyVendorService;
    @Setter
    CompanyVendorEntityService companyVendorEntityService;


    public void from(AbstractYongYouU8Ctx parent) {
        repository = parent.repository;
        companyService = parent.companyService;
        employeeService = parent.employeeService;
        companyCustomerService = parent.companyCustomerService;
        companyVendorService = parent.companyVendorService;
    }

    public void initializeRepository(MessageHolder holder) {
        if (repository != null) {
            return;
        }
        try {
            repository = getBean(YongYouU8Repository.class);
        } catch (BeansException e) {
            holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
        }
    }


    public CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = getBean(CompanyService.class);
        }
        return companyService;
    }

    EmployeeService getEmployeeService() {
        if (employeeService == null) {
            employeeService = getBean(EmployeeService.class);
        }
        return employeeService;
    }

    public CompanyCustomerService getCompanyCustomerService() {
        if (companyCustomerService == null) {
            companyCustomerService = getBean(CompanyCustomerService.class);
        }
        return companyCustomerService;
    }

    public CompanyCustomerEntityService getCompanyCustomerEntityService() {
        if (companyCustomerEntityService == null) {
            companyCustomerEntityService = getBean(CompanyCustomerEntityService.class);
        }
        return companyCustomerEntityService;
    }

    public CompanyVendorService getCompanyVendorService() {
        if (companyVendorService == null) {
            companyVendorService = getBean(CompanyVendorService.class);
        }
        return companyVendorService;
    }

    public CompanyVendorEntityService getCompanyVendorEntityService() {
        if (companyVendorEntityService == null) {
            companyVendorEntityService = getBean(CompanyVendorEntityService.class);
        }
        return companyVendorEntityService;
    }

    boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code, MessageHolder holder, String topic) {
        if (StringUtils.hasText(code)) {
            Employee employee = getEmployeeService().findByCode(code);
            if (employee != null) {
                if (!Objects.equals(getter.get(), employee)) {
                    setter.accept(employee);
                    holder.info(topic + "更新为 " + employee.getName());
                    return true;
                }
            }
        }
        return false;
    }


    boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name, MessageHolder holder, String topic) {
        if (StringUtils.hasText(name)) {
            Employee employee = getEmployeeService().findByName(name);
            if (employee != null) {
                if (!Objects.equals(getter.get(), employee)) {
                    setter.accept(employee);
                    holder.info(topic + "更新为 " + employee.getName());
                    return true;
                }
            }
        }
        return false;
    }

    boolean updateCompanyByCustomerCode(Supplier<Company> getter, Consumer<Company> setter, String customerCode, MessageHolder holder, String topic) {
        Company company = null;
        if (StringUtils.hasText(customerCode)) {
            CompanyCustomerEntityService customerEntityService = getCompanyCustomerEntityService();
            CompanyCustomerService customerService = getCompanyCustomerService();
            CompanyCustomerEntity entity = customerEntityService.findByCustomerCode(customerCode);
            if (entity == null) {
                holder.warn("无效" + topic + "：" + customerCode);
            } else {
                CompanyCustomer customer = entity.getCustomer();
                if (customer == null) {
                    holder.warn("无效" + topic + "：" + customerCode);
                } else {
                    if (!Hibernate.isInitialized(customer)) {
                        customer = customerService.findById(customer.getId());
                    }
                    company = customer.getCompany();
                }
            }
        }
        if (company == null) {
            setter.accept(null);
            holder.warn("无效" + topic + "：" + customerCode);
            return true;
        } else {
            if (!Objects.equals(getter.get(), company)) {
                setter.accept(company);
                if (!Hibernate.isInitialized(company)) {
                    company = getCompanyService().findById(company.getId());
                }
                holder.info(topic + "修改为: " + company.getName());
                return true;
            }
        }
        return false;
    }


    boolean updateCompanyByVendorCode(Supplier<Company> getter, Consumer<Company> setter, String vendorCode, MessageHolder holder, String topic) {
        Company company = null;
        if (StringUtils.hasText(vendorCode)) {
            CompanyVendorEntityService vendorEntityService = getCompanyVendorEntityService();
            CompanyVendorService customerService = getCompanyVendorService();
            CompanyVendorEntity entity = vendorEntityService.findByCode(vendorCode);
            if (entity == null) {
                holder.warn("无效" + topic + "：" + vendorCode);
            } else {
                CompanyVendor customer = entity.getVendor();
                if (customer == null) {
                    holder.warn("无效" + topic + "：" + vendorCode);
                } else {
                    if (!Hibernate.isInitialized(customer)) {
                        customer = customerService.findById(customer.getId());
                    }
                    company = customer.getCompany();
                }
            }
        }
        if (company == null) {
            setter.accept(null);
            holder.warn("无效" + topic + "：" + vendorCode);
            return true;
        } else {
            if (!Objects.equals(getter.get(), company)) {
                setter.accept(company);
                if (!Hibernate.isInitialized(company)) {
                    company = getCompanyService().findById(company.getId());
                }
                holder.info(topic + "修改为: " + company.getName());
                return true;
            }
        }
        return false;
    }


}
