-- 建材销售管家数据库表结构
-- 创建数据库: create database building_materials;

-- 用户表
CREATE TABLE IF NOT EXISTS users (
    user_id VARCHAR(36) PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    phone VARCHAR(20),
    password VARCHAR(255) NOT NULL,
    wechat_openid VARCHAR(128),
    wechat_unionid VARCHAR(128),
    alipay_openid VARCHAR(128),
    role VARCHAR(20) NOT NULL DEFAULT 'sales',
    status INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 客户表
CREATE TABLE IF NOT EXISTS customers (
    customer_id VARCHAR(36) PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    phone VARCHAR(20),
    wechat_openid VARCHAR(128),
    wechat_nickname VARCHAR(100),
    wechat_avatar VARCHAR(500),
    address VARCHAR(500),
    remark TEXT,
    total_amount DECIMAL(12,2) DEFAULT 0,
    created_by VARCHAR(36),
    last_login_at TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 商品分类表
CREATE TABLE IF NOT EXISTS categories (
    category_id VARCHAR(36) PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    parent_id VARCHAR(36),
    sort_order INTEGER DEFAULT 0,
    icon VARCHAR(255),
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 商品表
CREATE TABLE IF NOT EXISTS products (
    product_id VARCHAR(36) PRIMARY KEY,
    category_id VARCHAR(36),
    name VARCHAR(100) NOT NULL,
    spec VARCHAR(100),
    unit VARCHAR(20) DEFAULT '个',
    price DECIMAL(10,2) NOT NULL,
    cost_price DECIMAL(10,2),
    image_url VARCHAR(500),
    barcode VARCHAR(50),
    stock_alert INTEGER DEFAULT 10,
    description TEXT,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES categories(category_id)
);

-- 订单表
CREATE TABLE IF NOT EXISTS orders (
    order_id VARCHAR(36) PRIMARY KEY,
    order_no VARCHAR(32) NOT NULL UNIQUE,
    customer_id VARCHAR(36),
    customer_name VARCHAR(100),
    customer_phone VARCHAR(20),
    customer_wechat VARCHAR(128),
    total_amount DECIMAL(12,2) NOT NULL,
    discount_amount DECIMAL(12,2) DEFAULT 0,
    actual_amount DECIMAL(12,2) NOT NULL,
    discount_rate DECIMAL(5,2) DEFAULT 100,
    status INTEGER NOT NULL DEFAULT 1,
    payment_method VARCHAR(20),
    remark TEXT,
    operator_id VARCHAR(36),
    operator_name VARCHAR(50),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

-- 订单明细表
CREATE TABLE IF NOT EXISTS order_items (
    item_id VARCHAR(36) PRIMARY KEY,
    order_id VARCHAR(36) NOT NULL,
    product_id VARCHAR(36) NOT NULL,
    product_name VARCHAR(100),
    product_spec VARCHAR(100),
    unit VARCHAR(20),
    price DECIMAL(10,2) NOT NULL,
    quantity INTEGER NOT NULL,
    subtotal DECIMAL(12,2) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (order_id) REFERENCES orders(order_id),
    FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- 库存表
CREATE TABLE IF NOT EXISTS stock (
    stock_id VARCHAR(36) PRIMARY KEY,
    product_id VARCHAR(36) NOT NULL UNIQUE,
    warehouse_id VARCHAR(36),
    quantity INTEGER NOT NULL DEFAULT 0,
    locked_quantity INTEGER DEFAULT 0,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- 库存流水表
CREATE TABLE IF NOT EXISTS stock_flow (
    flow_id VARCHAR(36) PRIMARY KEY,
    product_id VARCHAR(36) NOT NULL,
    type INTEGER NOT NULL,
    quantity INTEGER NOT NULL,
    before_quantity INTEGER,
    after_quantity INTEGER,
    related_id VARCHAR(36),
    related_type VARCHAR(20),
    operator_id VARCHAR(36),
    remark TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- 系统配置表
CREATE TABLE IF NOT EXISTS system_config (
    config_id VARCHAR(36) PRIMARY KEY,
    config_key VARCHAR(50) NOT NULL UNIQUE,
    config_value TEXT,
    remark VARCHAR(200),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

