review.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/videoup-task/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. // ErrTaskMISS .
  13. var ErrTaskMISS = fmt.Errorf("任务缓存查找失败")
  14. // ReviewForm 复审表单
  15. func (s *Service) ReviewForm(c context.Context, tid int64) (form *model.SubmitForm, err error) {
  16. return s.dao.ReviewForm(c, tid)
  17. }
  18. // CheckReview 检查任务复审
  19. func (s *Service) CheckReview(c context.Context, form *model.SubmitForm) (isReview bool, err error) {
  20. var (
  21. v *model.Video
  22. attr int32
  23. tx *sql.Tx
  24. rows int64
  25. tp *model.TaskPriority
  26. )
  27. attr, err = s.dao.VideoAttribute(c, form.CID)
  28. if err != nil {
  29. log.Error("CheckReview VideoAttribute(aid%d,cid%d) miss(%v)", form.AID, form.CID, err)
  30. return false, ErrTaskMISS
  31. }
  32. v = &model.Video{Attribute: attr}
  33. tp, err = s.getReviewParams(c, form)
  34. if err != nil || tp == nil {
  35. log.Info("CheckReview(%d) 不需要复审(%+v)", form.TaskID, tp)
  36. return false, err
  37. }
  38. s.SyncRC(c)
  39. ck := s.reviewCache.Check(c, tp, form.UID)
  40. if !ck {
  41. log.Info("CheckReview(%d) 不需要复审(%+v)", form.TaskID, tp)
  42. return false, nil
  43. }
  44. if _, err = s.dao.InReviewForm(c, form, form.UID, form.Uname); err != nil {
  45. return false, err
  46. }
  47. if tx, err = s.dao.BeginTran(c); err != nil {
  48. return false, err
  49. }
  50. if rows, err = s.dao.TxUpTaskByID(tx, form.TaskID, map[string]interface{}{"state": model.TypeReview}); err != nil {
  51. tx.Rollback()
  52. return false, err
  53. }
  54. if rows > 0 {
  55. if _, err = s.dao.TxAddTaskHis(tx, 0, model.ActionSubmit, form.TaskID, form.CID, form.UID, 0, form.Status, "TaskReview"); err != nil {
  56. tx.Rollback()
  57. return false, err
  58. }
  59. }
  60. if err = tx.Commit(); err != nil {
  61. log.Error("tx.Commit error(%v)", err)
  62. return false, err
  63. }
  64. // log
  65. attrs := map[uint]int32{
  66. model.AttrBitNoRank: form.Norank,
  67. model.AttrBitNoDynamic: form.Noindex,
  68. model.AttrBitNoRecommend: form.NoRecommend,
  69. model.AttrBitNoSearch: form.Nosearch,
  70. model.AttrBitOverseaLock: form.OverseaBlock,
  71. model.AttrBitPushBlog: form.PushBlog,
  72. }
  73. var conts []string
  74. const template = "[%s]从[%s]设为[%s]"
  75. var yesOrNo = map[int32]string{model.AttrYes: "是", model.AttrNo: "否"}
  76. for bit, attr := range attrs {
  77. v.AttrSet(attr, bit)
  78. if attr == 1 {
  79. conts = append(conts, fmt.Sprintf(template, model.BitDesc(bit), yesOrNo[^attr&1], yesOrNo[attr]))
  80. log.Info("vid(%d) update video bit(%d) bitdesc(%s) attrs(%d)", form.ID, bit, model.BitDesc(bit), attr)
  81. }
  82. }
  83. vp := &model.VideoParam{
  84. ID: form.ID,
  85. Aid: form.AID,
  86. Mid: form.MID,
  87. RegionID: tp.TypeID,
  88. Status: form.Status,
  89. Cid: form.CID,
  90. Title: form.Eptitle,
  91. Desc: form.Description,
  92. UID: form.UID,
  93. TaskID: form.TaskID,
  94. Oname: form.Uname,
  95. TagID: form.TID,
  96. Reason: form.Reason,
  97. ReasonID: form.ReasonID,
  98. Note: form.Note,
  99. Attribute: v.Attribute,
  100. }
  101. oper := &model.VideoOper{Aid: form.AID, UID: form.UID, Vid: form.ID, Attribute: vp.Attribute, Status: form.Status, Remark: form.Note}
  102. operConts := append([]string{fmt.Sprintf("初审提交")}, conts...)
  103. operConts = append(operConts, s.diffVideoOper(vp)...)
  104. oper.Content = strings.Join(operConts, ",")
  105. s.addVideoOper(c, oper)
  106. s.sendVideoLog(c, vp, oper.Content)
  107. return true, nil
  108. }
  109. // ListReviewConfs 配置列表
  110. func (s *Service) ListReviewConfs(c context.Context, unames, bt, et, sort string, pn, ps int64) (rcs []*model.ReviewConf, count int64, err error) {
  111. var uids []int64
  112. if len(unames) > 0 {
  113. res, _ := s.dao.Uids(c, strings.Split(unames, ","))
  114. for _, uid := range res {
  115. uids = append(uids, uid)
  116. }
  117. }
  118. rcs, count, err = s.dao.ListConfs(c, uids, bt, et, sort, pn, ps)
  119. for _, v := range rcs {
  120. if v.Bt.TimeValue().IsZero() {
  121. v.Bt = ""
  122. }
  123. if v.Et.TimeValue().IsZero() {
  124. v.Et = ""
  125. }
  126. if len(v.Uids) > 0 {
  127. if unames, _ := s.dao.Unames(c, v.Uids); len(unames) > 0 {
  128. for _, uname := range unames {
  129. v.Unames = append(v.Unames, uname)
  130. }
  131. }
  132. }
  133. }
  134. return
  135. }
  136. // AddReviewConf 添加配置
  137. func (s *Service) AddReviewConf(c context.Context, rc *model.ReviewConf) (err error) {
  138. if len(rc.Types) > 0 {
  139. stypes, _ := xstr.SplitInts(s.tarnsType(c, xstr.JoinInts(rc.Types)))
  140. rc.Types = stypes
  141. }
  142. if _, err = s.dao.InReviewConf(c, rc); err != nil {
  143. log.Error("s.AddReviewConf(%+v) error(%v)", rc, err)
  144. return err
  145. }
  146. s.SyncRC(c)
  147. return
  148. }
  149. // EditReviewConf 修改配置
  150. func (s *Service) EditReviewConf(c context.Context, rc *model.ReviewConf) (err error) {
  151. if len(rc.Types) > 0 {
  152. stypes, _ := xstr.SplitInts(s.tarnsType(c, xstr.JoinInts(rc.Types)))
  153. rc.Types = stypes
  154. }
  155. if _, err = s.dao.UpReviewConf(c, rc); err != nil {
  156. log.Error("s.EditReviewConf(%+v) error(%v)", rc, err)
  157. return err
  158. }
  159. s.SyncRC(c)
  160. return
  161. }
  162. // DelReviewConf 删除配置
  163. func (s *Service) DelReviewConf(c context.Context, id int) (err error) {
  164. if _, err = s.dao.DelReviewConf(c, id); err != nil {
  165. log.Error("s.DelReviewConf(%d) error(%v)", id, err)
  166. return err
  167. }
  168. s.SyncRC(c)
  169. return
  170. }
  171. // SyncRC sync from db
  172. func (s *Service) SyncRC(c context.Context) {
  173. rcs, err := s.dao.ReviewConfs(context.TODO())
  174. if err != nil {
  175. log.Error("loadRC error(%v)", err)
  176. return
  177. }
  178. if len(rcs) > 0 {
  179. s.reviewCache.Mux.Lock()
  180. defer s.reviewCache.Mux.Unlock()
  181. s.reviewCache.MRC = make(map[int64]*model.ReviewConf)
  182. for _, item := range rcs {
  183. s.reviewCache.MRC[item.ID] = item
  184. }
  185. }
  186. }
  187. func (s *Service) loadRC() {
  188. s.SyncRC(context.TODO())
  189. }
  190. func (s *Service) loadRCproc() {
  191. for {
  192. time.Sleep(3 * time.Minute)
  193. s.SyncRC(context.TODO())
  194. }
  195. }
  196. func (s *Service) getReviewParams(c context.Context, form *model.SubmitForm) (tp *model.TaskPriority, err error) {
  197. t, err := s.dao.TaskByID(c, form.TaskID)
  198. if err != nil || t == nil {
  199. return nil, ErrTaskMISS
  200. }
  201. if t.State != model.TypeDispatched {
  202. log.Info("CheckReview(%d) 不需要复审 state(%d)", form.TaskID, t.State)
  203. return nil, nil
  204. }
  205. mp, err := s.dao.GetWeightRedis(c, []int64{form.TaskID})
  206. if err != nil || len(mp) == 0 {
  207. if mp, err = s.dao.GetWeightDB(c, []int64{form.TaskID}); err != nil || len(mp) == 0 {
  208. log.Error("GetWeightDB(%d) miss", form.TaskID)
  209. return nil, ErrTaskMISS
  210. }
  211. }
  212. if _, ok := mp[form.TaskID]; !ok {
  213. log.Error("mp(%d) miss", form.TaskID)
  214. return nil, ErrTaskMISS
  215. }
  216. tp = mp[form.TaskID]
  217. // 补充复审判断的参数
  218. if tp.TypeID == 0 {
  219. s.setReviewParams(c, form.MID, form.AID, tp)
  220. }
  221. return
  222. }
  223. func (s *Service) setReviewParams(c context.Context, mid, aid int64, tp *model.TaskPriority) {
  224. typeid, upfrom, err := s.dao.ArchiveParam(c, aid)
  225. if err == nil {
  226. tp.TypeID = typeid
  227. tp.UpFrom = upfrom
  228. }
  229. tp.UpGroups = s.getSpecial(mid)
  230. }