CREATE TABLE IF NOT EXISTS "wms_warehouse" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "code" varchar(20) NOT NULL,
    "name" varchar(50) NOT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "sort" int DEFAULT '0',
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 仓库表';

CREATE TABLE IF NOT EXISTS "wms_merchant" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "code" varchar(20) NOT NULL,
    "name" varchar(60) NOT NULL,
    "type" tinyint NOT NULL,
    "level" varchar(10) DEFAULT NULL,
    "bank_name" varchar(255) DEFAULT NULL,
    "bank_account" varchar(40) DEFAULT NULL,
    "address" varchar(200) DEFAULT NULL,
    "mobile" varchar(13) DEFAULT NULL,
    "telephone" varchar(13) DEFAULT NULL,
    "contact" varchar(30) DEFAULT NULL,
    "email" varchar(50) DEFAULT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 往来企业表';

CREATE TABLE IF NOT EXISTS "wms_item_brand" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "code" varchar(20) NOT NULL,
    "name" varchar(30) NOT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 商品品牌表';

CREATE TABLE IF NOT EXISTS "wms_item_category" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "parent_id" bigint NOT NULL DEFAULT '0',
    "code" varchar(20) NOT NULL,
    "name" varchar(30) NOT NULL,
    "sort" int DEFAULT '0',
    "status" tinyint NOT NULL DEFAULT '1',
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 商品分类表';

CREATE TABLE IF NOT EXISTS "wms_item" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "code" varchar(20) DEFAULT NULL,
    "name" varchar(60) NOT NULL,
    "category_id" bigint NOT NULL,
    "unit" varchar(20) DEFAULT NULL,
    "brand_id" bigint DEFAULT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 商品表';

CREATE TABLE IF NOT EXISTS "wms_item_sku" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "name" varchar(255) NOT NULL,
    "item_id" bigint NOT NULL,
    "bar_code" varchar(64) DEFAULT NULL,
    "code" varchar(64) DEFAULT NULL,
    "length" decimal(10, 1) DEFAULT NULL,
    "width" decimal(10, 1) DEFAULT NULL,
    "height" decimal(10, 1) DEFAULT NULL,
    "gross_weight" decimal(10, 3) DEFAULT NULL,
    "net_weight" decimal(10, 3) DEFAULT NULL,
    "cost_price" decimal(16, 2) DEFAULT NULL,
    "selling_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 商品 SKU 表';

CREATE TABLE IF NOT EXISTS "wms_inventory" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "sku_id" bigint NOT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "remark" varchar(255) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id"),
    CONSTRAINT "uk_sku_id_warehouse_id" UNIQUE ("sku_id", "warehouse_id")
) COMMENT 'WMS 库存表';

CREATE TABLE IF NOT EXISTS "wms_inventory_history" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "sku_id" bigint NOT NULL,
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "before_quantity" decimal(20, 2) DEFAULT NULL,
    "after_quantity" decimal(20, 2) DEFAULT NULL,
    "batch_no" varchar(64) DEFAULT NULL,
    "production_date" timestamp DEFAULT NULL,
    "expiration_date" timestamp DEFAULT NULL,
    "price" decimal(16, 2) DEFAULT NULL,
    "total_price" decimal(16, 2) DEFAULT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "order_id" bigint DEFAULT NULL,
    "order_no" varchar(64) DEFAULT NULL,
    "order_type" int DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 库存流水表';

CREATE TABLE IF NOT EXISTS "wms_receipt_order" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "no" varchar(64) NOT NULL,
    "type" int NOT NULL,
    "order_time" timestamp NOT NULL,
    "status" int NOT NULL DEFAULT '0',
    "biz_order_no" varchar(64) DEFAULT NULL,
    "merchant_id" bigint DEFAULT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "total_quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 入库单表';

CREATE TABLE IF NOT EXISTS "wms_receipt_order_detail" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "order_id" bigint NOT NULL,
    "sku_id" bigint NOT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "batch_no" varchar(64) DEFAULT NULL,
    "production_date" timestamp DEFAULT NULL,
    "expiration_date" timestamp DEFAULT NULL,
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "price" decimal(16, 2) DEFAULT NULL,
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 入库单明细表';

CREATE TABLE IF NOT EXISTS "wms_shipment_order" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "no" varchar(64) NOT NULL,
    "type" int NOT NULL,
    "order_time" timestamp NOT NULL,
    "status" int NOT NULL DEFAULT '0',
    "biz_order_no" varchar(64) DEFAULT NULL,
    "merchant_id" bigint DEFAULT NULL,
    "remark" varchar(255) DEFAULT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "total_quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 出库单表';

CREATE TABLE IF NOT EXISTS "wms_shipment_order_detail" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "order_id" bigint NOT NULL,
    "sku_id" bigint NOT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "inventory_detail_id" bigint DEFAULT NULL,
    "batch_no" varchar(64) DEFAULT NULL,
    "production_date" timestamp DEFAULT NULL,
    "expiration_date" timestamp DEFAULT NULL,
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "price" decimal(16, 2) DEFAULT NULL,
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 出库单明细表';

CREATE TABLE IF NOT EXISTS "wms_movement_order" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "no" varchar(64) NOT NULL,
    "order_time" timestamp NOT NULL,
    "status" int NOT NULL DEFAULT '0',
    "remark" varchar(255) DEFAULT NULL,
    "source_warehouse_id" bigint NOT NULL,
    "target_warehouse_id" bigint NOT NULL,
    "total_quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 移库单表';

CREATE TABLE IF NOT EXISTS "wms_movement_order_detail" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "order_id" bigint NOT NULL,
    "sku_id" bigint NOT NULL,
    "source_warehouse_id" bigint NOT NULL,
    "target_warehouse_id" bigint NOT NULL,
    "inventory_detail_id" bigint DEFAULT NULL,
    "batch_no" varchar(64) DEFAULT NULL,
    "production_date" timestamp DEFAULT NULL,
    "expiration_date" timestamp DEFAULT NULL,
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "price" decimal(16, 2) DEFAULT NULL,
    "total_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 移库单明细表';

CREATE TABLE IF NOT EXISTS "wms_check_order" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "no" varchar(64) NOT NULL,
    "order_time" timestamp NOT NULL,
    "status" int NOT NULL DEFAULT '0',
    "remark" varchar(255) DEFAULT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "total_quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "total_price" decimal(16, 2) DEFAULT NULL,
    "actual_price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 盘库单表';

CREATE TABLE IF NOT EXISTS "wms_check_order_detail" (
    "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
    "order_id" bigint NOT NULL,
    "sku_id" bigint NOT NULL,
    "warehouse_id" bigint NOT NULL,
    "area_id" bigint NOT NULL DEFAULT '0',
    "inventory_id" bigint DEFAULT NULL,
    "inventory_detail_id" bigint DEFAULT NULL,
    "batch_no" varchar(64) DEFAULT NULL,
    "production_date" timestamp DEFAULT NULL,
    "expiration_date" timestamp DEFAULT NULL,
    "receipt_time" timestamp DEFAULT NULL,
    "quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "check_quantity" decimal(20, 2) NOT NULL DEFAULT '0',
    "price" decimal(16, 2) DEFAULT NULL,
    "creator" varchar(64) DEFAULT '',
    "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updater" varchar(64) DEFAULT '',
    "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted" bit NOT NULL DEFAULT FALSE,
    "tenant_id" bigint NOT NULL DEFAULT '0',
    PRIMARY KEY ("id")
) COMMENT 'WMS 盘库单明细表';
