fav.go 956 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package archive
  2. import (
  3. "context"
  4. "sort"
  5. "strconv"
  6. "go-common/app/interface/main/creative/model/archive"
  7. )
  8. // get max 5 fav types
  9. func (s *Service) favTypes(c context.Context, mid int64) (favTps []*archive.Type) {
  10. favTps = make([]*archive.Type, 0)
  11. var res map[string]int64
  12. res, _ = s.arc.FavTypes(c, mid)
  13. if len(res) > 0 {
  14. type kv struct {
  15. TidStr string
  16. Timestamp int64
  17. }
  18. var kvSlice []kv
  19. for k, v := range res {
  20. kvSlice = append(kvSlice, kv{k, v})
  21. }
  22. sort.Slice(kvSlice, func(i, j int) bool {
  23. return kvSlice[i].Timestamp > kvSlice[j].Timestamp
  24. })
  25. for _, v := range kvSlice {
  26. tid, _ := strconv.Atoi(v.TidStr)
  27. if tp, ok := s.p.TypeMapCache[int16(tid)]; ok && len(favTps) < 5 {
  28. favTps = append(favTps, tp)
  29. }
  30. }
  31. }
  32. return
  33. }
  34. // Fav fn
  35. func (s *Service) Fav(c context.Context, mid int64) (res map[string]interface{}) {
  36. res = make(map[string]interface{})
  37. res["typelist"] = s.favTypes(c, mid)
  38. return
  39. }