package com.ecep.contract.cloud.u8;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
import com.ecep.contract.model.CustomerCatalog;
import com.ecep.contract.ui.Tasker;

import lombok.Setter;

/**
 * 同步客户分类
 */
public class CustomerClassSyncTask extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(CustomerClassSyncTask.class);
    @Setter
    private CompanyCustomerService companyCustomerService;

    public CustomerClassSyncTask() {
        updateTitle("用友U8系统-同步客户分类信息");
    }

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

    @Override
    protected Object execute(MessageHolder holder) throws Exception {
        YongYouU8Repository repository = null;
        try {
            repository = SpringApp.getBean(YongYouU8Repository.class);
        } catch (BeansException e) {
            logger.error("can't get bean of YongYouU8Repository", e);
            holder.error("can't get bean of YongYouU8Repository");
            return null;
        }

        AtomicInteger counter = new AtomicInteger(0);
        logger.info("读取 U8 系统 CustomerClass 数据表...");
        List<Map<String, Object>> list = repository.queryAllCustomerClass();
        int size = list.size();
        holder.debug("总共读取 CustomerClass 数据 " + size + " 条");

        for (Map<String, Object> map : list) {
            if (isCancelled()) {
                holder.info("Cancelled");
                return null;
            }
            MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
            sync(map, sub);
            // 更新进度
            updateProgress(counter.incrementAndGet(), size);
        }

        return null;
    }

    private void sync(Map<String, Object> map, MessageHolder holder) {
        boolean modified = false;

        String code = (String) map.get("cCCCode");
        String name = (String) map.get("cCCName");

        CompanyCustomerService customerService = getCompanyCustomerService();
        CustomerCatalog customerCatalog = customerService.findCatalogByCode(code);
        if (customerCatalog == null) {
            customerCatalog = new CustomerCatalog();
            holder.info("新建客户分类：" + code);
            modified = true;
        }

        if (!Objects.equals(customerCatalog.getCode(), code)) {
            customerCatalog.setCode(code);
            holder.info("客户分类代码：" + customerCatalog.getCode() + " -> " + code);
            modified = true;
        }
        if (!Objects.equals(customerCatalog.getName(), name)) {
            customerCatalog.setName(name);
            holder.info("客户分类名称：" + customerCatalog.getName() + " -> " + name);
            modified = true;
        }

        if (modified) {
            customerService.save(customerCatalog);
        }
    }
}
