comment.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/log"
  6. "sync"
  7. "sync/atomic"
  8. )
  9. const (
  10. _stateActive = 0
  11. _defaultType = 23
  12. )
  13. //CommentReg 评论开关
  14. func (s *Service) CommentReg(c context.Context, oid int64, state int16) (err error) {
  15. req := map[string]interface{}{
  16. "oid": oid,
  17. "type": _defaultType,
  18. "adid": 0,
  19. "mid": 0,
  20. "state": state,
  21. }
  22. err = s.dao.ReplyReg(c, req)
  23. return
  24. }
  25. // AutoRegAll 批量注册评论
  26. func (s *Service) AutoRegAll(c context.Context) {
  27. var (
  28. IDs []int64
  29. e int
  30. err error
  31. lastID int64
  32. num int64
  33. failNum int64
  34. )
  35. for {
  36. IDs, lastID, _ = s.dao.GetVideoByLastID(c, lastID)
  37. if len(IDs) == 0 {
  38. break
  39. }
  40. sum := len(IDs)
  41. partNum := 200
  42. rnum := int(sum / partNum)
  43. log.Info("rountine start num[%d]", rnum)
  44. var wg sync.WaitGroup
  45. for i := 0; i < rnum+1; i++ {
  46. wg.Add(1)
  47. if e = (i + 1) * partNum; e > sum {
  48. e = sum
  49. }
  50. r := IDs[i*partNum : e]
  51. go func(ids []int64, k int) {
  52. defer wg.Done()
  53. for _, svid := range ids {
  54. err = s.CommentReg(c, svid, _stateActive)
  55. if err != nil {
  56. atomic.AddInt64(&failNum, 1)
  57. fmt.Printf("Comment active fail [%v]\n", err)
  58. log.Errorv(c, log.KV("log", fmt.Sprintf("Comment active fail [%v]", err)))
  59. } else {
  60. atomic.AddInt64(&num, 1)
  61. fmt.Printf("Comment active success oid:[%d]\n", svid)
  62. log.Infov(c, log.KV("log", fmt.Sprintf("Comment active success oid:[%d]", svid)))
  63. }
  64. }
  65. }(r, i)
  66. }
  67. wg.Wait()
  68. }
  69. log.Info("comment reg complete succ[%d] fail[%d]", num, failNum)
  70. fmt.Printf("comment reg complete succ[%d] fail[%d]", num, failNum)
  71. }