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.vendor.repository.VendorClassRepository;
import com.ecep.contract.model.VendorCatalog;
import com.ecep.contract.ui.Tasker;

import lombok.Setter;

/**
 * 同步供应商分类
 */
public class VendorClassSyncTask extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(VendorClassSyncTask.class);
    @Setter
    private VendorClassRepository vendorClassRepository;

    public VendorClassSyncTask() {
        updateTitle("用友U8系统-同步供应商分类信息");
    }

    VendorClassRepository getVendorClassRepository() {
        if (vendorClassRepository == null) {
            vendorClassRepository = SpringApp.getBean(VendorClassRepository.class);
        }
        return vendorClassRepository;
    }

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

        AtomicInteger counter = new AtomicInteger(0);
        logger.info("读取 U8 系统 VendorClass 数据表...");
        List<Map<String, Object>> list = service.queryAllVendorClass();
        int size = list.size();
        holder.debug("总共读取 VendorClass 数据 " + 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("cVCCode");
        String name = (String) map.get("cVCName");

        VendorClassRepository repository = getVendorClassRepository();
        VendorCatalog vendorCatalog = repository.findByCode(code).orElse(null);
        if (vendorCatalog == null) {
            vendorCatalog = new VendorCatalog();
            holder.info("新建供应商分类：" + code);
            modified = true;
        }

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

        if (modified) {
            repository.save(vendorCatalog);
        }
    }
}
