/**
 * 创建操作日志表迁移
 * @file 20250118000010_create_operation_logs_table.js
 * @description 创建操作日志表，用于记录系统用户的操作记录
 */
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('operation_logs', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: '主键ID'
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '操作用户ID'
      },
      username: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '操作用户名'
      },
      user_role: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '操作用户角色'
      },
      operation_type: {
        type: Sequelize.ENUM('CREATE', 'UPDATE', 'DELETE'),
        allowNull: false,
        comment: '操作类型：CREATE-新增，UPDATE-编辑，DELETE-删除'
      },
      module_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '操作模块名称'
      },
      table_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '操作的数据表名'
      },
      record_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '操作的记录ID'
      },
      operation_desc: {
        type: Sequelize.TEXT,
        allowNull: false,
        comment: '操作描述'
      },
      old_data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '操作前的数据（编辑和删除时记录）'
      },
      new_data: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '操作后的数据（新增和编辑时记录）'
      },
      ip_address: {
        type: Sequelize.STRING(45),
        allowNull: true,
        comment: '操作IP地址'
      },
      user_agent: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '用户代理信息'
      },
      request_url: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '请求URL'
      },
      request_method: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: '请求方法'
      },
      response_status: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '响应状态码'
      },
      execution_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '执行时间（毫秒）'
      },
      error_message: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '错误信息（如果有）'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
        comment: '更新时间'
      }
    }, {
      comment: '操作日志表',
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci'
    });

    // 创建索引
    await queryInterface.addIndex('operation_logs', ['user_id'], {
      name: 'idx_operation_logs_user_id'
    });

    await queryInterface.addIndex('operation_logs', ['operation_type'], {
      name: 'idx_operation_logs_operation_type'
    });

    await queryInterface.addIndex('operation_logs', ['module_name'], {
      name: 'idx_operation_logs_module_name'
    });

    await queryInterface.addIndex('operation_logs', ['table_name'], {
      name: 'idx_operation_logs_table_name'
    });

    await queryInterface.addIndex('operation_logs', ['created_at'], {
      name: 'idx_operation_logs_created_at'
    });

    await queryInterface.addIndex('operation_logs', ['user_id', 'operation_type'], {
      name: 'idx_operation_logs_user_operation'
    });

    await queryInterface.addIndex('operation_logs', ['module_name', 'operation_type'], {
      name: 'idx_operation_logs_module_operation'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('operation_logs');
  }
};
