/**
 * 添加用户佣金比例字段
 * 运行: node add-commission-rate-field.js
 */

const { sequelize } = require('./src/config/database');

async function addCommissionRateField() {
  try {
    console.log('Connecting to database...');
    await sequelize.authenticate();
    console.log('Database connected.');

    // Check if column exists
    const [results] = await sequelize.query(`
      SELECT COLUMN_NAME 
      FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE TABLE_NAME = 'user' AND COLUMN_NAME = 'commission_rate'
    `);

    if (results.length > 0) {
      console.log('Column commission_rate already exists.');
    } else {
      console.log('Adding commission_rate column...');
      await sequelize.query(`
        ALTER TABLE user 
        ADD COLUMN commission_rate DECIMAL(5,4) NULL 
        COMMENT '用户专属佣金比例（如0.05表示5%），为空则使用系统默认比例'
      `);
      console.log('Column commission_rate added successfully.');
    }

    console.log('Done!');
    process.exit(0);
  } catch (error) {
    console.error('Error:', error);
    process.exit(1);
  }
}

addCommissionRateField();
