recommend.go 943 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/spf13/viper"
  6. "github.com/zhenghaoz/gorse/client"
  7. "time"
  8. )
  9. var gorse *client.GorseClient
  10. func init() {
  11. gorse = client.NewGorseClient(viper.GetString("gorse.addr"), "")
  12. }
  13. func Recommend(uid, goodsId, tie uint) {
  14. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  15. defer cancel()
  16. t := time.Unix(int64(tie), 0)
  17. date := t.Format("2006-01-02")
  18. _, err := gorse.InsertFeedback(ctx, []client.Feedback{
  19. {FeedbackType: "star",
  20. UserId: fmt.Sprintf("%d", uid),
  21. ItemId: fmt.Sprintf("%d", goodsId),
  22. Timestamp: date},
  23. })
  24. if err != nil {
  25. fmt.Println(err.Error())
  26. }
  27. }
  28. func GetRecommend(uid uint, cnt int) (goods []string, err error) {
  29. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  30. defer cancel()
  31. category, err := gorse.GetItemRecommendWithCategory(ctx, fmt.Sprintf("%d", uid), "", "", "", 0, cnt)
  32. return category, err
  33. }