subtitle_subject.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/dm2/model"
  5. "go-common/library/log"
  6. )
  7. // SubtitleSubject .
  8. func (s *Service) SubtitleSubject(c context.Context, aid int64) (subtitleSubjectReply *model.SubtitleSubjectReply, err error) {
  9. var (
  10. subtitleSubject *model.SubtitleSubject
  11. lan, lanDoc string
  12. )
  13. if subtitleSubject, err = s.subtitleSubject(c, aid); err != nil {
  14. log.Error("params(aid:%v).err(%v)", aid, err)
  15. err = nil
  16. }
  17. if subtitleSubject == nil {
  18. subtitleSubject = &model.SubtitleSubject{
  19. Allow: false,
  20. }
  21. }
  22. lan, lanDoc = s.subtitleLans.GetByID(int64(subtitleSubject.Lan))
  23. subtitleSubjectReply = &model.SubtitleSubjectReply{
  24. AllowSubmit: subtitleSubject.Allow,
  25. Lan: lan,
  26. LanDoc: lanDoc,
  27. }
  28. return
  29. }
  30. func (s *Service) subtitleSubject(c context.Context, aid int64) (sSubject *model.SubtitleSubject, err error) {
  31. var (
  32. cacheErr bool
  33. )
  34. if sSubject, err = s.dao.SubtitleSubjectCache(c, aid); err != nil {
  35. cacheErr = true
  36. err = nil
  37. }
  38. if sSubject != nil {
  39. if sSubject.Empty {
  40. err = nil
  41. sSubject = nil
  42. return
  43. }
  44. return
  45. }
  46. if sSubject, err = s.dao.GetSubtitleSubject(c, aid); err != nil {
  47. log.Error("params(aid:%v).err(%v)", aid, err)
  48. return
  49. }
  50. if sSubject == nil {
  51. sSubject = &model.SubtitleSubject{
  52. Aid: aid,
  53. Empty: true,
  54. }
  55. }
  56. if !cacheErr {
  57. temp := sSubject
  58. s.cache.Do(c, func(ctx context.Context) {
  59. s.dao.SetSubtitleSubjectCache(ctx, temp)
  60. })
  61. }
  62. if sSubject.Empty {
  63. err = nil
  64. sSubject = nil
  65. return
  66. }
  67. return
  68. }
  69. // SubtitleSubjectSubmit .
  70. func (s *Service) SubtitleSubjectSubmit(c context.Context, aid int64, allow bool, lan string) (err error) {
  71. var (
  72. lanCode int64
  73. subtitleSubject *model.SubtitleSubject
  74. )
  75. lanCode = s.subtitleLans.GetByLan(lan)
  76. subtitleSubject = &model.SubtitleSubject{
  77. Aid: aid,
  78. Allow: allow,
  79. Lan: uint8(lanCode),
  80. }
  81. if err = s.dao.AddSubtitleSubject(c, subtitleSubject); err != nil {
  82. log.Error("params(subtitleSubject:%+v).err(%v)", subtitleSubject, err)
  83. return
  84. }
  85. if err = s.dao.DelSubtitleSubjectCache(c, aid); err != nil {
  86. return
  87. }
  88. return
  89. }