-- 创建数据库
CREATE DATABASE IF NOT EXISTS iwb_data_middleware_v1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
USE iwb_data_middleware_v1;

-- 创建 sub_project 表
CREATE TABLE IF NOT EXISTS project (
    id INT AUTO_INCREMENT PRIMARY KEY,
    project_name VARCHAR(255),
    description VARCHAR(1000),
    is_del TINYINT DEFAULT 0,
    delete_by VARCHAR(255),
    deleted_at DATETIME,
    update_by VARCHAR(255),
    updated_at DATETIME,
    create_by VARCHAR(255),
    created_at DATETIME
) CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

-- 创建 sub_project 表
CREATE TABLE IF NOT EXISTS sub_project (
    id INT AUTO_INCREMENT PRIMARY KEY,
    project_id int,
    INDEX idx_project_id (project_id),
    sub_project_name VARCHAR(255),
    work_catalog VARCHAR(255),
    work_content VARCHAR(255),
    standard_version VARCHAR(50),
    file_paths TEXT,
    status TINYINT DEFAULT 0,
    is_del TINYINT DEFAULT 0,
    delete_by VARCHAR(255),
    deleted_at DATETIME,
    update_by VARCHAR(255),
    updated_at DATETIME,
    create_by VARCHAR(255),
    created_at DATETIME
) CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

-- 创建 project_item 表
CREATE TABLE IF NOT EXISTS sub_project_item (
    id INT AUTO_INCREMENT PRIMARY KEY,
    project_id int NOT NULL,
    sub_project_id int NOT NULL,
    device_name VARCHAR(255) NOT NULL,
    device_model VARCHAR(255) ,
    device_unit VARCHAR(50) ,
    device_count FLOAT DEFAULT 0,
    standard_version VARCHAR(50),
    standard_no VARCHAR(255) ,
    process_status TINYINT DEFAULT 0,
    process_time DATETIME,
    send_status TINYINT DEFAULT 0,
    send_time DATETIME,
    remark TEXT,
     is_del TINYINT DEFAULT 0,
    delete_by VARCHAR(255),
    deleted_at DATETIME,
    update_by VARCHAR(255),
    updated_at DATETIME,
    create_by VARCHAR(255),
    created_at DATETIME
) CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sys_user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_name VARCHAR(255) UNIQUE NOT NULL COMMENT '用户名',
    user_nickname VARCHAR(255) COMMENT '用户昵称',
    role VARCHAR(50) COMMENT '角色',
    password VARCHAR(255) NOT NULL COMMENT '密码哈希',
    is_del TINYINT DEFAULT 0 COMMENT '是否删除',
    delete_by VARCHAR(255) COMMENT '删除人',
    deleted_at DATETIME COMMENT '删除时间',
    create_by VARCHAR(255) COMMENT '创建人',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_by VARCHAR(255) COMMENT '更新人',
    INDEX idx_user_name (user_name)
    INDEX idx_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';


INSERT INTO `user` (`id`, `username`, `name`, `email`, `phone`, `password`, `last_login`, `is_del`, `delete_by`, `deleted_at`, `create_by`, `created_at`, `update_by`, `updated_at`)
VALUES (1, 'admin', 'Administrator', 'admin@example.com', '1234567890', '$2b$12$woIvmKHq.ndxT5NqzqChl.VF7TWKRbP1KbtAerXNKb/1pQgvRo/iG', NULL, 0, NULL, NULL, 'admin', '2025-03-10 13:56:57', 'admin', '2025-03-10 16:27:47');
INSERT INTO `user` (`id`, `username`, `name`, `email`, `phone`, `password`, `last_login`, `is_del`, `delete_by`, `deleted_at`, `create_by`, `created_at`, `update_by`, `updated_at`)
VALUES (2, 'system', 'System', 'system@example.com', '13312345001', '$2b$12$woIvmKHq.ndxT5NqzqChl.VF7TWKRbP1KbtAerXNKb/1pQgvRo/iG', NULL, 0, NULL, NULL, 'admin', '2025-03-10 13:56:57', 'admin', '2025-03-10 16:27:47');
INSERT INTO `user` (`id`, `username`, `name`, `email`, `phone`, `password`, `last_login`, `is_del`, `delete_by`, `deleted_at`, `create_by`, `created_at`, `update_by`, `updated_at`)
VALUES (3, 'demo01', 'Demo01', 'demo01@example.com', '13312345002', '$2b$12$woIvmKHq.ndxT5NqzqChl.VF7TWKRbP1KbtAerXNKb/1pQgvRo/iG', NULL, 0, NULL, NULL, 'admin', '2025-03-10 13:56:57', 'admin', '2025-03-10 16:27:47');
INSERT INTO `user` (`id`, `username`, `name`, `email`, `phone`, `password`, `last_login`, `is_del`, `delete_by`, `deleted_at`, `create_by`, `created_at`, `update_by`, `updated_at`)
VALUES (4, 'demo02', 'Demo02', 'demo02@example.com', '13312345002', '$2b$12$woIvmKHq.ndxT5NqzqChl.VF7TWKRbP1KbtAerXNKb/1pQgvRo/iG', NULL, 0, NULL, NULL, 'admin', '2025-03-10 13:56:57', 'admin', '2025-03-10 16:27:47');
INSERT INTO `user` (`id`, `username`, `name`, `email`, `phone`, `password`, `last_login`, `is_del`, `delete_by`, `deleted_at`, `create_by`, `created_at`, `update_by`, `updated_at`)
VALUES (5, 'demo03', 'Demo03', 'demo03@example.com', '13312345002', '$2b$12$woIvmKHq.ndxT5NqzqChl.VF7TWKRbP1KbtAerXNKb/1pQgvRo/iG', NULL, 0, NULL, NULL, 'admin', '2025-03-10 13:56:57', 'admin', '2025-03-10 16:27:47');

-- 创建日志表
CREATE TABLE IF NOT EXISTS sys_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL COMMENT '用户名',
    operation_type VARCHAR(50) NOT NULL COMMENT '操作类型',
    operation_desc VARCHAR(1000) COMMENT '操作描述',
    operation_result TINYINT COMMENT '操作结果(0:失败, 1:成功)',
    operation_module VARCHAR(100) COMMENT '操作模块',
    operation_data TEXT COMMENT '操作数据',
    data_changes TEXT COMMENT '数据变更记录',
    operation_ip VARCHAR(50) COMMENT '操作IP',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    INDEX idx_username (username),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作日志表';


-- -- 修改 project 表的时间字段名
-- ALTER TABLE project
-- CHANGE COLUMN delete_time deleted_at DATETIME,
-- CHANGE COLUMN update_time updated_at DATETIME,
-- CHANGE COLUMN create_time created_at DATETIME;

-- -- 修改 sub_project 表的时间字段名
-- ALTER TABLE sub_project
-- CHANGE COLUMN delete_time deleted_at DATETIME,
-- CHANGE COLUMN update_time updated_at DATETIME,
-- CHANGE COLUMN create_time created_at DATETIME;

-- -- 修改 sub_project_item 表的时间字段名
-- ALTER TABLE sub_project_item
-- CHANGE COLUMN update_time updated_at DATETIME;

