import java.sql.*;

import org.junit.Test;

import java.sql.*;
import java.time.LocalDateTime;

import org.junit.Test;

import java.util.Random;

public class DML {

    // 使用相对路径
    private static final String URL = "jdbc:sqlite:db/dbuml.db3";



    @Test
    public void createInboundTable() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {


            String sql = "CREATE TABLE IF NOT EXISTS warehouse (" +
                    "id TEXT PRIMARY KEY NOT NULL," +
                    "goods_id TEXT NOT NULL," +
                    "quantity INTEGER NOT NULL," +
                    "supplier TEXT," +
                    "inbound_time TIMESTAMP," +
                    "operator TEXT," +
                    "remark TEXT" +
                    ");";

            stmt.execute(sql);
            System.out.println("仓库数据表创建成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Test
    public void createLogTable() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 创建操作日志数据表
            String sql = "CREATE TABLE IF NOT EXISTS operation_logs (" +
                    "id TEXT PRIMARY KEY NOT NULL," +   //日志id
                    "operation_type TEXT NOT NULL," +  // 操作类型（入库、出库、修改等）
                    "operation_target TEXT NOT NULL," + // 操作对象（货物ID或名称）
                    "operator TEXT," +                 // 操作人
                    "operation_time TIMESTAMP," +      // 操作时间
                    "details TEXT" +                   // 操作详情
                    ");";

            stmt.execute(sql);
            System.out.println("日志数据表创建成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Test
    public void clearTable() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            String sql = "DELETE FROM warehouse";
            int result = stmt.executeUpdate(sql);
            System.out.println("已清空数据表，删除了 " + result + " 条记录");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }  //清空指定数据表数
    @Test
    public void dropTable() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 删除表的SQL 语句
            String sql = "DROP TABLE IF EXISTS inbound_records";

            // 执行 SQL 语句
            stmt.execute(sql);
            System.out.println(" 表删除成功");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }  //删除指定数据表

    @Test
    public void showCurrentStock() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 查询每个商品的当前库存（入库减去出库）
            String sql = "SELECT goods_id, SUM(quantity) as current_stock " +
                        "FROM warehouse " +
                        "GROUP BY goods_id";
            ResultSet rs = stmt.executeQuery(sql);

            // 打印表头
            System.out.println("===================== 当前库存状态 =====================");
            System.out.printf("%-20s %-15s%n", "货物名称", "当前库存");
            System.out.println("===================================================");

            // 打印数据
            while (rs.next()) {
                System.out.printf("%-20s %-15d%n",
                        rs.getString("goods_id"),
                        rs.getInt("current_stock")
                );
            }
            System.out.println("===================================================");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void showAllTransactions() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 查询所有出入库记录
            String sql = "SELECT *, " +
                        "CASE WHEN quantity > 0 THEN '入库' ELSE '出库' END as type " +
                        "FROM warehouse " +
                        "ORDER BY inbound_time DESC";
            ResultSet rs = stmt.executeQuery(sql);

            // 打印表头
            System.out.println("===================== 出入库记录 =====================");
            System.out.printf("%-15s %-15s %-8s %-8s %-15s %-25s%n",
                    "单号", "货物名称", "数量", "类型", "供应商", "时间");
            System.out.println("===================================================");

            // 打印数据
            while (rs.next()) {
                System.out.printf("%-15s %-15s %-8d %-8s %-15s %-25s%n",
                        rs.getString("id"),
                        rs.getString("goods_id"),
                        Math.abs(rs.getInt("quantity")),
                        rs.getString("type"),
                        rs.getString("supplier"),
                        rs.getString("inbound_time")
                );
            }
            System.out.println("===================================================");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void deleteOutboundRecord() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 删除出库记录的SQL语句
            String sql = "DELETE FROM warehouse WHERE id = ?";
            
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                // 这里可以设置要删除的记录ID
                pstmt.setString(1, "要删除的记录ID");  
                int result = pstmt.executeUpdate();
                
                if (result > 0) {
                    System.out.println("出库记录删除成功");
                } else {
                    System.out.println("未找到要删除的记录");
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除出库记录失败：" + e.getMessage());
        }
    }

    @Test
    public void createOperationLogsTable() {
        try (Connection conn = DriverManager.getConnection(URL);
             Statement stmt = conn.createStatement()) {

            // 删除已存在的表
            stmt.execute("DROP TABLE IF EXISTS operation_logs");

            // 创建新表
            String sql = "CREATE TABLE operation_logs (" +
                    "id TEXT PRIMARY KEY NOT NULL," +
                    "operation_type TEXT NOT NULL," +
                    "operation_target TEXT NOT NULL," +
                    "operator TEXT NOT NULL," +
                    "operation_time TEXT NOT NULL," +
                    "details TEXT" +
                    ")";

            stmt.execute(sql);
            System.out.println("操作日志表创建成功");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}