basket.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package basket
  2. import (
  3. "net/http"
  4. "sync"
  5. "golang-fave/engine/sqlw"
  6. "golang-fave/engine/wrapper/config"
  7. )
  8. type SBParam struct {
  9. R *http.Request
  10. DB *sqlw.DB
  11. Host string
  12. Config *config.Config
  13. SessionId string
  14. }
  15. type Basket struct {
  16. sync.RWMutex
  17. hosts map[string]*onehost
  18. }
  19. func New() *Basket {
  20. b := Basket{}
  21. b.hosts = map[string]*onehost{}
  22. return &b
  23. }
  24. func (this *Basket) Info(p *SBParam) string {
  25. if p.Host == "" || p.SessionId == "" {
  26. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  27. }
  28. this.Lock()
  29. defer this.Unlock()
  30. if h, ok := this.hosts[p.Host]; ok == true {
  31. if s, ok := h.sessions[p.SessionId]; ok == true {
  32. s.Preload(p)
  33. return s.String(p)
  34. } else {
  35. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  36. }
  37. } else {
  38. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  39. }
  40. }
  41. func (this *Basket) Plus(p *SBParam, product_id int) string {
  42. if p.Host == "" || p.SessionId == "" {
  43. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  44. }
  45. this.Lock()
  46. defer this.Unlock()
  47. if h, ok := this.hosts[p.Host]; ok == true {
  48. if s, ok := h.sessions[p.SessionId]; ok == true {
  49. s.Preload(p)
  50. s.Plus(p, product_id)
  51. }
  52. } else {
  53. s := &session{}
  54. s.listCurrencies = map[int]*currency{}
  55. s.Products = map[int]*product{}
  56. s.Preload(p)
  57. s.Plus(p, product_id)
  58. h := &onehost{}
  59. h.sessions = map[string]*session{}
  60. h.sessions[p.SessionId] = s
  61. this.hosts[p.Host] = h
  62. }
  63. return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
  64. }
  65. func (this *Basket) Minus(p *SBParam, product_id int) string {
  66. if p.Host == "" || p.SessionId == "" {
  67. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  68. }
  69. this.Lock()
  70. defer this.Unlock()
  71. if h, ok := this.hosts[p.Host]; ok == true {
  72. if s, ok := h.sessions[p.SessionId]; ok == true {
  73. s.Preload(p)
  74. s.Minus(p, product_id)
  75. }
  76. }
  77. return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
  78. }
  79. func (this *Basket) Remove(p *SBParam, product_id int) string {
  80. if p.Host == "" || p.SessionId == "" {
  81. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  82. }
  83. this.Lock()
  84. defer this.Unlock()
  85. if h, ok := this.hosts[p.Host]; ok == true {
  86. if s, ok := h.sessions[p.SessionId]; ok == true {
  87. s.Preload(p)
  88. s.Remove(p, product_id)
  89. }
  90. }
  91. return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
  92. }
  93. func (this *Basket) ProductsCount(p *SBParam) int {
  94. if p.Host != "" && p.SessionId != "" {
  95. this.Lock()
  96. defer this.Unlock()
  97. if h, ok := this.hosts[p.Host]; ok == true {
  98. if s, ok := h.sessions[p.SessionId]; ok == true {
  99. return s.ProductsCount()
  100. }
  101. }
  102. }
  103. return 0
  104. }