-- HR Position System Database Schema
-- Database: db_A102
-- Schema: hr_position_system

-- Create schema if not exists
CREATE SCHEMA IF NOT EXISTS hr_position_system DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hr_position_system;

-- ============================================================
-- Table: positions (崗位基礎資料)
-- ============================================================
CREATE TABLE IF NOT EXISTS positions (
    id VARCHAR(20) PRIMARY KEY COMMENT '崗位編號',
    position_code VARCHAR(20) NOT NULL UNIQUE COMMENT '崗位編號',
    position_name VARCHAR(100) NOT NULL COMMENT '崗位名稱',
    position_category VARCHAR(2) COMMENT '崗位類別代碼',
    position_category_name VARCHAR(50) COMMENT '崗位類別名稱',
    position_nature VARCHAR(2) COMMENT '崗位性質代碼',
    position_nature_name VARCHAR(50) COMMENT '崗位性質名稱',
    headcount INT COMMENT '編制人數',
    position_level VARCHAR(2) COMMENT '崗位級別',
    effective_date DATE COMMENT '生效日期',
    position_desc TEXT COMMENT '崗位描述',
    position_remark TEXT COMMENT '崗位備注',

    -- Recruitment Info
    min_education VARCHAR(3) COMMENT '最低學歷',
    required_gender VARCHAR(1) COMMENT '要求性別',
    salary_range VARCHAR(1) COMMENT '薪酬范圍',
    work_experience VARCHAR(2) COMMENT '工作經驗(年)',
    min_age INT COMMENT '最小年齡',
    max_age INT COMMENT '最大年齡',
    job_type VARCHAR(2) COMMENT '工作性質',
    recruit_position VARCHAR(3) COMMENT '招聘職位',
    job_title VARCHAR(100) COMMENT '職位名稱',
    job_desc TEXT COMMENT '職位描述',
    position_req TEXT COMMENT '崗位要求',
    title_req VARCHAR(4) COMMENT '職稱要求',
    major_req VARCHAR(200) COMMENT '專業要求',
    skill_req VARCHAR(200) COMMENT '技能要求',
    lang_req VARCHAR(100) COMMENT '語言要求',
    other_req VARCHAR(200) COMMENT '其他要求',
    superior_position VARCHAR(20) COMMENT '上級崗位編號',
    recruit_remark TEXT COMMENT '備注說明',

    -- Metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
    created_by VARCHAR(50) COMMENT '建立人',
    updated_by VARCHAR(50) COMMENT '更新人',

    INDEX idx_position_code (position_code),
    INDEX idx_position_name (position_name),
    INDEX idx_position_category (position_category),
    INDEX idx_position_level (position_level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='崗位基礎資料表';

-- ============================================================
-- Table: jobs (職務基礎資料)
-- ============================================================
CREATE TABLE IF NOT EXISTS jobs (
    id VARCHAR(20) PRIMARY KEY COMMENT '職務編號',
    job_category_code VARCHAR(4) NOT NULL COMMENT '職務類別編號',
    job_category_name VARCHAR(50) COMMENT '職務類別名稱',
    job_code VARCHAR(20) NOT NULL UNIQUE COMMENT '職務編號',
    job_name VARCHAR(100) NOT NULL COMMENT '職務名稱',
    job_name_en VARCHAR(100) COMMENT '職務英文名稱',
    job_effective_date DATE COMMENT '生效日期',
    job_headcount INT COMMENT '編制人數',
    job_sort_order INT COMMENT '排列順序',
    job_remark TEXT COMMENT '備注說明',
    job_level VARCHAR(50) COMMENT '職務層級',
    has_attendance_bonus BOOLEAN DEFAULT FALSE COMMENT '是否有全勤',
    has_housing_allowance BOOLEAN DEFAULT FALSE COMMENT '是否住房補貼',

    -- Metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
    created_by VARCHAR(50) COMMENT '建立人',
    updated_by VARCHAR(50) COMMENT '更新人',

    INDEX idx_job_code (job_code),
    INDEX idx_job_name (job_name),
    INDEX idx_job_category (job_category_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='職務基礎資料表';

-- ============================================================
-- Table: job_descriptions (崗位描述)
-- ============================================================
CREATE TABLE IF NOT EXISTS job_descriptions (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT '主鍵',
    emp_no VARCHAR(20) COMMENT '工號',
    emp_name VARCHAR(50) COMMENT '姓名',
    position_code VARCHAR(20) COMMENT '崗位代碼',
    version_date DATE COMMENT '版本更新日期',

    -- Position Info
    position_name VARCHAR(100) COMMENT '崗位名稱',
    department VARCHAR(100) COMMENT '所屬部門',
    position_effective_date DATE COMMENT '崗位生效日期',
    direct_supervisor VARCHAR(100) COMMENT '直接領導職務',
    position_grade_job VARCHAR(100) COMMENT '崗位職等&職務',
    report_to VARCHAR(100) COMMENT '匯報對象職務',
    direct_reports VARCHAR(200) COMMENT '直接下級',
    work_location VARCHAR(3) COMMENT '任職地點',
    emp_attribute VARCHAR(2) COMMENT '員工屬性',

    -- Responsibilities
    position_purpose VARCHAR(500) COMMENT '崗位設置目的',
    main_responsibilities TEXT COMMENT '主要崗位職責',

    -- Requirements
    education VARCHAR(200) COMMENT '教育程度',
    basic_skills TEXT COMMENT '基本技能',
    professional_knowledge TEXT COMMENT '專業知識',
    work_experience_req TEXT COMMENT '工作經驗',
    other_requirements TEXT COMMENT '其他要求',

    -- Metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
    created_by VARCHAR(50) COMMENT '建立人',
    updated_by VARCHAR(50) COMMENT '更新人',

    INDEX idx_emp_no (emp_no),
    INDEX idx_position_code (position_code),
    INDEX idx_version_date (version_date),
    FOREIGN KEY (position_code) REFERENCES positions(position_code) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='崗位描述表';

-- ============================================================
-- Table: reference_codes (參照資料代碼表)
-- ============================================================
CREATE TABLE IF NOT EXISTS reference_codes (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT '主鍵',
    code_type VARCHAR(50) NOT NULL COMMENT '代碼類型',
    code_value VARCHAR(10) NOT NULL COMMENT '代碼值',
    code_name VARCHAR(100) NOT NULL COMMENT '代碼名稱',
    code_name_en VARCHAR(100) COMMENT '英文名稱',
    sort_order INT DEFAULT 0 COMMENT '排序',
    is_active BOOLEAN DEFAULT TRUE COMMENT '是否啟用',
    remark VARCHAR(200) COMMENT '備注',

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',

    UNIQUE KEY uk_code_type_value (code_type, code_value),
    INDEX idx_code_type (code_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='參照資料代碼表';

-- ============================================================
-- Table: audit_logs (審計日誌)
-- ============================================================
CREATE TABLE IF NOT EXISTS audit_logs (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT '主鍵',
    action VARCHAR(20) NOT NULL COMMENT '操作類型(CREATE/UPDATE/DELETE)',
    entity_type VARCHAR(50) NOT NULL COMMENT '實體類型',
    entity_id VARCHAR(50) NOT NULL COMMENT '實體ID',
    old_data JSON COMMENT '變更前資料',
    new_data JSON COMMENT '變更後資料',
    user_id VARCHAR(50) COMMENT '操作用戶',
    ip_address VARCHAR(50) COMMENT 'IP地址',
    user_agent VARCHAR(200) COMMENT '用戶代理',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '操作時間',

    INDEX idx_entity_type_id (entity_type, entity_id),
    INDEX idx_user_id (user_id),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='審計日誌表';

-- ============================================================
-- Insert Reference Data (參照資料初始化)
-- ============================================================

-- 崗位類別 (Position Category)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('POSITION_CATEGORY', '01', '技術職', 1),
('POSITION_CATEGORY', '02', '管理職', 2),
('POSITION_CATEGORY', '03', '業務職', 3),
('POSITION_CATEGORY', '04', '行政職', 4);

-- 崗位性質 (Position Nature)
INSERT INTO reference_codes (code_type, code_value, code_name, code_name_en, sort_order) VALUES
('POSITION_NATURE', 'FT', '全職', 'Full-time', 1),
('POSITION_NATURE', 'PT', '兼職', 'Part-time', 2),
('POSITION_NATURE', 'CT', '約聘', 'Contract', 3),
('POSITION_NATURE', 'IN', '實習', 'Intern', 4);

-- 崗位級別 (Position Level)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('POSITION_LEVEL', 'L1', '基層員工', 1),
('POSITION_LEVEL', 'L2', '資深員工', 2),
('POSITION_LEVEL', 'L3', '主管', 3),
('POSITION_LEVEL', 'L4', '經理', 4),
('POSITION_LEVEL', 'L5', '總監', 5),
('POSITION_LEVEL', 'L6', '副總', 6),
('POSITION_LEVEL', 'L7', '總經理', 7);

-- 職務類別 (Job Category)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('JOB_CATEGORY', 'MGR', '管理職', 1),
('JOB_CATEGORY', 'TECH', '技術職', 2),
('JOB_CATEGORY', 'SALE', '業務職', 3),
('JOB_CATEGORY', 'ADMIN', '行政職', 4),
('JOB_CATEGORY', 'RD', '研發職', 5),
('JOB_CATEGORY', 'PROD', '生產職', 6);

-- 學歷 (Education)
INSERT INTO reference_codes (code_type, code_value, code_name, code_name_en, sort_order) VALUES
('EDUCATION', 'HS', '高中', 'High School', 1),
('EDUCATION', 'JC', '專科', 'Junior College', 2),
('EDUCATION', 'BA', '大學', 'Bachelor', 3),
('EDUCATION', 'MA', '碩士', 'Master', 4),
('EDUCATION', 'PHD', '博士', 'PhD', 5);

-- 薪酬范圍 (Salary Range)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('SALARY_RANGE', 'A', 'A級', 1),
('SALARY_RANGE', 'B', 'B級', 2),
('SALARY_RANGE', 'C', 'C級', 3),
('SALARY_RANGE', 'D', 'D級', 4),
('SALARY_RANGE', 'E', 'E級', 5),
('SALARY_RANGE', 'N', '面議', 6);

-- 任職地點 (Work Location)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('WORK_LOCATION', 'HQ', '總部', 1),
('WORK_LOCATION', 'TPE', '台北辦公室', 2),
('WORK_LOCATION', 'TYC', '桃園廠區', 3),
('WORK_LOCATION', 'KHH', '高雄廠區', 4),
('WORK_LOCATION', 'SH', '上海辦公室', 5),
('WORK_LOCATION', 'SZ', '深圳辦公室', 6);

-- 員工屬性 (Employee Attribute)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('EMP_ATTRIBUTE', 'FT', '正式員工', 1),
('EMP_ATTRIBUTE', 'CT', '約聘人員', 2),
('EMP_ATTRIBUTE', 'PT', '兼職人員', 3),
('EMP_ATTRIBUTE', 'IN', '實習生', 4),
('EMP_ATTRIBUTE', 'DP', '派遣人員', 5);

-- 招聘職位 (Recruit Position)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('RECRUIT_POSITION', 'ENG', '工程師', 1),
('RECRUIT_POSITION', 'MGR', '經理', 2),
('RECRUIT_POSITION', 'AST', '助理', 3),
('RECRUIT_POSITION', 'OP', '操作員', 4),
('RECRUIT_POSITION', 'SAL', '業務', 5);

-- 職稱要求 (Title Requirement)
INSERT INTO reference_codes (code_type, code_value, code_name, sort_order) VALUES
('TITLE_REQ', 'NONE', '無要求', 1),
('TITLE_REQ', 'CERT', '需證書', 2),
('TITLE_REQ', 'LIC', '需執照', 3);

-- ============================================================
-- End of Schema
-- ============================================================
