package repository import ( "gorm.io/gorm" "wails-app/internal/dao" ) // ParkingSessionRepository persists the parking-session aggregate in the // existing vehicle_record table. The table remains the historical parking // ledger while this module owns its active-session semantics. type ParkingSessionRepository struct{} func (r *ParkingSessionRepository) CreateTx(db *gorm.DB, session *dao.VehicleRecord) error { return db.Create(session).Error } func (r *ParkingSessionRepository) FindByIDTx(db *gorm.DB, id uint) (*dao.VehicleRecord, error) { var session dao.VehicleRecord if err := db.First(&session, id).Error; err != nil { return nil, err } return &session, nil } func (r *ParkingSessionRepository) FindActiveByIDTx(db *gorm.DB, id uint) (*dao.VehicleRecord, error) { var session dao.VehicleRecord if err := db.Where("id = ? AND exit_time IS NULL", id).First(&session).Error; err != nil { return nil, err } return &session, nil } func (r *ParkingSessionRepository) FindActiveByPlateTx(db *gorm.DB, plateNumber string) ([]dao.VehicleRecord, error) { var sessions []dao.VehicleRecord err := db.Where("plate_number = ? AND exit_time IS NULL", plateNumber). Order("entry_time DESC, id DESC").Find(&sessions).Error return sessions, err } func (r *ParkingSessionRepository) FindActiveByRFIDTx(db *gorm.DB, rfidTag string) ([]dao.VehicleRecord, error) { var sessions []dao.VehicleRecord err := db.Where("rfid_tag = ? AND exit_time IS NULL", rfidTag). Order("entry_time DESC, id DESC").Find(&sessions).Error return sessions, err } func (r *ParkingSessionRepository) FindActiveByExactIdentityTx(db *gorm.DB, plateNumber, rfidTag string) (*dao.VehicleRecord, error) { var session dao.VehicleRecord if err := db.Where("plate_number = ? AND rfid_tag = ? AND exit_time IS NULL", plateNumber, rfidTag). First(&session).Error; err != nil { return nil, err } return &session, nil } func (r *ParkingSessionRepository) UpdateActiveTx(db *gorm.DB, id uint, updates map[string]interface{}) (int64, error) { result := db.Model(&dao.VehicleRecord{}).Where("id = ? AND exit_time IS NULL", id).Updates(updates) return result.RowsAffected, result.Error } func (r *ParkingSessionRepository) UpdateUnpaidActiveTx(db *gorm.DB, id uint, updates map[string]interface{}) (int64, error) { result := db.Model(&dao.VehicleRecord{}). Where("id = ? AND exit_time IS NULL AND COALESCE(payment_status, '') <> ?", id, "paid"). Updates(updates) return result.RowsAffected, result.Error }