timemachine.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package timemachine
  2. import (
  3. "context"
  4. "sort"
  5. "strconv"
  6. "strings"
  7. "time"
  8. model "go-common/app/interface/main/activity/model/timemachine"
  9. tagmdl "go-common/app/interface/main/tag/api"
  10. artmdl "go-common/app/interface/openplatform/article/model"
  11. accmdl "go-common/app/service/main/account/api"
  12. arcmdl "go-common/app/service/main/archive/api"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. "go-common/library/net/metadata"
  16. "go-common/library/sync/errgroup.v2"
  17. )
  18. // StartTmproc start tm proc.
  19. //func (s *Service) StartTmproc(c context.Context) (err error) {
  20. // s.dao.StartTmProc(context.Background())
  21. // return
  22. //}
  23. // StopTmproc start tm proc.
  24. //func (s *Service) StopTmproc(c context.Context) (err error) {
  25. // s.dao.StopTmproc(c)
  26. // return
  27. //}
  28. // Timemachine2018Raw .
  29. func (s *Service) Timemachine2018Raw(c context.Context, loginMid, mid int64) (data *model.Item, err error) {
  30. if _, ok := s.tmMidMap[loginMid]; !ok {
  31. err = ecode.AccessDenied
  32. return
  33. }
  34. if mid == 0 {
  35. mid = loginMid
  36. }
  37. if data, err = s.dao.RawTimemachine(c, mid); err != nil {
  38. log.Error("Timemachine2018 s.dao.RawTimemachine(%d) error(%v)", mid, err)
  39. }
  40. return
  41. }
  42. // Timemachine2018Cache .
  43. func (s *Service) Timemachine2018Cache(c context.Context, loginMid, mid int64) (data *model.Item, err error) {
  44. if _, ok := s.tmMidMap[loginMid]; !ok {
  45. err = ecode.AccessDenied
  46. return
  47. }
  48. if mid == 0 {
  49. mid = loginMid
  50. }
  51. if data, err = s.dao.CacheTimemachine(c, mid); err != nil {
  52. log.Error("Timemachine2018Cache s.dao.CacheTimemachine(%d) error(%v)", mid, err)
  53. }
  54. return
  55. }
  56. // Timemachine2018 .
  57. func (s *Service) Timemachine2018(c context.Context, loginMid, mid int64) (data *model.Timemachine, err error) {
  58. if _, ok := s.tmMidMap[loginMid]; !ok {
  59. mid = loginMid
  60. } else {
  61. if mid == 0 {
  62. mid = loginMid
  63. }
  64. }
  65. var item *model.Item
  66. if item, err = s.dao.CacheTimemachine(c, mid); err != nil {
  67. log.Error("Timemachine2018 s.dao.Timemachine(%d) error(%v)", mid, err)
  68. err = nil
  69. }
  70. if item == nil || item.DurationHour == 0 || (item.LikeUpAvDuration == 0 && item.LikeUpLiveDuration == 0) {
  71. item = &model.Item{Mid: mid}
  72. }
  73. data = s.groupTmData(c, item)
  74. return
  75. }
  76. func (s *Service) groupTmData(c context.Context, item *model.Item) (data *model.Timemachine) {
  77. var (
  78. aids, viewAids, upAids, artIDs, mids []int64
  79. totalView, ugcView, pgcView []*model.AidView
  80. upBestLiveFanMid, liveMinute, upBestFanMid, favVv, subTidBest int64
  81. )
  82. data = &model.Timemachine{
  83. Mid: item.Mid,
  84. IsUp: item.IsUp,
  85. DurationHour: item.DurationHour,
  86. ArchiveVv: item.ArchiveVv,
  87. BrainwashCirVv: item.BrainwashCirVv,
  88. FirstSubmitType: item.FirstSubmitType,
  89. LikeSubtidVv: item.LikeSubtidVv,
  90. LikeUpAvDuration: item.LikeUpAvDuration,
  91. LikeUpLiveDuration: item.LikeUpLiveDuration,
  92. LikeUpDuration: item.LikeUpAvDuration + item.LikeUpLiveDuration,
  93. LikeLiveUpSubTname: item.LikeLiveUpSubTname,
  94. BestAvidType: item.BestAvidType,
  95. AllVv: item.AllVv,
  96. BestAvidOldType: item.BestAvidOldType,
  97. OldAvVv: item.OldAvVv,
  98. UpLiveDuration: item.UpLiveDuration,
  99. IsLiveUp: item.IsLiveUp,
  100. ValidLiveDays: item.ValidLiveDays,
  101. AddAttentions: item.Attentions,
  102. WinRatio: item.WinRatio,
  103. SubmitAvsRds: item.SubmitAvsRds,
  104. }
  105. // win ratio fix
  106. if data.WinRatio == "100%" {
  107. data.WinRatio = "99%"
  108. }
  109. if data.ValidLiveDays > 365 {
  110. data.ValidLiveDays = 365
  111. }
  112. if item.Like2SubTids != "" {
  113. subTidBest = s.groupTagDesc(item, data)
  114. }
  115. if tagDesc, ok := s.tagDescMap[item.LikeTagID]; ok && tagDesc != nil {
  116. data.LikeTagDescFirst = tagDesc.Desc1
  117. data.LikeTagDescSecond = tagDesc.Desc2Line1
  118. data.LikeTagDescSecond2 = tagDesc.Desc2Line2
  119. } else if subTidBest > 0 {
  120. if tagRegDesc, ok := s.tagRegionDescMap[subTidBest]; ok && tagRegDesc != nil {
  121. data.LikeTagDescFirst = tagRegDesc.Desc1
  122. data.LikeTagDescSecond = tagRegDesc.Desc2Line1
  123. data.LikeTagDescSecond2 = tagRegDesc.Desc2Line2
  124. }
  125. }
  126. if braTime, e := time.Parse("20060102", item.BrainwashCirTime); e != nil {
  127. log.Warn("groupTmData BrainwashCirTime time.Parse(%s) warn(%v)", item.BrainwashCirTime, e)
  128. } else {
  129. data.BrainwashCirTime = braTime.Format("2006.01.02")
  130. }
  131. // LikeUpDuration data fix
  132. if data.LikeUpDuration > item.DurationHour*60 {
  133. if data.LikeUpDuration < (item.AvDurationHour*60 + item.LikeUpLiveDuration) {
  134. if (item.AvDurationHour*60 + item.LikeUpLiveDuration) > 0 {
  135. data.LikeUpDuration = int64(float64(item.DurationHour*60) * (float64(data.LikeUpDuration) / float64(item.AvDurationHour*60+item.LikeUpLiveDuration)))
  136. }
  137. } else {
  138. if (item.PlayDurationHourRep*60 + item.LikeUpLiveDuration) > 0 {
  139. data.LikeUpDuration = int64(float64(item.DurationHour*60) * (float64(item.LikeUpAvDurationRep+item.LikeUpLiveDuration) / float64(item.PlayDurationHourRep*60+item.LikeUpLiveDuration)))
  140. }
  141. }
  142. }
  143. realIP := metadata.String(c, metadata.RemoteIP)
  144. group := errgroup.WithCancel(c)
  145. // tag data
  146. if item.LikeTagID > 0 {
  147. group.Go(func(ctx context.Context) error {
  148. if tag, e := s.tagClient.Tag(ctx, &tagmdl.TagReq{Tid: item.LikeTagID}); e != nil || tag == nil {
  149. log.Error("s.tagClient.Tag tid(%d) error(%v)", item.LikeTagID, e)
  150. } else {
  151. data.LikeTagID = item.LikeTagID
  152. data.LikeTagName = tag.Tag.Name
  153. }
  154. return nil
  155. })
  156. }
  157. // group aids
  158. if item.BestAvid > 0 {
  159. aids = append(aids, item.BestAvid)
  160. }
  161. if item.LikesUgc3Avids != "" {
  162. list := strings.Split(item.LikesUgc3Avids, ",")
  163. for _, v := range list {
  164. items := strings.Split(v, ":")
  165. if len(items) == 2 {
  166. aid, e := strconv.ParseInt(items[0], 10, 64)
  167. if e != nil {
  168. continue
  169. }
  170. vv, e := strconv.ParseInt(items[1], 10, 64)
  171. if e != nil {
  172. continue
  173. }
  174. ugcView = append(ugcView, &model.AidView{Aid: aid, View: vv})
  175. }
  176. }
  177. }
  178. if item.LikePgc3Avids != "" {
  179. list := strings.Split(item.LikePgc3Avids, ",")
  180. for _, v := range list {
  181. items := strings.Split(v, "@")
  182. if len(items) == 2 {
  183. aid, e := strconv.ParseInt(items[0], 10, 64)
  184. if e != nil {
  185. continue
  186. }
  187. vv, e := strconv.ParseInt(items[1], 10, 64)
  188. if e != nil {
  189. continue
  190. }
  191. pgcView = append(pgcView, &model.AidView{Aid: aid, View: vv})
  192. }
  193. }
  194. }
  195. ugcViewLen := len(ugcView)
  196. if ugcViewLen > 3 {
  197. ugcView = ugcView[:_totalViewLen]
  198. }
  199. sort.Slice(ugcView, func(i, j int) bool {
  200. return ugcView[i].View > ugcView[j].View
  201. })
  202. pgcViewLen := len(pgcView)
  203. if pgcViewLen > 3 {
  204. pgcView = pgcView[:_totalViewLen]
  205. }
  206. sort.Slice(pgcView, func(i, j int) bool {
  207. return pgcView[i].View > pgcView[j].View
  208. })
  209. totalView = append(totalView, ugcView...)
  210. switch {
  211. case ugcViewLen == 0:
  212. totalView = append(totalView, pgcView...)
  213. case ugcViewLen == 1:
  214. switch {
  215. case pgcViewLen == 1:
  216. totalView = append(totalView, pgcView[0])
  217. case pgcViewLen > 1:
  218. totalView = append(totalView, pgcView[:2]...)
  219. }
  220. case ugcViewLen >= 2:
  221. if pgcViewLen > 0 {
  222. totalView = append(totalView, pgcView[0])
  223. }
  224. }
  225. sort.Slice(totalView, func(i, j int) bool {
  226. return totalView[i].View > totalView[j].View
  227. })
  228. if len(totalView) > _totalViewLen {
  229. totalView = totalView[:_totalViewLen]
  230. }
  231. for _, v := range totalView {
  232. viewAids = append(viewAids, v.Aid)
  233. }
  234. aids = append(aids, viewAids...)
  235. if item.LikeUp3Avs != "" {
  236. aidsStr := strings.Split(item.LikeUp3Avs, ",")
  237. for _, aidStr := range aidsStr {
  238. if aid, e := strconv.ParseInt(aidStr, 10, 64); e != nil {
  239. continue
  240. } else {
  241. upAids = append(upAids, aid)
  242. }
  243. }
  244. }
  245. aids = append(aids, upAids...)
  246. if item.BrainwashCirAvid > 0 {
  247. aids = append(aids, item.BrainwashCirAvid)
  248. }
  249. if item.BestAvid > 0 {
  250. switch item.BestAvidType {
  251. case _typeArticle:
  252. artIDs = append(artIDs, item.BestAvid)
  253. case _typeArchive:
  254. aids = append(aids, item.BestAvid)
  255. }
  256. }
  257. if item.BestAvidOld > 0 {
  258. switch item.BestAvidOldType {
  259. case _typeArticle:
  260. artIDs = append(artIDs, item.BestAvidOld)
  261. case _typeArchive:
  262. aids = append(aids, item.BestAvidOld)
  263. }
  264. }
  265. if item.FirstSubmitAvid > 0 {
  266. switch item.FirstSubmitType {
  267. case _typeArticle:
  268. artIDs = append(artIDs, item.FirstSubmitAvid)
  269. case _typeArchive:
  270. aids = append(aids, item.FirstSubmitAvid)
  271. }
  272. }
  273. if len(aids) > 0 {
  274. group.Go(func(ctx context.Context) error {
  275. s.groupArcData(ctx, aids, upAids, totalView, item, data)
  276. return nil
  277. })
  278. }
  279. // article
  280. if len(artIDs) > 0 {
  281. group.Go(func(ctx context.Context) error {
  282. s.groupArtData(ctx, artIDs, item, data)
  283. return nil
  284. })
  285. }
  286. group.Go(func(ctx context.Context) error {
  287. if acc, e := s.accClient.ProfileWithStat3(ctx, &accmdl.MidReq{Mid: item.Mid}); e != nil {
  288. log.Error("groupTmData s.arcClient.ProfileWithStat3(%v) error(%v)", item.Mid, e)
  289. } else {
  290. data.Uname = acc.Profile.Name
  291. data.Face = acc.Profile.Face
  292. data.Fans = acc.Follower
  293. data.RegTime = time.Unix(int64(acc.Profile.JoinTime), 0).Format("2006.01.02")
  294. data.RegDay = (time.Now().Unix() - int64(acc.Profile.JoinTime)) / 86400
  295. }
  296. return nil
  297. })
  298. // group mids
  299. if item.LikeBestUpID > 0 {
  300. mids = append(mids, item.LikeBestUpID)
  301. }
  302. if item.IsUp == 1 {
  303. if firstSubTime, e := time.Parse("2006-01-02 15:04:05", item.FirstSubmitTime); e != nil {
  304. log.Warn("groupTmData FirstSubmitTime time.Parse(%s) warn(%v)", item.FirstSubmitTime, e)
  305. } else {
  306. data.FirstSubmitTime = firstSubTime.Format("2006.01.02")
  307. }
  308. if item.UpBestFanVv != "" {
  309. list := strings.Split(item.UpBestFanVv, "@")
  310. if len(list) == 2 {
  311. if mid, e := strconv.ParseInt(list[0], 10, 64); e != nil {
  312. log.Error("UpBestFanVv parse(%s) error(%v)", list[0], e)
  313. } else {
  314. upBestFanMid = mid
  315. mids = append(mids, mid)
  316. }
  317. if vv, e := strconv.ParseInt(list[1], 10, 64); e != nil {
  318. log.Error("UpBestFanLiveMinute parse(%s) error(%v)", list[1], e)
  319. } else {
  320. favVv = vv
  321. }
  322. }
  323. }
  324. }
  325. if item.IsLiveUp == 1 {
  326. if maxCdnTime, e := time.Parse("20060102", item.MaxCdnNumDate); e != nil {
  327. log.Warn("groupTmData MaxCdnNumDate time.Parse(%s) warn(%v)", item.MaxCdnNumDate, e)
  328. } else {
  329. data.MaxCdnNumDate = maxCdnTime.Format("2006.01.02")
  330. }
  331. data.MaxCdnNum = item.MaxCdnNum
  332. if item.UpBestFanLiveMinute != "" {
  333. list := strings.Split(item.UpBestFanLiveMinute, "@")
  334. if len(list) == 2 {
  335. if mid, e := strconv.ParseInt(list[0], 10, 64); e != nil {
  336. log.Error("UpBestFanLiveMinute parse(%s) error(%v)", list[0], e)
  337. } else {
  338. upBestLiveFanMid = mid
  339. mids = append(mids, mid)
  340. }
  341. if minute, e := strconv.ParseInt(list[1], 10, 64); e != nil {
  342. log.Error("UpBestFanLiveMinute parse(%s) error(%v)", list[1], e)
  343. } else {
  344. liveMinute = minute
  345. }
  346. }
  347. }
  348. }
  349. if len(mids) > 0 {
  350. group.Go(func(ctx context.Context) error {
  351. if accs, e := s.accClient.Infos3(ctx, &accmdl.MidsReq{Mids: mids, RealIp: realIP}); e != nil || accs.Infos == nil {
  352. log.Error("groupTmData s.accClient.Cards3(%v) error(%v)", mids, e)
  353. } else {
  354. if item.LikeBestUpID > 0 {
  355. if info, ok := accs.Infos[item.LikeBestUpID]; ok {
  356. data.LikeBestUpID = item.LikeBestUpID
  357. data.LikeBestUpName = info.Name
  358. data.LikeBestUpFace = info.Face
  359. }
  360. }
  361. if item.IsUp == 1 && upBestFanMid > 0 {
  362. if info, ok := accs.Infos[upBestFanMid]; ok {
  363. data.UpBestFanVv = &model.FavVv{Mid: upBestFanMid, Name: info.Name, Face: info.Face, Vv: favVv}
  364. }
  365. }
  366. if item.IsLiveUp == 1 && upBestLiveFanMid > 0 {
  367. if info, ok := accs.Infos[upBestLiveFanMid]; ok {
  368. data.UpBestFanLiveMinute = &model.FanMinute{Mid: upBestLiveFanMid, Name: info.Name, Face: info.Face, Minute: liveMinute}
  369. }
  370. }
  371. }
  372. return nil
  373. })
  374. }
  375. if e := group.Wait(); e != nil {
  376. log.Error("groupTmData group.Wait error(%v)", e)
  377. }
  378. if len(data.LikeUp3Arcs) == 0 {
  379. data.LikeUp3Arcs = make([]*model.TmArc, 0)
  380. }
  381. return
  382. }
  383. func (s *Service) groupArcData(c context.Context, aids, upAids []int64, totalView []*model.AidView, item *model.Item, data *model.Timemachine) {
  384. if arcs, e := s.arcClient.Arcs(c, &arcmdl.ArcsRequest{Aids: aids}); e != nil {
  385. log.Error("groupTmData s.arcClient.Arcs(%v) error(%v)", aids, e)
  386. } else if arcs != nil {
  387. if item.BrainwashCirAvid > 0 {
  388. if arc, ok := arcs.Arcs[item.BrainwashCirAvid]; ok && arc.IsNormal() {
  389. data.BrainwashCirArc = &model.TmArc{Aid: item.BrainwashCirAvid, Title: arc.Title, Cover: arc.Pic, Author: arc.Author}
  390. }
  391. }
  392. if item.BestAvid > 0 && item.BestAvidType == _typeArchive {
  393. if arc, ok := arcs.Arcs[item.BestAvid]; ok && arc.IsNormal() {
  394. data.BestArc = &model.TmArc{Aid: item.BestAvid, Title: arc.Title, Cover: arc.Pic, Author: arc.Author}
  395. }
  396. }
  397. if item.BestAvidOld > 0 && item.BestAvidOldType == _typeArchive {
  398. if arc, ok := arcs.Arcs[item.BestAvidOld]; ok && arc.IsNormal() {
  399. data.BestArcOld = &model.TmArc{Aid: item.BestAvidOld, Title: arc.Title, Cover: arc.Pic, Author: arc.Author}
  400. }
  401. }
  402. if item.FirstSubmitAvid > 0 && item.FirstSubmitType == _typeArchive {
  403. if arc, ok := arcs.Arcs[item.FirstSubmitAvid]; ok && arc.IsNormal() {
  404. data.FirstSubmitArc = &model.TmArc{Aid: item.FirstSubmitAvid, Title: arc.Title, Cover: arc.Pic, Author: arc.Author}
  405. }
  406. }
  407. for _, aid := range upAids {
  408. if arc, ok := arcs.Arcs[aid]; ok && arc.IsNormal() {
  409. data.LikeUp3Arcs = append(data.LikeUp3Arcs, &model.TmArc{Aid: aid, Title: arc.Title, Cover: arc.Pic, Author: arc.Author})
  410. }
  411. }
  412. for _, item := range totalView {
  413. if arc, ok := arcs.Arcs[item.Aid]; ok && arc.IsNormal() {
  414. data.Likes3Arcs = append(data.Likes3Arcs, &model.TmArc{Aid: item.Aid, Title: arc.Title, Cover: arc.Pic, Author: arc.Author})
  415. }
  416. }
  417. }
  418. }
  419. func (s *Service) groupArtData(c context.Context, artIDs []int64, item *model.Item, data *model.Timemachine) {
  420. if arts, e := s.article.ArticleMetas(c, &artmdl.ArgAids{Aids: artIDs}); e != nil {
  421. log.Error("groupTmData s.article.ArticleMetas(%v) error(%v)", artIDs, e)
  422. } else {
  423. if item.BestAvid > 0 && item.BestAvidType == _typeArticle {
  424. if art, ok := arts[item.BestAvid]; ok {
  425. data.BestArc = &model.TmArc{Aid: item.BestAvid, Title: art.Title}
  426. }
  427. }
  428. if item.BestAvidOld > 0 && item.BestAvidOldType == _typeArticle {
  429. if art, ok := arts[item.BestAvidOld]; ok {
  430. data.BestArcOld = &model.TmArc{Aid: item.BestAvidOld, Title: art.Title}
  431. }
  432. }
  433. if item.FirstSubmitAvid > 0 && item.FirstSubmitType == _typeArticle {
  434. if art, ok := arts[item.FirstSubmitAvid]; ok {
  435. data.FirstSubmitArc = &model.TmArc{Aid: item.FirstSubmitAvid, Title: art.Title}
  436. }
  437. }
  438. }
  439. }
  440. func (s *Service) groupTagDesc(item *model.Item, data *model.Timemachine) (subTidBest int64) {
  441. var subTid int64
  442. subList := strings.Split(item.Like2SubTids, ",")
  443. if len(subList) == 2 {
  444. if subID, e := strconv.ParseInt(subList[0], 10, 64); e != nil {
  445. log.Warn("groupTmData Like2SubTids time.Parse(%s) warn(%v)", subList[0], e)
  446. } else if subID > 0 {
  447. subTidBest = subID
  448. }
  449. if subID, e := strconv.ParseInt(subList[1], 10, 64); e != nil {
  450. log.Warn("groupTmData Like2SubTids strconv.ParseInt(%s) warn(%v)", subList[1], e)
  451. } else if subID > 0 {
  452. subTid = subID
  453. }
  454. if regionDesc, ok := s.regionDescMap[subTidBest]; ok && regionDesc != nil {
  455. data.LikeSubDesc2 = regionDesc.Desc2
  456. data.LikeSubDesc3 = regionDesc.Desc3
  457. }
  458. if regionDesc, ok := s.regionDescMap[subTid]; ok && regionDesc != nil {
  459. data.LikeSubDesc1 = regionDesc.Desc1
  460. }
  461. }
  462. return
  463. }