// package serv_process -- процессинг сервиса package serv_process import ( "fmt" "log" "strings" "adt/pkg/alias" "adt/pkg/entities/hotel_room_busy" "adt/pkg/types" ) // ServProcess -- процессинг сервиса type ServProcess struct { store types.IStore // Хранилище сервиса } // NewServProcess -- возвращает новый объект процессинга сервиса func NewServProcess(store types.IStore) (*ServProcess, error) { log.Println("NewServProcess()") if store == nil { log.Fatalln("NewServProcess(): IStore==nil") } sf := &ServProcess{ store: store, } return sf, nil } // MakeOrder -- создаёт новый заказ бронирования func (sf *ServProcess) MakeHotelRoomOrder(order types.IHotelRoomOrder) error { if order == nil { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): IHotelRoomOrder==nil") } log.Printf("ServProcess.MakeHotelRoomOrder(): order=%v\n", order.Id()) lstOrderDay := order.ListDay() // Дни для бронирования lstRoomBusy := make(map[alias.Date]types.IHotelRoomBusy) // Занятые дни for _, day := range lstOrderDay { busyId := alias.BusyId(fmt.Sprintf("%v.%v.%v", order.Hotel(), order.Room(), day)) roomBusy, err := sf.store.GetHotelRoomBusy(busyId) if err != nil { if !strings.Contains(err.Error(), "not found") { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): in store.GetHotelRoomBusy, err=\n\t%w", err) } roomBusy, err = hotel_room_busy.NewHotelRoomBusy(order.Hotel(), order.Room(), day, false) if err != nil { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): in create IHotelRoomBusy, err=\n\t%w", err) } } lstRoomBusy[day] = roomBusy } for _, roomBusy := range lstRoomBusy { if roomBusy.IsBusy() { delete(lstRoomBusy, roomBusy.Date()) continue } roomBusy.SetBusy() if err := sf.store.SetHotelRoomBusy(roomBusy); err != nil { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, in store.SetHotelRoomBusy, err=\n\t%w", order.Id(), err) } } if len(lstRoomBusy) == 0 { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, hotel room is busy for selected dates: %v..%v", order.Id(), order.From(), order.To()) } if err := sf.store.SetOrder(order); err != nil { return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, in save.SetOrder, err=\n\t%w", order.Id(), err) } log.Printf("ServProcess.MakeHotelRoomOrder(): order=\n\t%#+v\n", order) return nil }