package com.persagy.proxy.migration.handler;

import cn.hutool.core.collection.CollUtil;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.common.entity.DmpResult;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.migration.constant.MigrationTable;
import com.persagy.proxy.migration.model.DataMigrationExcel;
import com.persagy.proxy.migration.model.MigrationInfo;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author lvxianyun
 * @title: DataMigrationHandler
 * @projectName adm-middleware
 * @date 2021/12/3 11:14
 */
@Component
public class DataMigrationHandler {

    private static Map<String, IDataMigration> migrationMap = new HashMap<>(16);

    /**
     * 检查标准字典数据，据此判断是否需要提示用户升级字典
     * @param context
     * @param migrationInfo
     */
    public Map<String, List<DataMigrationExcel>> handleDicData(InstanceUrlParam context, MigrationInfo migrationInfo){
        Map<String,List<DataMigrationExcel>> results = new HashMap<>(2);
        AdmResponse admResponseType = DefineTypeMigration.getInstance().compareByCount(context, migrationInfo);

        if(!admResponseType.getResult().equals(DmpResult.SUCCESS)){
            List<DataMigrationExcel> dataMigrationExcels = new ArrayList<>();
            dataMigrationExcels.addAll((Collection<? extends DataMigrationExcel>) admResponseType.getContent());
            results.put(MigrationTable.DTDEFINETYPE.getCode(), dataMigrationExcels);
        }
        AdmResponse admResponseInfo = DefineInfoMigration.getInstance().compareByCount(context, migrationInfo);
        if(!admResponseInfo.getResult().equals(DmpResult.SUCCESS)){
            List<DataMigrationExcel> dataMigrationExcels = new ArrayList<>();
            dataMigrationExcels.addAll((Collection<? extends DataMigrationExcel>) admResponseInfo.getContent());
            results.put(MigrationTable.DTDEFINEINFO.getCode(), dataMigrationExcels);
        }
        return results;
    }

    /**
     * 获取迁移数据信息
     * @param context
     * @param migrationInfo
     * @return
     */
    public Map<String, List<DataMigrationExcel>> handleAndGetExcelList(InstanceUrlParam context, MigrationInfo migrationInfo){
        Map<String,List<DataMigrationExcel>> results = new HashMap<>(16);
        if(CollUtil.isNotEmpty(migrationMap)){
            for (MigrationTable migrationTable : MigrationTable.values()){
                if(!migrationMap.containsKey(migrationTable.getCode())){
                    continue;
                }
                results.put(migrationTable.getCode(), migrationMap.get(migrationTable.getCode()).migrateForExcel(context, migrationInfo));
            }
        }
        return results;
    }

    /**
     * 迁移数据记录日志
     * @param context
     * @param migrationInfo
     */
    public void handleAndSaveLog(InstanceUrlParam context, MigrationInfo migrationInfo){
        if(CollUtil.isNotEmpty(migrationMap)){
            for (MigrationTable migrationTable : MigrationTable.values()){
                if(!migrationMap.containsKey(migrationTable.getCode())){
                    continue;
                }
                migrationMap.get(migrationTable.getCode()).migrateForLog(context, migrationInfo);
            }
        }
    }

    public void addMap(String tableName, IDataMigration iDataMigration){
        migrationMap.put(tableName, iDataMigration);
    }

    @PostConstruct
    private void initHandleService(){
        addMap(MigrationTable.DTDEFINETYPE.getCode(), DefineTypeMigration.getInstance());
        addMap(MigrationTable.DTDEFINEINFO.getCode(), DefineInfoMigration.getInstance());
        addMap(MigrationTable.DTOBJECT.getCode(), ObjectDigitalMigration.getInstance());
        addMap(MigrationTable.DTRELATION.getCode(), ObjectRelationMigration.getInstance());
    }
}
