service.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/app/job/main/passport-user-compare/conf"
  7. "go-common/app/job/main/passport-user-compare/dao"
  8. "go-common/app/job/main/passport-user-compare/model"
  9. "go-common/library/log"
  10. "github.com/robfig/cron"
  11. )
  12. const (
  13. publicKeyConst int8 = 0
  14. privateKeyConst int8 = 1
  15. aesKeyConst int8 = 2
  16. md5KeyConst int8 = 3
  17. timeFormat = "2006-01-02 15:04:05"
  18. pwdErrorType = 1
  19. statusErrorType = 2
  20. telErrorType = 3
  21. mailErrorType = 4
  22. safeErrorType = 5
  23. sinaErrorType = 6
  24. qqErrorType = 7
  25. notExistUserBase = 8
  26. notExistUserTel = 9
  27. notExistUserMail = 10
  28. notExistUserSafeQuestion = 11
  29. notExistUserThirdBind = 12
  30. userIDErrorType = 13
  31. notExistUserRegOriginType = 14
  32. userRegOriginErrorType = 15
  33. insertAction = "insert"
  34. updateAction = "update"
  35. fullDivSegment int64 = 10
  36. platformSina int64 = 1
  37. platformQQ int64 = 2
  38. filterStart int64 = 1536572121
  39. filterEnd int64 = 1536616436
  40. mySQLErrCodeDuplicateEntry = 1062
  41. )
  42. var (
  43. privateKey = ""
  44. publicKey = ""
  45. aesKey = ""
  46. md5slat = ""
  47. dynamicAccountStat = make(map[string]int64, 7)
  48. dynamicAccountInfoStat = make(map[string]int64, 2)
  49. dynamicAccountRegStat = make(map[string]int64, 2)
  50. accountStat = make(map[string]int64, 7)
  51. accountInfoStat = make(map[string]int64, 2)
  52. accountSnsStat = make(map[string]int64, 3)
  53. loc = time.Now().Location()
  54. )
  55. // Service service.
  56. type Service struct {
  57. c *conf.Config
  58. d *dao.Dao
  59. cron *cron.Cron
  60. fullFixChan chan *model.ErrorFix
  61. incFixChan chan *model.ErrorFix
  62. mu sync.Mutex
  63. countryMap map[int64]string
  64. dataFixSwitch bool
  65. incrDataFixSwitch bool
  66. }
  67. // New new service
  68. func New(c *conf.Config) (s *Service) {
  69. s = &Service{
  70. c: c,
  71. d: dao.New(c),
  72. cron: cron.New(),
  73. dataFixSwitch: c.DataFixSwitch,
  74. incrDataFixSwitch: c.IncrDataFixSwitch,
  75. fullFixChan: make(chan *model.ErrorFix, 1024),
  76. incFixChan: make(chan *model.ErrorFix, 2048),
  77. }
  78. var err error
  79. s.countryMap, err = s.d.QueryCountryCode(context.Background())
  80. if err != nil || len(s.countryMap) == 0 {
  81. log.Error("fail to get country map")
  82. panic(err)
  83. }
  84. s.initSecret()
  85. if s.c.FullTask.Switch {
  86. log.Info("start full compare and fixed")
  87. go s.fullFixed(s.fullFixChan)
  88. if err := s.cron.AddFunc(s.c.FullTask.CronFullStr, s.fullCompareAndFix); err != nil {
  89. panic(err)
  90. }
  91. s.cron.Start()
  92. }
  93. if s.c.IncTask.Switch {
  94. log.Info("start inc compare and fixed")
  95. go s.incFixed(s.incFixChan)
  96. if err := s.cron.AddFunc(s.c.IncTask.CronIncStr, s.incCompareAndFix); err != nil {
  97. panic(err)
  98. }
  99. s.cron.Start()
  100. }
  101. if s.c.DuplicateTask.Switch {
  102. log.Info("start check duplicate job")
  103. if err := s.cron.AddFunc(s.c.DuplicateTask.DuplicateCron, s.checkEmailDuplicateJob); err != nil {
  104. panic(err)
  105. }
  106. if err := s.cron.AddFunc(s.c.DuplicateTask.DuplicateCron, s.checkTelDuplicateJob); err != nil {
  107. panic(err)
  108. }
  109. s.cron.Start()
  110. }
  111. if s.c.FixEmailVerifiedSwitch {
  112. go s.fixEmailVerified()
  113. }
  114. return s
  115. }
  116. // Ping check server ok.
  117. func (s *Service) Ping(c context.Context) (err error) {
  118. return
  119. }
  120. // Close close service, including databus and outer service.
  121. func (s *Service) Close() (err error) {
  122. if err = s.d.Close(); err != nil {
  123. panic(err)
  124. }
  125. return
  126. }
  127. func (s *Service) initSecret() {
  128. res, err := s.d.LoadSecret(context.Background())
  129. if err != nil {
  130. panic(err)
  131. }
  132. for _, r := range res {
  133. if publicKeyConst == r.KeyType {
  134. publicKey = r.Key
  135. }
  136. if privateKeyConst == r.KeyType {
  137. privateKey = r.Key
  138. }
  139. if aesKeyConst == r.KeyType {
  140. aesKey = r.Key
  141. }
  142. if md5KeyConst == r.KeyType {
  143. md5slat = r.Key
  144. }
  145. }
  146. if len(aesKey) == 0 {
  147. panic("load secret error")
  148. }
  149. }