package com.muyu.auth.service;

import com.muyu.auth.form.Firm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;


@Component
public class SysFirmService {

    static final String USER="root";
    static final String PASSWORD="Lw030106";

    @Autowired
    private RedisTemplate redisTemplate;


    public Firm findFirmByName(String firmName){
        Firm firm = new Firm();
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            Connection connection= DriverManager.getConnection("jdbc:mysql://47.101.53.251:3306/datasource?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",USER,PASSWORD);
            String sql="select * from `datasource` where firm_name = '"+firmName+"'";

            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(sql);


            while (rs.next()){
                firm.setId(rs.getInt("id"));
                firm.setFirmName(rs.getString("firm_name"));
                firm.setDatabaseName(rs.getString("database_name"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //数据源不为空
        if (firm!=null){
            redisTemplate.opsForValue().set("datasource",firm.getDatabaseName());
        }
        return firm;
    };



}
