-- ============================================================
-- 个人绩效（与超期/顺延强制管理联动）
-- ============================================================

-- 月度绩效汇总（同一员工同一周期一行）
DROP TABLE IF EXISTS oa_performance_score;
CREATE TABLE oa_performance_score (
  id            BIGINT      NOT NULL AUTO_INCREMENT,
  user_id       BIGINT      NOT NULL,
  user_name     VARCHAR(64),
  period        VARCHAR(7)  NOT NULL COMMENT '考核周期 YYYY-MM',
  base_score    INT         NOT NULL DEFAULT 100,
  deduction     INT         NOT NULL DEFAULT 0 COMMENT '本周期总扣分（正数）',
  bonus         INT         NOT NULL DEFAULT 0 COMMENT '本周期加分（预留）',
  total_score   INT         NOT NULL DEFAULT 100 COMMENT '= base - deduction + bonus',
  grade         VARCHAR(4)           DEFAULT NULL COMMENT 'A+/A/A-/B+/B/B-/C+/C/C-/D',
  create_time   DATETIME             DEFAULT CURRENT_TIMESTAMP,
  update_time   DATETIME             DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_period (user_id, period)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='月度绩效汇总';

-- 每一笔扣/加分流水
DROP TABLE IF EXISTS oa_performance_deduction;
CREATE TABLE oa_performance_deduction (
  id             BIGINT      NOT NULL AUTO_INCREMENT,
  user_id        BIGINT      NOT NULL,
  user_name      VARCHAR(64),
  period         VARCHAR(7)  NOT NULL COMMENT 'YYYY-MM',
  source         VARCHAR(32) NOT NULL COMMENT 'postpone / overdue30 / manual',
  source_type    VARCHAR(32)          DEFAULT NULL COMMENT 'task/step/requirement',
  source_id      BIGINT               DEFAULT NULL COMMENT '业务表主键',
  points         INT         NOT NULL COMMENT '正=扣 负=加',
  reason         VARCHAR(500),
  create_time    DATETIME             DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_user_period (user_id, period),
  KEY idx_source (source_type, source_id),
  UNIQUE KEY uk_dedup (user_id, source, source_type, source_id) COMMENT '同一来源同一业务只扣一次（针对 overdue30 等一次性扣分）'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='绩效流水';
