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

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

import com.ecep.contract.vo.*;
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.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.ds.company.model.Company;
import com.ecep.contract.model.Employee;
import com.ecep.contract.ds.vendor.model.Vendor;
import com.ecep.contract.ds.vendor.model.VendorEntity;

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 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)) {
            var employee = getEmployeeService().findByCode(code);
            if (employee != null) {
                return updateEmployee(getter, setter, employee, holder, topic);
            }
        }
        return false;
    }

    boolean updateEmployee(Supplier<Employee> getter, Consumer<Employee> setter, EmployeeVo employee,
                           MessageHolder holder, String topic) {
        if (employee == null) {
            return false;
        }
        var service = getEmployeeService();
        var v1 = getter.get();
        if (v1 == null) {
            setter.accept(service.getById(employee.getId()));
            holder.info(topic + "更新为 " + employee.getName());
            return true;
        }
        if (!Hibernate.isInitialized(v1)) {
            v1 = service.getById(v1.getId());
        }
        if (!Objects.equals(v1.getId(), employee.getId())) {
            setter.accept(service.getById(employee.getId()));
            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)) {
            EmployeeVo employee = getEmployeeService().findByName(name);
            if (employee != null) {
                return updateEmployee(getter, setter, employee, holder, topic);
            }
        }
        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();
            CompanyCustomerEntityVo entity = customerEntityService.findByCustomerCode(customerCode);
            if (entity == null) {
                holder.warn("无效" + topic + "：" + customerCode);
            } else {
                CompanyCustomerVo customer = customerService.findById(entity.getCustomerId());
                if (customer == null) {
                    holder.warn("无效" + topic + "：" + customerCode);
                } else {
                    company = getCompanyService().getById(customer.getCompanyId());
                }
            }
        }
        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().getById(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();
            VendorEntityVo entity = vendorEntityService.findByCode(vendorCode);
            if (entity == null) {
                holder.warn("无效" + topic + "：" + vendorCode);
            } else {
                if (entity.getVendorId() == null) {
                    holder.warn("无效" + topic + "：" + vendorCode);
                } else {
                    VendorVo v1 = customerService.findById(entity.getVendorId());
                    company = getCompanyService().getById(v1.getCompanyId());
                }
            }
        }
        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().getById(company.getId());
                }
                holder.info(topic + "修改为: " + company.getName());
                return true;
            }
        }
        return false;
    }

}
