wave_form.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/dm2/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _waveFormCallBackSuccess = 1
  11. _waveFormPrefix = "http://i0.hdslb.com/bfs"
  12. _expire = 20 // 2*10
  13. )
  14. func (s *Service) waveForm(c context.Context, oid int64, tp int32) (waveForm *model.WaveForm, err error) {
  15. var (
  16. cacheError bool
  17. )
  18. if waveForm, err = s.dao.WaveFormCache(c, oid, tp); err != nil {
  19. cacheError = true
  20. err = nil
  21. }
  22. if waveForm != nil && !waveForm.Empty {
  23. return
  24. }
  25. if waveForm, err = s.dao.GetWaveForm(c, oid, tp); err != nil {
  26. log.Error("params(oid:%v,tp:%v),error(%v)", oid, tp, err)
  27. return
  28. }
  29. if waveForm == nil {
  30. waveForm = &model.WaveForm{
  31. Oid: oid,
  32. Type: tp,
  33. Empty: true,
  34. }
  35. }
  36. if !cacheError {
  37. temp := waveForm
  38. s.cache.Do(c, func(ctx context.Context) {
  39. s.dao.SetWaveFormCache(ctx, temp)
  40. })
  41. }
  42. return
  43. }
  44. // WaveForm .
  45. func (s *Service) WaveForm(c context.Context, aid, oid int64, tp int32, mid int64) (waveFormResp *model.WaveFormResp, err error) {
  46. var (
  47. uposErr error
  48. waveFromURL string
  49. waveForm *model.WaveForm
  50. )
  51. if err = s.SubtitlePermission(c, aid, oid, tp, mid); err != nil {
  52. return
  53. }
  54. if waveForm, err = s.waveForm(c, oid, tp); err != nil {
  55. log.Error("params(oid:%v,tp:%v),error(%v)", oid, tp, err)
  56. return
  57. }
  58. if !waveForm.Empty {
  59. waveFormResp = &model.WaveFormResp{
  60. State: waveForm.State,
  61. WaveFromURL: waveForm.WaveFromURL,
  62. }
  63. switch waveForm.State {
  64. case model.WaveFormStatusFailed, model.WaveFormStatusSuccess:
  65. return
  66. case model.WaveFormStatusWaitting:
  67. if time.Since(time.Unix(waveForm.Mtime, 0)) < _expire {
  68. return
  69. }
  70. }
  71. }
  72. waveForm.State = model.WaveFormStatusWaitting
  73. if waveFromURL, uposErr = s.dao.Upos(c, oid); uposErr != nil {
  74. log.Error("postUpos(oid:%v),error(%v)", oid, err)
  75. waveForm.State = model.WaveFormStatusError
  76. }
  77. waveForm.WaveFromURL = fmt.Sprintf("%s/%s", _waveFormPrefix, waveFromURL)
  78. if err = s.dao.UpsertWaveFrom(c, waveForm); err != nil {
  79. log.Error("params(waveForm:%+v),error(%v)", waveForm, err)
  80. return
  81. }
  82. if err = s.dao.DelWaveFormCache(c, oid, tp); err != nil {
  83. log.Error("DelWaveFormCache.params(oid:%v,tp:%v),error(%v)", oid, tp, err)
  84. return
  85. }
  86. waveFormResp = &model.WaveFormResp{
  87. State: waveForm.State,
  88. WaveFromURL: waveForm.WaveFromURL,
  89. }
  90. return
  91. }
  92. // WaveFormCallBack .
  93. func (s *Service) WaveFormCallBack(c context.Context, oid int64, tp int32, code int32, info string) (err error) {
  94. var (
  95. waveForm *model.WaveForm
  96. )
  97. if waveForm, err = s.waveForm(c, oid, tp); err != nil {
  98. log.Error("params(oid:%v,tp:%v),error(%v)", oid, tp, err)
  99. return
  100. }
  101. if code == _waveFormCallBackSuccess {
  102. waveForm.State = model.WaveFormStatusSuccess
  103. } else {
  104. waveForm.State = model.WaveFormStatusFailed
  105. log.Error("WaveFormCallBack.params(oid:%v,tp:%v).errorInfo(%s)", oid, tp, info)
  106. }
  107. if err = s.dao.UpsertWaveFrom(c, waveForm); err != nil {
  108. log.Error("params(waveForm:%+v),error(%v)", waveForm, err)
  109. return
  110. }
  111. if err = s.dao.DelWaveFormCache(c, oid, tp); err != nil {
  112. log.Error("DelWaveFormCache.params(oid:%v,tp:%v),error(%v)", oid, tp, err)
  113. return
  114. }
  115. return
  116. }