video.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package service
  2. import (
  3. "context"
  4. "runtime"
  5. "strings"
  6. "time"
  7. "fmt"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/library/database/sql"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. // VideoAudit upload first_round info.
  15. func (s *Service) VideoAudit(c context.Context, vp *archive.VideoParam, attrs map[uint]int32) (err error) {
  16. var (
  17. tx *sql.Tx
  18. qaVideo *archive.QAVideo
  19. qaErr error
  20. )
  21. defer func() {
  22. if r := recover(); r != nil {
  23. if tx != nil {
  24. tx.Rollback()
  25. }
  26. var buf [4096]byte
  27. n := runtime.Stack(buf[:], false)
  28. log.Error("wocao jingran recover le error(%v) panic stack(%s)", r, string(buf[:n]))
  29. }
  30. }()
  31. //只有一审任务会去新增质检任务
  32. if vp.TaskID > 0 {
  33. qaVideo, qaErr = s.fetchQAVideo(c, vp)
  34. if qaErr != nil {
  35. log.Error("VideoAudit s.fetchQAVideo error(%v) aid(%d) cid(%d)", qaErr, vp.Aid, vp.Cid)
  36. }
  37. }
  38. if tx, err = s.arc.BeginTran(c); err != nil {
  39. log.Error("s.arc.BeginTran() error(%v)", err)
  40. return
  41. }
  42. if err = s.txUpVideo(tx, vp.ID, vp.Title, vp.Desc); err != nil {
  43. tx.Rollback()
  44. log.Error("s.txUpVideo(%d,%s,%s) error(%v)", vp.ID, vp.Title, vp.Desc, err)
  45. return
  46. }
  47. log.Info("aid(%d) update video vid(%d) Title(%s) Desc(%s) status(%d) ", vp.Aid, vp.ID, vp.Title, vp.Desc, vp.Status)
  48. var (
  49. operConts []string
  50. attr int32
  51. attrChanged bool
  52. )
  53. if operConts, attr, err = s.txUpVideoAttr(c, tx, vp.ID, vp.Cid, attrs); err != nil {
  54. tx.Rollback()
  55. return
  56. }
  57. if len(operConts) > 0 {
  58. attrChanged = true
  59. }
  60. if err = s.txUpVideoAudit(tx, vp); err != nil {
  61. tx.Rollback()
  62. log.Error("s.txUpVideoAudit(vp(%+v)) error(%v)", vp, err)
  63. return
  64. }
  65. if err = tx.Commit(); err != nil {
  66. log.Error("tx.Commit() error(%v)", err)
  67. return
  68. }
  69. oper := &archive.VideoOper{AID: vp.Aid, UID: vp.UID, VID: vp.ID, Attribute: attr, Status: vp.Status, Remark: vp.Note}
  70. operConts = append(operConts, s.diffVideoOper(vp)...)
  71. oper.Content = strings.Join(operConts, ",")
  72. s.addVideoOper(c, oper)
  73. // databus
  74. s.busFirstRoundByPlayurl(c, vp, attrChanged)
  75. go s.busSecondRoundUpCredit(vp.Aid, vp.Cid, vp.Mid, vp.UID, int8(vp.Status), 0, vp.ReasonID, vp.Reason)
  76. // log set attr
  77. vp.Attribute = attr
  78. s.sendVideoLog(c, vp, oper.Content)
  79. if qaVideo != nil && qaErr == nil {
  80. qaVideo.Attribute = attr
  81. s.addQAVideo(c, qaVideo)
  82. }
  83. return
  84. }
  85. func (s *Service) busFirstRoundByPlayurl(c context.Context, vp *archive.VideoParam, attrChanged bool) (err error) {
  86. arc, err := s.arc.Archive(c, vp.Aid)
  87. if err != nil {
  88. log.Error("s.arc.Archive() (%d) error(%v)", vp.Aid, err)
  89. return
  90. }
  91. playurl := vp.Playurl
  92. if len(playurl) == 0 {
  93. playurl, err = s.arc.VideoPlayurl(c, vp.Cid)
  94. if err != nil {
  95. log.Error("s.busfirstroundbyplayurl get playurl by vid(%d) error(%v)", vp.ID, err)
  96. return
  97. }
  98. if len(playurl) == 0 {
  99. err = fmt.Errorf("video(%d) not exist", vp.ID)
  100. log.Error("s.busfirstroundbyplayurl get playurl by vid(%d) error(%v)", vp.ID, err)
  101. return
  102. }
  103. }
  104. log.Info("begin to bus first round by playurl(%s) vp(%v) err(%v)", playurl, vp, err)
  105. //adminChange=true发送报备邮件
  106. adminChange := false
  107. if vp.Status == archive.VideoStatusRecycle || vp.Status == archive.VideoStatusLock || attrChanged {
  108. adminChange = true
  109. }
  110. //get fans
  111. fans := int64(0)
  112. if pfl, _ := s.profile(c, arc.Mid); pfl != nil {
  113. fans = pfl.Follower
  114. }
  115. if strings.Contains(playurl, "/ugc/") {
  116. err = s.busUGCFirstRound(vp.Aid, vp.Filename, vp.EncodePurpose, vp.VideoDesign, vp.Status, vp.Encoding, vp.RegionID, arc.TypeID, fans, adminChange)
  117. } else {
  118. err = s.busFirstRound(vp.Aid, vp.Filename, vp.EncodePurpose, vp.VideoDesign, vp.Status, vp.Encoding, vp.RegionID, arc.TypeID, fans, adminChange)
  119. }
  120. return
  121. }
  122. // BatchVideo batch async video audit.
  123. func (s *Service) BatchVideo(c context.Context, vps []*archive.VideoParam, action string) (err error) {
  124. var mp = &archive.MultSyncParam{}
  125. var ok bool
  126. for _, vp := range vps {
  127. mp.Action = action
  128. mp.VideoParam = vp
  129. if ok, err = s.busCache.PushMultSync(c, mp); err != nil {
  130. log.Error("s.busCache.PushMultSync(vp(%+v)) error(%v)", vp, err)
  131. return
  132. }
  133. if !ok {
  134. log.Warn("s.busCache.PushMultSync(vp(%+v))", vp)
  135. continue
  136. }
  137. }
  138. return
  139. }
  140. func (s *Service) dealVideo(c context.Context, vp *archive.VideoParam) (err error) {
  141. var tx *sql.Tx
  142. var attrChanged bool
  143. if tx, err = s.arc.BeginTran(c); err != nil {
  144. log.Error("s.arc.BeginTran() error(%v)", err)
  145. return
  146. }
  147. defer func() {
  148. if r := recover(); r != nil {
  149. tx.Rollback()
  150. log.Error("wocao jingran recover le error(%v)", r)
  151. }
  152. }()
  153. if err = s.txUpVideoAudit(tx, vp); err != nil {
  154. tx.Rollback()
  155. log.Error("s.txUpVideoAudit(vp(%+v)) error(%v)", vp, err)
  156. return
  157. }
  158. //todo 批量支持家长模式 级联通过类tag逻辑
  159. if err = tx.Commit(); err != nil {
  160. log.Error("tx.Commit() error(%v)", err)
  161. return
  162. }
  163. oper := &archive.VideoOper{AID: vp.Aid, UID: vp.UID, VID: vp.ID, Attribute: vp.Attribute, Status: vp.Status, Remark: vp.Note}
  164. operConts := s.diffVideoOper(vp)
  165. oper.Content = strings.Join(operConts, ",")
  166. s.addVideoOper(c, oper)
  167. s.busFirstRoundByPlayurl(c, vp, attrChanged)
  168. s.sendVideoLog(c, vp, oper.Content)
  169. return
  170. }
  171. // UpVideo update archive_video title && desc
  172. func (s *Service) UpVideo(c context.Context, vp *archive.VideoParam) (err error) {
  173. var tx *sql.Tx
  174. if tx, err = s.arc.BeginTran(c); err != nil {
  175. log.Error("s.arc.BeginTran() error(%v)", err)
  176. return
  177. }
  178. defer func() {
  179. if r := recover(); r != nil {
  180. tx.Rollback()
  181. log.Error("wocao jingran recover le error(%v)", r)
  182. }
  183. }()
  184. if vp.ID != 0 {
  185. if err = s.txUpVideo(tx, vp.ID, vp.Title, vp.Desc); err != nil {
  186. tx.Rollback()
  187. log.Error("s.txUpVideo(%d,%s,%s) error(%v)", vp.ID, vp.Title, vp.Desc, err)
  188. return
  189. }
  190. vp.Note = "审核人员修改分P标题和简介!"
  191. } else {
  192. var (
  193. v *archive.Video
  194. ctime = xtime.Time(time.Now().Unix())
  195. )
  196. v = &archive.Video{Aid: vp.Aid, Title: vp.Title, Desc: vp.Desc, Filename: vp.Filename, SrcType: vp.SrcType, Cid: vp.Cid,
  197. Duration: vp.Duration, Filesize: vp.Filesize, Resolutions: vp.Resolutions, Index: vp.Index, Status: vp.Status, Playurl: vp.Playurl,
  198. Attribute: vp.Attribute, FailCode: vp.FailCode, XcodeState: vp.XcodeState, WebLink: vp.WebLink, CTime: ctime, MTime: ctime}
  199. if vp.ID, err = s.txAddVideo(tx, v); err != nil {
  200. tx.Rollback()
  201. log.Error("s.arc.txAddVideo(%+v) error(%v)", v, err)
  202. return
  203. }
  204. vp.Note = "审核人员添加分P!"
  205. }
  206. if _, err = s.arc.TxAddAudit(tx, vp.Aid, vp.ID, vp.TagID, vp.Oname, vp.Note, vp.Reason); err != nil {
  207. tx.Rollback()
  208. log.Error("s.arc.TxAddAudit(%d,%d,%d,%s,%s,%s)", vp.Aid, vp.ID, vp.TagID, vp.Oname, vp.Note, vp.Reason)
  209. return
  210. }
  211. if err = tx.Commit(); err != nil {
  212. log.Error("tx.Commit() error(%v)", err)
  213. return
  214. }
  215. s.busSecondRound(vp.Aid, 0, false, false, false, false, false, false, "", nil)
  216. s.sendVideoLog(c, vp, vp.Note)
  217. return
  218. }
  219. // UpWebLink update archive_video weblink
  220. func (s *Service) UpWebLink(c context.Context, vp *archive.VideoParam) (err error) {
  221. var tx *sql.Tx
  222. if tx, err = s.arc.BeginTran(c); err != nil {
  223. log.Error("s.arc.BeginTran() error(%v)", err)
  224. return
  225. }
  226. defer func() {
  227. if r := recover(); r != nil {
  228. tx.Rollback()
  229. log.Error("wocao jingran recover le error(%v)", r)
  230. }
  231. }()
  232. if err = s.txUpVideoLink(tx, vp.ID, vp.Cid, vp.WebLink); err != nil {
  233. tx.Rollback()
  234. log.Error("s.txUpVideoLink(%d,%d,%s)", vp.ID, vp.Cid, vp.WebLink)
  235. return
  236. }
  237. if err = tx.Commit(); err != nil {
  238. log.Error("tx.Commit() error(%v)", err)
  239. return
  240. }
  241. s.busSecondRound(vp.Aid, 0, false, false, false, false, false, false, "", nil)
  242. s.sendVideoLog(c, vp, "更新weblink")
  243. return
  244. }
  245. // DelVideo delete archive_video.
  246. func (s *Service) DelVideo(c context.Context, vp *archive.VideoParam) (err error) {
  247. var vs []*archive.Video
  248. if vs, _ = s.arc.NewVideosByAid(c, vp.Aid); len(vs) <= 0 {
  249. err = ecode.ArchiveNotExist
  250. return
  251. }
  252. var tx *sql.Tx
  253. if tx, err = s.arc.BeginTran(c); err != nil {
  254. log.Error("s.arc.BeginTran() error(%v)", err)
  255. return
  256. }
  257. defer func() {
  258. if r := recover(); r != nil {
  259. tx.Rollback()
  260. log.Error("wocao jingran recover le error(%v)", r)
  261. }
  262. }()
  263. if err = s.txDelVideo(tx, vp); err != nil {
  264. tx.Rollback()
  265. log.Error("s.txDelVideo(%d)", vp.ID)
  266. return
  267. }
  268. indexOrder := 1
  269. for _, v := range vs {
  270. if v.ID == vp.ID {
  271. continue
  272. }
  273. if err = s.txUpVideoIndex(tx, v.ID, indexOrder); err != nil {
  274. tx.Rollback()
  275. log.Error("s.txUpVideoIndex(%d,%d)", v.ID, indexOrder)
  276. return
  277. }
  278. indexOrder++
  279. }
  280. if err = tx.Commit(); err != nil {
  281. log.Error("tx.Commit() error(%v)", err)
  282. return
  283. }
  284. s.busSecondRound(vp.Aid, 0, false, false, false, false, false, false, "", nil)
  285. s.sendVideoLog(c, vp, "删除视频")
  286. return
  287. }
  288. // ChangeIndex change archive_video index.
  289. func (s *Service) ChangeIndex(c context.Context, lo *archive.IndexParam) (err error) {
  290. if vs, _ := s.arc.NewVideosByAid(c, lo.Aid); len(vs) <= 0 {
  291. err = ecode.ArchiveNotExist
  292. return
  293. }
  294. var tx *sql.Tx
  295. if tx, err = s.arc.BeginTran(c); err != nil {
  296. log.Error("s.arc.BeginTran() error(%v)", err)
  297. return
  298. }
  299. defer func() {
  300. if r := recover(); r != nil {
  301. tx.Rollback()
  302. log.Error("wocao jingran recover le error(%v)", r)
  303. }
  304. }()
  305. for _, v := range lo.ListOrder {
  306. if err = s.txUpVideoIndex(tx, v.ID, v.Index); err != nil {
  307. tx.Rollback()
  308. log.Error("s.arc.TxUpVideoIndex(%d,%d)", v.ID, v.Index)
  309. return
  310. }
  311. }
  312. if err = tx.Commit(); err != nil {
  313. log.Error("tx.Commit() error(%v)", err)
  314. return
  315. }
  316. s.busSecondRound(lo.Aid, 0, false, false, false, false, false, false, "", nil)
  317. return
  318. }