basket.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package basket
  2. import (
  3. "net/http"
  4. "os"
  5. "sync"
  6. "golang-fave/consts"
  7. "golang-fave/engine/sqlw"
  8. "golang-fave/engine/wrapper/config"
  9. "golang-fave/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) ProductsCount(p *SBParam) int {
  104. if p.Host != "" && p.SessionId != "" {
  105. this.Lock()
  106. defer this.Unlock()
  107. if h, ok := this.hosts[p.Host]; ok == true {
  108. if s, ok := h.sessions[p.SessionId]; ok == true {
  109. return s.ProductsCount()
  110. }
  111. }
  112. }
  113. return 0
  114. }
  115. func (this *Basket) Cleanup(p *SBParam) {
  116. this.Lock()
  117. defer this.Unlock()
  118. for host_name, host_data := range this.hosts {
  119. var remove []string
  120. for session_id, _ := range host_data.sessions {
  121. session_file := consts.ParamWwwDir + string(os.PathSeparator) + host_name + string(os.PathSeparator) + "tmp" + string(os.PathSeparator) + session_id
  122. if !utils.IsFileExists(session_file) {
  123. remove = append(remove, session_id)
  124. }
  125. }
  126. for _, session_id := range remove {
  127. delete(host_data.sessions, session_id)
  128. }
  129. }
  130. }