package com.tourism.common.report.util;

import com.tourism.common.core.exception.MyRuntimeException;
import com.tourism.common.dbutil.provider.DataSourceProvider;
import com.tourism.common.dbutil.util.DataSourceUtil;
import com.tourism.common.report.model.ReportDblink;
import com.tourism.common.report.service.ReportDblinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 报表打印模块动态加载的数据源工具类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Slf4j
@Component
public class ReportDataSourceUtil extends DataSourceUtil {

    @Autowired
    private ReportDblinkService dblinkService;

    @Override
    protected int getDblinkTypeByDblinkId(Long dblinkId) {
        DataSourceProvider provider = this.dblinkProviderMap.get(dblinkId);
        if (provider != null) {
            return provider.getDblinkType();
        }
        ReportDblink dblink = dblinkService.getById(dblinkId);
        if (dblink == null) {
            throw new MyRuntimeException("Report DblinkId [" + dblinkId + "] doesn't exist!");
        }
        this.dblinkProviderMap.put(dblinkId, this.getProvider(dblink.getDblinkType()));
        return dblink.getDblinkType();
    }

    @Override
    protected String getDblinkConfigurationByDblinkId(Long dblinkId) {
        ReportDblink dblink = dblinkService.getById(dblinkId);
        if (dblink == null) {
            throw new MyRuntimeException("Report DblinkId [" + dblinkId + "] doesn't exist!");
        }
        return dblink.getConfiguration();
    }
}
