Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.
Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
fa1b286463
241 files changed
+31188
No files matched your search
@@ -0,0 +1,220 @@
|
||||
using FluentMigrator;
|
||||
using FluentMigrator.Builders.Create.Table;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(1, "Create current WmsLite schema baseline")]
|
||||
public class CreateCurrentSchema0001 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
CreateWarehouseTable();
|
||||
CreateZoneTable();
|
||||
CreateLocationTable();
|
||||
CreateMaterialTable();
|
||||
CreateContainerTable();
|
||||
CreateInventoryTable();
|
||||
CreateInventoryTransactionTable();
|
||||
CreateSysTemplateTable();
|
||||
CreateHttpAccessLogTable();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
Delete.Table("http_access_log");
|
||||
Delete.Table("sys_template");
|
||||
Delete.Table("inventory_transaction");
|
||||
Delete.Table("inventory");
|
||||
Delete.Table("container");
|
||||
Delete.Table("material");
|
||||
Delete.Table("location");
|
||||
Delete.Table("zone");
|
||||
Delete.Table("warehouse");
|
||||
}
|
||||
|
||||
private void CreateWarehouseTable()
|
||||
{
|
||||
if (Schema.Table("warehouse").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("warehouse")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateZoneTable()
|
||||
{
|
||||
if (Schema.Table("zone").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("zone")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateLocationTable()
|
||||
{
|
||||
if (Schema.Table("location").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("location")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).Nullable()
|
||||
.WithColumn("row_no").AsInt32().Nullable()
|
||||
.WithColumn("column_no").AsInt32().Nullable()
|
||||
.WithColumn("layer_no").AsInt32().Nullable()
|
||||
.WithColumn("height").AsDecimal().Nullable()
|
||||
.WithColumn("location_status").AsInt32().NotNullable()
|
||||
.WithColumn("is_outer").AsBoolean().NotNullable()
|
||||
.WithColumn("container_code").AsString(64).Nullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateMaterialTable()
|
||||
{
|
||||
if (Schema.Table("material").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("material")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).NotNullable()
|
||||
.WithColumn("spec").AsString(512).Nullable()
|
||||
.WithColumn("unit").AsString(32).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateContainerTable()
|
||||
{
|
||||
if (Schema.Table("container").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("container")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).Nullable()
|
||||
.WithColumn("container_type").AsString(64).Nullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateInventoryTable()
|
||||
{
|
||||
if (Schema.Table("inventory").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("inventory")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("inventory_kind").AsInt32().NotNullable()
|
||||
.WithColumn("material_code").AsString(64).Nullable()
|
||||
.WithColumn("container_code").AsString(64).Nullable()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("location_code").AsString(64).NotNullable()
|
||||
.WithColumn("qty").AsDecimal().NotNullable()
|
||||
.WithColumn("pending_inbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("pending_outbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("frozen_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("inventory_status").AsInt32().NotNullable()
|
||||
.WithColumn("unit").AsString(32).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateInventoryTransactionTable()
|
||||
{
|
||||
if (Schema.Table("inventory_transaction").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("inventory_transaction")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("inventory_id").AsInt64().NotNullable()
|
||||
.WithColumn("movement_type").AsInt32().NotNullable()
|
||||
.WithColumn("inventory_status").AsInt32().NotNullable()
|
||||
.WithColumn("biz_no").AsString(128).Nullable()
|
||||
.WithColumn("before_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("after_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("before_pending_inbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("after_pending_inbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("before_pending_outbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("after_pending_outbound_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("before_frozen_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("after_frozen_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("change_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateSysTemplateTable()
|
||||
{
|
||||
if (Schema.Table("sys_template").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("sys_template")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("biz_type").AsString(64).NotNullable()
|
||||
.WithColumn("biz_name").AsString(128).NotNullable()
|
||||
.WithColumn("config_json").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateHttpAccessLogTable()
|
||||
{
|
||||
if (Schema.Table("http_access_log").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("http_access_log")
|
||||
.WithColumn("id").AsInt64().PrimaryKey().NotNullable()
|
||||
.WithColumn("trace_id").AsString(64).NotNullable()
|
||||
.WithColumn("http_method").AsString(16).NotNullable()
|
||||
.WithColumn("path").AsString(512).NotNullable()
|
||||
.WithColumn("query_string").AsCustom("text").Nullable()
|
||||
.WithColumn("status_code").AsInt32().NotNullable()
|
||||
.WithColumn("request_body").AsCustom("text").Nullable()
|
||||
.WithColumn("response_body").AsCustom("text").Nullable()
|
||||
.WithColumn("duration_ms").AsInt32().Nullable()
|
||||
.WithColumn("client_ip").AsString(64).Nullable()
|
||||
.WithColumn("user_id").AsString(128).Nullable()
|
||||
.WithColumn("create_time").AsDateTime().Nullable();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MigrationTableExtensions
|
||||
{
|
||||
public static ICreateTableColumnOptionOrWithColumnSyntax WithBaseColumns(this ICreateTableWithColumnOrSchemaOrDescriptionSyntax table)
|
||||
{
|
||||
return table
|
||||
.WithColumn("id").AsInt64().PrimaryKey().NotNullable()
|
||||
.WithColumn("tenant_id").AsString(64).NotNullable()
|
||||
.WithColumn("owner_code").AsString(64).NotNullable()
|
||||
.WithColumn("create_time").AsDateTime().Nullable()
|
||||
.WithColumn("create_by").AsString().Nullable()
|
||||
.WithColumn("update_by").AsString().Nullable()
|
||||
.WithColumn("update_time").AsDateTime().Nullable()
|
||||
.WithColumn("is_deleted").AsBoolean().NotNullable()
|
||||
.WithColumn("delete_time").AsDateTime().Nullable()
|
||||
.WithColumn("delete_by").AsString().Nullable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(2, "Add order tables, common indexes, and seed data")]
|
||||
public class AddOrdersIndexesAndSeedData0002 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
CreateOrderTables();
|
||||
CreateIndexes();
|
||||
SeedDemoData();
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
Execute.Sql("DELETE FROM sys_template WHERE tenant_id = 'default' AND owner_code = 'default';");
|
||||
Execute.Sql("DELETE FROM location WHERE tenant_id = 'default' AND owner_code = 'default';");
|
||||
Execute.Sql("DELETE FROM zone WHERE tenant_id = 'default' AND owner_code = 'default';");
|
||||
Execute.Sql("DELETE FROM warehouse WHERE tenant_id = 'default' AND owner_code = 'default';");
|
||||
|
||||
Delete.Table("stocktake_order_line");
|
||||
Delete.Table("stocktake_order");
|
||||
Delete.Table("transfer_order_line");
|
||||
Delete.Table("transfer_order");
|
||||
Delete.Table("outbound_order_line");
|
||||
Delete.Table("outbound_order");
|
||||
Delete.Table("inbound_order_line");
|
||||
Delete.Table("inbound_order");
|
||||
}
|
||||
|
||||
private void CreateOrderTables()
|
||||
{
|
||||
CreateOrderHeader("inbound_order");
|
||||
CreateInventoryOrderLine("inbound_order_line");
|
||||
CreateOrderHeader("outbound_order");
|
||||
CreateInventoryOrderLine("outbound_order_line");
|
||||
CreateTransferOrder();
|
||||
CreateTransferOrderLine();
|
||||
CreateOrderHeader("stocktake_order");
|
||||
CreateStocktakeOrderLine();
|
||||
}
|
||||
|
||||
private void CreateOrderHeader(string tableName)
|
||||
{
|
||||
if (Schema.Table(tableName).Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table(tableName)
|
||||
.WithBaseColumns()
|
||||
.WithColumn("order_no").AsString(128).NotNullable()
|
||||
.WithColumn("biz_type").AsString(64).Nullable()
|
||||
.WithColumn("order_status").AsInt32().NotNullable()
|
||||
.WithColumn("remark").AsString(512).Nullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateInventoryOrderLine(string tableName)
|
||||
{
|
||||
if (Schema.Table(tableName).Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table(tableName)
|
||||
.WithBaseColumns()
|
||||
.WithColumn("order_id").AsInt64().NotNullable()
|
||||
.WithColumn("line_no").AsInt32().NotNullable()
|
||||
.WithColumn("inventory_kind").AsInt32().NotNullable()
|
||||
.WithColumn("material_code").AsString(64).Nullable()
|
||||
.WithColumn("container_code").AsString(64).Nullable()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("location_code").AsString(64).NotNullable()
|
||||
.WithColumn("qty").AsDecimal().NotNullable()
|
||||
.WithColumn("unit").AsString(32).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateTransferOrder()
|
||||
{
|
||||
CreateOrderHeader("transfer_order");
|
||||
}
|
||||
|
||||
private void CreateTransferOrderLine()
|
||||
{
|
||||
if (Schema.Table("transfer_order_line").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("transfer_order_line")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("order_id").AsInt64().NotNullable()
|
||||
.WithColumn("line_no").AsInt32().NotNullable()
|
||||
.WithColumn("inventory_kind").AsInt32().NotNullable()
|
||||
.WithColumn("material_code").AsString(64).Nullable()
|
||||
.WithColumn("container_code").AsString(64).Nullable()
|
||||
.WithColumn("from_warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("from_zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("from_location_code").AsString(64).NotNullable()
|
||||
.WithColumn("to_warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("to_zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("to_location_code").AsString(64).NotNullable()
|
||||
.WithColumn("qty").AsDecimal().NotNullable()
|
||||
.WithColumn("unit").AsString(32).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateStocktakeOrderLine()
|
||||
{
|
||||
if (Schema.Table("stocktake_order_line").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("stocktake_order_line")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("order_id").AsInt64().NotNullable()
|
||||
.WithColumn("line_no").AsInt32().NotNullable()
|
||||
.WithColumn("inventory_kind").AsInt32().NotNullable()
|
||||
.WithColumn("material_code").AsString(64).Nullable()
|
||||
.WithColumn("container_code").AsString(64).Nullable()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("zone_code").AsString(64).NotNullable()
|
||||
.WithColumn("location_code").AsString(64).NotNullable()
|
||||
.WithColumn("qty").AsDecimal().NotNullable()
|
||||
.WithColumn("counted_qty").AsDecimal().NotNullable()
|
||||
.WithColumn("unit").AsString(32).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void CreateIndexes()
|
||||
{
|
||||
Execute.Sql("""
|
||||
CREATE INDEX IF NOT EXISTS idx_warehouse_tenant_owner_code ON warehouse(tenant_id, owner_code, code);
|
||||
CREATE INDEX IF NOT EXISTS idx_zone_tenant_owner_warehouse_code ON zone(tenant_id, owner_code, warehouse_code, code);
|
||||
CREATE INDEX IF NOT EXISTS idx_location_tenant_owner_wzl_code ON location(tenant_id, owner_code, warehouse_code, zone_code, code);
|
||||
CREATE INDEX IF NOT EXISTS idx_material_tenant_owner_code ON material(tenant_id, owner_code, code);
|
||||
CREATE INDEX IF NOT EXISTS idx_container_tenant_owner_code ON container(tenant_id, owner_code, code);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_tenant_owner_location ON inventory(tenant_id, owner_code, warehouse_code, zone_code, location_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_tenant_owner_material ON inventory(tenant_id, owner_code, material_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_tenant_owner_container ON inventory(tenant_id, owner_code, container_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_attrs_gin ON inventory USING GIN(attrs);
|
||||
CREATE INDEX IF NOT EXISTS idx_material_attrs_gin ON material USING GIN(attrs);
|
||||
CREATE INDEX IF NOT EXISTS idx_container_attrs_gin ON container USING GIN(attrs);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_template_tenant_owner_biz_type ON sys_template(tenant_id, owner_code, biz_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbound_order_tenant_owner_order_no ON inbound_order(tenant_id, owner_code, order_no);
|
||||
CREATE INDEX IF NOT EXISTS idx_outbound_order_tenant_owner_order_no ON outbound_order(tenant_id, owner_code, order_no);
|
||||
CREATE INDEX IF NOT EXISTS idx_transfer_order_tenant_owner_order_no ON transfer_order(tenant_id, owner_code, order_no);
|
||||
CREATE INDEX IF NOT EXISTS idx_stocktake_order_tenant_owner_order_no ON stocktake_order(tenant_id, owner_code, order_no);
|
||||
""");
|
||||
}
|
||||
|
||||
private void SeedDemoData()
|
||||
{
|
||||
Execute.Sql("""
|
||||
INSERT INTO warehouse (id, tenant_id, owner_code, code, name, attrs, create_time, create_by, is_deleted)
|
||||
VALUES (900000000000000001, 'default', 'default', 'WH-DEMO', 'Demo Warehouse', '{}'::jsonb, NOW(), 'system', FALSE)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO zone (id, tenant_id, owner_code, warehouse_code, code, name, attrs, create_time, create_by, is_deleted)
|
||||
VALUES (900000000000000002, 'default', 'default', 'WH-DEMO', 'A', 'Default Zone', '{}'::jsonb, NOW(), 'system', FALSE)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO location (id, tenant_id, owner_code, warehouse_code, zone_code, code, name, location_status, is_outer, attrs, create_time, create_by, is_deleted)
|
||||
VALUES (900000000000000003, 'default', 'default', 'WH-DEMO', 'A', 'A-01-01', 'Default Location', 1, FALSE, '{}'::jsonb, NOW(), 'system', FALSE)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO sys_template (id, tenant_id, owner_code, biz_type, biz_name, config_json, create_time, create_by, is_deleted)
|
||||
VALUES
|
||||
(900000000000000101, 'default', 'default', 'battery_inventory', 'Battery Inventory', '[{"field":"batchNo","label":"Batch No","type":"text","required":true,"queryable":true},{"field":"voltage","label":"Voltage","type":"number","queryMode":"eq"},{"field":"capacity","label":"Capacity","type":"number","queryMode":"eq"}]'::jsonb, NOW(), 'system', FALSE),
|
||||
(900000000000000102, 'default', 'default', 'steel_inventory', 'Steel Inventory', '[{"field":"batchNo","label":"Batch No","type":"text","required":true,"queryable":true},{"field":"thickness","label":"Thickness","type":"number","queryMode":"eq"},{"field":"grade","label":"Grade","type":"text","queryable":true}]'::jsonb, NOW(), 'system', FALSE),
|
||||
(900000000000000103, 'default', 'default', 'food_inventory', 'Food Inventory', '[{"field":"batchNo","label":"Batch No","type":"text","required":true,"queryable":true},{"field":"productionDate","label":"Production Date","type":"date","queryable":true},{"field":"expiryDate","label":"Expiry Date","type":"date","queryable":true}]'::jsonb, NOW(), 'system', FALSE),
|
||||
(900000000000000104, 'default', 'default', 'apparel_inventory', 'Apparel Inventory', '[{"field":"sku","label":"SKU","type":"text","required":true,"queryable":true},{"field":"color","label":"Color","type":"text","queryable":true},{"field":"size","label":"Size","type":"text","queryable":true}]'::jsonb, NOW(), 'system', FALSE)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
""");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(3, "Make audit columns nullable for legacy DBs")]
|
||||
public class MakeAuditColumnsNullable0003 : Migration
|
||||
{
|
||||
private static readonly string[] BaseEntityTables =
|
||||
{
|
||||
"warehouse",
|
||||
"zone",
|
||||
"location",
|
||||
"material",
|
||||
"container",
|
||||
"inventory",
|
||||
"inventory_transaction",
|
||||
"sys_template",
|
||||
"inbound_order",
|
||||
"inbound_order_line",
|
||||
"outbound_order",
|
||||
"outbound_order_line",
|
||||
"transfer_order",
|
||||
"transfer_order_line",
|
||||
"stocktake_order",
|
||||
"stocktake_order_line"
|
||||
};
|
||||
|
||||
private static readonly string[] AuditColumns =
|
||||
{
|
||||
"create_time",
|
||||
"create_by",
|
||||
"update_by",
|
||||
"update_time",
|
||||
"delete_time",
|
||||
"delete_by"
|
||||
};
|
||||
|
||||
public override void Up()
|
||||
{
|
||||
foreach (var table in BaseEntityTables)
|
||||
{
|
||||
if (!Schema.Table(table).Exists())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var column in AuditColumns)
|
||||
{
|
||||
if (!Schema.Table(table).Column(column).Exists())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN {column} DROP NOT NULL;");
|
||||
}
|
||||
}
|
||||
|
||||
if (Schema.Table("http_access_log").Exists()
|
||||
&& Schema.Table("http_access_log").Column("create_time").Exists())
|
||||
{
|
||||
Execute.Sql("ALTER TABLE http_access_log ALTER COLUMN create_time DROP NOT NULL;");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
// Irreversible: rows may already have NULL audit values after Up().
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(4, "Normalize sys_template seed: productized config_json and Chinese biz_name")]
|
||||
public class NormalizeSysTemplateConfigSeed0004 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
Execute.Sql("""
|
||||
UPDATE sys_template
|
||||
SET biz_name = '电池行业',
|
||||
config_json = '[{"field":"batchNo","label":"批次号","type":"text","required":true,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"voltage","label":"电压","type":"number","required":false,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"capacity","label":"容量","type":"number","required":false,"queryable":true,"queryMode":"eq","defaultValue":""}]'::jsonb
|
||||
WHERE tenant_id = 'default' AND owner_code = 'default' AND biz_type = 'battery_inventory';
|
||||
|
||||
UPDATE sys_template
|
||||
SET biz_name = '钢材行业',
|
||||
config_json = '[{"field":"batchNo","label":"批次号","type":"text","required":true,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"thickness","label":"厚度","type":"decimal","required":false,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"grade","label":"钢级","type":"text","required":false,"queryable":true,"queryMode":"eq","defaultValue":""}]'::jsonb
|
||||
WHERE tenant_id = 'default' AND owner_code = 'default' AND biz_type = 'steel_inventory';
|
||||
|
||||
UPDATE sys_template
|
||||
SET biz_name = '食品行业',
|
||||
config_json = '[{"field":"batchNo","label":"批次号","type":"text","required":true,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"productionDate","label":"生产日期","type":"date","required":false,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"expiryDate","label":"到期日期","type":"date","required":false,"queryable":true,"queryMode":"eq","defaultValue":""}]'::jsonb
|
||||
WHERE tenant_id = 'default' AND owner_code = 'default' AND biz_type = 'food_inventory';
|
||||
|
||||
UPDATE sys_template
|
||||
SET biz_name = '服装行业',
|
||||
config_json = '[{"field":"sku","label":"SKU","type":"text","required":true,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"color","label":"颜色","type":"text","required":false,"queryable":true,"queryMode":"eq","defaultValue":""},{"field":"size","label":"尺码","type":"text","required":false,"queryable":true,"queryMode":"eq","defaultValue":""}]'::jsonb
|
||||
WHERE tenant_id = 'default' AND owner_code = 'default' AND biz_type = 'apparel_inventory';
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
// Irreversible content normalization; prior snapshot not restored.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(5, "Inbound order line: received qty, line status, nullable location")]
|
||||
public class InboundOrderLineReceiveFields0005 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
UpInventoryStyleLineTable("inbound_order_line");
|
||||
UpInventoryStyleLineTable("outbound_order_line");
|
||||
UpInventoryStyleLineTable("stocktake_order_line");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
DownInventoryStyleLineTable("inbound_order_line");
|
||||
DownInventoryStyleLineTable("outbound_order_line");
|
||||
DownInventoryStyleLineTable("stocktake_order_line");
|
||||
}
|
||||
|
||||
private void UpInventoryStyleLineTable(string table)
|
||||
{
|
||||
if (!Schema.Table(table).Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Schema.Table(table).Column("received_qty").Exists())
|
||||
{
|
||||
Alter.Table(table)
|
||||
.AddColumn("received_qty").AsDecimal().NotNullable().WithDefaultValue(0);
|
||||
}
|
||||
|
||||
if (!Schema.Table(table).Column("line_status").Exists())
|
||||
{
|
||||
// OrderLineStatus: Open = 1 (outbound/stocktake unused paths keep default)
|
||||
Alter.Table(table)
|
||||
.AddColumn("line_status").AsInt32().NotNullable().WithDefaultValue(1);
|
||||
}
|
||||
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN location_code DROP NOT NULL;");
|
||||
}
|
||||
|
||||
private void DownInventoryStyleLineTable(string table)
|
||||
{
|
||||
if (!Schema.Table(table).Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Schema.Table(table).Column("line_status").Exists())
|
||||
{
|
||||
Delete.Column("line_status").FromTable(table);
|
||||
}
|
||||
|
||||
if (Schema.Table(table).Column("received_qty").Exists())
|
||||
{
|
||||
Delete.Column("received_qty").FromTable(table);
|
||||
}
|
||||
|
||||
Execute.Sql($"UPDATE {table} SET location_code = '' WHERE location_code IS NULL;");
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN location_code SET NOT NULL;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
/// <summary>
|
||||
/// 修复曾用 SqlSugar CodeFirst 加列但现有行为 NULL 的库(NOT NULL 约束失败)。
|
||||
/// </summary>
|
||||
[Migration(6, "Backfill order line received_qty / line_status nulls")]
|
||||
public class BackfillOrderLineQtyAndStatus0006 : Migration
|
||||
{
|
||||
private static readonly string[] Tables = { "inbound_order_line", "outbound_order_line", "stocktake_order_line" };
|
||||
|
||||
public override void Up()
|
||||
{
|
||||
foreach (var table in Tables)
|
||||
{
|
||||
if (!Schema.Table(table).Exists())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Schema.Table(table).Column("received_qty").Exists())
|
||||
{
|
||||
Execute.Sql($"UPDATE {table} SET received_qty = 0 WHERE received_qty IS NULL;");
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN received_qty SET DEFAULT 0;");
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN received_qty SET NOT NULL;");
|
||||
}
|
||||
|
||||
if (Schema.Table(table).Column("line_status").Exists())
|
||||
{
|
||||
Execute.Sql($"UPDATE {table} SET line_status = 1 WHERE line_status IS NULL;");
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN line_status SET DEFAULT 1;");
|
||||
Execute.Sql($"ALTER TABLE {table} ALTER COLUMN line_status SET NOT NULL;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(7, "Inbound order line: unit nullable until receive")]
|
||||
public class InboundOrderLineUnitNullable0007 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (Schema.Table("inbound_order_line").Exists()
|
||||
&& Schema.Table("inbound_order_line").Column("unit").Exists())
|
||||
{
|
||||
Execute.Sql("ALTER TABLE inbound_order_line ALTER COLUMN unit DROP NOT NULL;");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
if (Schema.Table("inbound_order_line").Exists()
|
||||
&& Schema.Table("inbound_order_line").Column("unit").Exists())
|
||||
{
|
||||
Execute.Sql("UPDATE inbound_order_line SET unit = '' WHERE unit IS NULL;");
|
||||
Execute.Sql("ALTER TABLE inbound_order_line ALTER COLUMN unit SET NOT NULL;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(8, "Inventory transaction trace: material, container, location snapshot")]
|
||||
public class InventoryTransactionTraceColumns0008 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (!Schema.Table("inventory_transaction").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Schema.Table("inventory_transaction").Column("material_code").Exists())
|
||||
{
|
||||
Alter.Table("inventory_transaction")
|
||||
.AddColumn("material_code").AsString(64).Nullable();
|
||||
}
|
||||
|
||||
if (!Schema.Table("inventory_transaction").Column("container_code").Exists())
|
||||
{
|
||||
Alter.Table("inventory_transaction")
|
||||
.AddColumn("container_code").AsString(64).Nullable();
|
||||
}
|
||||
|
||||
if (!Schema.Table("inventory_transaction").Column("location_code").Exists())
|
||||
{
|
||||
Alter.Table("inventory_transaction")
|
||||
.AddColumn("location_code").AsString(64).Nullable();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
if (!Schema.Table("inventory_transaction").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory_transaction").Column("location_code").Exists())
|
||||
{
|
||||
Delete.Column("location_code").FromTable("inventory_transaction");
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory_transaction").Column("container_code").Exists())
|
||||
{
|
||||
Delete.Column("container_code").FromTable("inventory_transaction");
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory_transaction").Column("material_code").Exists())
|
||||
{
|
||||
Delete.Column("material_code").FromTable("inventory_transaction");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(9, "Sys dictionary items for localized labels")]
|
||||
public class CreateSysDictItem0009 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (!Schema.Table("sys_dict_item").Exists())
|
||||
{
|
||||
Create.Table("sys_dict_item")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("category").AsString(128).NotNullable()
|
||||
.WithColumn("code").AsString(128).NotNullable()
|
||||
.WithColumn("label").AsString(512).NotNullable()
|
||||
.WithColumn("sort_order").AsInt32().NotNullable().WithDefaultValue(0)
|
||||
.WithColumn("is_enabled").AsBoolean().NotNullable().WithDefaultValue(true);
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_sys_dict_item_scope_cat_code
|
||||
ON sys_dict_item (tenant_id, owner_code, category, code)
|
||||
WHERE is_deleted = FALSE;
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_dict_item_tenant_cat
|
||||
ON sys_dict_item (tenant_id, owner_code, category)
|
||||
WHERE is_deleted = FALSE AND is_enabled = TRUE;
|
||||
""");
|
||||
|
||||
// Platform defaults: tenants merge these with tenant-specific rows (override same category+code).
|
||||
Execute.Sql("""
|
||||
INSERT INTO sys_dict_item (id, tenant_id, owner_code, category, code, label, sort_order, is_enabled, create_time, create_by, is_deleted)
|
||||
VALUES
|
||||
(910000000000000001, 'system', 'system', 'OrderStatus', 'draft', '草稿', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000002, 'system', 'system', 'OrderStatus', 'pending', '待处理', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000003, 'system', 'system', 'OrderStatus', 'completed', '已完成', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000004, 'system', 'system', 'OrderStatus', 'cancelled', '已取消', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000011, 'system', 'system', 'InboundOrder.bizType', 'inbound_default', '默认入库', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000012, 'system', 'system', 'OutboundOrder.bizType', 'outbound_default', '默认出库', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000013, 'system', 'system', 'TransferOrder.bizType', 'transfer_default', '默认移库', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000014, 'system', 'system', 'StocktakeOrder.bizType', 'stocktake_default', '默认盘点', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000021, 'system', 'system', 'InventoryKind', 'material', '物料', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000022, 'system', 'system', 'InventoryKind', 'emptyContainer', '空容器', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000023, 'system', 'system', 'InventoryKind', '1', '物料', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000024, 'system', 'system', 'InventoryKind', '2', '空容器', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000031, 'system', 'system', 'InventoryStatus', 'available', '可用', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000032, 'system', 'system', 'InventoryStatus', 'pendingInbound', '待入', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000033, 'system', 'system', 'InventoryStatus', 'pendingOutbound', '待出', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000034, 'system', 'system', 'InventoryStatus', 'frozen', '冻结', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000035, 'system', 'system', 'InventoryStatus', '1', '可用', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000036, 'system', 'system', 'InventoryStatus', '2', '待入', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000037, 'system', 'system', 'InventoryStatus', '3', '待出', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000038, 'system', 'system', 'InventoryStatus', '4', '冻结', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000041, 'system', 'system', 'InventoryMovementType', 'pendingInbound', '待入', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000042, 'system', 'system', 'InventoryMovementType', 'inboundConfirm', '入库确认', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000043, 'system', 'system', 'InventoryMovementType', 'pendingOutbound', '待出', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000044, 'system', 'system', 'InventoryMovementType', 'outboundConfirm', '出库确认', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000045, 'system', 'system', 'InventoryMovementType', 'freeze', '冻结', 50, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000046, 'system', 'system', 'InventoryMovementType', 'unfreeze', '解冻', 60, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000047, 'system', 'system', 'InventoryMovementType', 'adjust', '库存调整', 70, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000048, 'system', 'system', 'InventoryMovementType', '1', '待入', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000049, 'system', 'system', 'InventoryMovementType', '2', '入库确认', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000050, 'system', 'system', 'InventoryMovementType', '3', '待出', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000051, 'system', 'system', 'InventoryMovementType', '4', '出库确认', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000052, 'system', 'system', 'InventoryMovementType', '5', '冻结', 50, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000053, 'system', 'system', 'InventoryMovementType', '6', '解冻', 60, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000054, 'system', 'system', 'InventoryMovementType', '7', '库存调整', 70, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000061, 'system', 'system', 'OrderLineStatus', 'open', '未结', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000062, 'system', 'system', 'OrderLineStatus', 'completed', '已结', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000063, 'system', 'system', 'OrderLineStatus', '1', '未结', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000064, 'system', 'system', 'OrderLineStatus', '2', '已结', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
|
||||
(910000000000000071, 'system', 'system', 'LocationStatus', 'empty', '空', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000072, 'system', 'system', 'LocationStatus', 'hasStock', '有货', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000073, 'system', 'system', 'LocationStatus', 'pendingInbound', '待入', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000074, 'system', 'system', 'LocationStatus', 'pendingOutbound', '待出', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000075, 'system', 'system', 'LocationStatus', 'disabled', '禁用', 50, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000076, 'system', 'system', 'LocationStatus', '1', '空', 10, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000077, 'system', 'system', 'LocationStatus', '2', '有货', 20, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000078, 'system', 'system', 'LocationStatus', '3', '待入', 30, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000079, 'system', 'system', 'LocationStatus', '4', '待出', 40, TRUE, NOW(), 'migration', FALSE),
|
||||
(910000000000000080, 'system', 'system', 'LocationStatus', '5', '禁用', 50, TRUE, NOW(), 'migration', FALSE)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
Execute.Sql("""
|
||||
DROP INDEX IF EXISTS idx_sys_dict_item_tenant_cat;
|
||||
DROP INDEX IF EXISTS uq_sys_dict_item_scope_cat_code;
|
||||
""");
|
||||
if (Schema.Table("sys_dict_item").Exists())
|
||||
{
|
||||
Delete.Table("sys_dict_item");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(10, "Location: aisle_code for shelf batch")]
|
||||
public class LocationAddAisleCode0010 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (!Schema.Table("location").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Schema.Table("location").Column("aisle_code").Exists())
|
||||
{
|
||||
Alter.Table("location")
|
||||
.AddColumn("aisle_code").AsString(64).Nullable();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
if (Schema.Table("location").Exists()
|
||||
&& Schema.Table("location").Column("aisle_code").Exists())
|
||||
{
|
||||
Delete.Column("aisle_code").FromTable("location");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
using FluentMigrator;
|
||||
using FluentMigrator.Builders.Create.Table;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(11, "Area table; zone/location/inventory area_code; order lines use area_code; transfer from/to area; tx snapshot")]
|
||||
public class AreaWarehouseHierarchy0011 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
CreateAreaTable();
|
||||
SeedAreasAndBackfillZones();
|
||||
AlterLocationInventory();
|
||||
AlterOrderLines();
|
||||
AlterInventoryTransaction();
|
||||
RefreshIndexes();
|
||||
}
|
||||
|
||||
private void CreateAreaTable()
|
||||
{
|
||||
if (Schema.Table("area").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Create.Table("area")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("warehouse_code").AsString(64).NotNullable()
|
||||
.WithColumn("code").AsString(64).NotNullable()
|
||||
.WithColumn("name").AsString(256).NotNullable()
|
||||
.WithColumn("attrs").AsCustom("jsonb").Nullable();
|
||||
}
|
||||
|
||||
private void SeedAreasAndBackfillZones()
|
||||
{
|
||||
if (!Schema.Table("area").Exists() || !Schema.Table("zone").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
WITH d AS (
|
||||
SELECT DISTINCT tenant_id, owner_code, warehouse_code
|
||||
FROM zone
|
||||
WHERE NOT is_deleted
|
||||
UNION
|
||||
SELECT DISTINCT tenant_id, owner_code, warehouse_code
|
||||
FROM location
|
||||
WHERE NOT is_deleted
|
||||
UNION
|
||||
SELECT DISTINCT tenant_id, owner_code, warehouse_code
|
||||
FROM inventory
|
||||
WHERE NOT is_deleted
|
||||
), m AS (
|
||||
SELECT GREATEST(
|
||||
COALESCE((SELECT MAX(id) FROM area), 0),
|
||||
COALESCE((SELECT MAX(id) FROM zone), 0),
|
||||
COALESCE((SELECT MAX(id) FROM location), 0)
|
||||
)::bigint AS mx
|
||||
)
|
||||
INSERT INTO area (id, tenant_id, owner_code, warehouse_code, code, name, attrs, create_time, update_time, is_deleted)
|
||||
SELECT
|
||||
m.mx + ROW_NUMBER() OVER (ORDER BY d.tenant_id, d.owner_code, d.warehouse_code)::bigint,
|
||||
d.tenant_id,
|
||||
d.owner_code,
|
||||
d.warehouse_code,
|
||||
'DEFAULT',
|
||||
'默认库区',
|
||||
'{}'::jsonb,
|
||||
NOW(),
|
||||
NOW(),
|
||||
FALSE
|
||||
FROM d
|
||||
CROSS JOIN m
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM area a
|
||||
WHERE NOT a.is_deleted AND a.tenant_id = d.tenant_id AND a.owner_code = d.owner_code
|
||||
AND a.warehouse_code = d.warehouse_code AND a.code = 'DEFAULT');
|
||||
|
||||
ALTER TABLE zone ADD COLUMN IF NOT EXISTS area_code varchar(64);
|
||||
UPDATE zone SET area_code = 'DEFAULT' WHERE area_code IS NULL;
|
||||
ALTER TABLE zone ALTER COLUMN area_code SET NOT NULL;
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE zone ADD CONSTRAINT uq_zone_wh_area_code UNIQUE (tenant_id, owner_code, warehouse_code, area_code, code);
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN NULL;
|
||||
END $$;
|
||||
""");
|
||||
}
|
||||
|
||||
private void AlterLocationInventory()
|
||||
{
|
||||
if (!Schema.Table("location").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS area_code varchar(64);
|
||||
UPDATE location l SET area_code = z.area_code
|
||||
FROM zone z
|
||||
WHERE NOT l.is_deleted AND NOT z.is_deleted
|
||||
AND l.tenant_id = z.tenant_id AND l.owner_code = z.owner_code
|
||||
AND l.warehouse_code = z.warehouse_code AND l.zone_code = z.code
|
||||
AND l.area_code IS NULL;
|
||||
UPDATE location SET area_code = 'DEFAULT' WHERE area_code IS NULL;
|
||||
ALTER TABLE location ALTER COLUMN area_code SET NOT NULL;
|
||||
""");
|
||||
|
||||
if (!Schema.Table("inventory").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
ALTER TABLE inventory ADD COLUMN IF NOT EXISTS area_code varchar(64);
|
||||
UPDATE inventory inv SET area_code = l.area_code
|
||||
FROM location l
|
||||
WHERE NOT inv.is_deleted AND NOT l.is_deleted
|
||||
AND inv.tenant_id = l.tenant_id AND inv.owner_code = l.owner_code
|
||||
AND inv.warehouse_code = l.warehouse_code AND inv.zone_code = l.zone_code AND inv.location_code = l.code
|
||||
AND inv.area_code IS NULL;
|
||||
UPDATE inventory SET area_code = 'DEFAULT' WHERE area_code IS NULL;
|
||||
ALTER TABLE inventory ALTER COLUMN area_code SET NOT NULL;
|
||||
""");
|
||||
}
|
||||
|
||||
private void AlterOrderLines()
|
||||
{
|
||||
foreach (var table in new[] { "inbound_order_line", "outbound_order_line", "stocktake_order_line" })
|
||||
{
|
||||
if (!Schema.Table(table).Exists())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Execute.Sql($@"
|
||||
ALTER TABLE {table} ADD COLUMN IF NOT EXISTS area_code varchar(64);
|
||||
UPDATE {table} SET area_code = 'DEFAULT' WHERE area_code IS NULL;
|
||||
ALTER TABLE {table} DROP COLUMN IF EXISTS zone_code CASCADE;
|
||||
ALTER TABLE {table} ALTER COLUMN area_code SET NOT NULL;
|
||||
");
|
||||
}
|
||||
|
||||
if (!Schema.Table("transfer_order_line").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
ALTER TABLE transfer_order_line ADD COLUMN IF NOT EXISTS from_area_code varchar(64);
|
||||
ALTER TABLE transfer_order_line ADD COLUMN IF NOT EXISTS to_area_code varchar(64);
|
||||
UPDATE transfer_order_line SET from_area_code = 'DEFAULT', to_area_code = 'DEFAULT' WHERE from_area_code IS NULL OR to_area_code IS NULL;
|
||||
ALTER TABLE transfer_order_line DROP COLUMN IF EXISTS from_zone_code CASCADE;
|
||||
ALTER TABLE transfer_order_line DROP COLUMN IF EXISTS to_zone_code CASCADE;
|
||||
ALTER TABLE transfer_order_line ALTER COLUMN from_area_code SET NOT NULL;
|
||||
ALTER TABLE transfer_order_line ALTER COLUMN to_area_code SET NOT NULL;
|
||||
""");
|
||||
}
|
||||
|
||||
private void AlterInventoryTransaction()
|
||||
{
|
||||
if (!Schema.Table("inventory_transaction").Exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
ALTER TABLE inventory_transaction ADD COLUMN IF NOT EXISTS warehouse_code varchar(64);
|
||||
ALTER TABLE inventory_transaction ADD COLUMN IF NOT EXISTS area_code varchar(64);
|
||||
ALTER TABLE inventory_transaction ADD COLUMN IF NOT EXISTS zone_code varchar(64);
|
||||
|
||||
UPDATE inventory_transaction t SET
|
||||
warehouse_code = i.warehouse_code,
|
||||
area_code = i.area_code,
|
||||
zone_code = i.zone_code
|
||||
FROM inventory i
|
||||
WHERE t.inventory_id = i.id AND NOT i.is_deleted;
|
||||
|
||||
UPDATE inventory_transaction SET
|
||||
warehouse_code = 'DEFAULT',
|
||||
area_code = 'DEFAULT',
|
||||
zone_code = 'DEFAULT'
|
||||
WHERE warehouse_code IS NULL OR area_code IS NULL OR zone_code IS NULL;
|
||||
|
||||
ALTER TABLE inventory_transaction ALTER COLUMN warehouse_code SET NOT NULL;
|
||||
ALTER TABLE inventory_transaction ALTER COLUMN area_code SET NOT NULL;
|
||||
ALTER TABLE inventory_transaction ALTER COLUMN zone_code SET NOT NULL;
|
||||
""");
|
||||
}
|
||||
|
||||
private void RefreshIndexes()
|
||||
{
|
||||
Execute.Sql("""
|
||||
DROP INDEX IF EXISTS idx_zone_tenant_owner_warehouse_code;
|
||||
CREATE INDEX IF NOT EXISTS idx_zone_tenant_owner_wh_area_code ON zone(tenant_id, owner_code, warehouse_code, area_code, code);
|
||||
|
||||
DROP INDEX IF EXISTS idx_location_tenant_owner_wzl_code;
|
||||
CREATE INDEX IF NOT EXISTS idx_location_tenant_owner_wazl ON location(tenant_id, owner_code, warehouse_code, area_code, zone_code, code);
|
||||
|
||||
DROP INDEX IF EXISTS idx_inventory_tenant_owner_location;
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_tenant_owner_wazl ON inventory(tenant_id, owner_code, warehouse_code, area_code, zone_code, location_code);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_area_tenant_owner_wh_code ON area(tenant_id, owner_code, warehouse_code, code);
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
Execute.Sql("""
|
||||
DROP INDEX IF EXISTS idx_area_tenant_owner_wh_code;
|
||||
|
||||
DROP INDEX IF EXISTS idx_inventory_tenant_owner_wazl;
|
||||
CREATE INDEX IF NOT EXISTS idx_inventory_tenant_owner_location ON inventory(tenant_id, owner_code, warehouse_code, zone_code, location_code);
|
||||
|
||||
DROP INDEX IF EXISTS idx_location_tenant_owner_wazl;
|
||||
CREATE INDEX IF NOT EXISTS idx_location_tenant_owner_wzl_code ON location(tenant_id, owner_code, warehouse_code, zone_code, code);
|
||||
|
||||
DROP INDEX IF EXISTS idx_zone_tenant_owner_wh_area_code;
|
||||
CREATE INDEX IF NOT EXISTS idx_zone_tenant_owner_warehouse_code ON zone(tenant_id, owner_code, warehouse_code, code);
|
||||
""");
|
||||
|
||||
if (Schema.Table("inventory_transaction").Exists())
|
||||
{
|
||||
if (Schema.Table("inventory_transaction").Column("zone_code").Exists())
|
||||
{
|
||||
Delete.Column("zone_code").FromTable("inventory_transaction");
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory_transaction").Column("area_code").Exists())
|
||||
{
|
||||
Delete.Column("area_code").FromTable("inventory_transaction");
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory_transaction").Column("warehouse_code").Exists())
|
||||
{
|
||||
Delete.Column("warehouse_code").FromTable("inventory_transaction");
|
||||
}
|
||||
}
|
||||
|
||||
if (Schema.Table("transfer_order_line").Exists())
|
||||
{
|
||||
Execute.Sql("""
|
||||
ALTER TABLE transfer_order_line ADD COLUMN IF NOT EXISTS from_zone_code varchar(64) NOT NULL DEFAULT 'DEFAULT';
|
||||
ALTER TABLE transfer_order_line ADD COLUMN IF NOT EXISTS to_zone_code varchar(64) NOT NULL DEFAULT 'DEFAULT';
|
||||
ALTER TABLE transfer_order_line DROP COLUMN IF EXISTS from_area_code CASCADE;
|
||||
ALTER TABLE transfer_order_line DROP COLUMN IF EXISTS to_area_code CASCADE;
|
||||
""");
|
||||
}
|
||||
|
||||
foreach (var table in new[] { "inbound_order_line", "outbound_order_line", "stocktake_order_line" })
|
||||
{
|
||||
if (Schema.Table(table).Exists())
|
||||
{
|
||||
Execute.Sql($@"
|
||||
ALTER TABLE {table} ADD COLUMN IF NOT EXISTS zone_code varchar(64) NOT NULL DEFAULT 'DEFAULT';
|
||||
ALTER TABLE {table} DROP COLUMN IF EXISTS area_code CASCADE;
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
if (Schema.Table("inventory").Exists() && Schema.Table("inventory").Column("area_code").Exists())
|
||||
{
|
||||
Delete.Column("area_code").FromTable("inventory");
|
||||
}
|
||||
|
||||
if (Schema.Table("location").Exists() && Schema.Table("location").Column("area_code").Exists())
|
||||
{
|
||||
Delete.Column("area_code").FromTable("location");
|
||||
}
|
||||
|
||||
if (Schema.Table("zone").Exists() && Schema.Table("zone").Column("area_code").Exists())
|
||||
{
|
||||
Execute.Sql("""
|
||||
ALTER TABLE zone DROP CONSTRAINT IF EXISTS uq_zone_wh_area_code;
|
||||
ALTER TABLE zone DROP COLUMN IF EXISTS area_code;
|
||||
""");
|
||||
}
|
||||
|
||||
if (Schema.Table("area").Exists())
|
||||
{
|
||||
Delete.Table("area");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using FluentMigrator;
|
||||
using WmsLite.Api.Database.Migrations;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(12, "Sys user table and default admin seed")]
|
||||
public class CreateSysUser0012 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (!Schema.Table("sys_user").Exists())
|
||||
{
|
||||
Create.Table("sys_user")
|
||||
.WithBaseColumns()
|
||||
.WithColumn("username").AsString(64).NotNullable()
|
||||
.WithColumn("password_hash").AsString(256).NotNullable()
|
||||
.WithColumn("display_name").AsString(128).Nullable()
|
||||
.WithColumn("is_enabled").AsBoolean().NotNullable().WithDefaultValue(true);
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_sys_user_tenant_owner_username
|
||||
ON sys_user (tenant_id, owner_code, username)
|
||||
WHERE NOT is_deleted;
|
||||
""");
|
||||
|
||||
var hash = BCrypt.Net.BCrypt.HashPassword("admin123");
|
||||
var escaped = hash.Replace("'", "''");
|
||||
Execute.Sql($"""
|
||||
INSERT INTO sys_user (
|
||||
id, tenant_id, owner_code, username, password_hash, display_name, is_enabled,
|
||||
create_time, update_time, is_deleted
|
||||
)
|
||||
SELECT
|
||||
COALESCE((SELECT MAX(id) FROM sys_user), 0) + 1,
|
||||
'default',
|
||||
'default',
|
||||
'admin',
|
||||
'{escaped}',
|
||||
'系统管理员',
|
||||
TRUE,
|
||||
NOW(),
|
||||
NOW(),
|
||||
FALSE
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM sys_user
|
||||
WHERE NOT is_deleted AND tenant_id = 'default' AND owner_code = 'default' AND username = 'admin'
|
||||
);
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
if (Schema.Table("sys_user").Exists())
|
||||
{
|
||||
Delete.Table("sys_user");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(13, "Order number counter table")]
|
||||
public class OrderNoCounter0013 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
if (!Schema.Table("order_no_counter").Exists())
|
||||
{
|
||||
Create.Table("order_no_counter")
|
||||
.WithColumn("id").AsInt64().PrimaryKey().NotNullable()
|
||||
.WithColumn("tenant_id").AsString(64).NotNullable()
|
||||
.WithColumn("owner_code").AsString(64).NotNullable()
|
||||
.WithColumn("prefix").AsString(16).NotNullable()
|
||||
.WithColumn("date_yyyymmdd").AsString(8).NotNullable()
|
||||
.WithColumn("last_seq").AsInt32().NotNullable().WithDefaultValue(0)
|
||||
.WithColumn("create_time").AsDateTime().Nullable()
|
||||
.WithColumn("update_time").AsDateTime().Nullable();
|
||||
}
|
||||
|
||||
Execute.Sql("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_order_no_counter_scope
|
||||
ON order_no_counter (tenant_id, owner_code, prefix, date_yyyymmdd);
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
if (Schema.Table("order_no_counter").Exists())
|
||||
{
|
||||
Delete.Table("order_no_counter");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using FluentMigrator;
|
||||
|
||||
namespace WmsLite.Api.Database.Migrations;
|
||||
|
||||
[Migration(14, "Business unique constraints (partial indexes)")]
|
||||
public class BusinessUniqueConstraints0014 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
Execute.Sql("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_warehouse_code
|
||||
ON warehouse (tenant_id, owner_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_area_code
|
||||
ON area (tenant_id, owner_code, warehouse_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_zone_code
|
||||
ON zone (tenant_id, owner_code, warehouse_code, area_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_location_code
|
||||
ON location (tenant_id, owner_code, warehouse_code, area_code, zone_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_material_code
|
||||
ON material (tenant_id, owner_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_container_code
|
||||
ON container (tenant_id, owner_code, code) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_inbound_order_no
|
||||
ON inbound_order (tenant_id, owner_code, order_no) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_outbound_order_no
|
||||
ON outbound_order (tenant_id, owner_code, order_no) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_transfer_order_no
|
||||
ON transfer_order (tenant_id, owner_code, order_no) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_stocktake_order_no
|
||||
ON stocktake_order (tenant_id, owner_code, order_no) WHERE NOT is_deleted;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_inventory_material_stock
|
||||
ON inventory (tenant_id, owner_code, warehouse_code, area_code, zone_code, location_code, inventory_kind, material_code)
|
||||
WHERE NOT is_deleted AND material_code IS NOT NULL;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_inventory_container_stock
|
||||
ON inventory (tenant_id, owner_code, warehouse_code, area_code, zone_code, location_code, inventory_kind, container_code)
|
||||
WHERE NOT is_deleted AND container_code IS NOT NULL;
|
||||
""");
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
Execute.Sql("""
|
||||
DROP INDEX IF EXISTS uq_warehouse_code;
|
||||
DROP INDEX IF EXISTS uq_area_code;
|
||||
DROP INDEX IF EXISTS uq_zone_code;
|
||||
DROP INDEX IF EXISTS uq_location_code;
|
||||
DROP INDEX IF EXISTS uq_material_code;
|
||||
DROP INDEX IF EXISTS uq_container_code;
|
||||
DROP INDEX IF EXISTS uq_inbound_order_no;
|
||||
DROP INDEX IF EXISTS uq_outbound_order_no;
|
||||
DROP INDEX IF EXISTS uq_transfer_order_no;
|
||||
DROP INDEX IF EXISTS uq_stocktake_order_no;
|
||||
DROP INDEX IF EXISTS uq_inventory_material_stock;
|
||||
DROP INDEX IF EXISTS uq_inventory_container_stock;
|
||||
""");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
-- 已有 location 表时增量添加字段(与 Models/Location 一致)
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS row_no INTEGER NULL;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS column_no INTEGER NULL;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS layer_no INTEGER NULL;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS height NUMERIC(18, 6) NULL;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS location_status SMALLINT NOT NULL DEFAULT 1;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS is_outer BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
ALTER TABLE location ADD COLUMN IF NOT EXISTS container_code VARCHAR(64) NULL;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint WHERE conname = 'ck_location_status'
|
||||
) THEN
|
||||
ALTER TABLE location ADD CONSTRAINT ck_location_status CHECK (location_status IN (1, 2, 3, 4, 5));
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_location_container_code ON location (container_code) WHERE is_deleted = FALSE AND container_code IS NOT NULL;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- 增量:HTTP 访问日志表(与 Models/HttpAccessLog 一致)
|
||||
CREATE TABLE IF NOT EXISTS http_access_log (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
trace_id VARCHAR(64) NOT NULL,
|
||||
http_method VARCHAR(16) NOT NULL,
|
||||
path VARCHAR(512) NOT NULL,
|
||||
query_string TEXT NULL,
|
||||
status_code INTEGER NOT NULL,
|
||||
request_body TEXT NULL,
|
||||
response_body TEXT NULL,
|
||||
duration_ms INTEGER NULL,
|
||||
client_ip VARCHAR(64) NULL,
|
||||
user_id VARCHAR(128) NULL,
|
||||
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS ix_http_access_log_create_time ON http_access_log (create_time DESC);
|
||||
CREATE INDEX IF NOT EXISTS ix_http_access_log_trace_id ON http_access_log (trace_id);
|
||||
@@ -0,0 +1,158 @@
|
||||
-- PostgreSQL 主数据与库存结构(与 WmsLite.Api 实体、BaseEntity 审计字段一致)
|
||||
-- 列顺序:主键 id → 业务字段 → 审计字段(create/update/delete)置于表末
|
||||
-- 执行前请按需调整库名/Schema。
|
||||
|
||||
-- 仓库
|
||||
CREATE TABLE IF NOT EXISTS warehouse (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
code VARCHAR(64) NOT NULL,
|
||||
name VARCHAR(256) NOT NULL,
|
||||
attrs JSONB NULL,
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ux_warehouse_code ON warehouse (code) WHERE is_deleted = FALSE;
|
||||
|
||||
-- 库区(通过 warehouse_code 关联仓库)
|
||||
CREATE TABLE IF NOT EXISTS zone (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
warehouse_code VARCHAR(64) NOT NULL,
|
||||
code VARCHAR(64) NOT NULL,
|
||||
name VARCHAR(256) NOT NULL,
|
||||
attrs JSONB NULL,
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ux_zone_wh_zone ON zone (warehouse_code, code) WHERE is_deleted = FALSE;
|
||||
CREATE INDEX IF NOT EXISTS ix_zone_warehouse_code ON zone (warehouse_code) WHERE is_deleted = FALSE;
|
||||
|
||||
-- 储位(通过 warehouse_code + zone_code 关联层级;行列层/高度/状态/双伸位外侧/容器编码)
|
||||
CREATE TABLE IF NOT EXISTS location (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
warehouse_code VARCHAR(64) NOT NULL,
|
||||
zone_code VARCHAR(64) NOT NULL,
|
||||
code VARCHAR(64) NOT NULL,
|
||||
name VARCHAR(256) NULL,
|
||||
row_no INTEGER NULL,
|
||||
column_no INTEGER NULL,
|
||||
layer_no INTEGER NULL,
|
||||
height NUMERIC(18, 6) NULL,
|
||||
location_status SMALLINT NOT NULL DEFAULT 1,
|
||||
is_outer BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
container_code VARCHAR(64) NULL,
|
||||
attrs JSONB NULL,
|
||||
CONSTRAINT ck_location_status CHECK (location_status IN (1, 2, 3, 4, 5)),
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ux_location_wh_zone_loc
|
||||
ON location (warehouse_code, zone_code, code) WHERE is_deleted = FALSE;
|
||||
CREATE INDEX IF NOT EXISTS ix_location_container_code ON location (container_code) WHERE is_deleted = FALSE AND container_code IS NOT NULL;
|
||||
|
||||
-- 物料主数据
|
||||
CREATE TABLE IF NOT EXISTS material (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
code VARCHAR(64) NOT NULL,
|
||||
name VARCHAR(256) NOT NULL,
|
||||
spec VARCHAR(512) NULL,
|
||||
unit VARCHAR(32) NOT NULL,
|
||||
attrs JSONB NULL,
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ux_material_code ON material (code) WHERE is_deleted = FALSE;
|
||||
|
||||
-- 容器主数据
|
||||
CREATE TABLE IF NOT EXISTS container (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
code VARCHAR(64) NOT NULL,
|
||||
name VARCHAR(256) NULL,
|
||||
container_type VARCHAR(64) NULL,
|
||||
attrs JSONB NULL,
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ux_container_code ON container (code) WHERE is_deleted = FALSE;
|
||||
|
||||
-- 库存(含物料行与空容器行;通过 warehouse/zone/location code 与储位对齐)
|
||||
CREATE TABLE IF NOT EXISTS inventory (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
inventory_kind SMALLINT NOT NULL DEFAULT 1,
|
||||
material_code VARCHAR(64) NULL,
|
||||
container_code VARCHAR(64) NULL,
|
||||
warehouse_code VARCHAR(64) NOT NULL,
|
||||
zone_code VARCHAR(64) NOT NULL,
|
||||
location_code VARCHAR(64) NOT NULL,
|
||||
qty NUMERIC(18, 6) NOT NULL,
|
||||
unit VARCHAR(32) NOT NULL,
|
||||
attrs JSONB NULL,
|
||||
CONSTRAINT ck_inventory_kind CHECK (inventory_kind IN (1, 2)),
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS ix_inventory_wh_zone_loc ON inventory (warehouse_code, zone_code, location_code) WHERE is_deleted = FALSE;
|
||||
CREATE INDEX IF NOT EXISTS ix_inventory_material ON inventory (material_code) WHERE is_deleted = FALSE AND material_code IS NOT NULL;
|
||||
CREATE INDEX IF NOT EXISTS ix_inventory_container ON inventory (container_code) WHERE is_deleted = FALSE AND container_code IS NOT NULL;
|
||||
|
||||
-- HTTP 访问日志(TEXT 存请求/响应正文,不落库 SQL)
|
||||
CREATE TABLE IF NOT EXISTS http_access_log (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
trace_id VARCHAR(64) NOT NULL,
|
||||
http_method VARCHAR(16) NOT NULL,
|
||||
path VARCHAR(512) NOT NULL,
|
||||
query_string TEXT NULL,
|
||||
status_code INTEGER NOT NULL,
|
||||
request_body TEXT NULL,
|
||||
response_body TEXT NULL,
|
||||
duration_ms INTEGER NULL,
|
||||
client_ip VARCHAR(64) NULL,
|
||||
user_id VARCHAR(128) NULL,
|
||||
create_time TIMESTAMP NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS ix_http_access_log_create_time ON http_access_log (create_time DESC);
|
||||
CREATE INDEX IF NOT EXISTS ix_http_access_log_trace_id ON http_access_log (trace_id);
|
||||
|
||||
-- 业务模板
|
||||
CREATE TABLE IF NOT EXISTS sys_template (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
biz_type VARCHAR(64) NOT NULL,
|
||||
biz_name VARCHAR(128) NOT NULL,
|
||||
config_json JSONB NULL,
|
||||
create_time TIMESTAMP NULL,
|
||||
create_by VARCHAR(128) NULL,
|
||||
update_by VARCHAR(128) NULL,
|
||||
update_time TIMESTAMP NULL,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
delete_time TIMESTAMP NULL,
|
||||
delete_by VARCHAR(128) NULL
|
||||
);
|
||||
Reference in new issue
Block a user