package com.xjrsoft.config;

import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.utils.DatasourceUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author: tzx
 * @Date: 2022/5/20 15:56
 */
@Configuration
@ConfigurationProperties("xjrsoft.ureport")
@Data
public class UReportConfig implements BuildinDatasource {

    private final static String DEFAULT_NAME = "默认数据源";


    @Override
    public String name() {
        return DEFAULT_NAME;
    }



    @Override
    public Connection getConnection() {
        try {
            return DatasourceUtil.getDatasourceMaster().getConnection();
        } catch (SQLException e) {
            throw new MyException("UReport获取数据源失败！");
        }
    }


//    @Bean
//    public ServletRegistrationBean<DocumentUreportServlet> buildUReportServlet() {
//        return new ServletRegistrationBean<>(new DocumentUreportServlet(), "/ureport/*");
//    }

    @Bean
    public ServletRegistrationBean<UReportServlet> buildUReportServlet() {
        return new ServletRegistrationBean<>(new UReportServlet(), "/ureport/*");
    }

}
