extra_func.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/videoup/model/archive"
  7. "go-common/app/job/main/videoup/model/message"
  8. "go-common/app/job/main/videoup/model/redis"
  9. accApi "go-common/app/service/main/account/api"
  10. "go-common/library/conf/env"
  11. "go-common/library/database/sql"
  12. "go-common/library/log"
  13. xtime "go-common/library/time"
  14. "strings"
  15. )
  16. func (s *Service) archiveVideo(c context.Context, filename string) (v *archive.Video, a *archive.Archive, err error) {
  17. if v, err = s.arc.NewVideo(c, filename); err != nil {
  18. log.Error("s.arc.Video(%s) error(%v)", filename, err)
  19. return
  20. }
  21. if v == nil {
  22. log.Error("s.arc.Video(%s) video is nil", filename)
  23. err = fmt.Errorf("video(%s) is not exists", filename)
  24. return
  25. }
  26. if a, err = s.arc.Archive(c, v.Aid); err != nil {
  27. log.Error("s.arc.Archive(%d) filename(%s) error(%v)", v.Aid, filename, err)
  28. return
  29. }
  30. if a == nil {
  31. log.Error("s.arc.Archive(%s) archive(%d) is nil", filename, v.Aid)
  32. err = fmt.Errorf("archive(%d) filename(%s) is not exists", v.Aid, filename)
  33. }
  34. return
  35. }
  36. func (s *Service) archiveVideoByAid(c context.Context, filename string, aid int64) (v *archive.Video, a *archive.Archive, err error) {
  37. if v, err = s.arc.NewVideoByAid(c, filename, aid); err != nil {
  38. log.Error("s.arc.Video(%s) error(%v)", filename, err)
  39. return
  40. }
  41. if v == nil {
  42. log.Error("s.arc.Video(%s) video is nil", filename)
  43. err = fmt.Errorf("video(%s) is not exists", filename)
  44. return
  45. }
  46. if a, err = s.arc.Archive(c, aid); err != nil {
  47. log.Error("s.arc.Archive(%d) filename(%s) error(%v)", aid, filename, err)
  48. return
  49. }
  50. if a == nil {
  51. log.Error("s.arc.Archive(%s) archive(%d) is nil", filename, aid)
  52. err = fmt.Errorf("archive(%d) filename(%s) is not exists", aid, filename)
  53. }
  54. return
  55. }
  56. func (s *Service) isPorder(a *archive.Archive) bool {
  57. if a == nil {
  58. return false
  59. }
  60. return a.AttrVal(archive.AttrBitIsPorder) == archive.AttrYes
  61. }
  62. func (s *Service) isUGCPay(a *archive.Archive) bool {
  63. if a == nil {
  64. return false
  65. }
  66. return a.AttrVal(archive.AttrBitUGCPay) == archive.AttrYes
  67. }
  68. func (s *Service) isStaff(a *archive.Archive) bool {
  69. if a == nil {
  70. return false
  71. }
  72. return a.AttrVal(archive.AttrBitSTAFF) == archive.AttrYes
  73. }
  74. func (s *Service) canDo(mid int64) bool {
  75. return !s.c.Debug || (s.c.DebugMid == mid && mid > 0)
  76. }
  77. func (s *Service) archiveState(c context.Context, a *archive.Archive, v *archive.Video, ad *archive.AuditParam) (state int8, access int16, attr archive.Attr, forbidAttr *archive.ForbidAttr) {
  78. // videos
  79. var (
  80. vs []*archive.Video
  81. err error
  82. )
  83. if vs, err = s.arc.NewVideos(c, a.Aid); err != nil {
  84. log.Error("s.arc.Videos(%d) error(%v)", a.Aid, err)
  85. return
  86. }
  87. if len(vs) == 0 {
  88. state = archive.StateForbidWait
  89. log.Warn("archive(%d) have no videos", a.Aid)
  90. return
  91. }
  92. var (
  93. newState int8
  94. newAccess int16
  95. )
  96. forbidAttr, _ = s.arc.Forbid(c, a.Aid)
  97. //聚合状态和属性
  98. for _, tv := range vs {
  99. if v != nil && tv.Filename == v.Filename {
  100. tv = v // NOTE: v maybe change by tran begin, so use v.
  101. }
  102. if tv.Status == archive.VideoStatusDelete {
  103. continue
  104. }
  105. if tv.Status == archive.VideoStatusLock {
  106. newState = archive.StateForbidLock
  107. break
  108. }
  109. if tv.Status == archive.VideoStatusRecicle || newState == archive.StateForbidRecicle {
  110. newState = archive.StateForbidRecicle
  111. continue
  112. }
  113. if tv.Status == archive.VideoStatusXcodeFail || newState == archive.StateForbidXcodeFail {
  114. newState = archive.StateForbidXcodeFail
  115. continue
  116. }
  117. if tv.Status == archive.VideoStatusSubmit || newState == archive.StateForbidSubmit {
  118. newState = archive.StateForbidSubmit
  119. continue
  120. }
  121. if tv.Status == archive.VideoStatusWait || newState == archive.StateForbidSubmit {
  122. newState = archive.StateForbidSubmit
  123. continue
  124. }
  125. if tv.XcodeState <= archive.VideoXcodeHDFinish || newState == archive.StateForbidWait {
  126. newState = archive.StateForbidSubmit
  127. continue
  128. }
  129. if tv.XcodeState <= archive.VideoDispatchRunning || newState == archive.StateForbidSubmit {
  130. newState = archive.StateForbidSubmit
  131. continue
  132. }
  133. if tv.Status == archive.VideoStatusAccess || newAccess == archive.AccessMember {
  134. newState = archive.StateOpen
  135. newAccess = archive.AccessMember
  136. } else if tv.Status == archive.VideoStatusOpen {
  137. newState = archive.StateOpen
  138. }
  139. // attr
  140. if tv.AttrVal(archive.AttrBitNoRank) == archive.AttrYes || a.AttrVal(archive.AttrBitNoRank) == archive.AttrYes {
  141. attr.Set(archive.AttrYes, archive.AttrBitNoRank)
  142. forbidAttr.SetAttr(archive.ForbidRank, archive.AttrYes, archive.ForbidRankMain)
  143. }
  144. if tv.AttrVal(archive.AttrBitNoDynamic) == archive.AttrYes || a.AttrVal(archive.AttrBitNoDynamic) == archive.AttrYes {
  145. attr.Set(archive.AttrYes, archive.AttrBitNoDynamic)
  146. forbidAttr.SetAttr(archive.ForbidDynamic, archive.AttrYes, archive.ForbidDynamicMain)
  147. }
  148. if tv.AttrVal(archive.AttrBitOverseaLock) == archive.AttrYes || a.AttrVal(archive.AttrBitOverseaLock) == archive.AttrYes {
  149. attr.Set(archive.AttrYes, archive.AttrBitOverseaLock)
  150. forbidAttr.SetAttr(archive.ForbidShow, archive.AttrYes, archive.ForbidShowOversea)
  151. }
  152. if tv.AttrVal(archive.AttrBitNoRecommend) == archive.AttrYes || a.AttrVal(archive.AttrBitNoRecommend) == archive.AttrYes {
  153. attr.Set(archive.AttrYes, archive.AttrBitNoRecommend)
  154. forbidAttr.SetAttr(archive.ForbidRecommend, archive.AttrYes, archive.ForbidRecommendMain)
  155. }
  156. if tv.AttrVal(archive.AttrBitNoSearch) == archive.AttrYes || a.AttrVal(archive.AttrBitNoSearch) == archive.AttrYes {
  157. attr.Set(archive.AttrYes, archive.AttrBitNoSearch)
  158. // NOTE: not search forbit
  159. }
  160. if tv.AttrVal(archive.AttrNoPushBplus) == archive.AttrYes || a.AttrVal(archive.AttrNoPushBplus) == archive.AttrYes {
  161. attr.Set(archive.AttrYes, archive.AttrNoPushBplus)
  162. }
  163. if tv.AttrVal(archive.AttrBitParentMode) == archive.AttrYes || a.AttrVal(archive.AttrBitParentMode) == archive.AttrYes {
  164. attr.Set(archive.AttrYes, archive.AttrBitParentMode)
  165. }
  166. }
  167. if newState == archive.StateOpen {
  168. if s.hadPassed(c, a.Aid) {
  169. newState = archive.StateForbidFixed
  170. if ad != nil && ad.IsAudit {
  171. newState = archive.StateOpen
  172. }
  173. } else if s.isAuditType(a.TypeID) { // 多P或指定分区
  174. newState = archive.StateForbidWait
  175. } else if addit, _ := s.arc.Addit(c, a.Aid); addit != nil && (addit.OrderID > 0 || addit.UpFrom == archive.UpFromPGC || addit.UpFrom == archive.UpFromPGCSecret || addit.MissionID > 0) { // NOTE: order || up_from pgc || mission
  176. newState = archive.StateForbidWait
  177. } else if hisCnt, _ := s.arc.HistoryCount(c, a.Aid); hisCnt > 1 { // modified before dispatch finish
  178. newState = archive.StateForbidWait
  179. } else if delay, err := s.arc.Delay(c, a.Aid); err == nil && delay != nil {
  180. if delay.DTime.Before(time.Now()) {
  181. newState = archive.StateForbidWait
  182. } else if s.isPorder(a) && !s.isAuditType(a.TypeID) { //私单+定时+非特殊分区->newState = -1
  183. newState = archive.StateForbidWait
  184. } else if delay.Type == archive.DelayTypeForUser {
  185. newState = archive.StateForbidUserDelay
  186. }
  187. } else if s.isPorder(a) {
  188. newState = archive.StateForbidWait
  189. } else if s.isUGCPay(a) {
  190. //付费稿件在付费待审完成后job才可以自动开放
  191. newState = archive.StateForbidWait
  192. }
  193. if newState == archive.StateOpen && !s.isWhite(a.Mid) && !s.isBlack(a.Mid) {
  194. if pfl, _ := s.profile(c, a.Mid); pfl != nil && pfl.Follower < int64(s.fansCache) && s.isRoundType(a.TypeID) && !s.isMission(c, a.Aid) {
  195. newState = archive.StateOrange // NOTE: auto open must
  196. }
  197. }
  198. }
  199. state = newState
  200. access = newAccess
  201. return
  202. }
  203. func (s *Service) archiveRound(c context.Context, a *archive.Archive) (round int8) {
  204. if archive.NormalState(a.State) {
  205. //定时发布 或者 自动过审逻辑
  206. isAuditType := s.isAuditType(a.TypeID)
  207. if addit, _ := s.arc.Addit(c, a.Aid); addit != nil && (addit.OrderID > 0 || addit.UpFrom == archive.UpFromPGC || addit.UpFrom == archive.UpFromCoopera ||
  208. addit.MissionID > 0 || (s.isPorder(a) && isAuditType)) {
  209. round = archive.RoundEnd // NOTE: maybe -40 -> 0 商单稿件||pgc稿件|| 合作方嵌套稿件 || 活动||私单稿件 进round = 99
  210. } else if addit != nil && addit.UpFrom == archive.UpFromPGCSecret {
  211. round = archive.RoundTriggerClick // Note: 机密PGC通过后进机密回查
  212. } else if (a.Round == archive.RoundAuditThird || a.Round == archive.RoundAuditSecond) && isAuditType {
  213. round = archive.RoundEnd // NOTE: 特殊分区二三审进round = 99
  214. } else if s.isWhite(a.Mid) || s.isBlack(a.Mid) {
  215. round = archive.RoundReviewSecond
  216. } else if pfl, _ := s.profile(c, a.Mid); pfl != nil && pfl.Follower >= int64(s.fansCache) {
  217. round = archive.RoundReviewSecond
  218. } else if pfl != nil && pfl.Follower < int64(s.fansCache) && s.isRoundType(a.TypeID) {
  219. round = archive.RoundReviewFirst // NOTE: if audit type, state must not open!!! so cannot execute here...
  220. } else {
  221. round = archive.RoundReviewFirstWaitTrigger
  222. if pfl == nil {
  223. log.Info("archive(%d) card(%d) is nil", a.Aid, a.Mid)
  224. } else {
  225. log.Info("archive(%d) card(%d) fans(%d) little than config(%d)", a.Aid, a.Mid, pfl.Follower, s.fansCache)
  226. }
  227. }
  228. } else if a.State == archive.StateForbidWait {
  229. if addit, _ := s.arc.Addit(c, a.Aid); addit != nil && (addit.OrderID > 0 || addit.UpFrom == archive.UpFromPGC || addit.UpFrom == archive.UpFromPGCSecret || addit.UpFrom == archive.UpFromCoopera || addit.MissionID > 0) {
  230. //二审待审 商单、pgc,活动
  231. round = archive.RoundAuditSecond
  232. } else if s.isAuditType(a.TypeID) {
  233. //指定分区待审逻辑
  234. // 如果修改未过审 history >1 and not passed (用户修改未过审)走二审
  235. hadPassed := s.hadPassed(c, a.Aid)
  236. hasEdit, _ := s.arc.HistoryCount(c, a.Aid)
  237. if !hadPassed && hasEdit > 1 {
  238. round = archive.RoundAuditSecond
  239. } else if count, _ := s.arc.NewVideoCount(c, a.Aid); count == 1 {
  240. //如果 新增投稿 多p 进二审 单P 进三审
  241. round = archive.RoundAuditThird
  242. } else {
  243. round = archive.RoundAuditSecond
  244. }
  245. } else {
  246. hasEdit, _ := s.arc.HistoryCount(c, a.Aid)
  247. if s.isPorder(a) && hasEdit <= 1 {
  248. round = archive.RoundReviewFlow
  249. } else if s.isUGCPay(a) {
  250. //付费待审 s -1 r 24
  251. round = archive.RoundAuditUGCPayFlow
  252. } else {
  253. round = archive.RoundAuditSecond
  254. }
  255. }
  256. } else if a.State == archive.StateForbidFixed {
  257. round = archive.RoundAuditSecond
  258. } else if a.State == archive.StateForbidUserDelay {
  259. round = a.Round
  260. } else if a.State == archive.StateForbidLater {
  261. round = a.Round
  262. } else {
  263. round = archive.RoundBegin
  264. // NOTE: user delete?? admin later???
  265. }
  266. return
  267. }
  268. func (s *Service) tranRound(c context.Context, tx *sql.Tx, a *archive.Archive) (round int8, err error) {
  269. round = s.archiveRound(c, a)
  270. if _, err = s.arc.TxUpRound(tx, a.Aid, round); err != nil {
  271. log.Error("s.arc.TxUpRound(%d, %d) error(%v)", a.Aid, round, err)
  272. return
  273. }
  274. return
  275. }
  276. func (s *Service) tranArchiveOper(tx *sql.Tx, a *archive.Archive) (err error) {
  277. if _, err = s.arc.TxArchiveOper(tx, a.Aid, a.TypeID, a.State, a.Round, a.Attribute, archive.FirstRoundID, ""); err != nil {
  278. log.Error("s.arc.TxArchiveOper error(%v)", err)
  279. }
  280. return
  281. }
  282. func (s *Service) tranVideo(c context.Context, tx *sql.Tx, a *archive.Archive, v *archive.Video) (err error) {
  283. //up xcode_state
  284. if _, err = s.arc.TxUpXcodeState(tx, v.Filename, v.XcodeState); err != nil {
  285. log.Error("s.arc.TxUpXcodeState(%s, %d) error(%v)", v.Filename, v.XcodeState, err)
  286. return
  287. }
  288. if _, err = s.arc.TxUpVideoXState(tx, v.Filename, v.XcodeState); err != nil {
  289. log.Error("s.arc.TxUpVideoXState(%s, %d) error(%v)", v.Filename, v.XcodeState, err)
  290. return
  291. }
  292. log.Info("archive(%d) filename(%s) upXcodeState(%d)", a.Aid, v.Filename, v.XcodeState)
  293. var stCh bool // NOTE: status changed
  294. // check xcode state
  295. if v.XcodeState == archive.VideoXcodeSDFail || v.XcodeState == archive.VideoXcodeHDFail {
  296. if _, err = s.arc.TxUpFailCode(tx, v.Filename, v.FailCode); err != nil {
  297. log.Error("s.arc.TxUpFailCode(%s, %d, %d) error(%v)", v.Filename, v.FailCode, err)
  298. return
  299. }
  300. if _, err = s.arc.TxUpVideoFailCode(tx, v.Filename, v.FailCode); err != nil {
  301. log.Error("s.arc.TxUpVideoFailCode(%s, %d, %d) error(%v)", v.Filename, v.FailCode, err)
  302. return
  303. }
  304. log.Info("archive(%d) filename(%s) upFailCode(%d)", a.Aid, v.Filename, v.FailCode)
  305. stCh = true
  306. } else if v.XcodeState == archive.VideoXcodeSDFinish {
  307. if _, err = s.arc.TxUpPlayurl(tx, v.Filename, v.Playurl); err != nil {
  308. log.Error("s.arc.TxUpPlayurl(%s, %s) error(%v)", v.Filename, v.Playurl, err)
  309. return
  310. }
  311. if _, err = s.arc.TxUpVideoPlayurl(tx, v.Filename, v.Playurl); err != nil {
  312. log.Error("s.arc.TxUpVideoPlayurl(%s, %s) error(%v)", v.Filename, v.Playurl, err)
  313. return
  314. }
  315. log.Info("archive(%d) filename(%s) upPlayurl(%s)", a.Aid, v.Filename, v.Playurl)
  316. if _, err = s.arc.TxUpVideoDuration(tx, v.Filename, v.Duration); err != nil {
  317. log.Error("s.arc.TxUpVideoDuration(%s, %d) error(%v)", v.Filename, v.Duration, err)
  318. return
  319. }
  320. if _, err = s.arc.TxUpVDuration(tx, v.Filename, v.Duration); err != nil {
  321. log.Error("s.arc.TxUpVDuration(%s, %d) error(%v)", v.Filename, v.Duration, err)
  322. return
  323. }
  324. log.Info("archive(%d) filename(%s) upVdoDuration(%d)", a.Aid, v.Filename, v.Duration)
  325. stCh = true
  326. } else if v.XcodeState == archive.VideoXcodeHDFinish {
  327. if _, err = s.arc.TxUpResolutions(tx, v.Filename, v.Resolutions); err != nil {
  328. log.Error("s.arc.TxUpResolutions(%s, %s) error(%v)", v.Filename, v.Resolutions, err)
  329. return
  330. }
  331. if _, err = s.arc.TxUpVideoResolutionsAndDimensions(tx, v.Filename, v.Resolutions, v.Dimensions); err != nil {
  332. log.Error("s.arc.TxUpVideoResolutions(%s,%s, %s) error(%v)", v.Filename, v.Resolutions, v.Dimensions, err)
  333. return
  334. }
  335. log.Info("archive(%d) filename(%s) upResolution(%s)", a.Aid, v.Filename, v.Resolutions)
  336. if _, err = s.arc.TxUpVideoDuration(tx, v.Filename, v.Duration); err != nil {
  337. log.Error("s.arc.TxUpVideoDuration(%s, %d) error(%v)", v.Filename, v.Duration, err)
  338. return
  339. }
  340. if _, err = s.arc.TxUpVDuration(tx, v.Filename, v.Duration); err != nil {
  341. log.Error("s.arc.TxUpVDuration(%s, %d) error(%v)", v.Filename, v.Duration, err)
  342. return
  343. }
  344. log.Info("archive(%d) filename(%s) upVdoDuration(%d)", a.Aid, v.Filename, v.Duration)
  345. if _, err = s.arc.TxUpFilesize(tx, v.Filename, v.Filesize); err != nil {
  346. log.Error("s.arc.TxUpFilesize(%s, %d) error(%v)", v.Filename, v.Filesize, err)
  347. return
  348. }
  349. if _, err = s.arc.TxUpVideoFilesize(tx, v.Filename, v.Filesize); err != nil {
  350. log.Error("s.arc.TxUpVideoFilesize(%s, %d) error(%v)", v.Filename, v.Filesize, err)
  351. return
  352. }
  353. log.Info("archive(%d) filename(%s) upFilesize(%d)", a.Aid, v.Filename, v.Filesize)
  354. }
  355. //else if v.XcodeState == archive.VideoDispatchRunning || v.XcodeState == archive.VideoDispatchFinish {
  356. // // TODO ???
  357. //}
  358. if !stCh {
  359. return
  360. }
  361. if _, err = s.arc.TxUpStatus(tx, v.Filename, v.Status); err != nil {
  362. log.Error("s.arc.TxUpStatus(%s, %d) error(%v) or rows==0", v.Filename, v.Status, err)
  363. return
  364. }
  365. if v.Status == archive.VideoStatusDelete {
  366. if _, err = s.arc.TxUpRelationStatus(tx, v.Cid, archive.StateForbidUpDelete); err != nil {
  367. log.Error("s.arc.TxUpRelationStatus(%d, %d) error(%v) or rows==0", v.Cid, archive.StateOpen, err)
  368. return
  369. }
  370. } else {
  371. if _, err = s.arc.TxUpVideoStatus(tx, v.Filename, v.Status); err != nil {
  372. log.Error("s.arc.TxUpVideoStatus(%s, %d) error(%v) or rows==0", v.Filename, v.Status, err)
  373. return
  374. }
  375. }
  376. // NOTE: reset relation back to active for data consistent. -100 still -100.
  377. if v.Cid > 0 && v.Status != archive.VideoStatusDelete {
  378. if _, err = s.arc.TxUpRelationStatus(tx, v.Cid, archive.StateOpen); err != nil {
  379. log.Error("s.arc.TxUpRelationStatus(%d, %d) error(%v) or rows==0", v.Cid, archive.StateOpen, err)
  380. return
  381. }
  382. }
  383. log.Info("archive(%d) filename(%s) upStatus(%d)", a.Aid, v.Filename, v.Status)
  384. var reason string
  385. if v.Status == archive.VideoStatusXcodeFail {
  386. if v.XcodeState == archive.VideoXcodeSDFail {
  387. reason = "转码失败:" + archive.XcodeFailMsgs[v.FailCode]
  388. } else if v.XcodeState == archive.VideoXcodeHDFail {
  389. reason = "转码失败:" + archive.XcodeFailMsgs[v.FailCode]
  390. }
  391. } else if v.Status == archive.VideoStatusOpen {
  392. reason = "生产组稿件一转成功"
  393. } else if v.Status == archive.VideoStatusWait {
  394. reason = "一转成功"
  395. }
  396. if _, err = s.arc.TxAddAudit(tx, v.ID, a.Aid, reason); err != nil {
  397. log.Error("s.arc.TxAddAudit(%d, %d) filename(%s) reason(%s) error(%v)", v.ID, a.Aid, v.Filename, reason, err)
  398. return
  399. }
  400. log.Info("archive(%d) filename(%s) addAudit reason(%s)", a.Aid, v.Filename, reason)
  401. return
  402. }
  403. func (s *Service) tranArchive(c context.Context, tx *sql.Tx, a *archive.Archive, v *archive.Video, ad *archive.AuditParam) (change bool, err error) {
  404. // start archive
  405. if a.NotAllowUp() {
  406. log.Warn("archive(%d) filename(%s) state(%d) not allow update", a.Aid, v.Filename, a.State)
  407. return
  408. }
  409. var (
  410. state, access, attr, forbidAttr = s.archiveState(c, a, v, ad)
  411. now = time.Now()
  412. )
  413. if state == a.State {
  414. log.Warn("archive(%d) filename(%s) newState(%d)==oldState(%d)", a.Aid, v.Filename, state, a.State)
  415. } else {
  416. change = true
  417. firstPass := false
  418. // archive
  419. if firstPass, err = s.txUpArcState(c, tx, a.Aid, state); err != nil {
  420. log.Error("s.txUpArcState(%d, %d) filename(%s) error(%v)", a.Aid, state, v.Filename, err)
  421. return
  422. }
  423. a.State = state
  424. log.Info("archive(%d) filename(%s) upState(%d)", a.Aid, v.Filename, a.State)
  425. if firstPass {
  426. if _, err = s.arc.TxUpPTime(tx, a.Aid, now); err != nil {
  427. log.Error("s.arc.TxUpPTime(%d, %d) error(%v)", a.Aid, now.Unix(), err)
  428. return
  429. }
  430. a.PTime = xtime.Time(now.Unix())
  431. log.Info("archive(%d) filename(%s) upPTime(%d)", a.Aid, v.Filename, a.PTime)
  432. }
  433. }
  434. if a.Access != access {
  435. if _, err = s.arc.TxUpAccess(tx, a.Aid, access); err != nil {
  436. log.Error("s.arc.TxUpAccess(%d, %d) filename(%s) error(%v)", a.Aid, access, v.Filename, err)
  437. return
  438. }
  439. a.Access = access
  440. log.Info("archive(%d) filename(%s) upAccess(%d)", a.Aid, v.Filename, a.Access)
  441. }
  442. if err = s.tranSumDuration(c, tx, a); err != nil {
  443. log.Info("s.tranSumDuration error(%v)", err)
  444. return
  445. }
  446. if a.Attribute != (a.Attribute | int32(attr)) {
  447. if _, err = s.arc.TxUpAttr(tx, a.Aid, attr); err != nil {
  448. log.Error("s.arc.TxUpAttr(%d, %d) filename(%s) error(%v)", a.Aid, attr, v.Filename, err)
  449. return
  450. }
  451. a.WithAttr(attr)
  452. log.Info("archive(%d) filename(%s) upAttribute(%d)", a.Aid, v.Filename, a.Attribute)
  453. }
  454. if _, err = s.arc.TxUpForbid(tx, forbidAttr); err != nil {
  455. log.Error("s.arc.TxUpForbid(%+v) error(%v)", forbidAttr, err)
  456. return
  457. }
  458. log.Info("archive(%d) filename(%s) forbidAttr(%+v)", a.Aid, v.Filename, forbidAttr)
  459. return
  460. }
  461. func (s *Service) tranSumDuration(c context.Context, tx *sql.Tx, a *archive.Archive) (err error) {
  462. var sum int64
  463. if sum, err = s.arc.NewSumDuration(c, a.Aid); err != nil {
  464. log.Error("s.arc.SumDuration(%d) error(%v)", a.Aid, err)
  465. err = nil
  466. } else if sum > 0 && a.Duration != sum {
  467. if _, err = s.arc.TxUpArcDuration(tx, a.Aid, sum); err != nil {
  468. log.Error("s.arc.TxUpArcDuration(%d, %d) error(%v)", a.Aid, sum, err)
  469. return
  470. }
  471. a.Duration = sum
  472. log.Info("archive(%d) upArcDuration(%d)", a.Aid, a.Duration)
  473. }
  474. return
  475. }
  476. func (s *Service) tranArcCover(c context.Context, tx *sql.Tx, a *archive.Archive, v *archive.Video) (err error) {
  477. if a.Cover != "" {
  478. return
  479. }
  480. // NOTE: first round need view archive cover, delete when three covers select for user
  481. var cvs []string
  482. //从ai处获取封面,若失败/没有封面信息,则直接返回
  483. if cvs, err = s.arc.AICover(c, v.Filename); err != nil || len(cvs) == 0 {
  484. log.Error("a.arc.AICover(aid(%d) filename(%s)) got covers from AI error(%v), cvs(%v) ", a.Aid, v.Filename, err, cvs)
  485. err = nil
  486. return
  487. }
  488. log.Info("s.arc.AICover(aid(%d), filename(%s)) got covers from AI: cvs(%v)", a.Aid, v.Filename, cvs)
  489. a.Cover = cvs[0] //ai cover只取第一个元素
  490. if _, err = s.arc.TxUpCover(tx, a.Aid, strings.Replace(a.Cover, "https:", "", -1)); err != nil {
  491. log.Error("s.arc.TxUpCover(%d, %s) filename(%s) error(%v)", a.Aid, v.Filename, a.Cover, err)
  492. return
  493. }
  494. log.Info("archive(%d) filename(%s) upCover(%s)", a.Aid, v.Filename, a.Cover)
  495. return
  496. }
  497. func (s *Service) hadPassed(c context.Context, aid int64) (had bool) {
  498. id, err := s.arc.GetFirstPassByAID(c, aid)
  499. if err != nil {
  500. log.Error("hadPassed s.arc.GetFirstPassByAID error(%v) aid(%d)", err, aid)
  501. return
  502. }
  503. had = id > 0
  504. return
  505. }
  506. func (s *Service) profile(c context.Context, mid int64) (p *accApi.ProfileStatReply, err error) {
  507. if p, err = s.accRPC.ProfileWithStat3(c, &accApi.MidReq{Mid: mid}); err != nil {
  508. p = nil
  509. log.Error("s.accRPC.ProfileWithStat3(%d) error(%v)", mid, err)
  510. }
  511. return
  512. }
  513. func (s *Service) changeMission(c context.Context, a *archive.Archive, missionID int64) (err error) {
  514. if missionID > 0 {
  515. s.activity.UpVideo(c, a, missionID)
  516. return
  517. }
  518. var addit *archive.Addit
  519. if addit, err = s.arc.Addit(c, a.Aid); err != nil {
  520. log.Error("s.arc.Addit(%d) error(%v)", a.Aid, err)
  521. return
  522. }
  523. if addit == nil {
  524. return
  525. }
  526. if addit.MissionID > 0 {
  527. s.activity.AddVideo(c, a, addit.MissionID)
  528. }
  529. return
  530. }
  531. func (s *Service) unBindMission(c context.Context, a *archive.Archive, missionID int64) (err error) {
  532. if missionID == 0 {
  533. var addit *archive.Addit
  534. if addit, err = s.arc.Addit(c, a.Aid); err != nil {
  535. log.Error("s.arc.Addit(%d) error(%v)", a.Aid, err)
  536. return
  537. }
  538. if addit == nil || addit.MissionID == 0 {
  539. return
  540. }
  541. missionID = addit.MissionID
  542. }
  543. s.activity.UpVideo(c, a, missionID)
  544. return
  545. }
  546. func (s *Service) cidsByAid(c context.Context, aid int64) (cids []int64, err error) {
  547. var (
  548. vs []*archive.Video
  549. )
  550. if vs, err = s.arc.NewVideos(c, aid); err != nil {
  551. log.Error("archive(%d) cidsByAid s.arc.Videos error(%v)", aid, err)
  552. return
  553. }
  554. for _, v := range vs {
  555. cids = append(cids, v.Cid)
  556. }
  557. return
  558. }
  559. func (s *Service) syncBVC(c context.Context, a *archive.Archive) (err error) {
  560. if env.DeployEnv == env.DeployEnvFat1 || env.DeployEnv == env.DeployEnvDev {
  561. log.Info("archive(%d) syncBVC stop for dev/fat1 env", a.Aid)
  562. return
  563. }
  564. var cids []int64
  565. if cids, err = s.cidsByAid(c, a.Aid); err != nil {
  566. log.Error("archive(%d) second_round cidsByAid error(%v)", a.Aid, err)
  567. s.syncRetry(c, a.Aid, a.Mid, redis.ActionForBvcCapable, "", "")
  568. return
  569. }
  570. var retryCids, okCids, noCids []int64
  571. for _, cid := range cids {
  572. //pgc付费 单独播放通道
  573. //ugc 付费 也是单独播放通道
  574. //ugc 通道普通视频播放控制
  575. if a.AttrVal(archive.AttrBitBadgepay) == archive.AttrYes || a.AttrVal(archive.AttrBitIsBangumi) == archive.AttrYes || a.AttrVal(archive.AttrBitUGCPay) == archive.AttrYes {
  576. noCids = append(noCids, cid)
  577. continue
  578. }
  579. var count int
  580. // playable videos num
  581. if count, err = s.arc.NewVideoCountCapable(c, cid); err != nil {
  582. log.Error("archive(%d) syncBVC checkCids cid(%d) s.arc.VideoCountCapable error(%v)", a.Aid, cid, err)
  583. retryCids = append(retryCids, cid)
  584. continue
  585. }
  586. if count == 0 {
  587. noCids = append(noCids, cid)
  588. } else if count == 1 {
  589. if !a.IsNormal() {
  590. noCids = append(noCids, cid)
  591. } else {
  592. okCids = append(okCids, cid)
  593. }
  594. } else {
  595. if a.IsForbid() {
  596. aids, _ := s.arc.ValidAidByCid(c, cid)
  597. fcnt := 0
  598. for _, aid := range aids {
  599. var arc *archive.Archive
  600. if arc, err = s.arc.Archive(c, aid); err != nil {
  601. log.Error("syncBVC get archive (%d) cid(%d) error(%v)", aid, cid, err)
  602. break
  603. }
  604. if arc != nil && arc.State >= 0 {
  605. fcnt++
  606. break
  607. }
  608. }
  609. log.Info("syncBVC ValidAidByCid cid(%d) aids(%v) fcnt(%d)", cid, aids, fcnt)
  610. // num of cids used in other open state archive
  611. if err != nil {
  612. log.Error("checkCids cid(%d) error(%v)", cid, err)
  613. retryCids = append(retryCids, cid)
  614. continue
  615. } else if fcnt == 0 {
  616. noCids = append(noCids, cid)
  617. continue
  618. }
  619. // NOTE: when fcnt>0, means cid has normal archive.
  620. }
  621. okCids = append(okCids, cid)
  622. }
  623. }
  624. var flagRetry = false
  625. if len(retryCids) != 0 {
  626. log.Warn("syncBVC aid(%d) cids(%v) need retry again", a.Aid, retryCids)
  627. flagRetry = true
  628. }
  629. if len(okCids) != 0 {
  630. if err = s.bvc.VideoCapable(c, a.Aid, okCids, message.CanPlay); err != nil {
  631. log.Error("syncBVC aid(%d) cids(%v) s.bvc.VideoCapable error(%v)", a.Aid, okCids, err)
  632. flagRetry = true
  633. }
  634. }
  635. if len(noCids) != 0 {
  636. if err = s.bvc.VideoCapable(c, a.Aid, noCids, message.CanNotPlay); err != nil {
  637. log.Error("syncBVC aid(%d) cids(%v) s.bvc.VideoCapable error(%v)", a.Aid, noCids, err)
  638. flagRetry = true
  639. }
  640. }
  641. if flagRetry {
  642. s.syncRetry(c, a.Aid, a.Mid, redis.ActionForBvcCapable, "", "")
  643. }
  644. return
  645. }
  646. //txAddFirstPass 添加第一次过审记录
  647. func (s *Service) txAddFirstPass(c context.Context, tx *sql.Tx, aid int64, state int8) (firstPass bool, err error) {
  648. if !archive.NormalState(state) || s.hadPassed(c, aid) {
  649. return
  650. }
  651. if err = s.arc.AddFirstPass(tx, aid); err != nil {
  652. log.Error("txAddFirstPass error(%v) aid(%d)", err, aid)
  653. return
  654. }
  655. firstPass = true
  656. return
  657. }
  658. //txUpArcState 更新稿件的state并联动添加第一次过审记录
  659. func (s *Service) txUpArcState(c context.Context, tx *sql.Tx, aid int64, state int8) (firstPass bool, err error) {
  660. if _, err = s.arc.TxUpState(tx, aid, state); err != nil {
  661. log.Error("txUpArcState s.arc.TxUpState error(%v) aid(%d) state(%d)", err, aid, state)
  662. return
  663. }
  664. if firstPass, err = s.txAddFirstPass(c, tx, aid, state); err != nil {
  665. log.Error("txUpArcState s.txAddFirstPass error(%v) aid(%d) state(%d)", err, aid, state)
  666. return
  667. }
  668. return
  669. }
  670. // IsUpperFirstPass 是否UP主第一次过审稿件
  671. func (s *Service) IsUpperFirstPass(c context.Context, mid, aid int64) (is bool, err error) {
  672. is = true
  673. sMap, err := s.arc.UpperArcStateMap(c, mid)
  674. if err != nil {
  675. log.Error("s.arc.UpperArcStateMap(%d,%d) error(%v)", mid, aid, err)
  676. return
  677. }
  678. delete(sMap, aid) //剔除当前稿件
  679. var aids []int64
  680. for k, v := range sMap {
  681. aids = append(aids, k)
  682. if archive.NormalState(v) {
  683. //如果有其它过审的稿件,那么当前UP主肯定不是第一次过审
  684. is = false
  685. break
  686. }
  687. }
  688. if is {
  689. //查询first_pass表
  690. var count int
  691. count, err = s.arc.FirstPassCount(c, aids)
  692. if err != nil {
  693. log.Error("s.arc.FirstPassCount(%v) error(%v)", aids, err)
  694. return
  695. }
  696. is = count == 0
  697. }
  698. return
  699. }