import java.sql.*;

import org.junit.Test;

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 inbound_records (" +
                    "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 createWarehouseTable() {
        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," +
                    "goods_name TEXT NOT NULL," +
                    "quantity INTEGER NOT NULL," +
                    "operation_type TEXT NOT NULL," +
                    "operator TEXT," +
                    "operation_time TIMESTAMP," +
                    "supplier TEXT," +
                    "customer 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 表名";
            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 表名";

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

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



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

            // 创建货物信息表
            String sql = "CREATE TABLE IF NOT EXISTS goods (" +
                    "id TEXT PRIMARY KEY NOT NULL," +
                    "name TEXT NOT NULL," +
                    "specification TEXT," +
                    "unit TEXT," +
                    "price REAL," +
                    "category TEXT," +
                    "quantity INTEGER," +
                    "remark TEXT," +
                    "inbound_time TIMESTAMP," +
                    "operator TEXT," +
                    "supplier TEXT" +
                    ");";

            stmt.execute(sql);
            System.out.println("货物信息表创建成功");

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

}