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.contract.service.ContractKindService;
import com.ecep.contract.model.ContractKind;
import com.ecep.contract.ui.Tasker;

import lombok.Setter;

/**
 * 同步合同分类
 */
public class ContractKindSyncTask extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(ContractKindSyncTask.class);
    @Setter
    private ContractKindService contractKindService;

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

    ContractKindService getContractKindService() {
        if (contractKindService == null) {
            contractKindService = SpringApp.getBean(ContractKindService.class);
        }
        return contractKindService;
    }

    @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 系统 CM_Kind 数据表...");
        List<Map<String, Object>> list = repository.queryAllContractKind();
        int size = list.size();
        holder.debug("总共读取 CM_Kind 数据 " + 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 typeCode = (String) map.get("KindID");
        String typeName = (String) map.get("KindName");
        String typeTitle = (String) map.get("Description");

        ContractKindService kindService = getContractKindService();
        ContractKind contractKind = kindService.findByCode(typeCode);
        if (contractKind == null) {
            contractKind = new ContractKind();
            holder.info("新建合同分类：" + typeCode);
            modified = true;
        }

        if (!Objects.equals(contractKind.getCode(), typeCode)) {
            contractKind.setCode(typeCode);
            holder.info("合同分类代码：" + contractKind.getCode() + " -> " + typeCode);
            modified = true;
        }
        if (!Objects.equals(contractKind.getName(), typeName)) {
            contractKind.setName(typeName);
            holder.info("合同分类名称：" + contractKind.getName() + " -> " + typeName);
            modified = true;
        }
        if (!Objects.equals(contractKind.getTitle(), typeTitle)) {
            contractKind.setTitle(typeTitle);
            holder.info("合同分类描述：" + contractKind.getTitle() + " -> " + typeTitle);
            modified = true;
        }

        if (modified) {
            kindService.save(contractKind);
        }
    }

}
