model.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package client
  2. import (
  3. "time"
  4. )
  5. type Model struct {
  6. // object unique id
  7. ID string
  8. // created event
  9. IsCreated bool
  10. // deleted event
  11. IsDeleted bool
  12. // updated event
  13. IsUpdated bool
  14. // updated time of this object
  15. LatestUpdatedTime *time.Time
  16. }
  17. func modelFromMap(m map[string]interface{}) *Model {
  18. createdAtStr := ToString(m["created_at"])
  19. updatedAtStr := ToString(m["updated_at"])
  20. deletedAtStr := ToString(m["deleted_at"])
  21. updatedAt, _ := ToTime(updatedAtStr)
  22. isCreated := false
  23. isUpdated := false
  24. isDeleted := false
  25. if deletedAtStr != "" {
  26. isDeleted = true
  27. } else if createdAtStr == updatedAtStr {
  28. isCreated = true
  29. } else {
  30. isUpdated = true
  31. }
  32. return &Model{ID: ToString(m["id"]), LatestUpdatedTime: updatedAt,
  33. IsCreated: isCreated, IsUpdated: isUpdated, IsDeleted: isDeleted}
  34. }
  35. type Product struct {
  36. Model
  37. // product name
  38. Name string
  39. // product color
  40. Color string
  41. }
  42. func ProductFromMap(m map[string]interface{}) *Product {
  43. return &Product{Model: *modelFromMap(m),
  44. Name: ToString(m["name"]),
  45. Color: ToString(m["color"])}
  46. }
  47. type PriceSchedule struct {
  48. Model
  49. // date
  50. Date time.Time
  51. // after shift change
  52. AfterShiftChange bool
  53. // product list
  54. PriceScheduleProducts []*PriceScheduleProduct
  55. }
  56. func PriceScheduleFromMap(m map[string]interface{}) (*PriceSchedule, error) {
  57. date, err := ToTimeFromISO(m["iso_date"])
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &PriceSchedule{Model: *modelFromMap(m),
  62. Date: *date,
  63. AfterShiftChange: ToBool(m["after_shift_change"])}, nil
  64. }
  65. type PriceScheduleProduct struct {
  66. // product id
  67. ProductID string
  68. // product name
  69. Name string
  70. // product color
  71. Color string
  72. // product price
  73. Price float32
  74. }
  75. func PriceScheduleProductFromMap(m map[string]interface{}) *PriceScheduleProduct {
  76. return &PriceScheduleProduct{
  77. ProductID: ToString(m["product_id"]),
  78. Name: ToString(m["name"]),
  79. Color: ToString(m["color"]),
  80. Price: ToFloat32(m["price"]),
  81. }
  82. }
  83. type PriceChangeHistory struct {
  84. // object unique id of local server
  85. ID string `json:"id"`
  86. // original schedule id from FHO
  87. //
  88. // omit this id, if price change is done by local server
  89. PriceScheduleID string `json:"price_schedule_id"`
  90. // price change time in DATETIME_FROM_ISO_FORMAT
  91. PriceChangeTime string `json:"price_change_time"`
  92. // price change status
  93. Status string `json:"status"`
  94. // products of price change history
  95. PriceChangeHistoryProducts []*PriceChangeHistoryProduct `json:"products"`
  96. }
  97. type PriceChangeHistoryProduct struct {
  98. // local database row id
  99. ID string `json:"id"`
  100. // product id
  101. ProductID string `json:"product_id"`
  102. // product name
  103. Name string `json:"name"`
  104. // product color
  105. Color string `json:"color"`
  106. // product price
  107. Price float32 `json:"price"`
  108. }