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

-- 用户表
CREATE TABLE 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',  -- admin, sales, customer
    status INTEGER NOT NULL DEFAULT 1,  -- 0:禁用, 1:启用
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 客户表
CREATE TABLE 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 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,  -- 0:禁用, 1:启用
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- 商品表
CREATE TABLE 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,  -- 0:下架, 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 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,  -- 1:已完成, 2:待付款, 3:已取消
    payment_method VARCHAR(20),  -- cash, wechat, alipay
    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 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 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 stock_flow (
    flow_id VARCHAR(36) PRIMARY KEY,
    product_id VARCHAR(36) NOT NULL,
    type INTEGER NOT NULL,  -- 1:入库, 2:出库, 3:调整
    quantity INTEGER NOT NULL,
    before_quantity INTEGER,
    after_quantity INTEGER,
    related_id VARCHAR(36),  -- 关联单据ID
    related_type VARCHAR(20),  -- order, manual
    operator_id VARCHAR(36),
    remark TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (product_id) REFERENCES products(product_id)
);

-- 系统配置表
CREATE TABLE 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
);

-- 初始化管理员账号 (密码: admin123)
INSERT INTO users (user_id, username, phone, password, role, status) 
VALUES ('admin-001', 'admin', '13800138000', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', 'admin', 1);

-- 初始化系统配置
INSERT INTO system_config (config_id, config_key, config_value, remark) VALUES
('config-001', 'order_view_days', '180', '顾客可查看订单天数'),
('config-002', 'default_discount_rate', '100', '默认折扣率');
