-- 工单主表
CREATE TABLE IF NOT EXISTS `app_ticket` (
  `ticket_id`          BIGINT       NOT NULL AUTO_INCREMENT COMMENT '工单ID',
  `ticket_no`          VARCHAR(32)  NOT NULL COMMENT '工单编号（TK-yyyyMMddHHmmss+4位随机）',
  `title`              VARCHAR(200) NOT NULL COMMENT '工单标题',
  `content`            TEXT         NOT NULL COMMENT '工单内容描述',
  `website_id`         BIGINT       DEFAULT NULL COMMENT '关联应用ID',
  `website_name`       VARCHAR(100) DEFAULT NULL COMMENT '应用名称（冗余）',
  `category`           VARCHAR(30)  NOT NULL DEFAULT 'other' COMMENT '分类: bug/feature/consultation/complaint/other',
  `priority`           VARCHAR(20)  NOT NULL DEFAULT 'normal' COMMENT '优先级: low/normal/high/urgent',
  `status`             VARCHAR(20)  NOT NULL DEFAULT 'pending' COMMENT '状态: pending/assigned/processing/resolved/closed/rejected',
  `attachments`        TEXT         DEFAULT NULL COMMENT '附件JSON数组',
  `submit_user_id`     INT          NOT NULL COMMENT '提交人用户ID',
  `submit_user_name`   VARCHAR(50)  DEFAULT NULL COMMENT '提交人昵称（冗余）',
  `submit_user_avatar` VARCHAR(500) DEFAULT NULL COMMENT '提交人头像（冗余）',
  `assignee_id`        INT          DEFAULT NULL COMMENT '处理人用户ID',
  `assignee_name`      VARCHAR(50)  DEFAULT NULL COMMENT '处理人昵称（冗余）',
  `assignee_avatar`    VARCHAR(500) DEFAULT NULL COMMENT '处理人头像（冗余）',
  `reply_count`        INT          NOT NULL DEFAULT 0 COMMENT '回复数量',
  `resolved_time`      DATETIME     DEFAULT NULL COMMENT '解决时间',
  `closed_time`        DATETIME     DEFAULT NULL COMMENT '关闭时间',
  `deleted`            TINYINT      NOT NULL DEFAULT 0 COMMENT '是否删除: 0否 1是',
  `create_time`        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time`        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`ticket_id`),
  UNIQUE KEY `uk_ticket_no` (`ticket_no`),
  KEY `idx_submit_user` (`submit_user_id`),
  KEY `idx_assignee` (`assignee_id`),
  KEY `idx_website_status` (`website_id`, `status`),
  KEY `idx_status_create` (`status`, `create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='应用工单';

-- 工单回复表
CREATE TABLE IF NOT EXISTS `app_ticket_reply` (
  `reply_id`    BIGINT       NOT NULL AUTO_INCREMENT COMMENT '回复ID',
  `ticket_id`   BIGINT       NOT NULL COMMENT '关联工单ID',
  `content`     TEXT         NOT NULL COMMENT '回复内容',
  `attachments` TEXT         DEFAULT NULL COMMENT '附件JSON数组',
  `user_id`     INT          NOT NULL COMMENT '回复人用户ID',
  `user_name`   VARCHAR(50)  DEFAULT NULL COMMENT '回复人昵称（冗余）',
  `user_avatar` VARCHAR(500) DEFAULT NULL COMMENT '回复人头像（冗余）',
  `is_staff`    TINYINT      NOT NULL DEFAULT 0 COMMENT '是否技术人员/客服: 0否 1是',
  `deleted`     TINYINT      NOT NULL DEFAULT 0 COMMENT '是否删除: 0否 1是',
  `create_time` DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`reply_id`),
  KEY `idx_ticket_id` (`ticket_id`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工单回复';
