const Sequelize = require("sequelize");

module.exports = (db) => {
  return db.define(
    "biz_audit_log",
    {
      id: {
        type: Sequelize.BIGINT.UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      admin_user_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: true,
      },
      biz_user_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: true,
      },
      action: {
        type: Sequelize.STRING(64),
        allowNull: false,
      },
      resource_type: {
        type: Sequelize.STRING(64),
        allowNull: false,
        defaultValue: "",
      },
      resource_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: true,
      },
      detail: {
        type: Sequelize.JSON,
        allowNull: true,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    },
    {
      tableName: "biz_audit_log",
      timestamps: false,
      underscored: true,
      comment: "审计日志",
      hooks: {
        beforeCreate(row) {
          if (!row.created_at) row.created_at = new Date();
        },
      },
    }
  );
};
