-- =============================================
-- WMS报表数据库表设计
-- 基于WmsReportMapper中的查询SQL设计
-- =============================================

-- 1. 退库原因分析报表表
CREATE TABLE wms_report_return_reason_analysis (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    return_reason_category NVARCHAR(100) NOT NULL,
    return_order_count INT NOT NULL DEFAULT 0,
    total_return_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
    order_count_ratio DECIMAL(10,2) NOT NULL DEFAULT 0,
    amount_ratio DECIMAL(10,2) NOT NULL DEFAULT 0,
    material_name NVARCHAR(200),
    material_category_name NVARCHAR(100),
    material_code NVARCHAR(100),
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_date (tenant_id, statistics_date),
    INDEX IX_category (return_reason_category),
    INDEX IX_material (material_code)
);

-- 2. 库存变动趋势分析报表表
CREATE TABLE wms_report_inventory_trend_analysis (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    material_id BIGINT NOT NULL,
    material_code NVARCHAR(100) NOT NULL,
    material_name NVARCHAR(200) NOT NULL,
    material_category_name NVARCHAR(100),
    statistics_month NVARCHAR(7) NOT NULL, -- YYYY-MM格式
    statistics_week INT NOT NULL,
    current_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    week_instock_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    week_outstock_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    last_week_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    key_node_mark NVARCHAR(50),
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_material (tenant_id, material_id),
    INDEX IX_month_week (statistics_month, statistics_week),
    INDEX IX_material_code (material_code)
);

-- 3. 安全库存预警报表表
CREATE TABLE wms_report_safety_stock_alert (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    material_id BIGINT NOT NULL,
    material_code NVARCHAR(100) NOT NULL,
    material_name NVARCHAR(200) NOT NULL,
    material_category_name NVARCHAR(100),
    current_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    safe_stock_amount DECIMAL(18,2),
    min_stock_amount DECIMAL(18,2),
    max_stock_amount DECIMAL(18,2),
    alert_status NVARCHAR(50) NOT NULL, -- 短缺预警/积压预警/正常
    difference_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
    last_update_time DATETIME2,
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_material (tenant_id, material_id),
    INDEX IX_alert_status (alert_status),
    INDEX IX_material_code (material_code)
);

-- 4. 呆滞料库存报表表
CREATE TABLE wms_report_stagnant_inventory (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    material_id BIGINT NOT NULL,
    material_code NVARCHAR(100) NOT NULL,
    material_name NVARCHAR(200) NOT NULL,
    material_category_name NVARCHAR(100),
    stagnant_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    material_unit NVARCHAR(20),
    last_outstock_time NVARCHAR(50), -- 可能是"从未出库"或日期时间
    stagnant_days INT NOT NULL DEFAULT 0,
    stagnant_reason NVARCHAR(100),
    material_spec NVARCHAR(200),
    warehouse_name NVARCHAR(100),
    last_activity_time NVARCHAR(50),
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_material (tenant_id, material_id),
    INDEX IX_stagnant_days (stagnant_days),
    INDEX IX_material_code (material_code)
);-- 5.
 库存差异报表表
CREATE TABLE wms_report_inventory_difference (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    check_code NVARCHAR(100) NOT NULL,
    check_type NVARCHAR(50) NOT NULL, -- 抽检/盘点/库位/货架盘点
    warehouse_id BIGINT,
    warehouse_name NVARCHAR(100),
    material_id BIGINT NOT NULL,
    material_code NVARCHAR(100) NOT NULL,
    material_name NVARCHAR(200) NOT NULL,
    material_category_name NVARCHAR(100),
    book_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    actual_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    difference_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    difference_type NVARCHAR(20) NOT NULL, -- 无差异/盘盈/盘亏
    difference_rate DECIMAL(10,2) NOT NULL DEFAULT 0,
    difference_level NVARCHAR(20) NOT NULL, -- 重大差异/一般差异/轻微差异/无差异
    check_time DATETIME2,
    check_by NVARCHAR(100),
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_check (tenant_id, check_code),
    INDEX IX_difference_type (difference_type),
    INDEX IX_material_code (material_code)
);

-- 6. 库存周转报表表
CREATE TABLE wms_report_inventory_turnover (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    material_id BIGINT NOT NULL,
    material_code NVARCHAR(100) NOT NULL,
    material_name NVARCHAR(200) NOT NULL,
    material_category_name NVARCHAR(100),
    statistics_month NVARCHAR(7) NOT NULL, -- YYYY-MM格式
    begin_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    end_inventory_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    month_outstock_qty DECIMAL(18,2) NOT NULL DEFAULT 0,
    inventory_turnover_rate DECIMAL(10,2) NOT NULL DEFAULT 0,
    simple_turnover_rate DECIMAL(10,2) NOT NULL DEFAULT 0,
    turnover_evaluation NVARCHAR(50), -- 无流动/快速周转/正常周转/缓慢周转
    statistics_date DATE NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_material (tenant_id, material_id),
    INDEX IX_month (statistics_month),
    INDEX IX_turnover_rate (inventory_turnover_rate),
    INDEX IX_material_code (material_code)
);

-- =============================================
-- 报表数据汇总表（可选，用于快速查询）
-- =============================================
CREATE TABLE wms_report_summary (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    tenant_id NVARCHAR(50) NOT NULL,
    report_type NVARCHAR(50) NOT NULL, -- 报表类型
    report_date DATE NOT NULL,
    total_records INT NOT NULL DEFAULT 0,
    last_update_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    create_time DATETIME2 NOT NULL DEFAULT GETDATE(),
    INDEX IX_tenant_type_date (tenant_id, report_type, report_date)
);