basket.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. } else {
  52. s := &session{}
  53. s.listCurrencies = map[int]*currency{}
  54. s.Products = map[int]*product{}
  55. s.Preload(p)
  56. s.Plus(p, product_id)
  57. h.sessions[p.SessionId] = s
  58. }
  59. } else {
  60. s := &session{}
  61. s.listCurrencies = map[int]*currency{}
  62. s.Products = map[int]*product{}
  63. s.Preload(p)
  64. s.Plus(p, product_id)
  65. h := &onehost{}
  66. h.sessions = map[string]*session{}
  67. h.sessions[p.SessionId] = s
  68. this.hosts[p.Host] = h
  69. }
  70. return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
  71. }
  72. func (this *Basket) Minus(p *SBParam, product_id int) string {
  73. if p.Host == "" || p.SessionId == "" {
  74. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  75. }
  76. this.Lock()
  77. defer this.Unlock()
  78. if h, ok := this.hosts[p.Host]; ok == true {
  79. if s, ok := h.sessions[p.SessionId]; ok == true {
  80. s.Preload(p)
  81. s.Minus(p, product_id)
  82. }
  83. }
  84. return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
  85. }
  86. func (this *Basket) Remove(p *SBParam, product_id int) string {
  87. if p.Host == "" || p.SessionId == "" {
  88. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  89. }
  90. this.Lock()
  91. defer this.Unlock()
  92. if h, ok := this.hosts[p.Host]; ok == true {
  93. if s, ok := h.sessions[p.SessionId]; ok == true {
  94. s.Preload(p)
  95. s.Remove(p, product_id)
  96. }
  97. }
  98. return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
  99. }
  100. func (this *Basket) ProductsCount(p *SBParam) int {
  101. if p.Host != "" && p.SessionId != "" {
  102. this.Lock()
  103. defer this.Unlock()
  104. if h, ok := this.hosts[p.Host]; ok == true {
  105. if s, ok := h.sessions[p.SessionId]; ok == true {
  106. return s.ProductsCount()
  107. }
  108. }
  109. }
  110. return 0
  111. }