import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { SysUserRole } from './SysUserRole';
import { SysUserLog } from './SysUserLog';

@Entity('sys_user')
export class SysUser {
  @PrimaryGeneratedColumn({ name: 'uid' })
  uid: number;

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

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

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

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

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

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

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

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

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

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

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

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

  // 关联关系
  @OneToMany(() => SysUserRole, userRole => userRole.user)
  user_role: SysUserRole[];

  @OneToMany(() => SysUserLog, userLog => userLog.user)
  user_logs: SysUserLog[];

  // 业务方法
  getStatusText(): string {
    return this.status === 1 ? '正常' : '禁用';
  }

  getCreateTimeText(): string {
    return this.create_time ? new Date(this.create_time * 1000).toLocaleString() : '';
  }

  getLastTimeText(): string {
    return this.last_time ? new Date(this.last_time * 1000).toLocaleString() : '';
  }
} 