season.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/openplatform/sug/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _setup int8 = 1
  11. _remove int8 = 0
  12. _none int8 = -1
  13. )
  14. // SourceSearch search season or items.
  15. func (s *Service) SourceSearch(c context.Context, params *model.SourceSearch) (interface{}, error) {
  16. switch params.Type {
  17. case model.TypeSeason:
  18. return s.dao.SeasonList(c, params)
  19. case model.TypeItems:
  20. return s.dao.ItemList(c, params)
  21. }
  22. return nil, ecode.SugSearchTypeErr
  23. }
  24. // Search search season and items match list.
  25. func (s *Service) Search(c context.Context, params *model.Search) (list []model.SugList, err error) {
  26. list, err = s.dao.SearchV2(c, params)
  27. if err != nil {
  28. err = ecode.SugEsSearchErr
  29. }
  30. if listLen := len(list); listLen > 0 {
  31. for i := 0; i < listLen-1; i++ {
  32. for j := 0; j < listLen-i-1; j++ {
  33. if list[j].Score < list[j+1].Score {
  34. list[j], list[j+1] = list[j+1], list[j]
  35. }
  36. }
  37. }
  38. }
  39. return
  40. }
  41. // MatchOperate match operate.
  42. func (s *Service) MatchOperate(c context.Context, params *model.MatchOperate) (result interface{}, err error) {
  43. item, err := s.dao.GetItem(c, params.ItemsID)
  44. if err != nil {
  45. err = ecode.SugItemNone
  46. return
  47. }
  48. season, err := s.dao.GetSeason(c, params.SeasonID)
  49. if err != nil || season.ID == 0 {
  50. err = ecode.SugSeasonNone
  51. return
  52. }
  53. switch params.OpType {
  54. case _setup:
  55. if _, err = s.AddMatch(c, item, season); err != nil {
  56. err = ecode.SugOpErr
  57. return
  58. }
  59. case _remove:
  60. if err = s.DelMatch(c, item, season); err != nil {
  61. err = ecode.SugOpErr
  62. return
  63. }
  64. default:
  65. err = ecode.SugOpTypeErr
  66. }
  67. return
  68. }
  69. // DbOperate db operate.
  70. func (s *Service) DbOperate(c context.Context, op int8, item model.Item, season model.Season, location string) (err error) {
  71. matchType, err := s.dao.GetMatchType(c, season.ID, item.ItemsID)
  72. if err != nil {
  73. return err
  74. }
  75. if matchType == _none {
  76. if _, err = s.dao.InsertMatch(c, season, item, op, time.Now().Unix(), location); err != nil {
  77. return err
  78. }
  79. }
  80. if _, err = s.dao.UpdateMatch(c, season, item, op, location); err != nil {
  81. return err
  82. }
  83. return
  84. }
  85. // AddMatch add match.
  86. func (s *Service) AddMatch(c context.Context, item model.Item, season model.Season) (location string, err error) {
  87. if location, _ = s.dao.CreateItemPNG(item); location == "" {
  88. return
  89. }
  90. if err = s.DbOperate(c, _setup, item, season, location); err != nil {
  91. return
  92. }
  93. if _, err := s.dao.SetItem(c, &item, location); err != nil {
  94. log.Error("set item(%v) error(%v)", item, err)
  95. }
  96. if err := s.dao.SetSug(c, season.ID, item.ItemsID, time.Now().Unix()); err != nil {
  97. log.Error("zAdd season(%v) error(%v)", season, err)
  98. }
  99. return
  100. }
  101. // DelMatch del match.
  102. func (s *Service) DelMatch(c context.Context, item model.Item, season model.Season) (err error) {
  103. if err = s.DbOperate(c, _remove, item, season, ""); err != nil {
  104. return
  105. }
  106. if err = s.dao.DelSug(c, season.ID, item.ItemsID); err != nil {
  107. log.Error("zRem item(%d) error(%v)", item, err)
  108. }
  109. return
  110. }