archive_result.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/job/main/archive/model/archive"
  5. "go-common/app/job/main/archive/model/result"
  6. "go-common/app/job/main/archive/model/retry"
  7. arcmdl "go-common/app/service/main/archive/model/archive"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. func (s *Service) updateSubjectMid(nw *result.Archive, old *result.Archive) {
  12. if nw.Mid == old.Mid {
  13. return
  14. }
  15. if err := s.replyDao.ChangeSubjectMid(nw.AID, nw.Mid); err == nil {
  16. log.Info("/x/v2/reply/admin/subject/mid called")
  17. }
  18. }
  19. // sendMail send e-mail after state change
  20. func (s *Service) sendMail(nw *result.Archive, old *result.Archive) {
  21. // nw.Attribute == old.Attribute tmp remove
  22. if nw.Mid == old.Mid && nw.TypeID == old.TypeID && nw.Duration == old.Duration && nw.Title == old.Title &&
  23. nw.Cover == old.Cover && nw.Content == old.Content && nw.Copyright == old.Copyright &&
  24. nw.State == old.State && nw.Access == old.Access && nw.Forward == old.Forward && nw.PubTime == old.PubTime && nw.CTime == old.CTime { // all field
  25. log.Info("archive(%d) change only mtime no email", nw.ID)
  26. return
  27. }
  28. a, err := s.arcServices[0].Archive3(context.TODO(), &arcmdl.ArgAid2{Aid: nw.AID})
  29. if err != nil || a == nil {
  30. log.Error("s.arcRPC.Archive3(%d) error(%v) or arc is nil", nw.AID, err)
  31. return
  32. }
  33. // not bangumi or movie
  34. if a.AttrVal(arcmdl.AttrBitIsBangumi) == arcmdl.AttrNo && a.AttrVal(arcmdl.AttrBitIsMovie) == arcmdl.AttrNo {
  35. return
  36. }
  37. s.emailDao.PGCNotifyMail(a, nw, old)
  38. log.Info("pgc notify(%d) mail send success", nw.AID)
  39. }
  40. func (s *Service) updateResultCache(nw *result.Archive, old *result.Archive) (err error) {
  41. var (
  42. c = context.TODO()
  43. oldMid int64
  44. )
  45. if old != nil && old.Mid != nw.Mid {
  46. oldMid = old.Mid
  47. }
  48. for k, rpc := range s.arcServices {
  49. if err = rpc.ArcCache2(c, &arcmdl.ArgCache2{Aid: nw.AID, Tp: arcmdl.CacheUpdate}); err != nil {
  50. log.Error("s.arcRPC(%d).ArcCache2(%d, %s) error(%v)", k, nw.AID, arcmdl.CacheUpdate, err)
  51. }
  52. if nw.State >= 0 || nw.State == -6 {
  53. if err = rpc.ArcCache2(c, &arcmdl.ArgCache2{Aid: nw.AID, OldMid: oldMid, Tp: arcmdl.CacheAdd}); err != nil {
  54. log.Error("s.arcRpc(%d).ArcCache2(%d, %s) error(%v)", k, nw.AID, arcmdl.CacheAdd, err)
  55. }
  56. } else {
  57. if err = rpc.ArcCache2(c, &arcmdl.ArgCache2{Aid: nw.AID, OldMid: oldMid, Tp: arcmdl.CacheDelete}); err != nil {
  58. log.Error("s.arcRpc(%d).ArcCache2(%d, %s) error(%v)", k, nw.AID, arcmdl.CacheDelete, err)
  59. }
  60. }
  61. if err != nil {
  62. rt := &retry.Info{Action: retry.FailUpCache}
  63. rt.Data.Aid = nw.AID
  64. rt.Data.State = nw.State
  65. s.PushFail(c, rt)
  66. log.Error("updateResultCache error(%v)", err)
  67. }
  68. }
  69. return
  70. }
  71. func (s *Service) updateResultField(nw *result.Archive, old *result.Archive) {
  72. var (
  73. c = context.TODO()
  74. err error
  75. )
  76. if nw.TypeID != old.TypeID {
  77. for k, rpc := range s.arcServices {
  78. if err = rpc.ArcFieldCache2(c, &arcmdl.ArgFieldCache2{Aid: nw.AID, TypeID: nw.TypeID, OldTypeID: old.TypeID}); err != nil {
  79. log.Error("s.arcRPC(%d).ArcFieldCache2(%d, %d, %d) error(%v)", k, nw.AID, nw.TypeID, old.TypeID, err)
  80. }
  81. }
  82. }
  83. }
  84. func (s *Service) tranResult(c context.Context, aid int64) (changed bool, upCids []int64, delCids []int64, err error) {
  85. var (
  86. tx *sql.Tx
  87. rows int64
  88. a *archive.Archive
  89. aResult *result.Archive
  90. ad *archive.Addit
  91. vs []*archive.Video
  92. videosCnt int
  93. staff []*archive.Staff
  94. )
  95. defer func() {
  96. if err != nil {
  97. rt := &retry.Info{Action: retry.FailResultAdd}
  98. rt.Data.Aid = aid
  99. s.PushFail(c, rt)
  100. log.Error("tranResult error(%v)", err)
  101. }
  102. }()
  103. if a, err = s.archiveDao.Archive(c, aid); err != nil || a == nil {
  104. log.Error("s.arc.Archive(%d) error(%v)", aid, err)
  105. return
  106. }
  107. if !a.IsSyncState() {
  108. log.Info("archive(%d) state(%d) cant change", aid, a.State)
  109. // FIXME: eeeee
  110. if s.isPGC(aid) && !s.hadPassed(c, aid) {
  111. log.Info("archive(%d) is PGC first change", aid)
  112. } else {
  113. return
  114. }
  115. }
  116. if ad, err = s.archiveDao.Addit(c, aid); err != nil {
  117. log.Error("s.arc.Addit(%d) error(%v)", aid, err)
  118. return
  119. }
  120. if ad == nil {
  121. ad = &archive.Addit{Aid: aid}
  122. }
  123. // if aid%10 == 0 || aid%10 == 1 || aid%10 == 2 {
  124. if vs, err = s.archiveDao.Videos2(c, aid); err != nil {
  125. log.Error("s.arc.Videos2(%d) error(%v)", aid, err)
  126. return
  127. }
  128. // } else {
  129. // if vs, err = s.archiveDao.Videos(c, aid); err != nil {
  130. // log.Error("s.arc.Videos(%d) error(%v)", aid, err)
  131. // return
  132. // }
  133. // }
  134. for _, v := range vs {
  135. if (v.Status == archive.VideoStatusAccess || v.Status == archive.VideoStatusOpen) && v.State >= 0 {
  136. videosCnt++
  137. }
  138. }
  139. // 辣鸡dede, check cid
  140. for _, v := range vs {
  141. if v.Cid == 0 && v.Status == archive.VideoStatusSubmit {
  142. // NOTE: 刚上传,没必要同步去
  143. log.Error("aid(%d) vid(%d) cid(%d) videoStatus(%d) return", v.Aid, v.ID, v.Cid, v.Status)
  144. return
  145. }
  146. }
  147. if aResult, err = s.resultDao.Archive(c, aid); err != nil {
  148. log.Error("s.resultDao.Archive error(%+v)", err)
  149. return
  150. }
  151. if tx, err = s.resultDao.BeginTran(c); err != nil {
  152. log.Error("s.result.BeginTran error(%v)", err)
  153. return
  154. }
  155. var (
  156. duration int
  157. firstCid int64
  158. dimensions string
  159. )
  160. for _, v := range vs {
  161. if (v.Status == archive.VideoStatusAccess || v.Status == archive.VideoStatusOpen) && v.State == archive.VideoRelationBind {
  162. if _, err = s.resultDao.TxAddVideo(c, tx, v); err != nil {
  163. tx.Rollback()
  164. log.Error("s.result.TxAddVideo error(%v)", err)
  165. break
  166. }
  167. duration += int(v.Duration)
  168. upCids = append(upCids, v.Cid)
  169. if v.Index == 1 && v.SrcType == "vupload" {
  170. firstCid = v.Cid
  171. dimensions = v.Dimensions
  172. }
  173. } else {
  174. if _, err = s.resultDao.TxDelVideoByCid(c, tx, aid, v.Cid); err != nil {
  175. tx.Rollback()
  176. log.Error("s.result.TxDelVideoByCid error(%v)")
  177. break
  178. }
  179. delCids = append(delCids, v.Cid)
  180. }
  181. }
  182. a.Duration = duration
  183. if rows, err = s.resultDao.TxAddArchive(c, tx, a, ad, videosCnt, firstCid, dimensions); err != nil {
  184. tx.Rollback()
  185. log.Error("s.result.TxAddArchive error(%v)", err)
  186. return
  187. }
  188. if rows == 0 {
  189. if _, err = s.resultDao.TxUpArchive(c, tx, a, ad, videosCnt, firstCid, dimensions); err != nil {
  190. tx.Rollback()
  191. log.Error("s.result.TxUpArchive error(%v)")
  192. return
  193. }
  194. }
  195. // 更新联合投稿人
  196. if a.AttrVal(archive.AttrBitIsCooperation) == archive.AttrYes {
  197. if staff, err = s.archiveDao.Staff(c, aid); err != nil {
  198. tx.Rollback()
  199. log.Error("s.archiveDao.Staff aid(%d) error(%v)", aid, err)
  200. return
  201. }
  202. if err = s.resultDao.TxDelStaff(c, tx, aid); err != nil {
  203. tx.Rollback()
  204. log.Error("s.result.TxDelStaff aid(%d) error(%v)", aid, err)
  205. return
  206. }
  207. if staff != nil {
  208. if err = s.resultDao.TxAddStaff(c, tx, aid, staff); err != nil {
  209. tx.Rollback()
  210. log.Error("s.result.TxAddStaff aid(%d) error(%v)", aid, err)
  211. return
  212. }
  213. }
  214. } else { //从联合投稿改为非联合投稿的 删除staff数据
  215. if aResult != nil && aResult.AttrVal(archive.AttrBitIsCooperation) == archive.AttrYes {
  216. if err = s.resultDao.TxDelStaff(c, tx, aid); err != nil {
  217. tx.Rollback()
  218. log.Error("s.result.TxDelStaff aid(%d) error(%v)", aid, err)
  219. return
  220. }
  221. }
  222. }
  223. if err = tx.Commit(); err != nil {
  224. log.Error("tx.Commit error(%v)")
  225. return
  226. }
  227. log.Info("aid(%d) upCids(%d) delCids(%d) db updated", aid, len(upCids), len(delCids))
  228. changed = true
  229. return
  230. }