subtitle_lan.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "encoding/json"
  6. "go-common/app/interface/main/dm2/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _subtitleLanFileName = "subtitle_lan.json"
  11. )
  12. // SubtitleLanOp .
  13. func (s *Service) SubtitleLanOp(c context.Context, code uint8, lan, docZh, docEn string, isDelete bool) (err error) {
  14. var (
  15. subtitleLans []*model.SubtitleLan
  16. subtitleLan *model.SubtitleLan
  17. bs []byte
  18. )
  19. subtitleLan = &model.SubtitleLan{
  20. Code: int64(code),
  21. Lan: lan,
  22. DocZh: docZh,
  23. DocEn: docEn,
  24. IsDelete: isDelete,
  25. }
  26. if err = s.dao.SubtitleLanAdd(c, subtitleLan); err != nil {
  27. log.Error("params(subtitleLan:%+v).error(%v)", subtitleLan, err)
  28. return
  29. }
  30. if subtitleLans, err = s.dao.SubtitleLans(c); err != nil {
  31. log.Error("SubtitleLans.error(%v)", err)
  32. return
  33. }
  34. if bs, err = json.Marshal(subtitleLans); err != nil {
  35. log.Error("json.Marshal.params(subtitleLan:%+v).error(%v)", subtitleLan, err)
  36. return
  37. }
  38. // reload bfs
  39. if _, err = s.dao.UploadBfs(c, _subtitleLanFileName, bs); err != nil {
  40. log.Error("UploadBfs.params.error(%v)", err)
  41. return
  42. }
  43. return
  44. }
  45. func (s *Service) isSubtitleLanLock(c context.Context, oid int64, tp int32, lan string) (subtitle *model.Subtitle, err error) {
  46. var (
  47. vss []*model.VideoSubtitle
  48. subtitleID int64
  49. )
  50. if vss, err = s.getVideoSubtitles(c, oid, tp); err != nil {
  51. log.Error("params(oid:%v, tp:%v) error(%v)", oid, tp, err)
  52. return
  53. }
  54. for _, vs := range vss {
  55. if vs.Lan == lan {
  56. subtitleID = vs.ID
  57. break
  58. }
  59. }
  60. if subtitleID > 0 {
  61. if subtitle, err = s.getSubtitle(c, oid, subtitleID); err != nil {
  62. log.Error("params(oid:%v, subtitleID:%v) error(%v)", oid, subtitleID, err)
  63. return
  64. }
  65. }
  66. return
  67. }
  68. // SubtitleLans .
  69. func (s *Service) SubtitleLans(c context.Context, oid int64, tp int32, mid int64) (lans []*model.Language, err error) {
  70. var (
  71. vss []*model.VideoSubtitle
  72. res *model.SearchSubtitleResult
  73. _searchStatus = []int64{int64(model.SubtitleStatusDraft), int64(model.SubtitleStatusToAudit), int64(model.SubtitleStatusAuditBack), int64(model.SubtitleStatusCheckToAudit)}
  74. subtitleIds []int64
  75. subtitles []*model.Subtitle
  76. subtitlesM map[int64]*model.Subtitle
  77. mapLans map[string]*model.Language
  78. _maxPageSize = 200
  79. subtitleMap map[string][]*model.Subtitle
  80. lan *model.Language
  81. ok bool
  82. )
  83. mapLans = make(map[string]*model.Language)
  84. if vss, err = s.getVideoSubtitles(c, oid, tp); err != nil {
  85. log.Error("params(oid:%v,tp:%v).error(%v)", oid, tp, err)
  86. return
  87. }
  88. if res, err = s.dao.SearchSubtitles(c, 1, int32(_maxPageSize), mid, nil, 0, oid, tp, _searchStatus); err == nil {
  89. if res != nil {
  90. for _, result := range res.Results {
  91. subtitleIds = append(subtitleIds, result.ID)
  92. }
  93. }
  94. } else {
  95. log.Error("SearchSubtitles.params(mid:%v,oid:%v),error(%v)", mid, oid, err)
  96. err = nil
  97. }
  98. for _, vs := range vss {
  99. mapLans[vs.Lan] = &model.Language{
  100. Lan: vs.Lan,
  101. LanDoc: vs.LanDoc,
  102. Pub: &model.LanguagePub{
  103. SubtitleID: vs.ID,
  104. IsLock: vs.IsLock,
  105. IsPub: true,
  106. },
  107. }
  108. }
  109. if len(subtitleIds) > 0 {
  110. if subtitlesM, err = s.getSubtitles(c, oid, subtitleIds); err != nil {
  111. log.Error("params(oid:%v,subtitleDraftIds:%v).error(%v)", oid, subtitleIds, err)
  112. return
  113. }
  114. subtitleMap = make(map[string][]*model.Subtitle)
  115. for _, subtitle := range subtitlesM {
  116. tlan, tlanDoc := s.subtitleLans.GetByID(int64(subtitle.Lan))
  117. if lan, ok = mapLans[tlan]; !ok {
  118. lan = &model.Language{
  119. Lan: tlan,
  120. LanDoc: tlanDoc,
  121. }
  122. mapLans[tlan] = lan
  123. }
  124. switch subtitle.Status {
  125. case model.SubtitleStatusDraft:
  126. lan.Draft = &model.LanguageID{
  127. SubtitleID: subtitle.ID,
  128. }
  129. case model.SubtitleStatusToAudit:
  130. lan.Audit = &model.LanguageID{
  131. SubtitleID: subtitle.ID,
  132. }
  133. case model.SubtitleStatusAuditBack:
  134. subtitleMap[tlan] = append(subtitleMap[tlan], subtitle)
  135. }
  136. }
  137. for _, subtitles = range subtitleMap {
  138. if len(subtitles) > 0 {
  139. sort.Slice(subtitles, func(i, j int) bool {
  140. return subtitles[i].PubTime > subtitles[j].PubTime
  141. })
  142. tlan, tlanDoc := s.subtitleLans.GetByID(int64(subtitles[0].Lan))
  143. if lan, ok = mapLans[tlan]; !ok {
  144. lan = &model.Language{
  145. Lan: tlan,
  146. LanDoc: tlanDoc,
  147. }
  148. mapLans[tlan] = lan
  149. }
  150. lan.AuditBack = &model.LanguageID{
  151. SubtitleID: subtitles[0].ID,
  152. }
  153. }
  154. }
  155. }
  156. lans = make([]*model.Language, 0, len(mapLans))
  157. for _, mapLan := range mapLans {
  158. lans = append(lans, mapLan)
  159. }
  160. return
  161. }