sug.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/job/openplatform/open-sug/model"
  6. "go-common/library/log"
  7. )
  8. func (s *Service) buildData() (err error) {
  9. var (
  10. itemList []*model.Item
  11. scoreSlice []model.Score
  12. )
  13. if itemList, err = s.dao.FetchItem(context.TODO()); err != nil {
  14. log.Error("pull project list error (%v)", err)
  15. return
  16. }
  17. for _, item := range itemList {
  18. if scoreSlice, err = s.dao.SeasonData(context.TODO(), item); err != nil {
  19. log.Error("es item match season fail")
  20. continue
  21. }
  22. if len(scoreSlice) > 0 {
  23. if _, err = s.dao.SetItem(context.TODO(), item); err != nil {
  24. log.Error("set item redis error(%v)", err)
  25. continue
  26. }
  27. for _, score := range scoreSlice {
  28. score.Score = score.Score * s.BuildScore(item, score.SeasonID)
  29. if rowNum, _ := s.dao.InsertMatch(context.TODO(), item, score); rowNum > 0 {
  30. if _, err = s.dao.SetSug(context.TODO(), score.SeasonID, item.ID, score.Score); err != nil {
  31. log.Error("redis dao.SetSug error(%v)", err)
  32. continue
  33. }
  34. }
  35. }
  36. }
  37. }
  38. return
  39. }
  40. // BuildScore ...
  41. func (s *Service) BuildScore(item *model.Item, seasonID string) (normalizationScore float64) {
  42. normalizationScore = 0.5*(s.Normalization(item.SalesCount, s.dao.ItemSalesMax[seasonID], s.dao.ItemSalesMin[seasonID])) + 0.2*(s.Normalization(item.CommentCount, s.dao.ItemCommentMax[seasonID], s.dao.ItemCommentMin[seasonID])) + 0.3*(s.Normalization(item.WishCount, s.dao.ItemWishMax[seasonID], s.dao.ItemCommentMin[seasonID]))
  43. return
  44. }
  45. // Normalization ...
  46. func (s *Service) Normalization(x int, max int, min int) (normalizationScore float64) {
  47. if x == 0 {
  48. return 0
  49. }
  50. if float64(max-min) == 0 {
  51. return 1
  52. }
  53. return float64(x-min) / float64(max-min)
  54. }
  55. // Filter ...
  56. func (s *Service) Filter() {
  57. c := context.TODO()
  58. if bindItems, err := s.dao.GetBind(c); err == nil {
  59. for _, buildItem := range bindItems {
  60. s.dao.GetItem(c, buildItem)
  61. log.Info("add filter data items_id(%d) season_id(%d)", buildItem.Item.ID, buildItem.SeasonID)
  62. if _, err = s.dao.SetItem(c, buildItem.Item); err != nil {
  63. log.Error("set item redis error(%v)", err)
  64. continue
  65. }
  66. log.Info("add sug res items_id(%d) season_id(%d)", buildItem.Item.ID, buildItem.SeasonID)
  67. if _, err = s.dao.SetSug(c, strconv.FormatInt(buildItem.SeasonID, 10), buildItem.Item.ID, float64(buildItem.Score)); err != nil {
  68. log.Error("redis dao.SetSug error(%v)", err)
  69. continue
  70. }
  71. s.dao.UpdatePic(c, buildItem)
  72. }
  73. }
  74. }