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

import lombok.Setter;

/**
 * 同步合同分组
 */
public class ContractGroupSyncTask extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(ContractGroupSyncTask.class);
    @Setter
    private ContractGroupService contractGroupService;

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

    ContractGroupService getContractGroupService() {
        if (contractGroupService == null) {
            contractGroupService = SpringApp.getBean(ContractGroupService.class);
        }
        return contractGroupService;
    }

    @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 系统 CM_Group 数据表...");
        List<Map<String, Object>> list = service.queryAllContractGroup();
        int size = list.size();
        holder.debug("总共读取 CM_Group 数据 " + 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 groupCode = (String) map.get("cGroupID");
        String groupName = (String) map.get("cGroupName");
        String groupTitle = (String) map.get("cRemark");

        ContractGroupService service = getContractGroupService();
        ContractGroup contractGroup = service.findByCode(groupCode);
        if (contractGroup == null) {
            contractGroup = service.newContractGroup();
            holder.info("新建合同分组：" + groupCode);
            modified = true;
        }

        if (!Objects.equals(contractGroup.getCode(), groupCode)) {
            contractGroup.setCode(groupCode);
            holder.info("合同分组代码：" + contractGroup.getCode() + " -> " + groupCode);
            modified = true;
        }
        if (!Objects.equals(contractGroup.getName(), groupName)) {
            contractGroup.setName(groupName);
            holder.info("合同分组名称：" + contractGroup.getName() + " -> " + groupName);
            modified = true;
        }
        if (!Objects.equals(contractGroup.getTitle(), groupTitle)) {
            contractGroup.setTitle(groupTitle);
            holder.info("合同分组标题：" + contractGroup.getTitle() + " -> " + groupTitle);
            modified = true;
        }

        if (modified) {
            service.save(contractGroup);
        }
    }
}
