123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package basket
- import (
- "net/http"
- "os"
- "sync"
- "golang-fave/engine/config"
- "golang-fave/engine/consts"
- "golang-fave/engine/sqlw"
- "golang-fave/engine/utils"
- )
- type SBParam struct {
- R *http.Request
- DB *sqlw.DB
- Host string
- Config *config.Config
- SessionId string
- }
- type Basket struct {
- sync.RWMutex
- hosts map[string]*onehost
- }
- func New() *Basket {
- b := Basket{}
- b.hosts = map[string]*onehost{}
- return &b
- }
- func (this *Basket) Info(p *SBParam) string {
- if p.Host == "" || p.SessionId == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- s.Preload(p)
- return s.String(p)
- } else {
- return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
- }
- } else {
- return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
- }
- }
- func (this *Basket) Plus(p *SBParam, product_id int) string {
- if p.Host == "" || p.SessionId == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- s.Preload(p)
- s.Plus(p, product_id)
- } else {
- s := &session{}
- s.listCurrencies = map[int]*currency{}
- s.Products = map[int]*product{}
- s.Preload(p)
- s.Plus(p, product_id)
- h.sessions[p.SessionId] = s
- }
- } else {
- s := &session{}
- s.listCurrencies = map[int]*currency{}
- s.Products = map[int]*product{}
- s.Preload(p)
- s.Plus(p, product_id)
- h := &onehost{}
- h.sessions = map[string]*session{}
- h.sessions[p.SessionId] = s
- this.hosts[p.Host] = h
- }
- return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
- }
- func (this *Basket) Minus(p *SBParam, product_id int) string {
- if p.Host == "" || p.SessionId == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- s.Preload(p)
- s.Minus(p, product_id)
- }
- }
- return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
- }
- func (this *Basket) Remove(p *SBParam, product_id int) string {
- if p.Host == "" || p.SessionId == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- s.Preload(p)
- s.Remove(p, product_id)
- }
- }
- return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
- }
- func (this *Basket) ClearBasket(p *SBParam) {
- if p.Host == "" || p.SessionId == "" {
- return
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- s.Preload(p)
- s.ClearBasket(p)
- }
- }
- }
- func (this *Basket) ProductsCount(p *SBParam) int {
- if p.Host != "" && p.SessionId != "" {
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- return s.ProductsCount()
- }
- }
- }
- return 0
- }
- func (this *Basket) GetAll(p *SBParam) *utils.MySql_basket {
- if p.Host != "" && p.SessionId != "" {
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[p.Host]; ok == true {
- if s, ok := h.sessions[p.SessionId]; ok == true {
- return s.GetAll(p)
- }
- }
- }
- return nil
- }
- func (this *Basket) Cleanup() {
- this.Lock()
- defer this.Unlock()
- for host_name, host_data := range this.hosts {
- var remove []string
- for session_id, _ := range host_data.sessions {
- session_file := consts.ParamWwwDir + string(os.PathSeparator) + host_name + string(os.PathSeparator) + "tmp" + string(os.PathSeparator) + session_id
- if !utils.IsFileExists(session_file) {
- remove = append(remove, session_id)
- }
- }
- for _, session_id := range remove {
- delete(host_data.sessions, session_id)
- }
- }
- }
|