import { Entity, Column, PrimaryGeneratedColumn, UpdateDateColumn, Unique, ManyToOne, JoinColumn } from 'typeorm';
import { AreaEntity } from '@d8d/geo-areas';

@Entity('salary_level', { comment: '省市薪资水平表' })
@Unique(['provinceId', 'cityId'])
export class SalaryLevel {
  @PrimaryGeneratedColumn({
    name: 'salary_id',
    type: 'int',
    unsigned: true,
    comment: '薪资ID'
  })
  id!: number;

  @Column({
    name: 'province_id',
    type: 'int',
    unsigned: true,
    nullable: false,
    comment: '省份ID'
  })
  provinceId!: number;

  @Column({
    name: 'city_id',
    type: 'int',
    unsigned: true,
    nullable: false,
    comment: '城市ID'
  })
  cityId!: number;

  @Column({
    name: 'district_id',
    type: 'int',
    unsigned: true,
    nullable: true,
    comment: '区县ID'
  })
  districtId?: number;

  @Column({
    name: 'basic_salary',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: false,
    comment: '基本工资'
  })
  basicSalary!: number;

  @Column({
    name: 'allowance',
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0.00,
    comment: '津贴补贴'
  })
  allowance!: number;

  @Column({
    name: 'insurance',
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0.00,
    comment: '保险费用'
  })
  insurance!: number;

  @Column({
    name: 'housing_fund',
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0.00,
    comment: '住房公积金'
  })
  housingFund!: number;

  @Column({
    name: 'total_salary',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: false,
    comment: '总薪资'
  })
  totalSalary!: number;

  @UpdateDateColumn({
    name: 'update_time',
    type: 'timestamp',
    comment: '更新时间'
  })
  updateTime!: Date;

  // 区域关联关系
  @ManyToOne(() => AreaEntity)
  @JoinColumn({ name: 'province_id' })
  province!: AreaEntity;

  @ManyToOne(() => AreaEntity)
  @JoinColumn({ name: 'city_id' })
  city!: AreaEntity;

  @ManyToOne(() => AreaEntity)
  @JoinColumn({ name: 'district_id' })
  district?: AreaEntity;

  constructor(partial?: Partial<SalaryLevel>) {
    Object.assign(this, partial);
  }
}