package com.qiaoba.common.database.context;

import cn.hutool.core.collection.CollUtil;
import com.qiaoba.common.database.entity.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 备用(未在使用中)数据源
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/28 10:36
 */
@Component
@Slf4j
@SuppressWarnings("unchecked")
public class BackupDatasourceContext {

    private static final String BACKUP_DATASOURCE_KET = "backup_datasource:";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 获取租户备用数据源
     *
     * @param tenantId 租户ID
     * @return 数据源集合
     */
    public Set<DynamicDataSource> get(String tenantId) {
        return (Set<DynamicDataSource>) redisTemplate.opsForValue().get(BACKUP_DATASOURCE_KET + tenantId);
    }

    /**
     * 设置租户备用数据源
     *
     * @param tenantId 租户ID
     * @param list     数据源集合
     */
    private void set(String tenantId, Set<DynamicDataSource> list) {
        if (CollUtil.isNotEmpty(list)) {
            redisTemplate.opsForValue().set(BACKUP_DATASOURCE_KET + tenantId, list);
        }
    }

    /**
     * 添加租户备用数据源
     *
     * @param dynamicDataSource 数据源
     */
    public void addBackupMap(DynamicDataSource dynamicDataSource) {
        log.info("添加租户备用数据源, 租户ID:{}, 数据源IP:{}", dynamicDataSource.getTenantId(), dynamicDataSource.getUrl());
        Set<DynamicDataSource> dataSourceSet = get(dynamicDataSource.getTenantId());
        if (CollUtil.isEmpty(dataSourceSet)) {
            Set<DynamicDataSource> set = new HashSet<>();
            set.add(dynamicDataSource);
            set(dynamicDataSource.getTenantId(), set);
        } else {
            dataSourceSet.add(dynamicDataSource);
            set(dynamicDataSource.getTenantId(), dataSourceSet);
        }
    }

    public void deleteBackupMap(DynamicDataSource dynamicDataSource) {
        log.info("移除租户备用数据源, 租户ID:{}, 数据源IP:{}", dynamicDataSource.getTenantId(), dynamicDataSource.getUrl());
        Set<DynamicDataSource> dataSourceSet = get(dynamicDataSource.getTenantId());
        if (CollUtil.isNotEmpty(dataSourceSet)) {
            dataSourceSet.remove(dynamicDataSource);
        }

        if (CollUtil.isEmpty(dataSourceSet)) {
            // 删除
            redisTemplate.delete(BACKUP_DATASOURCE_KET + dynamicDataSource.getTenantId());
        } else {
            set(dynamicDataSource.getTenantId(), dataSourceSet);
        }
    }

    /**
     * 添加租户备用数据源
     *
     * @param tenantId    租户ID
     * @param dataSources 数据源集合
     */
    public void addBackupMap(String tenantId, Set<DynamicDataSource> dataSources) {
        set(tenantId, dataSources);
    }

    /**
     * 修改租户备用数据源
     *
     * @param dataSource 数据源
     */
    public void updateBackupMap(DynamicDataSource dataSource) {
        Set<DynamicDataSource> dataSourceSet = get(dataSource.getTenantId());
        if (CollUtil.isNotEmpty(dataSourceSet)) {
            for (DynamicDataSource dynamicDataSource : dataSourceSet) {
                if (dataSource.getDatasourceId().equals(dynamicDataSource.getDatasourceId())) {
                    dataSourceSet.remove(dynamicDataSource);
                    break;
                }
            }
            dataSourceSet.add(dataSource);
            set(dataSource.getTenantId(), dataSourceSet);
        }
    }
}
