query.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package client
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func QueryPrice(client *FhoClient, lastUpdatedTime time.Time) ([]*PriceSchedule, error) {
  7. data, err := client.query("station_price_updates", lastUpdatedTime)
  8. if err != nil {
  9. fmt.Println("query error:", err.Error())
  10. return nil, err
  11. }
  12. var prices []*PriceSchedule
  13. pricesMap := make(map[string]*PriceSchedule)
  14. for _, rec := range data {
  15. _price, err := PriceScheduleFromMap(rec)
  16. if err != nil {
  17. return nil, err
  18. }
  19. if _, ok := pricesMap[_price.ID]; ok {
  20. _price = pricesMap[_price.ID]
  21. } else {
  22. pricesMap[_price.ID] = _price
  23. prices = append(prices, _price)
  24. }
  25. _product := PriceScheduleProductFromMap(rec)
  26. _price.PriceScheduleProducts = append(_price.PriceScheduleProducts, _product)
  27. }
  28. return prices, nil
  29. }
  30. func QueryProducts(client *FhoClient, lastUpdatedTime time.Time) ([]*Product, error) {
  31. data, err := client.query("products", lastUpdatedTime)
  32. if err != nil {
  33. fmt.Println("query error:", err.Error())
  34. return nil, err
  35. }
  36. var products []*Product
  37. for _, rec := range data {
  38. _product := ProductFromMap(rec)
  39. products = append(products, _product)
  40. }
  41. return products, nil
  42. }