dynamic_compare.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package service
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "strings"
  6. "time"
  7. "go-common/app/job/main/passport-user-compare/model"
  8. "go-common/library/log"
  9. )
  10. // Dynamic inc fixed data
  11. func (s *Service) incCompareAndFix() {
  12. startTime, err := time.ParseInLocation(timeFormat, s.c.IncTask.StartTime, loc)
  13. if err != nil {
  14. log.Error("failed to parse end time, time.ParseInLocation(%s, %s, %v), error(%v)", timeFormat, s.c.IncTask.StartTime, loc, err)
  15. return
  16. }
  17. log.Info("start inc compare and fixed")
  18. for {
  19. stepDuration := time.Duration(s.c.IncTask.StepDuration)
  20. endTime := startTime.Add(stepDuration)
  21. if time.Now().Before(endTime) {
  22. log.Info("break dynamic ")
  23. break
  24. }
  25. s.compareAndFixedUserBaseInc(context.Background(), startTime, endTime)
  26. s.compareAndFixedSafeQuestionInc(context.Background(), startTime, endTime)
  27. s.compareAndFixedUserRegOriginInc(context.Background(), startTime, endTime)
  28. startTime = endTime
  29. endTime = startTime.Add(stepDuration)
  30. }
  31. if len(dynamicAccountStat) != 0 {
  32. if err = s.d.SendWechat(dynamicAccountStat); err != nil {
  33. log.Error("s.d.SendWeChat account stat,error is (%+v)", err)
  34. return
  35. }
  36. }
  37. if len(dynamicAccountInfoStat) != 0 {
  38. if err := s.d.SendWechat(dynamicAccountInfoStat); err != nil {
  39. log.Error("s.d.SendWeChat account info stat ,error is (%+v)", err)
  40. return
  41. }
  42. }
  43. if len(dynamicAccountRegStat) != 0 {
  44. if err := s.d.SendWechat(dynamicAccountRegStat); err != nil {
  45. log.Error("s.d.SendWeChat account reg stat ,error is (%+v)", err)
  46. return
  47. }
  48. }
  49. log.Info("dynamic compare and fix ,chan size is %d", len(s.incFixChan))
  50. }
  51. func (s *Service) compareAndFixedUserBaseInc(c context.Context, start, end time.Time) {
  52. var (
  53. originAccounts []*model.OriginAccount
  54. origin *model.OriginAccount
  55. err error
  56. )
  57. log.Info("dynamic compare and fix user base,tel,mail ,time start is(%+v), end time is (%+v)", start, end)
  58. if originAccounts, err = s.d.BatchQueryAccountByTime(c, start, end); err != nil {
  59. log.Error("dynamic query batch account inc error,error is (%+v)", err)
  60. return
  61. }
  62. for _, originAccount := range originAccounts {
  63. for {
  64. mid := originAccount.Mid
  65. var userBase *model.UserBase
  66. if userBase, err = s.d.QueryUserBase(c, mid); err != nil {
  67. log.Error("dynamic query user base error,mid is %d,err is(+v)", mid, err)
  68. continue
  69. }
  70. // 对比密码和盐
  71. if userBase == nil {
  72. log.Info("dynamic user base not exist,mid is %d", mid)
  73. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserBase, Action: insertAction}
  74. s.incFixChan <- errorFix
  75. s.mu.Lock()
  76. dynamicAccountStat["notExistUserBase"] = dynamicAccountStat["notExistUserBase"] + 1
  77. s.mu.Unlock()
  78. continue
  79. }
  80. if originAccount.UserID != userBase.UserID {
  81. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  82. continue
  83. }
  84. if origin.UserID != userBase.UserID {
  85. log.Info("dynamic pwd compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccount.Pwd, hex.EncodeToString(userBase.Pwd))
  86. errorFix := &model.ErrorFix{Mid: mid, ErrorType: userIDErrorType, Action: updateAction}
  87. s.incFixChan <- errorFix
  88. s.mu.Lock()
  89. dynamicAccountStat["userid"] = dynamicAccountStat["userid"] + 1
  90. s.mu.Unlock()
  91. }
  92. }
  93. if originAccount.Pwd != hex.EncodeToString(userBase.Pwd) || originAccount.Salt != userBase.Salt {
  94. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  95. continue
  96. }
  97. if origin.Pwd != hex.EncodeToString(userBase.Pwd) || origin.Salt != userBase.Salt {
  98. log.Info("dynamic pwd compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccount.Pwd, hex.EncodeToString(userBase.Pwd))
  99. errorFix := &model.ErrorFix{Mid: mid, ErrorType: pwdErrorType, Action: updateAction}
  100. s.incFixChan <- errorFix
  101. s.mu.Lock()
  102. dynamicAccountStat["pwd"] = dynamicAccountStat["pwd"] + 1
  103. s.mu.Unlock()
  104. }
  105. }
  106. if originAccount.Isleak != userBase.Status {
  107. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  108. continue
  109. }
  110. if origin.Isleak != userBase.Status {
  111. log.Info("dynamic status compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccount.Isleak, userBase.Status)
  112. errorFix := &model.ErrorFix{Mid: mid, ErrorType: statusErrorType, Action: updateAction}
  113. s.incFixChan <- errorFix
  114. s.mu.Lock()
  115. dynamicAccountStat["status"] = dynamicAccountStat["status"] + 1
  116. s.mu.Unlock()
  117. }
  118. }
  119. // 对比手机号
  120. var userTel *model.UserTel
  121. if userTel, err = s.d.QueryUserTel(c, mid); err != nil {
  122. log.Error("dynamic query user tel error,mid is %d,err is(+v)", mid, err)
  123. continue
  124. }
  125. originTel := originAccount.Tel
  126. if originTel != "" && userTel == nil {
  127. log.Info("dynamic tel not exist,mid is %d", mid)
  128. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserTel, Action: insertAction}
  129. s.incFixChan <- errorFix
  130. s.mu.Lock()
  131. dynamicAccountStat["notExistUserTel"] = dynamicAccountStat["notExistUserTel"] + 1
  132. s.mu.Unlock()
  133. continue
  134. }
  135. if userTel != nil {
  136. var tel string
  137. if tel, err = s.doDecrypt(userTel.Tel); err != nil {
  138. log.Error("dynamic doDecrypt tel error,mid is %d,tel is (%+v), err is(+v)", mid, userTel.Tel, err)
  139. continue
  140. }
  141. ot := strings.Trim(strings.ToLower(originTel), "")
  142. if ot != tel {
  143. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  144. continue
  145. }
  146. ot = strings.Trim(strings.ToLower(origin.Tel), "")
  147. if ot != tel {
  148. log.Info("dynamic tel compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, ot, tel)
  149. errorFix := &model.ErrorFix{Mid: mid, ErrorType: telErrorType, Action: updateAction}
  150. s.incFixChan <- errorFix
  151. s.mu.Lock()
  152. dynamicAccountStat["tel"] = dynamicAccountStat["tel"] + 1
  153. s.mu.Unlock()
  154. }
  155. }
  156. }
  157. // 对比邮箱
  158. var userEmail *model.UserEmail
  159. if userEmail, err = s.d.QueryUserMail(c, mid); err != nil {
  160. log.Error("dynamic query user mail error,error is (%+v)", err)
  161. continue
  162. }
  163. originMail := originAccount.Email
  164. if originMail != "" && userEmail == nil {
  165. log.Info("dynamic mail not exist,mid is %d", mid)
  166. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserMail, Action: insertAction}
  167. s.incFixChan <- errorFix
  168. s.mu.Lock()
  169. dynamicAccountStat["notExistUserMail"] = dynamicAccountStat["notExistUserMail"] + 1
  170. s.mu.Unlock()
  171. continue
  172. }
  173. if userEmail != nil {
  174. var mail string
  175. if mail, err = s.doDecrypt(userEmail.Email); err != nil {
  176. log.Error("dynamic doDecrypt email error,mid is %d,email is (%+v), err is(+v)", mid, userEmail.Email, err)
  177. continue
  178. }
  179. om := strings.Trim(strings.ToLower(originMail), "")
  180. if om != mail {
  181. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  182. continue
  183. }
  184. om = strings.Trim(strings.ToLower(origin.Email), "")
  185. if om != mail {
  186. log.Info("dynamic mail compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, om, mail)
  187. errorFix := &model.ErrorFix{Mid: mid, ErrorType: mailErrorType, Action: updateAction}
  188. s.incFixChan <- errorFix
  189. s.mu.Lock()
  190. dynamicAccountStat["mail"] = dynamicAccountStat["mail"] + 1
  191. s.mu.Unlock()
  192. }
  193. }
  194. }
  195. break
  196. }
  197. }
  198. s.mu.Lock()
  199. dynamicAccountStat["dynamicUserBase"] = 0
  200. s.mu.Unlock()
  201. }
  202. func (s *Service) compareAndFixedSafeQuestionInc(c context.Context, start, end time.Time) {
  203. for i := 0; i < 30; i++ {
  204. var (
  205. err error
  206. originAccountInfos []*model.OriginAccountInfo
  207. originInfo *model.OriginAccountInfo
  208. )
  209. log.Info("dynamic compare and fix safe ,time start is(%+v), end time is (%+v)", start, end)
  210. if originAccountInfos, err = s.d.BatchQueryAccountInfoByTime(c, start, end, i); err != nil {
  211. log.Error("dynamic query batch account info inc error,error is (%+v)", err)
  212. return
  213. }
  214. for _, originAccountInfo := range originAccountInfos {
  215. for {
  216. mid := originAccountInfo.Mid
  217. // fixed userSafeQuestion
  218. var userSafeQuestion *model.UserSafeQuestion
  219. if userSafeQuestion, err = s.d.QueryUserSafeQuestion(c, mid); err != nil {
  220. log.Error("dynamic query user safe question err, mid is %d,err is(+v)", mid, err)
  221. continue
  222. }
  223. if len(originAccountInfo.SafeAnswer) == 0 && userSafeQuestion == nil {
  224. continue
  225. }
  226. if len(originAccountInfo.SafeAnswer) != 0 && userSafeQuestion == nil {
  227. log.Info("dynamic safe question not exist, mid is %d", mid)
  228. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserSafeQuestion, Action: insertAction}
  229. s.incFixChan <- errorFix
  230. s.mu.Lock()
  231. dynamicAccountInfoStat["notExistUserSafeQuestion"] = dynamicAccountInfoStat["notExistUserSafeQuestion"] + 1
  232. s.mu.Unlock()
  233. continue
  234. }
  235. originSafeQuestion := originAccountInfo.SafeQuestion
  236. newSafeQuestion := userSafeQuestion.SafeQuestion
  237. if originSafeQuestion != newSafeQuestion {
  238. if originInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  239. continue
  240. }
  241. if originInfo.SafeQuestion != newSafeQuestion {
  242. log.Info("dynamic safe question index compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originSafeQuestion, newSafeQuestion)
  243. errorFix := &model.ErrorFix{Mid: mid, ErrorType: safeErrorType, Action: updateAction}
  244. s.incFixChan <- errorFix
  245. s.mu.Lock()
  246. dynamicAccountInfoStat["safe"] = dynamicAccountInfoStat["safe"] + 1
  247. s.mu.Unlock()
  248. continue
  249. }
  250. }
  251. originSafeAnswerBytes := s.doHash(originAccountInfo.SafeAnswer)
  252. if hex.EncodeToString(originSafeAnswerBytes) != hex.EncodeToString(userSafeQuestion.SafeAnswer) {
  253. if originInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  254. continue
  255. }
  256. if hex.EncodeToString(s.doHash(originAccountInfo.SafeAnswer)) != hex.EncodeToString(userSafeQuestion.SafeAnswer) {
  257. log.Info("dynamic safe question answer compare not match,mid is %d, origin is (%+v),new is (%+v)", mid, originAccountInfo.SafeAnswer, hex.EncodeToString(userSafeQuestion.SafeAnswer))
  258. errorFix := &model.ErrorFix{Mid: mid, ErrorType: safeErrorType, Action: updateAction}
  259. s.incFixChan <- errorFix
  260. s.mu.Lock()
  261. dynamicAccountInfoStat["safe"] = dynamicAccountInfoStat["safe"] + 1
  262. s.mu.Unlock()
  263. continue
  264. }
  265. }
  266. var uro *model.UserRegOrigin
  267. if uro, err = s.d.GetUserRegOriginByMid(c, mid); err != nil {
  268. log.Error("dynamic query user reg origin err, mid is %d,err is(+v)", mid, err)
  269. continue
  270. }
  271. if uro == nil {
  272. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserRegOriginType, Action: insertAction}
  273. s.incFixChan <- errorFix
  274. s.mu.Lock()
  275. dynamicAccountRegStat["notExistUserRegOrigin"] = dynamicAccountRegStat["notExistUserRegOrigin"] + 1
  276. s.mu.Unlock()
  277. continue
  278. }
  279. if uro.JoinIP != InetAtoN(originAccountInfo.JoinIP) || uro.JoinTime != originAccountInfo.JoinTime {
  280. if originInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  281. continue
  282. }
  283. if uro.JoinIP != InetAtoN(originInfo.JoinIP) || uro.JoinTime != originInfo.JoinTime {
  284. errorFix := &model.ErrorFix{Mid: mid, ErrorType: userRegOriginErrorType, Action: updateAction}
  285. s.incFixChan <- errorFix
  286. s.mu.Lock()
  287. dynamicAccountRegStat["userRegOrigin"] = dynamicAccountRegStat["userRegOrigin"] + 1
  288. s.mu.Unlock()
  289. continue
  290. }
  291. }
  292. break
  293. }
  294. }
  295. }
  296. s.mu.Lock()
  297. dynamicAccountInfoStat["dynamicUserSafeQuestion"] = 0
  298. s.mu.Unlock()
  299. }
  300. func (s *Service) compareAndFixedUserRegOriginInc(c context.Context, start, end time.Time) {
  301. for i := 0; i < 20; i++ {
  302. var (
  303. err error
  304. originAccountRegs []*model.OriginAccountReg
  305. )
  306. log.Info("dynamic compare and fix account origin reg ,time start is(%+v), end time is (%+v)", start, end)
  307. if originAccountRegs, err = s.d.BatchQueryAccountRegByTime(c, start, end, i); err != nil {
  308. log.Error("dynamic query batch account info inc error,error is (%+v)", err)
  309. return
  310. }
  311. for _, originAccountReg := range originAccountRegs {
  312. mid := originAccountReg.Mid
  313. // fixed userSafeQuestion
  314. var uro *model.UserRegOrigin
  315. if uro, err = s.d.GetUserRegOriginByMid(c, mid); err != nil {
  316. log.Error("dynamic query user reg origin err, mid is %d,err is(+v)", mid, err)
  317. continue
  318. }
  319. if uro == nil {
  320. log.Info("dynamic user reg origin not exist, mid is %d", mid)
  321. errorFix := &model.ErrorFix{Mid: mid, ErrorType: notExistUserRegOriginType, Action: insertAction}
  322. s.incFixChan <- errorFix
  323. s.mu.Lock()
  324. dynamicAccountRegStat["notExistUserRegOrigin"] = dynamicAccountRegStat["notExistUserRegOrigin"] + 1
  325. s.mu.Unlock()
  326. continue
  327. }
  328. if mid <= 250531100 {
  329. continue
  330. }
  331. if uro.RegType != originAccountReg.RegType || uro.Origin != originAccountReg.OriginType {
  332. log.Info("dynamic user reg origin update, mid is %d", mid)
  333. errorFix := &model.ErrorFix{Mid: mid, ErrorType: userRegOriginErrorType, Action: updateAction}
  334. s.incFixChan <- errorFix
  335. s.mu.Lock()
  336. dynamicAccountRegStat["userRegOrigin"] = dynamicAccountRegStat["userRegOrigin"] + 1
  337. s.mu.Unlock()
  338. continue
  339. }
  340. }
  341. }
  342. s.mu.Lock()
  343. dynamicAccountRegStat["dynamicUserRegOrigin"] = 0
  344. s.mu.Unlock()
  345. }