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.ContractTypeService;
import com.ecep.contract.model.ContractType;
import com.ecep.contract.ui.Tasker;

import lombok.Setter;

/**
 * 同步合同类型
 */
public class ContractTypeSyncTask extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(ContractTypeSyncTask.class);
    @Setter
    private ContractTypeService contractTypeService;

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

    ContractTypeService getContractTypeService() {
        if (contractTypeService == null) {
            contractTypeService = SpringApp.getBean(ContractTypeService.class);
        }
        return contractTypeService;
    }

    @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 YongYouU8Service", e);
            holder.error("can't get bean of YongYouU8Service");
            return null;
        }

        AtomicInteger counter = new AtomicInteger(0);
        logger.info("读取 U8 系统 CM_Type,CM_TypeClass 数据表...");
        List<Map<String, Object>> list = repository.queryAllContractType();
        int size = list.size();
        holder.debug("总共读取 CM_Type,CM_TypeClass 数据 " + 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("cTypeCode");
        String typeName = (String) map.get("cTypeName");
        String typeTitle = (String) map.get("cCharacter");
        String typeCatalog = (String) map.get("cClassName");
        String typeDirection = (String) map.get("cDirection");

        ContractTypeService typeService = getContractTypeService();
        ContractType contractType = typeService.findByCode(typeCode);
        if (contractType == null) {
            contractType = new ContractType();
            holder.info("新建合同类型：" + typeCode);
            modified = true;
        }

        if (!Objects.equals(contractType.getCode(), typeCode)) {
            contractType.setCode(typeCode);
            holder.info("合同类型代码：" + contractType.getCode() + " -> " + typeCode);
            modified = true;
        }
        if (!Objects.equals(contractType.getName(), typeName)) {
            contractType.setName(typeName);
            holder.info("合同类型名称：" + contractType.getName() + " -> " + typeName);
            modified = true;
        }
        if (!Objects.equals(contractType.getTitle(), typeTitle)) {
            contractType.setTitle(typeTitle);
            holder.info("合同类型标题：" + contractType.getTitle() + " -> " + typeTitle);
            modified = true;
        }
        if (!Objects.equals(contractType.getCatalog(), typeCatalog)) {
            contractType.setCatalog(typeCatalog);
            holder.info("合同类型分类：" + contractType.getCatalog() + " -> " + typeCatalog);
            modified = true;
        }
        if (!Objects.equals(contractType.getDirection(), typeDirection)) {
            contractType.setDirection(typeDirection);
            holder.info("合同类型方向：" + contractType.getDirection() + " -> " + typeDirection);
            modified = true;
        }

        if (modified) {
            typeService.save(contractType);
        }
    }

}
