subtitle_search.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "go-common/app/interface/main/dm2/model"
  7. account "go-common/app/service/main/account/api"
  8. "go-common/app/service/main/archive/api"
  9. archive "go-common/app/service/main/archive/model/archive"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/sync/errgroup"
  13. )
  14. const (
  15. _searchMaxSize = 100
  16. )
  17. // SearchAuthor .
  18. func (s *Service) SearchAuthor(c context.Context, mid int64, status int32, page, size int32) (authorResult *model.SearchSubtitleAuthor, err error) {
  19. var (
  20. res *model.SearchSubtitleResponse
  21. searchItems []*model.SearchSubtitleAuthorItem
  22. formUpper = false
  23. countSubtitle *model.CountSubtitleResult
  24. )
  25. if size > _searchMaxSize {
  26. err = ecode.RequestErr
  27. return
  28. }
  29. if res, err = s.searchSubtitle(c, 0, 0, 0, mid, nil, status, page, size, formUpper); err != nil {
  30. log.Error("psearchSubtitle.params(mid:%v,status:%v,page:%v,size:%v),error(%v)", mid, status, page, size, err)
  31. return
  32. }
  33. if res == nil || res.Page == nil {
  34. log.Error("psearchSubtitle.params(mid:%v,status:%v,page:%v,size:%v),error(%v)", mid, status, page, size, err)
  35. err = ecode.NothingFound
  36. return
  37. }
  38. if countSubtitle, err = s.dao.CountSubtitles(c, mid, nil, 0, 0, 0); err != nil {
  39. log.Error("CountSubtitles.params(mid:%v),error(%v)", mid, err)
  40. return
  41. }
  42. for _, rs := range res.Subtitles {
  43. searchItem := &model.SearchSubtitleAuthorItem{
  44. ID: rs.ID,
  45. Oid: rs.Oid,
  46. Aid: rs.Aid,
  47. Type: rs.Type,
  48. ArchiveName: rs.ArchiveName,
  49. ArchivePic: rs.ArchivePic,
  50. VideoName: rs.VideoName,
  51. Lan: rs.Lan,
  52. LanDoc: rs.LanDoc,
  53. Status: rs.Status,
  54. IsSign: rs.IsSign,
  55. IsLock: rs.IsLock,
  56. Mtime: rs.Mtime,
  57. }
  58. if searchItem.Status == int32(model.SubtitleStatusManagerBack) {
  59. searchItem.Status = int32(model.SubtitleStatusAuditBack)
  60. }
  61. if searchItem.Status == int32(model.SubtitleStatusAuditBack) {
  62. searchItem.RejectComment = rs.RejectComment
  63. }
  64. searchItems = append(searchItems, searchItem)
  65. }
  66. authorResult = &model.SearchSubtitleAuthor{
  67. Page: res.Page,
  68. Subtitles: searchItems,
  69. DraftCount: countSubtitle.Draft,
  70. AuditCount: countSubtitle.ToAudit,
  71. BackCount: countSubtitle.AuditBack,
  72. PublishCount: countSubtitle.Publish,
  73. }
  74. authorResult.Total = authorResult.DraftCount + authorResult.AuditCount + authorResult.BackCount + authorResult.PublishCount
  75. return
  76. }
  77. // SearchAssist .
  78. func (s *Service) SearchAssist(c context.Context, aid, oid int64, tp int32, mid int64, status int32, page, size int32) (assistResult *model.SearchSubtitleAssit, err error) {
  79. var (
  80. res *model.SearchSubtitleResponse
  81. upMids []int64
  82. fromUpper = true
  83. countSubtitle *model.CountSubtitleResult
  84. )
  85. if size > _searchMaxSize {
  86. err = ecode.RequestErr
  87. return
  88. }
  89. upMids = append(upMids, mid)
  90. if res, err = s.searchSubtitle(c, aid, oid, tp, 0, upMids, status, page, size, fromUpper); err != nil {
  91. return
  92. }
  93. if res == nil || res.Page == nil {
  94. err = ecode.NothingFound
  95. return
  96. }
  97. if countSubtitle, err = s.dao.CountSubtitles(c, 0, upMids, aid, oid, tp); err != nil {
  98. log.Error("CountSubtitles.params(mid:%v),error(%v)", mid, err)
  99. return
  100. }
  101. assistResult = &model.SearchSubtitleAssit{
  102. Page: res.Page,
  103. Subtitles: res.Subtitles,
  104. AuditCount: countSubtitle.ToAudit,
  105. PublishCount: countSubtitle.Publish,
  106. }
  107. assistResult.Total = assistResult.AuditCount + assistResult.PublishCount
  108. return
  109. }
  110. func (s *Service) buildSearchStatus(c context.Context, status int32, fromUpper bool) (searchStatus []int64, err error) {
  111. if fromUpper {
  112. switch status {
  113. case 0:
  114. searchStatus = []int64{
  115. int64(model.SubtitleStatusPublish),
  116. int64(model.SubtitleStatusToAudit),
  117. int64(model.SubtitleStatusCheckPublish),
  118. }
  119. case int32(model.SubtitleStatusPublish):
  120. searchStatus = []int64{
  121. int64(model.SubtitleStatusPublish),
  122. int64(model.SubtitleStatusCheckPublish),
  123. }
  124. case int32(model.SubtitleStatusToAudit):
  125. searchStatus = []int64{int64(status)}
  126. default:
  127. err = ecode.SubtitlePermissionDenied
  128. return
  129. }
  130. } else {
  131. switch status {
  132. case int32(model.SubtitleStatusPublish):
  133. searchStatus = []int64{
  134. int64(model.SubtitleStatusPublish),
  135. int64(model.SubtitleStatusCheckPublish),
  136. }
  137. case int32(model.SubtitleStatusAuditBack):
  138. searchStatus = []int64{
  139. int64(model.SubtitleStatusAuditBack),
  140. int64(model.SubtitleStatusManagerBack),
  141. }
  142. case int32(model.SubtitleStatusDraft):
  143. searchStatus = []int64{int64(status)}
  144. case int32(model.SubtitleStatusToAudit):
  145. searchStatus = []int64{
  146. int64(model.SubtitleStatusCheckToAudit),
  147. int64(model.SubtitleStatusToAudit),
  148. }
  149. case 0:
  150. searchStatus = []int64{
  151. int64(model.SubtitleStatusPublish),
  152. int64(model.SubtitleStatusToAudit),
  153. int64(model.SubtitleStatusDraft),
  154. int64(model.SubtitleStatusAuditBack),
  155. int64(model.SubtitleStatusCheckToAudit),
  156. int64(model.SubtitleStatusCheckPublish),
  157. int64(model.SubtitleStatusManagerBack),
  158. }
  159. default:
  160. err = ecode.SubtitlePermissionDenied
  161. return
  162. }
  163. }
  164. return
  165. }
  166. func (s *Service) searchSubtitle(c context.Context, aid, oid int64, tp int32, mid int64, upMids []int64, status int32, page, size int32, fromUpper bool) (result *model.SearchSubtitleResponse, err error) {
  167. var (
  168. res *model.SearchSubtitleResult
  169. dmidsMap map[int64][]int64
  170. eg errgroup.Group
  171. subtitleMap map[string]*model.Subtitle
  172. subtitle *model.Subtitle
  173. results []*model.SearchSubtitle
  174. searchSubtitle *model.SearchSubtitle
  175. mutex sync.Mutex
  176. ok bool
  177. profileReply *account.ProfileReply
  178. archiveAids []int64
  179. archiveMap map[int64]*api.Arc
  180. searchStatus []int64
  181. )
  182. key := func(oid, id int64) string {
  183. return fmt.Sprintf("%d:%d", oid, id)
  184. }
  185. if searchStatus, err = s.buildSearchStatus(c, status, fromUpper); err != nil {
  186. return
  187. }
  188. if res, err = s.dao.SearchSubtitles(c, page, size, mid, upMids, aid, oid, tp, searchStatus); err != nil {
  189. return
  190. }
  191. if res == nil || res.Page == nil {
  192. return
  193. }
  194. result = &model.SearchSubtitleResponse{
  195. Page: &model.SearchPage{
  196. Num: res.Page.Num,
  197. Size: res.Page.Size,
  198. Total: res.Page.Total,
  199. },
  200. }
  201. dmidsMap = make(map[int64][]int64)
  202. subtitleMap = make(map[string]*model.Subtitle)
  203. for _, rs := range res.Results {
  204. dmidsMap[rs.Oid] = append(dmidsMap[rs.Oid], rs.ID)
  205. }
  206. for oid, ids := range dmidsMap {
  207. tempOid := oid
  208. tempIds := ids
  209. eg.Go(func() (err error) {
  210. var (
  211. subtitles map[int64]*model.Subtitle
  212. )
  213. if subtitles, err = s.getSubtitles(c, tempOid, tempIds); err != nil {
  214. log.Error("params(tempOid:%v, tempIds:%v) error(%v)", tempOid, tempIds, err)
  215. return
  216. }
  217. mutex.Lock()
  218. for _, subtitle := range subtitles {
  219. subtitleMap[key(subtitle.Oid, subtitle.ID)] = subtitle
  220. archiveAids = append(archiveAids, subtitle.Aid)
  221. }
  222. mutex.Unlock()
  223. return
  224. })
  225. }
  226. if err = eg.Wait(); err != nil {
  227. log.Error("eg.Wait() error(%v)", err)
  228. return
  229. }
  230. if archiveMap, err = s.arcRPC.Archives3(c, &archive.ArgAids2{
  231. Aids: archiveAids,
  232. }); err != nil {
  233. log.Error("params(aids:%v) error(%v)", archiveAids, err)
  234. return
  235. }
  236. results = make([]*model.SearchSubtitle, 0, len(subtitleMap))
  237. for _, rs := range res.Results {
  238. var (
  239. archiveName string
  240. archivePic string
  241. archiveVideo *api.Page
  242. videoName string
  243. )
  244. if subtitle, ok = subtitleMap[key(rs.Oid, rs.ID)]; !ok {
  245. continue
  246. }
  247. if a, ok := archiveMap[subtitle.Aid]; ok {
  248. archiveName = a.Title
  249. archivePic = a.Pic
  250. }
  251. if archiveVideo, err = s.arcRPC.Video3(c, &archive.ArgVideo2{
  252. Aid: subtitle.Aid,
  253. Cid: subtitle.Oid,
  254. }); err != nil {
  255. log.Error("params(aid:%v,oid:%v) error(%v)", subtitle.Aid, subtitle.Oid, err)
  256. err = nil
  257. } else {
  258. videoName = archiveVideo.Part
  259. }
  260. lan, lanDoc := s.subtitleLans.GetByID(int64(subtitle.Lan))
  261. searchSubtitle = &model.SearchSubtitle{
  262. ID: rs.ID,
  263. Oid: rs.Oid,
  264. Aid: subtitle.Aid,
  265. Type: subtitle.Type,
  266. ArchiveName: archiveName,
  267. ArchivePic: archivePic,
  268. VideoName: videoName,
  269. Lan: lan,
  270. LanDoc: lanDoc,
  271. Status: int32(subtitle.Status),
  272. IsSign: subtitle.IsSign,
  273. IsLock: subtitle.IsLock,
  274. RejectComment: subtitle.RejectComment,
  275. Mtime: subtitle.Mtime,
  276. }
  277. switch subtitle.Status {
  278. case model.SubtitleStatusCheckToAudit:
  279. searchSubtitle.Status = int32(model.SubtitleStatusToAudit)
  280. case model.SubtitleStatusCheckPublish:
  281. searchSubtitle.Status = int32(model.SubtitleStatusPublish)
  282. }
  283. // up主搜索需要以下字段
  284. if fromUpper {
  285. var (
  286. profileName string
  287. authorPic string
  288. )
  289. if profileReply, err = s.accountRPC.Profile3(c, &account.MidReq{
  290. Mid: subtitle.Mid,
  291. }); err != nil {
  292. log.Error("params(Mid:%v) error(%v)", subtitle.Mid, err)
  293. err = nil
  294. } else {
  295. profileName = profileReply.GetProfile().GetName()
  296. authorPic = profileReply.GetProfile().GetFace()
  297. }
  298. searchSubtitle.Author = profileName
  299. searchSubtitle.AuthorPic = authorPic
  300. searchSubtitle.AuthorID = subtitle.AuthorID
  301. }
  302. results = append(results, searchSubtitle)
  303. }
  304. result.Subtitles = results
  305. return
  306. }