basket.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package basket
  2. import (
  3. "net/http"
  4. "os"
  5. "sync"
  6. "golang-fave/engine/config"
  7. "golang-fave/engine/consts"
  8. "golang-fave/engine/sqlw"
  9. "golang-fave/engine/utils"
  10. )
  11. type SBParam struct {
  12. R *http.Request
  13. DB *sqlw.DB
  14. Host string
  15. Config *config.Config
  16. SessionId string
  17. }
  18. type Basket struct {
  19. sync.RWMutex
  20. hosts map[string]*onehost
  21. }
  22. func New() *Basket {
  23. b := Basket{}
  24. b.hosts = map[string]*onehost{}
  25. return &b
  26. }
  27. func (this *Basket) Info(p *SBParam) string {
  28. if p.Host == "" || p.SessionId == "" {
  29. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  30. }
  31. this.Lock()
  32. defer this.Unlock()
  33. if h, ok := this.hosts[p.Host]; ok == true {
  34. if s, ok := h.sessions[p.SessionId]; ok == true {
  35. s.Preload(p)
  36. return s.String(p)
  37. } else {
  38. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  39. }
  40. } else {
  41. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  42. }
  43. }
  44. func (this *Basket) Plus(p *SBParam, product_id int) string {
  45. if p.Host == "" || p.SessionId == "" {
  46. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  47. }
  48. this.Lock()
  49. defer this.Unlock()
  50. if h, ok := this.hosts[p.Host]; ok == true {
  51. if s, ok := h.sessions[p.SessionId]; ok == true {
  52. s.Preload(p)
  53. s.Plus(p, product_id)
  54. } else {
  55. s := &session{}
  56. s.listCurrencies = map[int]*currency{}
  57. s.Products = map[int]*product{}
  58. s.Preload(p)
  59. s.Plus(p, product_id)
  60. h.sessions[p.SessionId] = s
  61. }
  62. } else {
  63. s := &session{}
  64. s.listCurrencies = map[int]*currency{}
  65. s.Products = map[int]*product{}
  66. s.Preload(p)
  67. s.Plus(p, product_id)
  68. h := &onehost{}
  69. h.sessions = map[string]*session{}
  70. h.sessions[p.SessionId] = s
  71. this.hosts[p.Host] = h
  72. }
  73. return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
  74. }
  75. func (this *Basket) Minus(p *SBParam, product_id int) string {
  76. if p.Host == "" || p.SessionId == "" {
  77. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  78. }
  79. this.Lock()
  80. defer this.Unlock()
  81. if h, ok := this.hosts[p.Host]; ok == true {
  82. if s, ok := h.sessions[p.SessionId]; ok == true {
  83. s.Preload(p)
  84. s.Minus(p, product_id)
  85. }
  86. }
  87. return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
  88. }
  89. func (this *Basket) Remove(p *SBParam, product_id int) string {
  90. if p.Host == "" || p.SessionId == "" {
  91. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  92. }
  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. s.Preload(p)
  98. s.Remove(p, product_id)
  99. }
  100. }
  101. return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
  102. }
  103. func (this *Basket) ClearBasket(p *SBParam) {
  104. if p.Host == "" || p.SessionId == "" {
  105. return
  106. }
  107. this.Lock()
  108. defer this.Unlock()
  109. if h, ok := this.hosts[p.Host]; ok == true {
  110. if s, ok := h.sessions[p.SessionId]; ok == true {
  111. s.Preload(p)
  112. s.ClearBasket(p)
  113. }
  114. }
  115. }
  116. func (this *Basket) ProductsCount(p *SBParam) int {
  117. if p.Host != "" && p.SessionId != "" {
  118. this.Lock()
  119. defer this.Unlock()
  120. if h, ok := this.hosts[p.Host]; ok == true {
  121. if s, ok := h.sessions[p.SessionId]; ok == true {
  122. return s.ProductsCount()
  123. }
  124. }
  125. }
  126. return 0
  127. }
  128. func (this *Basket) GetAll(p *SBParam) *utils.MySql_basket {
  129. if p.Host != "" && p.SessionId != "" {
  130. this.Lock()
  131. defer this.Unlock()
  132. if h, ok := this.hosts[p.Host]; ok == true {
  133. if s, ok := h.sessions[p.SessionId]; ok == true {
  134. return s.GetAll(p)
  135. }
  136. }
  137. }
  138. return nil
  139. }
  140. func (this *Basket) Cleanup() {
  141. this.Lock()
  142. defer this.Unlock()
  143. for host_name, host_data := range this.hosts {
  144. var remove []string
  145. for session_id, _ := range host_data.sessions {
  146. session_file := consts.ParamWwwDir + string(os.PathSeparator) + host_name + string(os.PathSeparator) + "tmp" + string(os.PathSeparator) + session_id
  147. if !utils.IsFileExists(session_file) {
  148. remove = append(remove, session_id)
  149. }
  150. }
  151. for _, session_id := range remove {
  152. delete(host_data.sessions, session_id)
  153. }
  154. }
  155. }