import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity('sys_role')
export class SysRole {
  @PrimaryGeneratedColumn({ name: 'role_id' })
  role_id: number;

  @Column({ name: 'site_id', type: 'int', default: 0 })
  site_id: number;

  @Column({ name: 'role_name', type: 'varchar', length: 255, default: '' })
  role_name: string;

  @Column({ name: 'rules', type: 'text', nullable: true })
  rules: string;

  @Column({ name: 'status', type: 'tinyint', default: 1 })
  status: number;

  @Column({ name: 'is_del', type: 'tinyint', default: 0 })
  is_del: number;

  @CreateDateColumn({ name: 'create_time', type: 'int' })
  create_time: number;

  @UpdateDateColumn({ name: 'update_time', type: 'int' })
  update_time: number;

  // 业务逻辑方法 - 与 PHP 项目保持一致
  getStatusText(): string {
    const statusMap: { [key: number]: string } = { 0: '禁用', 1: '正常' };
    return statusMap[this.status] || '未知';
  }

  // JSON 字段处理方法
  getRulesArray(): string[] {
    if (!this.rules) return [];
    try {
      return JSON.parse(this.rules);
    } catch {
      return [];
    }
  }

  setRulesArray(value: string[]): void {
    this.rules = JSON.stringify(value);
  }
} 