service.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package data
  2. import (
  3. "context"
  4. "strings"
  5. "go-common/app/interface/main/creative/conf"
  6. "go-common/app/interface/main/creative/dao/account"
  7. "go-common/app/interface/main/creative/dao/activity"
  8. "go-common/app/interface/main/creative/dao/archive"
  9. "go-common/app/interface/main/creative/dao/article"
  10. "go-common/app/interface/main/creative/dao/bfs"
  11. "go-common/app/interface/main/creative/dao/data"
  12. "go-common/app/interface/main/creative/dao/medal"
  13. "go-common/app/interface/main/creative/dao/tag"
  14. arcMdl "go-common/app/interface/main/creative/model/archive"
  15. dataMdl "go-common/app/interface/main/creative/model/data"
  16. "go-common/app/interface/main/creative/service"
  17. "go-common/library/ecode"
  18. "go-common/library/log"
  19. "go-common/library/stat/prom"
  20. )
  21. //Service struct
  22. type Service struct {
  23. c *conf.Config
  24. data *data.Dao
  25. acc *account.Dao
  26. arc *archive.Dao
  27. art *article.Dao
  28. dtag *tag.Dao
  29. dbfs *bfs.Dao
  30. medal *medal.Dao
  31. act *activity.Dao
  32. p *service.Public
  33. pCacheHit *prom.Prom
  34. pCacheMiss *prom.Prom
  35. }
  36. //New get service
  37. func New(c *conf.Config, rpcdaos *service.RPCDaos, p *service.Public) *Service {
  38. s := &Service{
  39. c: c,
  40. data: data.New(c),
  41. acc: rpcdaos.Acc,
  42. arc: rpcdaos.Arc,
  43. art: rpcdaos.Art,
  44. dtag: tag.New(c),
  45. dbfs: bfs.New(c),
  46. medal: medal.New(c),
  47. act: activity.New(c),
  48. p: p,
  49. pCacheHit: prom.CacheHit,
  50. pCacheMiss: prom.CacheMiss,
  51. }
  52. return s
  53. }
  54. // Stat get stat.
  55. func (s *Service) Stat(c context.Context, mid int64, ip string) (r *dataMdl.Stat, err error) {
  56. if r, err = s.data.Stat(c, ip, mid); err != nil {
  57. return
  58. }
  59. pfl, err := s.acc.ProfileWithStat(c, mid)
  60. if err != nil {
  61. return
  62. }
  63. r.Fan = int64(pfl.Follower)
  64. return
  65. }
  66. // Tags get predict tag.
  67. func (s *Service) Tags(c context.Context, mid int64, tid uint16, title, filename, desc, cover string, tagFrom int8) (res *dataMdl.Tags, err error) {
  68. res = &dataMdl.Tags{
  69. Tags: make([]string, 0),
  70. }
  71. var ctags []*dataMdl.CheckedTag
  72. ctags, err = s.TagsWithChecked(c, mid, tid, title, filename, desc, cover, tagFrom)
  73. if len(ctags) > 0 {
  74. for _, t := range ctags {
  75. res.Tags = append(res.Tags, t.Tag)
  76. }
  77. }
  78. return
  79. }
  80. // TagsWithChecked get predict tag with checked mark.
  81. func (s *Service) TagsWithChecked(c context.Context, mid int64, tid uint16, title, filename, desc, cover string, tagFrom int8) (retTags []*dataMdl.CheckedTag, err error) {
  82. retTags = make([]*dataMdl.CheckedTag, 0)
  83. var checkedTags []*dataMdl.CheckedTag
  84. if checkedTags, err = s.data.TagsWithChecked(c, mid, tid, title, filename, desc, cover, tagFrom); err != nil {
  85. log.Error("s.data.TagsWithChecked error(%v)", err)
  86. err = nil
  87. }
  88. // no recommend tag, return
  89. if len(checkedTags) == 0 {
  90. return
  91. }
  92. actTagsMap := make(map[string]int64)
  93. acts, _ := s.act.Activities(c)
  94. // no acts, no filter and return
  95. if len(acts) == 0 {
  96. retTags = checkedTags
  97. return
  98. }
  99. // filter act tags
  100. for _, act := range acts {
  101. tagsSlice := strings.Split(act.Tags, ",")
  102. if len(tagsSlice) > 0 {
  103. for _, tag := range tagsSlice {
  104. actTagsMap[tag] = act.ID
  105. }
  106. }
  107. actTagsMap[act.Name] = act.ID
  108. }
  109. for _, checkedTag := range checkedTags {
  110. if _, ok := actTagsMap[checkedTag.Tag]; !ok {
  111. retTags = append(retTags, checkedTag)
  112. }
  113. }
  114. return
  115. }
  116. // VideoQuitPoints get video quit data.
  117. func (s *Service) VideoQuitPoints(c context.Context, cid, mid int64, ip string) (res []int64, err error) {
  118. // check cid owner
  119. isWhite := false
  120. for _, m := range s.c.Whitelist.DataMids {
  121. if m == mid {
  122. isWhite = true
  123. break
  124. }
  125. }
  126. if !isWhite {
  127. var video *arcMdl.Video
  128. if video, err = s.arc.VideoByCid(c, int64(cid), ip); err != nil {
  129. err = ecode.AccessDenied
  130. return
  131. }
  132. if arc, errr := s.arc.Archive(c, video.Aid, ip); errr == nil {
  133. if arc.Author.Mid != mid {
  134. err = ecode.AccessDenied
  135. return
  136. }
  137. }
  138. }
  139. res, err = s.data.VideoQuitPoints(c, cid)
  140. return
  141. }
  142. // ArchiveStat get video quit data.
  143. func (s *Service) ArchiveStat(c context.Context, aid, mid int64, ip string) (stat *dataMdl.ArchiveData, err error) {
  144. // check aid valid and owner
  145. isWhite := false
  146. for _, m := range s.c.Whitelist.DataMids {
  147. if m == mid {
  148. isWhite = true
  149. break
  150. }
  151. }
  152. if !isWhite {
  153. a, re := s.arc.Archive(c, aid, ip)
  154. if re != nil {
  155. err = re
  156. return
  157. }
  158. if a == nil {
  159. log.Warn("arc not exist")
  160. err = ecode.AccessDenied
  161. return
  162. }
  163. if a.Author.Mid != mid {
  164. log.Warn("owner mid(%d) and viewer mid(%d) are different ", a.Author.Mid, mid)
  165. err = ecode.AccessDenied
  166. return
  167. }
  168. }
  169. if stat, err = s.data.ArchiveStat(c, aid); err != nil {
  170. return
  171. }
  172. if stat == nil {
  173. return
  174. }
  175. if stat.ArchiveStat != nil && stat.ArchiveStat.Play >= 100 {
  176. if stat.ArchiveAreas, err = s.data.ArchiveArea(c, aid); err != nil {
  177. log.Error(" s.data.ArchiveArea aid(%d)|mid(%d), err(%+v)", aid, mid, err)
  178. return
  179. }
  180. }
  181. //获取播放数据
  182. if stat.ArchivePlay, err = s.data.UpArcPlayAnalysis(c, aid); err != nil {
  183. log.Error(" s.data.UpArcPlayAnalysis aid(%d)|mid(%d), err(%+v)", aid, mid, err)
  184. return
  185. }
  186. return
  187. }
  188. // Covers get covers from ai
  189. func (s *Service) Covers(c context.Context, mid int64, fns []string, ip string) (cvs []string, err error) {
  190. if cvs, err = s.data.RecommendCovers(c, mid, fns); err != nil {
  191. log.Error("s.data.RecommendCovers err(%v)", mid, err)
  192. return
  193. }
  194. return
  195. }
  196. // Ping service
  197. func (s *Service) Ping(c context.Context) (err error) {
  198. if err = s.data.Ping(c); err != nil {
  199. log.Error("s.data.Dao.PingDb err(%v)", err)
  200. }
  201. return
  202. }
  203. // Close dao
  204. func (s *Service) Close() {
  205. s.data.Close()
  206. }
  207. // IsWhite check white.
  208. func (s *Service) IsWhite(mid int64) (isWhite bool) {
  209. for _, m := range s.c.Whitelist.DataMids {
  210. if m == mid {
  211. isWhite = true
  212. break
  213. }
  214. }
  215. return
  216. }
  217. // IsForbidVideoup fn.
  218. func (s *Service) IsForbidVideoup(mid int64) (isForbid bool) {
  219. for _, m := range s.c.Whitelist.ForbidVideoupMids {
  220. if m == mid {
  221. isForbid = true
  222. break
  223. }
  224. }
  225. return
  226. }