package com.muyu.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.muyu.common.system.domain.SysUser;
import com.muyu.system.service.DataBaseUpd;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;

import java.sql.*;

/**
 * @Author：weiran
 * @Package：com.muyu.system.service.impl
 * @Project：cloud-server-8
 * @name：DataBaseUpdImpl
 * @Date：2024/10/6 15:07
 */
@Service
public class DataBaseUpdImpl implements DataBaseUpd {
    private static final String DB_URL="jdbc:mysql://159.75.188.178:3306?useSSL=false";
    private static final String USER="root";
    private static final String PASS="bwie-8666";


    @Override
    public void updDatbase(SysUser user) {

        Connection conn=null;
        PreparedStatement preparedStatement=null;
        try {
            //连接数据库
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            Integer firmId = user.getFirmId();
            String databaseName = user.getDatabaseName();
            // 构建 SQL 更新语句
            String sql = "UPDATE " + databaseName + ".sys_user SET firm_id = ? WHERE user_id = 1";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, firmId);
            // 执行更新操作
            int rowsAffected = preparedStatement.executeUpdate();
            // 可以检查 rowsAffected 来确认是否更新了预期的行数


        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("数据库修改失败");
        }finally {
            try {
                if (preparedStatement!=null){
                    preparedStatement.close();
                }
                if (conn!=null){
                    conn.close();
                }
            }catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
