subtitle_get.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/dm2/model"
  5. "go-common/library/log"
  6. )
  7. // getSubtitlDraft get a subtitle
  8. // cache throught
  9. func (s *Service) getSubtitlDraft(c context.Context, oid int64, tp int32, mid int64, lanCode uint8) (draft *model.Subtitle, err error) {
  10. var (
  11. cacheErr bool
  12. )
  13. if draft, err = s.dao.SubtitleDraftCache(c, oid, tp, mid, lanCode); err != nil {
  14. cacheErr = true
  15. err = nil
  16. }
  17. if draft != nil {
  18. if draft.ID <= 0 {
  19. draft = nil
  20. err = nil
  21. }
  22. return
  23. }
  24. if draft, err = s.dao.GetSubtitleDraft(c, oid, tp, mid, lanCode); err != nil {
  25. log.Error("params(oid:%v,tp:%v,mid:%v,lanCode:%v).error(%v)", oid, tp, mid, lanCode, err)
  26. return
  27. }
  28. if draft == nil {
  29. draft = &model.Subtitle{
  30. Oid: oid,
  31. Type: tp,
  32. Mid: mid,
  33. Lan: lanCode,
  34. }
  35. }
  36. if !cacheErr {
  37. temp := draft
  38. s.cache.Do(c, func(ctx context.Context) {
  39. s.dao.SetSubtitleDraftCache(ctx, temp)
  40. })
  41. }
  42. if draft.ID <= 0 {
  43. draft = nil
  44. err = nil
  45. }
  46. return
  47. }
  48. // GetSubtitle get a subtitle
  49. func (s *Service) getSubtitle(c context.Context, oid int64, subtitleID int64) (subtitle *model.Subtitle, err error) {
  50. var (
  51. cacheErr bool
  52. )
  53. if subtitle, err = s.dao.SubtitleCache(c, oid, subtitleID); err != nil {
  54. cacheErr = true
  55. err = nil
  56. }
  57. if subtitle != nil {
  58. if subtitle.Empty {
  59. subtitle = nil
  60. err = nil
  61. }
  62. return
  63. }
  64. if subtitle, err = s.dao.GetSubtitle(c, oid, subtitleID); err != nil {
  65. log.Error("params(oid:%v, subtitleID:%v).error(%v)", oid, subtitleID, err)
  66. return
  67. }
  68. if subtitle == nil {
  69. subtitle = &model.Subtitle{
  70. Oid: oid,
  71. ID: subtitleID,
  72. Empty: true,
  73. }
  74. }
  75. if !cacheErr {
  76. temp := subtitle
  77. s.cache.Do(c, func(ctx context.Context) {
  78. s.dao.SetSubtitleCache(ctx, temp)
  79. })
  80. }
  81. if subtitle.Empty {
  82. subtitle = nil
  83. err = nil
  84. }
  85. return
  86. }
  87. // getSubtitles 不保证顺序
  88. func (s *Service) getSubtitles(c context.Context, oid int64, subtitleIds []int64) (subtitles map[int64]*model.Subtitle, err error) {
  89. var (
  90. hits map[int64]*model.Subtitle
  91. missed []int64
  92. cacheErr bool
  93. missedSubtitles []*model.Subtitle
  94. )
  95. if hits, missed, err = s.dao.SubtitlesCache(c, oid, subtitleIds); err != nil {
  96. cacheErr = true
  97. err = nil
  98. }
  99. subtitles = make(map[int64]*model.Subtitle)
  100. for _, subtitle := range hits {
  101. if subtitle.Empty {
  102. missed = append(missed, subtitle.ID)
  103. continue
  104. }
  105. subtitles[subtitle.ID] = subtitle
  106. }
  107. if len(missed) > 0 {
  108. if missedSubtitles, err = s.dao.GetSubtitles(c, oid, missed); err != nil {
  109. log.Error("getSubtitles(oid:%v,subtitleIds:%v),error(%v)", oid, subtitleIds, err)
  110. return
  111. }
  112. }
  113. for _, subtitle := range missedSubtitles {
  114. subtitles[subtitle.ID] = subtitle
  115. }
  116. if !cacheErr {
  117. for _, subtitle := range missedSubtitles {
  118. temp := subtitle
  119. s.cache.Do(c, func(ctx context.Context) {
  120. s.dao.SetSubtitleCache(ctx, temp)
  121. })
  122. }
  123. }
  124. return
  125. }