serv_process.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // package serv_process -- процессинг сервиса
  2. package serv_process
  3. import (
  4. "fmt"
  5. "log"
  6. "strings"
  7. "adt/pkg/alias"
  8. "adt/pkg/entities/hotel_room_busy"
  9. "adt/pkg/types"
  10. )
  11. // ServProcess -- процессинг сервиса
  12. type ServProcess struct {
  13. store types.IStore // Хранилище сервиса
  14. }
  15. // NewServProcess -- возвращает новый объект процессинга сервиса
  16. func NewServProcess(store types.IStore) (*ServProcess, error) {
  17. log.Println("NewServProcess()")
  18. if store == nil {
  19. log.Fatalln("NewServProcess(): IStore==nil")
  20. }
  21. sf := &ServProcess{
  22. store: store,
  23. }
  24. return sf, nil
  25. }
  26. // MakeOrder -- создаёт новый заказ бронирования
  27. func (sf *ServProcess) MakeHotelRoomOrder(order types.IHotelRoomOrder) error {
  28. if order == nil {
  29. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): IHotelRoomOrder==nil")
  30. }
  31. log.Printf("ServProcess.MakeHotelRoomOrder(): order=%v\n", order.Id())
  32. lstOrderDay := order.ListDay() // Дни для бронирования
  33. lstRoomBusy := make(map[alias.Date]types.IHotelRoomBusy) // Занятые дни
  34. for _, day := range lstOrderDay {
  35. busyId := alias.BusyId(fmt.Sprintf("%v.%v.%v", order.Hotel(), order.Room(), day))
  36. roomBusy, err := sf.store.GetHotelRoomBusy(busyId)
  37. if err != nil {
  38. if !strings.Contains(err.Error(), "not found") {
  39. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): in store.GetHotelRoomBusy, err=\n\t%w", err)
  40. }
  41. roomBusy, err = hotel_room_busy.NewHotelRoomBusy(order.Hotel(), order.Room(), day, false)
  42. if err != nil {
  43. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): in create IHotelRoomBusy, err=\n\t%w", err)
  44. }
  45. }
  46. lstRoomBusy[day] = roomBusy
  47. }
  48. for _, roomBusy := range lstRoomBusy {
  49. if roomBusy.IsBusy() {
  50. delete(lstRoomBusy, roomBusy.Date())
  51. continue
  52. }
  53. roomBusy.SetBusy()
  54. if err := sf.store.SetHotelRoomBusy(roomBusy); err != nil {
  55. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, in store.SetHotelRoomBusy, err=\n\t%w", order.Id(), err)
  56. }
  57. }
  58. if len(lstRoomBusy) == 0 {
  59. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, hotel room is busy for selected dates: %v..%v", order.Id(), order.From(), order.To())
  60. }
  61. if err := sf.store.SetOrder(order); err != nil {
  62. return fmt.Errorf("ServProcess.MakeHotelRoomOrder(): order=%q, in save.SetOrder, err=\n\t%w", order.Id(), err)
  63. }
  64. log.Printf("ServProcess.MakeHotelRoomOrder(): order=\n\t%#+v\n", order)
  65. return nil
  66. }