package util;

import java.sql.*;
import java.time.LocalDateTime;
import model.InboundRecord;
import model.OutboundRecord;
import model.OperationLog;

public class DatabaseUtil {
    private static final String URL = "jdbc:sqlite:db/dbuml.db3";

    // 记录入库操作
    public static void recordInbound(InboundRecord record) {
        try (Connection conn = DriverManager.getConnection(URL)) {
            String sql = "INSERT INTO warehouse (id, goods_id, goods_name, quantity, operation_type, " +
                        "operator, operation_time, supplier, remark) " +
                        "VALUES (?, ?, ?, ?, 'IN', ?, ?, ?, ?)";
            
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, record.getId());
                pstmt.setString(2, record.getGoodsId());
                pstmt.setString(3, ""); // 需要从Goods获取名称
                pstmt.setInt(4, record.getQuantity());
                pstmt.setString(5, record.getOperator());
                pstmt.setString(6, record.getInboundTime().toString());
                pstmt.setString(7, record.getSupplier());
                pstmt.setString(8, record.getRemark());
                
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 记录出库操作
    public static void recordOutbound(OutboundRecord record) {
        try (Connection conn = DriverManager.getConnection(URL)) {
            String sql = "INSERT INTO warehouse (id, goods_id, goods_name, quantity, operation_type, " +
                        "operator, operation_time, customer, remark) " +
                        "VALUES (?, ?, ?, ?, 'OUT', ?, ?, ?, ?)";
            
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, record.getId());
                pstmt.setString(2, record.getGoodsId());
                pstmt.setString(3, ""); // 需要从Goods获取名称
                pstmt.setInt(4, record.getQuantity());
                pstmt.setString(5, record.getOperator());
                pstmt.setString(6, record.getOutboundTime().toString());
                pstmt.setString(7, record.getCustomer());
                pstmt.setString(8, record.getRemark());
                
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 记录操作日志
    public static void recordOperationLog(OperationLog log) {
        try (Connection conn = DriverManager.getConnection(URL)) {
            String sql = "INSERT INTO operation_logs (id, operation_type, operation_target, " +
                        "operator, operation_time, details) " +
                        "VALUES (?, ?, ?, ?, ?, ?)";
            
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, log.getId());
                pstmt.setString(2, log.getOperationType());
                pstmt.setString(3, log.getOperationTarget());
                pstmt.setString(4, log.getOperator());
                pstmt.setString(5, log.getOperationTime().toString());
                pstmt.setString(6, log.getDetails());
                
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
} 