basket.go 2.8 KB

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