import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { UserEntity as User } from '@d8d/user-module';
import { Merchant } from '@d8d/merchant-module';
import { Supplier } from '@d8d/supplier-module';
import { DeliveryAddress } from '@d8d/delivery-address-module';

@Entity('orders')
export class Order {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

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

  @Column({ name: 'user_id', type: 'int', unsigned: true, comment: '用户ID' })
  userId!: number;

  @Column({ name: 'auth_code', type: 'varchar', length: 32, nullable: true, comment: '付款码' })
  authCode!: string | null;

  @Column({ name: 'card_no', type: 'varchar', length: 32, nullable: true, comment: '卡号' })
  cardNo!: string | null;

  @Column({ name: 'sjt_card_no', type: 'varchar', length: 32, nullable: true, comment: '盛京通卡号' })
  sjtCardNo!: string | null;

  @Column({ name: 'amount', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '订单金额' })
  amount!: number;

  @Column({ name: 'cost_amount', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '成本金额' })
  costAmount!: number;

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

  @Column({ name: 'discount_amount', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '优惠金额' })
  discountAmount!: number;

  @Column({ name: 'pay_amount', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '实际支付金额' })
  payAmount!: number;

  @Column({ name: 'device_no', type: 'varchar', length: 255, nullable: true, comment: '设备编号' })
  deviceNo!: string | null;

  @Column({ name: 'description', type: 'varchar', length: 255, nullable: true, comment: '订单描述' })
  description!: string | null;

  @Column({ name: 'goods_detail', type: 'varchar', length: 2000, nullable: true, comment: '订单详情 json' })
  goodsDetail!: string | null;

  @Column({ name: 'goods_tag', type: 'varchar', length: 255, nullable: true, comment: '订单优惠标记' })
  goodsTag!: string | null;

  @Column({ name: 'address', type: 'varchar', length: 255, nullable: true, comment: '地址' })
  address!: string | null;

  @Column({ name: 'order_type', type: 'int', default: 1, comment: '订单类型 1实物订单 2虚拟订单' })
  orderType!: number;

  @Column({ name: 'pay_type', type: 'int', default: 0, comment: '支付类型1积分2礼券' })
  payType!: number;

  @Column({ name: 'pay_state', type: 'int', default: 0, comment: '0未支付1支付中2支付成功3已退款4支付失败5订单关闭' })
  payState!: number;

  @Column({ name: 'state', type: 'int', default: 0, comment: '订单状态0未发货1已发货2收货成功3已退货' })
  state!: number;

  @Column({ name: 'user_phone', type: 'varchar', length: 50, nullable: true, comment: '用户手机号' })
  userPhone!: string | null;

  @Column({ name: 'merchant_id', type: 'int', unsigned: true, default: 0, comment: '商户id' })
  merchantId!: number;

  @Column({ name: 'merchant_no', type: 'int', unsigned: true, nullable: true, comment: '商户号' })
  merchantNo!: number | null;

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

  @Column({ name: 'address_id', type: 'int', unsigned: true, default: 0, comment: '地址id' })
  addressId!: number;

  @Column({ name: 'receiver_mobile', type: 'varchar', length: 255, nullable: true, comment: '收货人手机号' })
  receiverMobile!: string | null;

  @Column({ name: 'recevier_name', type: 'varchar', length: 255, nullable: true, comment: '收货人姓名' })
  recevierName!: string | null;

  @Column({ name: 'recevier_province', type: 'bigint', default: 0, comment: '收货人所在省' })
  recevierProvince!: number;

  @Column({ name: 'recevier_city', type: 'bigint', default: 0, comment: '收货人所在市' })
  recevierCity!: number;

  @Column({ name: 'recevier_district', type: 'bigint', default: 0, comment: '收货人所在区' })
  recevierDistrict!: number;

  @Column({ name: 'recevier_town', type: 'bigint', default: 0, comment: '收货人所在街道' })
  recevierTown!: number;

  @Column({ name: 'refund_time', type: 'timestamp', nullable: true, comment: '退款时间' })
  refundTime!: Date | null;

  @Column({ name: 'close_time', type: 'timestamp', nullable: true, comment: '订单关闭时间' })
  closeTime!: Date | null;

  @Column({ name: 'remark', type: 'varchar', length: 255, nullable: true, comment: '管理员备注信息' })
  remark!: string | null;

  @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;

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

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

  // 关联关系
  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user!: User;

  @ManyToOne(() => Merchant)
  @JoinColumn({ name: 'merchant_id', referencedColumnName: 'id' })
  merchant!: Merchant;

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

  @ManyToOne(() => DeliveryAddress)
  @JoinColumn({ name: 'address_id', referencedColumnName: 'id' })
  deliveryAddress!: DeliveryAddress;
}