package com.ruoyi.quartz.util;

import com.ruoyi.quartz.domain.c_rb_product_price;
import com.ruoyi.quartz.domain.c_rb_productbase_price;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

@Component
public class JDBCBatchInsert {

    private static String databaseURL;
    public static String getDatabaseURL() {
        return databaseURL;
    }

    @Value(value = "${spring.datasource.druid.quot.url}")
    public void setDatabaseUR(String databaseURL) {
        JDBCBatchInsert.databaseURL = databaseURL;
    }

    private static String username;
    public static String getUsername() {return username;}

    @Value(value = "${spring.datasource.druid.quot.username}")
    public void setUsername(String username) {
        JDBCBatchInsert.username = username;
    }

    private static String password;
    public static String getPassword() {
        return password;
    }

    @Value(value = "${spring.datasource.druid.quot.password}")
    public void setPassword(String password) {
        JDBCBatchInsert.password = password;
    }

    public static void insertRbProductPrice(List<c_rb_product_price> list) throws IOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DriverManager.getConnection(databaseURL, username, password);
            // 关闭自动提交事务，改为手动提交
            connection.setAutoCommit(false);
            System.out.println("===== 开始插入数据 =====");
            long startTime = System.currentTimeMillis();
            String sqlInsert = "INSERT INTO c_rb_product_price ( material_xingh, material_guig, material_diany, material_rb_price) VALUES ( ?, ?, ?, ?)";
            preparedStatement = connection.prepareStatement(sqlInsert);

            Random random = new Random();
            for (int i = 0; i < list.size(); i++) {
                preparedStatement.setString(1, list.get(i).getMaterial_xingh());
                preparedStatement.setString(2, list.get(i).getMaterial_guig());
                preparedStatement.setString(3, list.get(i).getMaterial_diany());
                preparedStatement.setString(4, list.get(i).getMaterial_rb_price());
                // 添加到批处理中
                preparedStatement.addBatch();

                if (i % 10000 == 0) {
                    // 每1000条数据提交一次
                    preparedStatement.executeBatch();
                    connection.commit();
                    System.out.println("成功插入第 "+ i+" 条数据");
                }

            }
            // 处理剩余的数据
            preparedStatement.executeBatch();
            connection.commit();
            long spendTime = System.currentTimeMillis()-startTime;
            System.out.println("成功插入"+ list.size()+" 条数据,耗时："+spendTime+"毫秒");
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void insertRbProductBasePrice(List<c_rb_productbase_price> list) throws IOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DriverManager.getConnection(databaseURL, username, password);
            // 关闭自动提交事务，改为手动提交
            connection.setAutoCommit(false);
            System.out.println("===== 开始插入数据 =====");
            long startTime = System.currentTimeMillis();
            String sqlInsert = "INSERT INTO c_rb_productbase_price ( material_xingh, material_guig, material_diany, material_cb_price) VALUES ( ?, ?, ?, ?)";
            preparedStatement = connection.prepareStatement(sqlInsert);

            Random random = new Random();
            for (int i = 0; i < list.size(); i++) {
                preparedStatement.setString(1, list.get(i).getMaterial_xingh());
                preparedStatement.setString(2, list.get(i).getMaterial_guig());
                preparedStatement.setString(3, list.get(i).getMaterial_diany());
                preparedStatement.setString(4, list.get(i).getMaterial_cb_price());
                // 添加到批处理中
                preparedStatement.addBatch();

                if (i % 10000 == 0) {
                    // 每1000条数据提交一次
                    preparedStatement.executeBatch();
                    connection.commit();
                    System.out.println("成功插入第 "+ i+" 条数据");
                }

            }
            // 处理剩余的数据
            preparedStatement.executeBatch();
            connection.commit();
            long spendTime = System.currentTimeMillis()-startTime;
            System.out.println("成功插入"+ list.size()+" 条数据,耗时："+spendTime+"毫秒");
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
