-- 建材销售管家数据库初始化脚本
-- PostgreSQL

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

-- 2. 商品分类表
CREATE TABLE "categories" (
  "category_id" UUID PRIMARY KEY,
  "name" VARCHAR(50) NOT NULL,
  "parent_id" VARCHAR(32) DEFAULT '0',
  "sort_order" INTEGER DEFAULT 0,
  "icon" VARCHAR(255) DEFAULT NULL,
  "status" INTEGER DEFAULT 1,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 默认分类数据
INSERT INTO "categories" ("category_id", "name", "parent_id", "sort_order") VALUES
('CAT001', '五金建材', '0', 1),
('CAT002', '板材', '0', 2),
('CAT003', '木门', '0', 3),
('CAT004', '地板', '0', 4);

-- 3. 商品表
CREATE TABLE "products" (
  "product_id" UUID PRIMARY KEY,
  "category_id" VARCHAR(32) NOT NULL,
  "name" VARCHAR(100) NOT NULL,
  "spec" VARCHAR(100) DEFAULT NULL,
  "unit" VARCHAR(20) DEFAULT '个',
  "price" DECIMAL(10,2) NOT NULL DEFAULT 0,
  "cost_price" DECIMAL(10,2) DEFAULT 0,
  "image_url" VARCHAR(500) DEFAULT NULL,
  "barcode" VARCHAR(50) DEFAULT NULL,
  "stock_alert" INTEGER DEFAULT 10,
  "description" TEXT DEFAULT NULL,
  "status" INTEGER DEFAULT 1,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 4. 客户表
CREATE TABLE "customers" (
  "customer_id" UUID PRIMARY KEY,
  "name" VARCHAR(100) NOT NULL,
  "phone" VARCHAR(20) DEFAULT NULL,
  "wechat_openid" VARCHAR(64) DEFAULT NULL,
  "address" VARCHAR(255) DEFAULT NULL,
  "remark" TEXT DEFAULT NULL,
  "total_amount" DECIMAL(12,2) DEFAULT 0,
  "created_by" VARCHAR(32) DEFAULT NULL,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

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

-- 6. 订单明细表
CREATE TABLE "order_items" (
  "item_id" UUID PRIMARY KEY,
  "order_id" VARCHAR(32) NOT NULL,
  "product_id" VARCHAR(32) NOT NULL,
  "product_name" VARCHAR(100) NOT NULL,
  "product_spec" VARCHAR(100) DEFAULT NULL,
  "unit" VARCHAR(20) DEFAULT NULL,
  "price" DECIMAL(10,2) NOT NULL,
  "quantity" INTEGER NOT NULL DEFAULT 1,
  "subtotal" DECIMAL(12,2) NOT NULL,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 7. 库存表
CREATE TABLE "stock" (
  "stock_id" UUID PRIMARY KEY,
  "product_id" VARCHAR(32) NOT NULL,
  "warehouse_id" VARCHAR(32) DEFAULT 'WH001',
  "quantity" INTEGER NOT NULL DEFAULT 0,
  "locked_quantity" INTEGER DEFAULT 0,
  "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE ("product_id", "warehouse_id")
);

-- 8. 库存流水表
CREATE TABLE "stock_flow" (
  "flow_id" UUID PRIMARY KEY,
  "product_id" VARCHAR(32) NOT NULL,
  "type" INTEGER NOT NULL,
  "quantity" INTEGER NOT NULL,
  "before_quantity" INTEGER NOT NULL,
  "after_quantity" INTEGER NOT NULL,
  "related_id" VARCHAR(32) DEFAULT NULL,
  "related_type" VARCHAR(20) DEFAULT NULL,
  "operator_id" VARCHAR(32) NOT NULL,
  "remark" VARCHAR(255) DEFAULT NULL,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 9. 仓库表
CREATE TABLE "warehouses" (
  "warehouse_id" UUID PRIMARY KEY,
  "name" VARCHAR(50) NOT NULL,
  "address" VARCHAR(255) DEFAULT NULL,
  "remark" VARCHAR(255) DEFAULT NULL,
  "status" INTEGER DEFAULT 1,
  "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 默认仓库
INSERT INTO "warehouses" ("warehouse_id", "name", "address") VALUES
('WH001', '主仓库', '默认仓库');

-- 创建索引
CREATE INDEX idx_products_category ON "products"("category_id");
CREATE INDEX idx_products_status ON "products"("status");
CREATE INDEX idx_orders_customer ON "orders"("customer_id");
CREATE INDEX idx_orders_status ON "orders"("status");
CREATE INDEX idx_orders_created ON "orders"("created_at");
CREATE INDEX idx_order_items_order ON "order_items"("order_id");
CREATE INDEX idx_stock_product ON "stock"("product_id");
CREATE INDEX idx_stock_flow_product ON "stock_flow"("product_id");
CREATE INDEX idx_stock_flow_created ON "stock_flow"("created_at");

-- 创建测试用户 (密码: admin123)
INSERT INTO "users" ("user_id", "username", "phone", "password", "role")
VALUES ('USER001', '管理员', '13800138000', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', 'admin');
