-- 员工月度绩效考核系统数据库初始化脚本

CREATE DATABASE IF NOT EXISTS employee_performance
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_unicode_ci;

USE employee_performance;

-- 1. 用户表
CREATE TABLE IF NOT EXISTS user (
  user_id     INT PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID',
  username    VARCHAR(50)  NOT NULL UNIQUE COMMENT '用户名（工号）',
  password    VARCHAR(255) NOT NULL COMMENT '密码（明文存储，测试环境使用）',
  name        VARCHAR(50)  NOT NULL COMMENT '姓名',
  role        ENUM('employee', 'manager', 'generalManager') NOT NULL COMMENT '角色',
  department  VARCHAR(50)  NOT NULL COMMENT '部门',
  position    VARCHAR(50)  NOT NULL COMMENT '岗位',
  manager_id  INT          NULL COMMENT '直属管理层ID',
  status      ENUM('active', 'inactive') DEFAULT 'active' COMMENT '状态',
  created_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at  TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_role    (role),
  INDEX idx_manager (manager_id),
  FOREIGN KEY (manager_id) REFERENCES user(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 2. 绩效主表
CREATE TABLE IF NOT EXISTS performance_month (
  perf_id        INT PRIMARY KEY AUTO_INCREMENT COMMENT '绩效记录ID',
  user_id        INT          NOT NULL COMMENT '员工ID',
  month          VARCHAR(7)   NOT NULL COMMENT '考核月份（YYYY-MM）',
  status         ENUM('draft', 'submitted', 'under_review', 'completed', 'rejected') NOT NULL DEFAULT 'draft' COMMENT '状态',
  self_score     DECIMAL(5,2) NULL COMMENT '员工自评总分',
  ai_score       DECIMAL(5,2) NULL COMMENT 'AI评分总分',
  manager_score  DECIMAL(5,2) NULL COMMENT '管理层审核总分',
  total_score    DECIMAL(5,2) NULL COMMENT '最终总分',
  level          ENUM('excellent', 'qualified', 'need_motivation', 'unqualified') NULL COMMENT '绩效等级',
  reward_punish  VARCHAR(255) NULL COMMENT '奖惩说明',
  work_summary   TEXT         NULL COMMENT '工作汇总',
  submit_time    TIMESTAMP    NULL COMMENT '提交时间',
  review_time    TIMESTAMP    NULL COMMENT '审核时间',
  review_opinion TEXT         NULL COMMENT '审核意见',
  created_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at     TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_user_month (user_id, month),
  INDEX idx_status (status),
  INDEX idx_month  (month),
  FOREIGN KEY (user_id) REFERENCES user(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='绩效主表';

-- 3. 绩效项明细表
CREATE TABLE IF NOT EXISTS perf_item (
  item_id             INT PRIMARY KEY AUTO_INCREMENT COMMENT '绩效项ID',
  perf_id             INT          NOT NULL COMMENT '绩效记录ID',
  item_name           VARCHAR(100) NOT NULL COMMENT '考核项名称',
  item_category       ENUM('business', 'comprehensive') NOT NULL COMMENT '考核项类别',
  weight              INT          NOT NULL COMMENT '权重（分数）',
  user_content        TEXT         NULL COMMENT '员工填写内容',
  self_score          DECIMAL(5,2) NULL COMMENT '员工自评分',
  ai_score            DECIMAL(5,2) NULL COMMENT 'AI评分',
  ai_explanation      TEXT         NULL COMMENT 'AI评分说明',
  manager_score       DECIMAL(5,2) NULL COMMENT '管理层评分',
  manager_explanation TEXT         NULL COMMENT '管理层评分说明',
  evidence_url        VARCHAR(500) NULL COMMENT '佐证材料URL',
  created_at          TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at          TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_perf (perf_id),
  FOREIGN KEY (perf_id) REFERENCES performance_month(perf_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='绩效项明细表';

-- 4. 考勤表
CREATE TABLE IF NOT EXISTS attendance (
  attendance_id    INT PRIMARY KEY AUTO_INCREMENT COMMENT '考勤ID',
  perf_id          INT          NOT NULL COMMENT '绩效记录ID',
  leave_days       INT          DEFAULT 0 COMMENT '事假天数',
  late_times       INT          DEFAULT 0 COMMENT '迟到次数',
  absent_days      INT          DEFAULT 0 COMMENT '旷工天数',
  lack_card_times  INT          DEFAULT 0 COMMENT '缺卡次数',
  attendance_score DECIMAL(5,2) NULL COMMENT '考勤得分',
  remark           TEXT         NULL COMMENT '备注',
  created_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_perf (perf_id),
  FOREIGN KEY (perf_id) REFERENCES performance_month(perf_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='考勤表';

-- 5. AI 结果表
CREATE TABLE IF NOT EXISTS ai_result (
  ai_id          INT PRIMARY KEY AUTO_INCREMENT COMMENT 'AI结果ID',
  perf_id        INT          NOT NULL COMMENT '绩效记录ID',
  ai_score_json  TEXT         NOT NULL COMMENT 'AI评分详情（JSON格式）',
  ai_total_score DECIMAL(5,2) NOT NULL COMMENT 'AI总分',
  problems       TEXT         NULL COMMENT 'AI总结的问题（JSON数组）',
  suggestions    TEXT         NULL COMMENT 'AI改进建议（JSON数组）',
  api_response   TEXT         NULL COMMENT 'FastGPT原始响应',
  create_time    TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '生成时间',
  UNIQUE KEY uk_perf (perf_id),
  FOREIGN KEY (perf_id) REFERENCES performance_month(perf_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI结果表';

-- 6. 考核规则配置表
CREATE TABLE IF NOT EXISTS performance_rules (
  rule_id          INT PRIMARY KEY AUTO_INCREMENT COMMENT '规则ID',
  rule_key         VARCHAR(100) NOT NULL UNIQUE COMMENT '规则键名',
  rule_value       TEXT         NOT NULL COMMENT '规则值（JSON格式）',
  description      VARCHAR(255) NULL COMMENT '规则描述',
  effective_cycle  VARCHAR(7)   NULL COMMENT '生效周期（YYYY-MM，NULL表示立即生效）',
  updated_by       INT          NULL COMMENT '最后修改人ID',
  created_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (updated_by) REFERENCES user(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='考核规则配置表';

-- 7. 操作日志表
CREATE TABLE IF NOT EXISTS operation_log (
  log_id           INT PRIMARY KEY AUTO_INCREMENT COMMENT '日志ID',
  user_id          INT          NOT NULL COMMENT '操作人ID',
  operation_type   VARCHAR(50)  NOT NULL COMMENT '操作类型',
  target_type      VARCHAR(50)  NULL COMMENT '目标类型',
  target_id        INT          NULL COMMENT '目标ID',
  operation_detail TEXT         NULL COMMENT '操作详情',
  ip_address       VARCHAR(50)  NULL COMMENT 'IP地址',
  created_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user    (user_id),
  INDEX idx_created (created_at),
  FOREIGN KEY (user_id) REFERENCES user(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='操作日志表';
