route.entity.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
  2. import { ActivityEntity } from '../activities/activity.entity';
  3. import { LocationEntity } from '../locations/location.entity';
  4. import { DeleteStatus, DisabledStatus } from '../../share/types';
  5. import { VehicleType, TravelMode } from './route.schema';
  6. @Entity({ name: 'routes' })
  7. export class RouteEntity {
  8. @PrimaryGeneratedColumn({ unsigned: true, comment: '路线ID' })
  9. id!: number;
  10. @Column({ name: 'name', type: 'varchar', length: 255, comment: '路线名称' })
  11. name!: string;
  12. @Column({ name: 'description', type: 'text', nullable: true, comment: '路线描述' })
  13. description!: string | null;
  14. @Column({ name: 'start_location_id', type: 'int', unsigned: true, comment: '出发地点ID' })
  15. startLocationId!: number;
  16. @Column({ name: 'end_location_id', type: 'int', unsigned: true, comment: '目的地点ID' })
  17. endLocationId!: number;
  18. @Column({ name: 'pickup_point', type: 'varchar', length: 255, comment: '上车点' })
  19. pickupPoint!: string;
  20. @Column({ name: 'dropoff_point', type: 'varchar', length: 255, comment: '下车点' })
  21. dropoffPoint!: string;
  22. @Column({ name: 'departure_time', type: 'timestamp', comment: '出发时间' })
  23. departureTime!: Date;
  24. @Column({ name: 'vehicle_type', type: 'enum', enum: VehicleType, comment: '车型' })
  25. vehicleType!: VehicleType;
  26. @Column({ name: 'travel_mode', type: 'enum', enum: TravelMode, comment: '出行方式', default: TravelMode.CARPOOL })
  27. travelMode!: TravelMode;
  28. @Column({ name: 'price', type: 'decimal', precision: 10, scale: 2, comment: '价格' })
  29. price!: number;
  30. @Column({ name: 'seat_count', type: 'int', unsigned: true, comment: '座位数' })
  31. seatCount!: number;
  32. @Column({ name: 'available_seats', type: 'int', unsigned: true, comment: '可用座位数' })
  33. availableSeats!: number;
  34. @Column({ name: 'activity_id', type: 'int', unsigned: true, comment: '关联活动ID' })
  35. activityId!: number;
  36. @ManyToOne(() => ActivityEntity, (activity) => activity.routes)
  37. @JoinColumn({ name: 'activity_id', referencedColumnName: 'id' })
  38. activity!: ActivityEntity;
  39. @ManyToOne(() => LocationEntity, { nullable: false })
  40. @JoinColumn({ name: 'start_location_id', referencedColumnName: 'id' })
  41. startLocation!: LocationEntity;
  42. @ManyToOne(() => LocationEntity, { nullable: false })
  43. @JoinColumn({ name: 'end_location_id', referencedColumnName: 'id' })
  44. endLocation!: LocationEntity;
  45. @Column({ name: 'is_disabled', type: 'int', default: DisabledStatus.ENABLED, comment: '是否禁用(0:启用,1:禁用)' })
  46. isDisabled!: DisabledStatus;
  47. @Column({ name: 'is_deleted', type: 'int', default: DeleteStatus.NOT_DELETED, comment: '是否删除(0:未删除,1:已删除)' })
  48. isDeleted!: DeleteStatus;
  49. @Column({ name: 'created_by', type: 'int', nullable: true, comment: '创建人ID' })
  50. createdBy!: number | null;
  51. @Column({ name: 'updated_by', type: 'int', nullable: true, comment: '更新人ID' })
  52. updatedBy!: number | null;
  53. @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  54. createdAt!: Date;
  55. @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  56. updatedAt!: Date;
  57. constructor(partial?: Partial<RouteEntity>) {
  58. Object.assign(this, partial);
  59. }
  60. /**
  61. * 计算路线类型(去程/返程)
  62. * 去程路线: 目的地 = 活动举办地点
  63. * 返程路线: 出发地 = 活动举办地点
  64. */
  65. get routeType(): 'departure' | 'return' {
  66. if (!this.activity || !this.activity.venueLocationId) {
  67. return 'departure'; // 默认返回去程类型
  68. }
  69. if (this.endLocationId === this.activity.venueLocationId) {
  70. return 'departure';
  71. } else if (this.startLocationId === this.activity.venueLocationId) {
  72. return 'return';
  73. }
  74. return 'departure'; // 默认返回去程类型
  75. }
  76. }