pgc.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/videoup/model/archive"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. // PassByPGC update pgc archive state to StateOpen.
  13. func (s *Service) PassByPGC(c context.Context, aid int64, gid int64, attrs map[uint]int32, redirectURL string, now time.Time) (err error) {
  14. // archive
  15. var a *archive.Archive
  16. if a, err = s.arc.Archive(c, aid); err != nil || a == nil {
  17. log.Error("s.arc.Archive(%d) error(%v) or a==nil", aid, err)
  18. return
  19. }
  20. log.Info("aid(%d) begin tran pass pgc", aid)
  21. // begin tran
  22. var tx *sql.Tx
  23. if tx, err = s.arc.BeginTran(c); err != nil {
  24. log.Error("s.arc.BeginTran() error(%v)", err)
  25. return
  26. }
  27. defer func() {
  28. if r := recover(); r != nil {
  29. tx.Rollback()
  30. log.Error("wocao jingran recover le error(%v)", r)
  31. }
  32. }()
  33. if a.State != archive.StateOpen {
  34. var firstPass bool
  35. if firstPass, err = s.txUpArcState(c, tx, a.Aid, archive.StateOpen); err != nil {
  36. tx.Rollback()
  37. log.Error("PassByPGC s.txUpArcState(aid(%d),state(%d)) error(%v)", aid, archive.StateOpen, err)
  38. return
  39. }
  40. a.State = archive.StateOpen
  41. log.Info("archive(%d) update archive state(%d)", a.Aid, a.State)
  42. // archive ptime
  43. if firstPass {
  44. pTime := xtime.Time(now.Unix())
  45. if _, err = s.arc.TxUpArcPTime(tx, a.Aid, pTime); err != nil {
  46. tx.Rollback()
  47. log.Error("s.arc.TxUpArcPTime(%d, %d) error(%v)", a.Aid, pTime, err)
  48. return
  49. }
  50. a.PTime = pTime
  51. log.Info("archive(%d) second_round upPTime(%d)", a.Aid, a.PTime)
  52. }
  53. var round = s.archiveRound(c, a, a.Aid, a.Mid, a.TypeID, a.Round, a.State, false)
  54. if _, err = s.arc.TxUpArcRound(tx, a.Aid, round); err != nil {
  55. tx.Rollback()
  56. log.Error("s.arc.TxUpArcRound(%d, %d) error(%v)", a.Aid, round, err)
  57. return
  58. }
  59. a.Round = round
  60. log.Info("archive(%d) second_round upRound(%d)", a.Aid, a.Round)
  61. }
  62. var conts []string
  63. if conts, err = s.txUpArcAttrs(tx, a, attrs, redirectURL); err != nil {
  64. tx.Rollback()
  65. return
  66. }
  67. if err = tx.Commit(); err != nil {
  68. log.Error("tx.Commit() error(%v)", err)
  69. return
  70. }
  71. log.Info("aid(%d) end tran pass pgc", aid)
  72. if _, err := s.oversea.UpPolicyRelation(c, aid, gid); err != nil {
  73. conts = append(conts, fmt.Sprintf("[地区展示]应用策略组ID[%d]", gid))
  74. }
  75. if len(conts) > 0 {
  76. s.arc.AddArcOper(c, a.Aid, 221, a.Attribute, a.TypeID, int16(a.State), a.Round, 1, strings.Join(conts, ","), "")
  77. }
  78. // NOTE: send second_round for sync dede.
  79. s.busSecondRound(aid, 0, false, false, false, false, false, false, "", nil)
  80. return
  81. }
  82. // ModifyByPGC update pgc archive attributes.
  83. func (s *Service) ModifyByPGC(c context.Context, aid int64, gid int64, attrs map[uint]int32, redirectURL string) (err error) {
  84. // archive
  85. var a *archive.Archive
  86. if a, err = s.arc.Archive(c, aid); err != nil || a == nil {
  87. log.Error("s.arc.Archive(%d) error(%v) or a==nil", aid, err)
  88. return
  89. }
  90. log.Info("aid(%d) begin tran modify pgc", aid)
  91. // begin tran
  92. var tx *sql.Tx
  93. if tx, err = s.arc.BeginTran(c); err != nil {
  94. log.Error("s.arc.BeginTran() error(%v)", err)
  95. return
  96. }
  97. defer func() {
  98. if r := recover(); r != nil {
  99. tx.Rollback()
  100. log.Error("wocao jingran recover le error(%v)", r)
  101. }
  102. }()
  103. var conts []string
  104. if conts, err = s.txUpArcAttrs(tx, a, attrs, redirectURL); err != nil {
  105. tx.Rollback()
  106. return
  107. }
  108. if err = tx.Commit(); err != nil {
  109. log.Error("tx.Commit() error(%v)", err)
  110. return
  111. }
  112. log.Info("aid(%d) end tran modify pgc", aid)
  113. if _, err := s.oversea.UpPolicyRelation(c, aid, gid); err != nil {
  114. conts = append(conts, fmt.Sprintf("[地区展示]应用策略组ID[%d]", gid))
  115. }
  116. if len(conts) > 0 {
  117. s.arc.AddArcOper(c, a.Aid, 221, a.Attribute, a.TypeID, int16(a.State), a.Round, 1, strings.Join(conts, ","), "")
  118. }
  119. // NOTE: send second_round for sync dede.
  120. s.busSecondRound(aid, 0, false, false, false, false, false, false, "", nil)
  121. return
  122. }
  123. // LockByPGC update pgc archive state to StateForbidLock.
  124. func (s *Service) LockByPGC(c context.Context, aid int64) (err error) {
  125. // archive
  126. var a *archive.Archive
  127. if a, err = s.arc.Archive(c, aid); err != nil || a == nil {
  128. log.Error("s.arc.Archive(%d) error(%v) or a==nil", aid, err)
  129. return
  130. }
  131. if a.State == archive.StateForbidLock {
  132. return
  133. }
  134. log.Info("aid(%d) begin tran lock pgc", aid)
  135. // begin tran
  136. var tx *sql.Tx
  137. if tx, err = s.arc.BeginTran(c); err != nil {
  138. log.Error("s.arc.BeginTran() error(%v)", err)
  139. return
  140. }
  141. defer func() {
  142. if r := recover(); r != nil {
  143. tx.Rollback()
  144. log.Error("wocao jingran recover le error(%v)", r)
  145. }
  146. }()
  147. if _, err = s.txUpArcState(c, tx, a.Aid, archive.StateForbidLock); err != nil {
  148. tx.Rollback()
  149. log.Error("s.txUpArcState(aid(%d),state(%d)) error(%v)", aid, archive.StateForbidLock, err)
  150. return
  151. }
  152. a.State = archive.StateForbidLock
  153. log.Info("archive(%d) update archive state(%d)", a.Aid, a.State)
  154. if err = tx.Commit(); err != nil {
  155. log.Error("tx.Commit() error(%v)", err)
  156. return
  157. }
  158. log.Info("aid(%d) end tran lock pgc", aid)
  159. s.arc.AddArcOper(c, a.Aid, 221, a.Attribute, a.TypeID, int16(a.State), a.Round, 1, "", "")
  160. // NOTE: send second_round for sync dede.
  161. s.busSecondRound(aid, 0, false, false, false, false, false, false, "", nil)
  162. return
  163. }