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

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.vendor.service.VendorEntityService;
import com.ecep.contract.ds.vendor.service.VendorService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CompanyCustomerEntity;
import com.ecep.contract.model.Vendor;
import com.ecep.contract.model.VendorEntity;
import com.ecep.contract.model.Employee;

import lombok.Getter;
import lombok.Setter;

public class AbstractYongYouU8Ctx extends AbstractCtx {
    @Getter
    @Setter
    YongYouU8Repository repository;

    public void from(AbstractYongYouU8Ctx parent) {
        repository = parent.repository;
    }

    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() {
        return getCachedBean(CompanyService.class);
    }

    public CompanyCustomerService getCompanyCustomerService() {
        return getCachedBean(CompanyCustomerService.class);
    }

    public CompanyCustomerEntityService getCompanyCustomerEntityService() {
        return getCachedBean(CompanyCustomerEntityService.class);
    }

    public VendorService getCompanyVendorService() {
        return getCachedBean(VendorService.class);
    }

    public VendorEntityService getCompanyVendorEntityService() {
        return getCachedBean(VendorEntityService.class);
    }

    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)) {
            VendorEntityService vendorEntityService = getCompanyVendorEntityService();
            VendorService customerService = getCompanyVendorService();
            VendorEntity entity = vendorEntityService.findByCode(vendorCode);
            if (entity == null) {
                holder.warn("无效" + topic + "：" + vendorCode);
            } else {
                Vendor 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;
    }

}
