cards.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/openplatform/article/model"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/library/ecode"
  8. "go-common/library/sync/errgroup"
  9. )
  10. // FindCard find card from source
  11. func (s *Service) FindCard(c context.Context, idStr string) (res interface{}, err error) {
  12. var id int64
  13. if len(idStr) < 3 {
  14. err = ecode.RequestErr
  15. return
  16. }
  17. id, _ = strconv.ParseInt(idStr[2:], 10, 64)
  18. prefix := idStr[:2]
  19. if prefix == model.CardPrefixAudio {
  20. resp, err1 := s.dao.AudioCard(c, []int64{id})
  21. if err1 != nil {
  22. return nil, err1
  23. }
  24. if resp != nil {
  25. res = resp[id]
  26. }
  27. return res, err1
  28. }
  29. if prefix == model.CardPrefixBangumi {
  30. resp, err1 := s.dao.BangumiCard(c, []int64{id}, nil)
  31. if err1 != nil {
  32. return nil, err1
  33. }
  34. if resp != nil {
  35. res = resp[id]
  36. }
  37. return res, err1
  38. }
  39. if prefix == model.CardPrefixBangumiEp {
  40. resp, err1 := s.dao.BangumiCard(c, nil, []int64{id})
  41. if err1 != nil {
  42. return nil, err1
  43. }
  44. if resp != nil {
  45. res = resp[id]
  46. }
  47. return res, err1
  48. }
  49. if prefix == model.CardPrefixTicket {
  50. resp, err1 := s.dao.TicketCard(c, []int64{id})
  51. if err1 != nil {
  52. return nil, err1
  53. }
  54. if resp != nil {
  55. res = resp[id]
  56. }
  57. return res, err1
  58. }
  59. if prefix == model.CardPrefixMall {
  60. resp, err1 := s.dao.MallCard(c, []int64{id})
  61. if err1 != nil {
  62. return nil, err1
  63. }
  64. if resp != nil {
  65. res = resp[id]
  66. }
  67. return res, err1
  68. }
  69. if prefix == model.CardPrefixArchive {
  70. resp, err1 := s.Archives(c, []int64{id}, "")
  71. if err1 != nil {
  72. return nil, err1
  73. }
  74. if resp != nil {
  75. res = resp[id]
  76. }
  77. return res, err1
  78. }
  79. if prefix == model.CardPrefixArticle {
  80. resp, err1 := s.ArticleMeta(c, id)
  81. if err1 != nil {
  82. return nil, err1
  83. }
  84. return resp, err1
  85. }
  86. err = ecode.RequestErr
  87. return
  88. }
  89. // FindCards find cards
  90. func (s *Service) FindCards(c context.Context, ids []string) (res map[string]interface{}, err error) {
  91. var (
  92. bangumis, eps, audios, malls, tickets, archives, articles []int64
  93. bangumiRes map[int64]*model.BangumiCard
  94. epRes map[int64]*model.BangumiCard
  95. audioRes map[int64]*model.AudioCard
  96. mallRes map[int64]*model.MallCard
  97. ticketRes map[int64]*model.TicketCard
  98. archiveRes map[int64]*api.Arc
  99. articleRes map[int64]*model.Meta
  100. )
  101. for _, idStr := range ids {
  102. if len(idStr) < 2 {
  103. continue
  104. }
  105. id, _ := strconv.ParseInt(idStr[2:], 10, 64)
  106. switch idStr[:2] {
  107. case model.CardPrefixAudio:
  108. audios = append(audios, id)
  109. case model.CardPrefixBangumi:
  110. bangumis = append(bangumis, id)
  111. case model.CardPrefixBangumiEp:
  112. eps = append(eps, id)
  113. case model.CardPrefixMall:
  114. malls = append(malls, id)
  115. case model.CardPrefixTicket:
  116. tickets = append(tickets, id)
  117. case model.CardPrefixArchive:
  118. archives = append(archives, id)
  119. case model.CardPrefixArticle:
  120. articles = append(articles, id)
  121. }
  122. }
  123. group := errgroup.Group{}
  124. group.Go(func() (err error) {
  125. if len(bangumis) < 1 {
  126. return nil
  127. }
  128. if bangumiRes, err = s.dao.BangumiCard(c, bangumis, nil); err == nil {
  129. cache.Save(func() {
  130. s.dao.AddBangumiCardsCache(context.TODO(), bangumiRes)
  131. })
  132. } else {
  133. bangumiRes, _ = s.dao.BangumiCardsCache(c, bangumis)
  134. }
  135. return nil
  136. })
  137. group.Go(func() (err error) {
  138. if len(eps) < 1 {
  139. return nil
  140. }
  141. if epRes, err = s.dao.BangumiCard(c, nil, eps); err == nil {
  142. cache.Save(func() {
  143. s.dao.AddBangumiEpCardsCache(context.TODO(), epRes)
  144. })
  145. } else {
  146. epRes, _ = s.dao.BangumiEpCardsCache(c, eps)
  147. }
  148. return nil
  149. })
  150. group.Go(func() (err error) {
  151. if len(audios) < 1 {
  152. return nil
  153. }
  154. if audioRes, err = s.dao.AudioCard(c, audios); err == nil {
  155. cache.Save(func() {
  156. s.dao.AddAudioCardsCache(context.TODO(), audioRes)
  157. })
  158. } else {
  159. audioRes, _ = s.dao.AudioCardsCache(c, audios)
  160. }
  161. return nil
  162. })
  163. group.Go(func() (err error) {
  164. if len(malls) < 1 {
  165. return nil
  166. }
  167. if mallRes, err = s.dao.MallCard(c, malls); err == nil {
  168. cache.Save(func() {
  169. s.dao.AddMallCardsCache(context.TODO(), mallRes)
  170. })
  171. } else {
  172. mallRes, _ = s.dao.MallCardsCache(c, malls)
  173. }
  174. return nil
  175. })
  176. group.Go(func() (err error) {
  177. if len(tickets) < 1 {
  178. return nil
  179. }
  180. if ticketRes, err = s.dao.TicketCard(c, tickets); err == nil {
  181. cache.Save(func() {
  182. s.dao.AddTicketCardsCache(context.TODO(), ticketRes)
  183. })
  184. } else {
  185. ticketRes, _ = s.dao.TicketCardsCache(c, tickets)
  186. }
  187. return nil
  188. })
  189. group.Go(func() (err error) {
  190. if len(archives) < 1 {
  191. return nil
  192. }
  193. archiveRes, _ = s.Archives(c, archives, "")
  194. return nil
  195. })
  196. group.Go(func() (err error) {
  197. if len(articles) < 1 {
  198. return nil
  199. }
  200. articleRes, _ = s.ArticleMetas(c, articles)
  201. return nil
  202. })
  203. group.Wait()
  204. res = make(map[string]interface{})
  205. for id, v := range bangumiRes {
  206. res[model.CardPrefixBangumi+strconv.FormatInt(id, 10)] = v
  207. }
  208. for id, v := range epRes {
  209. res[model.CardPrefixBangumiEp+strconv.FormatInt(id, 10)] = v
  210. }
  211. for id, v := range ticketRes {
  212. res[model.CardPrefixTicket+strconv.FormatInt(id, 10)] = v
  213. }
  214. for id, v := range mallRes {
  215. res[model.CardPrefixMall+strconv.FormatInt(id, 10)] = v
  216. }
  217. for id, v := range audioRes {
  218. res[model.CardPrefixAudio+strconv.FormatInt(id, 10)] = v
  219. }
  220. for id, v := range archiveRes {
  221. res[model.CardPrefixArchive+strconv.FormatInt(id, 10)] = v
  222. }
  223. for id, v := range articleRes {
  224. res[model.CardPrefixArticle+strconv.FormatInt(id, 10)] = v
  225. }
  226. return
  227. }