full_compare.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package service
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "strconv"
  6. "strings"
  7. "go-common/app/job/main/passport-user-compare/model"
  8. "go-common/library/log"
  9. )
  10. // full compare data and fix data
  11. func (s *Service) fullCompareAndFix() {
  12. go s.compareAndFixedUserBase(context.Background())
  13. go s.compareAndFixedSafeQuestion(context.Background())
  14. go s.compareAndFixedSns(context.Background())
  15. }
  16. // full div compare and fixed use base info .
  17. func (s *Service) compareAndFixedUserBase(c context.Context) {
  18. diff := s.c.FullTask.AccountEnd / fullDivSegment
  19. var i int64
  20. for i = 0; i < fullDivSegment; i++ {
  21. go s.compareAndFixedUserBaseDiv(context.Background(), i, diff*i, diff*(i+1))
  22. }
  23. }
  24. // full div compare safe question .
  25. func (s *Service) compareAndFixedSafeQuestion(c context.Context) {
  26. for i := 0; i < 30; i++ {
  27. go s.compareAndFixedSafeQuestionDiv(context.Background(), i)
  28. }
  29. }
  30. // full div compare and fixed sns.
  31. func (s *Service) compareAndFixedSns(c context.Context) {
  32. diff := s.c.FullTask.AccountSnsEnd / fullDivSegment
  33. var i int64
  34. for i = 0; i < fullDivSegment; i++ {
  35. go s.compareAndFixSnsDiv(context.Background(), i, diff*i, diff*(i+1))
  36. }
  37. }
  38. func (s *Service) compareAndFixedUserBaseDiv(c context.Context, index, start, end int64) {
  39. var (
  40. originAccounts []*model.OriginAccount
  41. err error
  42. )
  43. for {
  44. log.Info("start full compare basic_account,index %d,start is %d ,end is %d ,step is %d ", index, start, end, s.c.FullTask.Step)
  45. if originAccounts, err = s.d.BatchQueryAccount(c, start, s.c.FullTask.Step); err != nil {
  46. log.Error("query batch account error,error is (%+v)", err)
  47. continue
  48. }
  49. for _, originAccount := range originAccounts {
  50. mid := originAccount.Mid
  51. // 密码、状态对比
  52. var userBase *model.UserBase
  53. if userBase, err = s.d.QueryUserBase(c, mid); err != nil {
  54. log.Error("full query user base error,mid is %d,err is(+v)", mid, err)
  55. continue
  56. }
  57. if userBase == nil {
  58. log.Info("full userBase not exist,mid is %d", mid)
  59. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserBase, Action: insertAction}
  60. s.fullFixChan <- errorFix
  61. s.mu.Lock()
  62. accountStat["notExistUserBase"] = accountStat["notExistUserBase"] + 1
  63. s.mu.Unlock()
  64. continue
  65. }
  66. if originAccount.Pwd != hex.EncodeToString(userBase.Pwd) || originAccount.Salt != userBase.Salt {
  67. log.Info("full pwd compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccount.Pwd, hex.EncodeToString(userBase.Pwd))
  68. errorFix := &model.ErrorFix{Mid: mid, ErrorType: pwdErrorType, Action: updateAction}
  69. s.fullFixChan <- errorFix
  70. s.mu.Lock()
  71. accountStat["pwd"] = accountStat["pwd"] + 1
  72. s.mu.Unlock()
  73. }
  74. if originAccount.Isleak != userBase.Status {
  75. log.Info("full status compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccount.Isleak, userBase.Status)
  76. errorFix := &model.ErrorFix{Mid: mid, ErrorType: statusErrorType, Action: updateAction}
  77. s.fullFixChan <- errorFix
  78. s.mu.Lock()
  79. accountStat["status"] = accountStat["status"] + 1
  80. s.mu.Unlock()
  81. }
  82. // 对比手机号
  83. var userTel *model.UserTel
  84. if userTel, err = s.d.QueryUserTel(c, mid); err != nil {
  85. log.Error("full query user tel error,mid is %d,err is(+v)", mid, err)
  86. continue
  87. }
  88. originTel := originAccount.Tel
  89. if originTel != "" && userTel == nil {
  90. log.Info("dynamic tel not exist,mid is %d", mid)
  91. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserTel, Action: insertAction}
  92. s.fullFixChan <- errorFix
  93. s.mu.Lock()
  94. accountStat["notExistUserTel"] = accountStat["notExistUserTel"] + 1
  95. s.mu.Unlock()
  96. continue
  97. }
  98. if userTel != nil {
  99. var tel string
  100. if tel, err = s.doDecrypt(userTel.Tel); err != nil {
  101. log.Error("full doDecrypt tel error,mid is %d,tel is (%+v), err is(+v)", mid, userTel.Tel, err)
  102. continue
  103. }
  104. ot := strings.Trim(strings.ToLower(originTel), "")
  105. if ot != tel {
  106. log.Info("full tel compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, ot, tel)
  107. errorFix := &model.ErrorFix{Mid: mid, ErrorType: telErrorType, Action: updateAction}
  108. s.fullFixChan <- errorFix
  109. s.mu.Lock()
  110. dynamicAccountStat["tel"] = dynamicAccountStat["tel"] + 1
  111. s.mu.Unlock()
  112. }
  113. }
  114. // 对比邮箱
  115. var userEmail *model.UserEmail
  116. if userEmail, err = s.d.QueryUserMail(c, mid); err != nil {
  117. log.Error("full query user mail error,error is (%+v)", err)
  118. continue
  119. }
  120. originMail := originAccount.Email
  121. if originMail != "" && userEmail == nil {
  122. log.Info("full mail not exist,mid is %d", mid)
  123. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserMail, Action: insertAction}
  124. s.fullFixChan <- errorFix
  125. s.mu.Lock()
  126. accountStat["notExistUserMail"] = accountStat["notExistUserMail"] + 1
  127. s.mu.Unlock()
  128. continue
  129. }
  130. if userEmail != nil {
  131. var mail string
  132. if mail, err = s.doDecrypt(userEmail.Email); err != nil {
  133. log.Error("full doDecrypt email error,mid is %d,email is (%+v), err is(+v)", mid, userEmail.Email, err)
  134. continue
  135. }
  136. om := strings.Trim(strings.ToLower(originMail), "")
  137. if om != mail {
  138. log.Info("full mail compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originMail, mail)
  139. errorFix := &model.ErrorFix{Mid: mid, ErrorType: mailErrorType, Action: updateAction}
  140. s.fullFixChan <- errorFix
  141. s.mu.Lock()
  142. accountStat["mail"] = accountStat["mail"] + 1
  143. s.mu.Unlock()
  144. }
  145. }
  146. }
  147. if start > end || len(originAccounts) == 0 {
  148. break
  149. }
  150. start = originAccounts[len(originAccounts)-1].Mid
  151. }
  152. log.Info("End full compare basic_account,index %d ", index)
  153. s.mu.Lock()
  154. accountStat["user_base_index"] = index
  155. s.mu.Unlock()
  156. if err = s.d.SendWechat(accountStat); err != nil {
  157. log.Error("s.d.SendWechat account stat,error is (%+v)", err)
  158. return
  159. }
  160. }
  161. func (s *Service) compareAndFixedSafeQuestionDiv(c context.Context, tableIndex int) {
  162. var (
  163. start int64
  164. err error
  165. )
  166. for {
  167. log.Info("start full compare aso_account_info,table is %d,start is %d,step is %d", tableIndex, start, s.c.FullTask.Step)
  168. var originAccountInfos []*model.OriginAccountInfo
  169. if originAccountInfos, err = s.d.BatchQueryAccountInfo(c, start, s.c.FullTask.Step, tableIndex); err != nil {
  170. log.Error("full query batch account info error,error is (%+v)", err)
  171. continue
  172. }
  173. for _, originAccountInfo := range originAccountInfos {
  174. mid := originAccountInfo.Mid
  175. if len(originAccountInfo.SafeAnswer) != 0 {
  176. var userSafeQuestion *model.UserSafeQuestion
  177. if userSafeQuestion, err = s.d.QueryUserSafeQuestion(c, mid); err != nil {
  178. log.Error("full query user safe question err, mid is %d,err is(+v)", mid, err)
  179. continue
  180. }
  181. if userSafeQuestion == nil {
  182. log.Info("full userSafeQuestion not exist,mid is %d", mid)
  183. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserSafeQuestion, Action: insertAction}
  184. s.fullFixChan <- errorFix
  185. s.mu.Lock()
  186. accountInfoStat["notExistUserSafeQuestion"] = accountInfoStat["notExistUserSafeQuestion"] + 1
  187. s.mu.Unlock()
  188. continue
  189. }
  190. originSafeAnswerBytes := s.doHash(originAccountInfo.SafeAnswer)
  191. if hex.EncodeToString(originSafeAnswerBytes) != hex.EncodeToString(userSafeQuestion.SafeAnswer) {
  192. log.Info("full safe question compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccountInfo.SafeAnswer, hex.EncodeToString(userSafeQuestion.SafeAnswer))
  193. errorFix := &model.ErrorFix{Mid: mid, ErrorType: safeErrorType, Action: updateAction}
  194. s.fullFixChan <- errorFix
  195. s.mu.Lock()
  196. accountInfoStat["safe"] = accountInfoStat["safe"] + 1
  197. s.mu.Unlock()
  198. }
  199. }
  200. }
  201. if start > s.c.FullTask.AccountInfoEnd || len(originAccountInfos) == 0 {
  202. break
  203. }
  204. start = originAccountInfos[len(originAccountInfos)-1].ID
  205. }
  206. log.Info("end full compare and fix account info table is %d", tableIndex)
  207. s.mu.Lock()
  208. accountInfoStat["user_safe_question_index"] = int64(tableIndex)
  209. s.mu.Unlock()
  210. if err := s.d.SendWechat(accountInfoStat); err != nil {
  211. log.Error("s.d.SendWeChat account info stat ,error is (%+v)", err)
  212. return
  213. }
  214. }
  215. func (s *Service) compareAndFixSnsDiv(c context.Context, index, start, end int64) {
  216. var err error
  217. for {
  218. log.Info("start full compare aso_account_sns,index is %d,start is %d,step is %d", index, start, s.c.FullTask.Step)
  219. var originAccountSnses []*model.OriginAccountSns
  220. if originAccountSnses, err = s.d.BatchQueryAccountSns(c, start, s.c.FullTask.Step); err != nil {
  221. log.Error("full query batch account sns error,error is (%+v)", err)
  222. continue
  223. }
  224. for _, sns := range originAccountSnses {
  225. mid := sns.Mid
  226. if sns.QQOpenid != "" {
  227. var userThirdBind *model.UserThirdBind
  228. if userThirdBind, err = s.d.QueryUserThirdBind(c, mid, platformQQ); err != nil {
  229. log.Error("full query user bind error ,mid is %d, error is %(+v)", mid, err)
  230. continue
  231. }
  232. if userThirdBind == nil {
  233. log.Info("full sns not exist,mid is %d", mid)
  234. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserThirdBind, Action: insertAction}
  235. s.fullFixChan <- errorFix
  236. s.mu.Lock()
  237. accountSnsStat["notExistUserThirdBind"] = accountSnsStat["notExistUserThirdBind"] + 1
  238. s.mu.Unlock()
  239. continue
  240. }
  241. if userThirdBind.PlatForm == 2 && userThirdBind.OpenID != sns.QQOpenid {
  242. log.Info("full sns qq compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, string(sns.QQOpenid), userThirdBind.OpenID)
  243. errorFix := &model.ErrorFix{Mid: mid, ErrorType: qqErrorType, Action: updateAction}
  244. s.fullFixChan <- errorFix
  245. s.mu.Lock()
  246. accountSnsStat["sns"] = accountSnsStat["sns"] + 1
  247. s.mu.Unlock()
  248. }
  249. }
  250. if sns.SinaUID != 0 {
  251. var userThirdBind *model.UserThirdBind
  252. if userThirdBind, err = s.d.QueryUserThirdBind(c, mid, platformSina); err != nil {
  253. log.Error("full query user bind error ,mid is %d, error is %(+v)", mid, err)
  254. continue
  255. }
  256. if userThirdBind == nil {
  257. log.Info("full sns not exist,mid is %d", mid)
  258. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserThirdBind, Action: insertAction}
  259. s.fullFixChan <- errorFix
  260. s.mu.Lock()
  261. accountSnsStat["notExistUserThirdBind"] = accountSnsStat["notExistUserThirdBind"] + 1
  262. s.mu.Unlock()
  263. continue
  264. }
  265. var openID int64
  266. if openID, err = strconv.ParseInt(userThirdBind.OpenID, 10, 64); err != nil {
  267. log.Error("parse error.")
  268. return
  269. }
  270. if userThirdBind.PlatForm == 1 && openID != sns.SinaUID {
  271. log.Info("full sns sina compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, sns.SinaUID, userThirdBind.OpenID)
  272. errorFix := &model.ErrorFix{Mid: mid, ErrorType: sinaErrorType, Action: updateAction}
  273. s.fullFixChan <- errorFix
  274. s.mu.Lock()
  275. accountSnsStat["sns"] = accountSnsStat["sns"] + 1
  276. s.mu.Unlock()
  277. }
  278. }
  279. }
  280. if start > end || len(originAccountSnses) == 0 {
  281. break
  282. }
  283. start = originAccountSnses[len(originAccountSnses)-1].Mid
  284. }
  285. log.Info("end full compare and fix account sns index %d", index)
  286. s.mu.Lock()
  287. accountSnsStat["user_sns_index"] = index
  288. s.mu.Unlock()
  289. if err = s.d.SendWechat(accountSnsStat); err != nil {
  290. log.Error("s.d.SendWeChat sns stat,error is (%+v)", err)
  291. return
  292. }
  293. }