tx.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. // txUpArcAttrs update archive attrs
  11. func (s *Service) txUpArcAttrs(tx *sql.Tx, a *archive.Archive, attrs map[uint]int32, redirectURL string) (cont []string, err error) {
  12. const template = "[%s]从[%s]设为[%s]"
  13. var yesOrNo = map[int32]string{archive.AttrYes: "是", archive.AttrNo: "否"}
  14. for bit, attr := range attrs {
  15. if bit == archive.AttrBitJumpURL && (attr == archive.AttrNo || redirectURL == "") {
  16. redirectURL = ""
  17. attr = archive.AttrNo
  18. }
  19. var arcRows int64
  20. if arcRows, err = s.arc.TxUpArcAttr(tx, a.Aid, bit, attr); err != nil {
  21. log.Error("s.arc.TxUpArcAttr(%d,%d,%d) error(%v)", a.Aid, bit, attr, err)
  22. return
  23. }
  24. if bit == archive.AttrBitJumpURL {
  25. var additRows int64
  26. if additRows, err = s.arc.TxUpAdditRedirect(tx, a.Aid, redirectURL); err != nil {
  27. log.Error("s.arc.TxUpAddit(%d,%s) error(%v)", a.Aid, redirectURL, err)
  28. return
  29. }
  30. if arcRows != 0 || additRows != 0 {
  31. log.Info("aid(%d) update addit redirectURL(%s)", a.Aid, redirectURL)
  32. var desc = redirectURL
  33. if desc == "" {
  34. desc = "空"
  35. }
  36. cont = append(cont, fmt.Sprintf("[%s]设为[%s]", "跳转地址", desc))
  37. }
  38. }
  39. a.AttrSet(attr, bit)
  40. if arcRows <= 0 {
  41. continue
  42. }
  43. cont = append(cont, fmt.Sprintf(template, archive.BitDesc(bit), yesOrNo[^attr&1], yesOrNo[attr]))
  44. log.Info("aid(%d) update archive bit(%d) bitdesc(%s) attrs(%d)", a.Aid, bit, archive.BitDesc(bit), attr)
  45. }
  46. return
  47. }
  48. // txUpArcForbidAttrs update archive forbid attrs
  49. func (s *Service) txUpArcForbidAttrs(c context.Context, tx *sql.Tx, a *archive.Archive, forbidAttrs map[string]map[uint]int32) (conts []string, err error) {
  50. const template = "[%s]从[%s]设为[%s]"
  51. var (
  52. yesOrNo = map[int32]string{archive.AttrYes: "是", archive.AttrNo: "否"}
  53. forbid *archive.ForbidAttr
  54. change bool
  55. )
  56. // forbid
  57. if forbid, err = s.arc.Forbid(c, a.Aid); err != nil {
  58. log.Error("s.arc.Forbid(%d) error(%v)", a.Aid, err)
  59. return
  60. }
  61. for name, attrs := range forbidAttrs {
  62. for bit, attr := range attrs {
  63. if change = forbid.SetAttr(name, attr, bit); change {
  64. conts = append(conts, fmt.Sprintf(template, archive.ForbidBitDesc(name, bit), yesOrNo[^attr&1], yesOrNo[attr]))
  65. log.Info("aid(%d) update archive forbid name(%s) bit(%d) bitdesc(%s) attrs(%d)", a.Aid, name, bit, archive.ForbidBitDesc(name, bit), attr)
  66. }
  67. }
  68. }
  69. forbid.Convert()
  70. if _, err = s.arc.TxUpForbid(tx, forbid); err != nil {
  71. log.Error("s.arc.TxUpForbid(%+v) error(%v)", forbid, err)
  72. }
  73. return
  74. }
  75. //txAddFirstPass 添加稿件第一次过审的记录
  76. func (s *Service) txAddFirstPass(c context.Context, tx *sql.Tx, aid int64, state int8) (firstPass bool, err error) {
  77. if !archive.NormalState(state) || s.hadPassed(c, aid) {
  78. return
  79. }
  80. if err = s.arc.AddFirstPass(tx, aid); err != nil {
  81. log.Error("txUpArcState s.arc.AddFirstPass error(%v) aid(%d)", err, aid)
  82. return
  83. }
  84. firstPass = true
  85. return
  86. }
  87. //txUpArcState 更新稿件属性时,联动添加第一次过审记录
  88. func (s *Service) txUpArcState(c context.Context, tx *sql.Tx, aid int64, state int8) (firstPass bool, err error) {
  89. if _, err = s.arc.TxUpArcState(tx, aid, state); err != nil {
  90. log.Error("txUpArcState s.arc.TxUpArcState error(%v) aid(%d) state(%d)", err, aid, state)
  91. return
  92. }
  93. if firstPass, err = s.txAddFirstPass(c, tx, aid, state); err != nil {
  94. log.Error("txUpArcState s.txAddFirstPass error(%v) aid(%d) state(%d)", err, aid, state)
  95. return
  96. }
  97. return
  98. }
  99. // txUpArcMainState update archive states
  100. func (s *Service) txUpArcMainState(c context.Context, tx *sql.Tx, aid, forward int64, typeID, access int16, state, round int8, reason string) (racs int16, err error) {
  101. log.Info("aid(%d) get archive state(%d)", aid, state)
  102. if ok := s.isAccess(c, aid); ok && access == archive.AccessDefault {
  103. access = archive.AccessMember
  104. }
  105. if _, err = s.txUpArcState(c, tx, aid, state); err != nil {
  106. log.Error("txUpArcMainState s.txUpArcState error(%v) aid(%d) state(%d)", err, aid, state)
  107. return
  108. }
  109. log.Info("aid(%d) update archive state(%d)", aid, state)
  110. if _, err = s.arc.TxUpArcAccess(tx, aid, access); err != nil {
  111. log.Error("s.arc.TxUpArcAccess(%d,%d) error(%v)", aid, access, err)
  112. return
  113. }
  114. racs = access
  115. log.Info("aid(%d) update archive access(%d)", aid, access)
  116. if typeID != 0 {
  117. if _, err = s.arc.TxUpArcTypeID(tx, aid, typeID); err != nil {
  118. log.Error("s.arc.TxUpArcTypeID(%d,%d) error(%v)", aid, typeID, err)
  119. return
  120. }
  121. log.Info("aid(%d) update archive type_id(%d)", aid, typeID)
  122. }
  123. if _, err = s.arc.TxUpArcRound(tx, aid, round); err != nil {
  124. log.Error("s.arc.TxUpArcRound(%d,%d) error(%v)", aid, round, err)
  125. return
  126. }
  127. log.Info("aid(%d) update archive round(%d)", aid, round)
  128. if _, err = s.arc.TxUpArcReason(tx, aid, forward, reason); err != nil {
  129. log.Error("s.arc.TxUpArcReason(%d,%d,%s) error(%v)", aid, forward, reason, err)
  130. return
  131. }
  132. log.Info("aid(%d) update archive reason(%s) forward_id(%d)", aid, reason, forward)
  133. return
  134. }
  135. // txUpArcDelay update archive delay
  136. func (s *Service) txUpArcDelay(c context.Context, tx *sql.Tx, aid, mid int64, state int8, isDelay bool, dTime xtime.Time) (rs int8, cont string, err error) {
  137. rs = state
  138. var delay *archive.Delay
  139. if delay, _ = s.arc.Delay(c, aid, archive.DelayTypeForUser); delay == nil && dTime <= 0 {
  140. return
  141. }
  142. if !isDelay || archive.NotAllowDelay(state) {
  143. if _, err = s.arc.TxDelDelay(tx, aid, archive.DelayTypeForUser); err != nil {
  144. log.Error("s.arc.TxDelDelay(%d) error(%v)", aid, err)
  145. return
  146. }
  147. cont = archive.Operformat(archive.OperTypeDelay, dTime.Time().Format("2006-01-02 15:04:05"), "无", archive.OperStyleOne)
  148. log.Info("aid(%d) err delay time delete archive_delay", aid)
  149. return
  150. }
  151. if dTime <= 0 && delay != nil {
  152. dTime = delay.DTime
  153. }
  154. if _, err = s.arc.TxUpDelay(tx, mid, aid, state, archive.DelayTypeForUser, dTime); err != nil {
  155. log.Error("s.arc.TxUpDelay(%d,%d,%d,%d,%d) error(%v)", aid, mid, state, archive.DelayTypeForUser, dTime, err)
  156. return
  157. }
  158. if archive.NormalState(state) {
  159. rs = archive.StateForbidUserDelay
  160. }
  161. if delay != nil && dTime != delay.DTime {
  162. cont = archive.Operformat(archive.OperTypeDelay, delay.DTime.Time().Format("2006-01-02 15:04:05"), dTime.Time().Format("2006-01-02 15:04:05"), archive.OperStyleOne)
  163. } else if delay == nil {
  164. cont = archive.Operformat(archive.OperTypeDelay, "无", dTime.Time().Format("2006-01-02 15:04:05"), archive.OperStyleOne)
  165. }
  166. log.Info("aid(%d) second_round submit update archive_delay mid(%d) state(%d) type(%d) dtime(%v)", aid, mid, state, archive.DelayTypeForUser, dTime)
  167. return
  168. }
  169. // TxUpArchiveAttr update archive attr by aid.
  170. func (s *Service) TxUpArchiveAttr(c context.Context, tx *sql.Tx, a *archive.Archive, aid, uid int64, attrs map[uint]int32, forbidAttrs map[string]map[uint]int32, redirectURL string) (conts []string, err error) {
  171. var cont []string
  172. log.Info("aid(%d) begin tran change attribute", aid)
  173. if cont, err = s.txUpArcAttrs(tx, a, attrs, redirectURL); err != nil {
  174. log.Error("s.txUpArcAttrs(%d) error(%v)", aid, err)
  175. return
  176. }
  177. conts = append(conts, cont...)
  178. if _, err = s.txUpArcForbidAttrs(c, tx, a, forbidAttrs); err != nil {
  179. log.Error("s.txUpArcForbidAttrs(%d) error(%v)", aid, err)
  180. return
  181. }
  182. log.Info("aid(%d) end tran change attribute", aid)
  183. return
  184. }
  185. // txUpVideoStatus update video status by vid and cid.
  186. func (s *Service) txUpVideoStatus(tx *sql.Tx, vid, cid int64, status int16) (err error) {
  187. //update archive_video
  188. if _, err = s.arc.TxUpVideoStatus(tx, vid, status); err != nil {
  189. log.Error("s.arc.TxUpVideoStatus vid(%d) status(%d) error(%v)", vid, status, err)
  190. return
  191. }
  192. //update video
  193. if _, err = s.arc.TxUpStatus(tx, cid, status); err != nil {
  194. log.Error("s.arc.TxUpStatus cid(%d) status(%d) error(%v)", cid, status, err)
  195. return
  196. }
  197. //update archive_video_relation to 0
  198. if _, err = s.arc.TxUpRelationState(tx, vid, archive.VideoStatusOpen); err != nil {
  199. log.Error("s.arc.TxUpRelationState cid(%d) status(%d) error(%v)", cid, archive.VideoStatusOpen, err)
  200. return
  201. }
  202. log.Info("vid(%d) cid(%d) update video status(%d)", vid, cid, status)
  203. return
  204. }
  205. // txAddVideo insert video get vid.
  206. func (s *Service) txAddVideo(tx *sql.Tx, v *archive.Video) (vid int64, err error) {
  207. if vid, err = s.arc.TxAddVideo(tx, v); err != nil {
  208. log.Error("s.arc.TxAddVideo video(%+v) error(%v)", v, err)
  209. return
  210. }
  211. v.ID = vid
  212. if _, err = s.arc.TxAddRelation(tx, v); err != nil {
  213. log.Error("s.arc.TxAddRelation video(%+v) error(%v)", v, err)
  214. return
  215. }
  216. log.Info("aid(%d) update video vid(%d) cid(%d) index(%d) title(%s) desc(%s) filename(%s) srctype(%s)", v.Aid, vid, v.Cid, v.Index, v.Title, v.Desc, v.Filename, v.SrcType)
  217. return
  218. }
  219. // txUpVideo update video title and desc by vid.
  220. func (s *Service) txUpVideo(tx *sql.Tx, vid int64, title, desc string) (err error) {
  221. if _, err = s.arc.TxUpVideo(tx, vid, title, desc); err != nil {
  222. log.Error("s.arc.TxUpVideo vid(%d) title(%s) desc(%s) error(%v)", vid, title, desc, err)
  223. return
  224. }
  225. if _, err = s.arc.TxUpRelation(tx, vid, title, desc); err != nil {
  226. log.Error("s.arc.TxUpRelation vid(%d) title(%s) desc(%s) error(%v)", vid, title, desc, err)
  227. return
  228. }
  229. log.Info("vid(%d) update video title(%s) desc(%s)", vid, title, desc)
  230. return
  231. }
  232. // txUpVideoLink update video weblink by vid and cid.
  233. func (s *Service) txUpVideoLink(tx *sql.Tx, vid, cid int64, webLink string) (err error) {
  234. if _, err = s.arc.TxUpVideoLink(tx, vid, webLink); err != nil {
  235. log.Error("s.arc.TxUpVideoLink(%d,%s)", vid, webLink)
  236. return
  237. }
  238. if _, err = s.arc.TxUpWebLink(tx, cid, webLink); err != nil {
  239. log.Error("s.arc.TxUpWebLink(%d,%s)", cid, webLink)
  240. return
  241. }
  242. log.Info("vid(%d) cid(%d) update webLink(%s)", vid, cid, webLink)
  243. return
  244. }
  245. // txDelVideo delete video by vid.
  246. func (s *Service) txDelVideo(tx *sql.Tx, vp *archive.VideoParam) (err error) {
  247. if _, err = s.arc.TxUpVideoStatus(tx, vp.ID, archive.VideoStatusDelete); err != nil {
  248. log.Error("s.arc.TxUpVideoStatus(%d,%d)", vp.ID, archive.VideoStatusDelete)
  249. return
  250. }
  251. if _, err = s.arc.TxUpRelationState(tx, vp.ID, archive.VideoStatusDelete); err != nil {
  252. log.Error("s.arc.TxUpRelationState(%d,%d)", vp.ID, archive.VideoStatusDelete)
  253. return
  254. }
  255. log.Info("del video cid(%d) vid(%d)", vp.Cid, vp.ID)
  256. return
  257. }
  258. // txUpVideoIndex update video index by vid.
  259. func (s *Service) txUpVideoIndex(tx *sql.Tx, vid int64, index int) (err error) {
  260. if _, err = s.arc.TxUpVideoIndex(tx, vid, index); err != nil {
  261. log.Error("s.arc.TxUpVideoIndex(%d,%d)", vid, index)
  262. return
  263. }
  264. if _, err = s.arc.TxUpRelationOrder(tx, vid, index); err != nil {
  265. log.Error("s.arc.TxUpRelationOrder(%d,%d)", vid, index)
  266. return
  267. }
  268. log.Info("vid(%d) update index(%d)", vid, index)
  269. return
  270. }
  271. func (s *Service) txUpVideoAttr(c context.Context, tx *sql.Tx, vid, cid int64, attrs map[uint]int32) (conts []string, attribute int32, err error) {
  272. var v *archive.Video
  273. if v, err = s.arc.NewVideoByID(c, vid); err != nil || v == nil {
  274. log.Error("s.arc.VideoByID(%d) error(%v)", vid, err)
  275. return
  276. }
  277. const template = "[%s]从[%s]设为[%s]"
  278. var yesOrNo = map[int32]string{archive.AttrYes: "是", archive.AttrNo: "否"}
  279. for bit, attr := range attrs {
  280. var rows int64
  281. if rows, err = s.arc.TxUpVideoAttr(tx, vid, bit, attr); err != nil {
  282. log.Error("s.arc.TxUpVideoAttr id(%d) bit(%d) attr(%d) error(%v)", vid, bit, attr, err)
  283. return
  284. }
  285. if _, err = s.arc.TxUpAttr(tx, cid, bit, attr); err != nil {
  286. log.Error("s.arc.TxUpAttr cid(%d) bit(%d) attr(%d) error(%v)", cid, bit, attr, err)
  287. return
  288. }
  289. v.AttrSet(attr, bit)
  290. if rows <= 0 {
  291. continue
  292. }
  293. conts = append(conts, fmt.Sprintf(template, archive.BitDesc(bit), yesOrNo[^attr&1], yesOrNo[attr]))
  294. log.Info("vid(%d) update video bit(%d) bitdesc(%s) attrs(%d)", vid, bit, archive.BitDesc(bit), attr)
  295. }
  296. attribute = v.Attribute
  297. return
  298. }
  299. // txUpVideoAudit update video audit by videoParam.
  300. func (s *Service) txUpVideoAudit(tx *sql.Tx, vp *archive.VideoParam) (err error) {
  301. if err = s.txUpVideoStatus(tx, vp.ID, vp.Cid, vp.Status); err != nil {
  302. log.Error("s.arc.TxUpVideoStatus id(%d) cid(%d) status(%d) error(%v)", vp.ID, vp.Cid, vp.Status, err)
  303. return
  304. }
  305. log.Info("aid(%d) vid(%d) update video status(%d)", vp.Aid, vp.ID, vp.Status)
  306. if _, err = s.arc.TxAddAudit(tx, vp.Aid, vp.ID, vp.TagID, vp.Oname, vp.Note, vp.Reason); err != nil {
  307. log.Error("s.arc.TxAddAudit(%d,%d,%d,%s,%s,%s)", vp.Aid, vp.ID, vp.TagID, vp.Oname, vp.Note, vp.Reason)
  308. return
  309. }
  310. log.Info("aid(%d) vid(%d) audit log tag(%d) oname(%s) note(%s) reason(%s)", vp.Aid, vp.ID, vp.TagID, vp.Oname, vp.Note, vp.Reason)
  311. return
  312. }