import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn, Index } from 'typeorm';
import { OrderMt } from './order.mt.entity';
import { GoodsMt } from '@d8d/goods-module-mt';
import { SupplierMt } from '@d8d/supplier-module-mt';
import { FileMt } from '@d8d/file-module-mt';

@Entity('orders_goods_mt')
@Index(['tenantId'])
@Index(['tenantId', 'id'])
@Index(['tenantId', 'orderId'])
@Index(['tenantId', 'goodsId'])
@Index(['tenantId', 'supplierId'])
export class OrderGoodsMt {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({ name: 'tenant_id', type: 'int', unsigned: true, comment: '租户ID' })
  tenantId!: number;

  @Column({ name: 'order_id', type: 'int', unsigned: true, comment: '订单表id' })
  orderId!: number;

  @Column({ name: 'order_no', type: 'varchar', length: 50, nullable: true, comment: '订单号' })
  orderNo!: string | null;

  @Column({ name: 'goods_id', type: 'int', unsigned: true, comment: '商品id' })
  goodsId!: number;

  @Column({ name: 'goods_name', type: 'varchar', length: 255, nullable: true, comment: '商品名称' })
  goodsName!: string | null;

  @Column({ name: 'goods_type', type: 'int', default: 1, comment: '1实物产品2虚拟订单' })
  goodsType!: number;

  @Column({ name: 'supplier_id', type: 'int', unsigned: true, nullable: true, comment: '供货商id' })
  supplierId!: number | null;

  @Column({ name: 'cost_price', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '成本价' })
  costPrice!: number;

  @Column({ name: 'price', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '售价' })
  price!: number;

  @Column({ name: 'num', type: 'int', default: 0, comment: '数量' })
  num!: number;

  @Column({ name: 'freight_amount', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '运费' })
  freightAmount!: number;

  @Column({ name: 'state', type: 'int', default: 0, comment: '状态(0未发货、1已发货)' })
  state!: number;

  @Column({ name: 'express_name', type: 'varchar', length: 255, nullable: true, comment: '快递名称' })
  expressName!: string | null;

  @Column({ name: 'express_no', type: 'int', nullable: true, comment: '单号' })
  expressNo!: number | null;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt!: Date;

  @Column({ name: 'created_by', type: 'int', unsigned: true, nullable: true, comment: '创建人ID' })
  createdBy!: number | null;

  @Column({ name: 'updated_by', type: 'int', unsigned: true, nullable: true, comment: '更新人ID' })
  updatedBy!: number | null;

  @Column({ name: 'image_file_id', type: 'int', unsigned: true, nullable: true, comment: '商品图片文件ID' })
  imageFileId!: number | null;

  // 关联关系
  @ManyToOne(() => OrderMt)
  @JoinColumn({ name: 'order_id', referencedColumnName: 'id' })
  order!: OrderMt;

  @ManyToOne(() => GoodsMt)
  @JoinColumn({ name: 'goods_id', referencedColumnName: 'id' })
  goods!: GoodsMt;

  @ManyToOne(() => SupplierMt)
  @JoinColumn({ name: 'supplier_id', referencedColumnName: 'id' })
  supplier!: SupplierMt;

  @ManyToOne(() => FileMt, { nullable: true })
  @JoinColumn({ name: 'image_file_id', referencedColumnName: 'id' })
  imageFile!: FileMt | null;
}