data_fix.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. "github.com/go-sql-driver/mysql"
  10. "github.com/pkg/errors"
  11. )
  12. func (s *Service) fullFixed(msg chan *model.ErrorFix) {
  13. for {
  14. fix, ok := <-msg
  15. if !ok {
  16. log.Error("consumer full info closed")
  17. return
  18. }
  19. log.Info("full data fixed consumer msg,(%+v)", fix)
  20. if s.c.DataFixSwitch {
  21. errType := fix.ErrorType
  22. if notExistUserBase == errType || pwdErrorType == errType || statusErrorType == errType || statusErrorType == userIDErrorType {
  23. s.fixUserBase(fix.Mid, fix.Action, "full")
  24. }
  25. if notExistUserTel == errType || telErrorType == errType {
  26. s.fixUserTel(fix.Mid, fix.Action, "full")
  27. }
  28. if notExistUserMail == errType || mailErrorType == errType {
  29. s.fixUserMail(fix.Mid, fix.Action, "full")
  30. }
  31. if notExistUserSafeQuestion == errType || safeErrorType == errType {
  32. s.fixSafeQuestion(fix.Mid, fix.Action, "full")
  33. }
  34. if notExistUserThirdBind == errType || sinaErrorType == errType || qqErrorType == errType {
  35. s.fixUserSns(fix.Mid, errType, fix.Action, "full")
  36. }
  37. }
  38. }
  39. }
  40. func (s *Service) incFixed(msg chan *model.ErrorFix) {
  41. for {
  42. fix, ok := <-msg
  43. if !ok {
  44. log.Error("consumer inc info closed")
  45. return
  46. }
  47. log.Info("dynamic data fixed consumer msg,(%+v)", fix)
  48. if s.incrDataFixSwitch {
  49. errType := fix.ErrorType
  50. if notExistUserBase == errType || pwdErrorType == errType || statusErrorType == errType || statusErrorType == userIDErrorType {
  51. s.fixUserBase(fix.Mid, fix.Action, "incr")
  52. }
  53. if notExistUserTel == errType || telErrorType == errType {
  54. s.fixUserTel(fix.Mid, fix.Action, "incr")
  55. }
  56. if notExistUserMail == errType || mailErrorType == errType {
  57. s.fixUserMail(fix.Mid, fix.Action, "incr")
  58. }
  59. if notExistUserSafeQuestion == errType || safeErrorType == errType {
  60. s.fixSafeQuestion(fix.Mid, fix.Action, "incr")
  61. }
  62. if notExistUserThirdBind == errType || sinaErrorType == errType || qqErrorType == errType {
  63. s.fixUserSns(fix.Mid, errType, fix.Action, "incr")
  64. }
  65. if notExistUserRegOriginType == errType || userRegOriginErrorType == errType {
  66. s.fixUserRegOrigin(fix.Mid, errType, fix.Action, "incr")
  67. }
  68. }
  69. }
  70. }
  71. func (s *Service) fixUserBase(mid int64, action, tpe string) {
  72. var (
  73. origin *model.OriginAccount
  74. err error
  75. )
  76. log.Info("data fix user base,mid is %d,action %s,type %s", mid, action, tpe)
  77. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  78. log.Error("data fix query account by mid error,mid is %d,err is (%+v)", mid, err)
  79. return
  80. }
  81. var pwdByte []byte
  82. if pwdByte, err = hex.DecodeString(origin.Pwd); err != nil {
  83. log.Error("data fix hex.DecodeString(origin.Pwd) error,mid is %d,err is (%+v),origin is(%+v)", mid, err, origin)
  84. return
  85. }
  86. if insertAction == action {
  87. a := &model.UserBase{
  88. Mid: origin.Mid,
  89. UserID: origin.UserID,
  90. Pwd: pwdByte,
  91. Salt: origin.Salt,
  92. Status: origin.Isleak,
  93. Deleted: 0,
  94. MTime: origin.MTime,
  95. }
  96. if _, err = s.d.InsertUserBase(context.Background(), a); err != nil {
  97. log.Error("data fix s.d.InsertUserBase by mid error,mid is %d,err is (%+v)", mid, err)
  98. return
  99. }
  100. }
  101. if updateAction == action {
  102. a := &model.UserBase{
  103. Mid: origin.Mid,
  104. UserID: origin.UserID,
  105. Pwd: pwdByte,
  106. Salt: origin.Salt,
  107. Status: origin.Isleak,
  108. }
  109. if _, err = s.d.UpdateUserBase(context.Background(), a); err != nil {
  110. log.Error("data fix s.d.UpdateUserBase by mid error,mid is %d,err is (%+v)", mid, err)
  111. return
  112. }
  113. }
  114. }
  115. func (s *Service) fixUserTel(mid int64, action, tpe string) {
  116. var (
  117. origin *model.OriginAccount
  118. err error
  119. )
  120. log.Info("data fix user tel,mid is %d,action %s,type %s", mid, action, tpe)
  121. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  122. log.Error("data fix query account by mid error,mid is %d,err is (%+v)", mid, err)
  123. return
  124. }
  125. ot := strings.Trim(strings.ToLower(origin.Tel), "")
  126. var telByte []byte
  127. if telByte, err = s.doEncrypt(ot); err != nil {
  128. log.Error("data fix doEncrypt tel by mid error,mid is %d,err is (%+v)", mid, err)
  129. return
  130. }
  131. var telBindTime int64
  132. if insertAction == action && len(ot) != 0 {
  133. ut := &model.UserTel{
  134. Mid: origin.Mid,
  135. Tel: telByte,
  136. Cid: s.countryMap[origin.CountryID],
  137. MTime: origin.MTime,
  138. }
  139. if telBindTime, err = s.d.QueryTelBindLog(context.Background(), mid); err != nil {
  140. log.Error("user not exist tel.mid %d", mid)
  141. }
  142. if telBindTime > int64(filterStart) && telBindTime < int64(filterEnd) {
  143. telBindTime = 0
  144. }
  145. ut.TelBindTime = telBindTime
  146. if _, err = s.d.InsertUserTel(context.Background(), ut); err != nil {
  147. switch nErr := errors.Cause(err).(type) {
  148. case *mysql.MySQLError:
  149. if nErr.Number == mySQLErrCodeDuplicateEntry {
  150. if err = s.handlerInsertTelDuplicate(ut); err != nil {
  151. log.Error("fail to handlerInsertTelDuplicate userTel(%+v) error(%+v)", ut, err)
  152. return
  153. }
  154. err = nil
  155. return
  156. }
  157. }
  158. log.Error("fail to add user tel userTel(%+v) error(%+v)", ut, err)
  159. return
  160. }
  161. }
  162. if updateAction == action {
  163. ut := &model.UserTel{
  164. Mid: origin.Mid,
  165. Tel: telByte,
  166. Cid: s.countryMap[origin.CountryID],
  167. }
  168. if _, err = s.d.UpdateUserTel(context.Background(), ut); err != nil {
  169. switch nErr := errors.Cause(err).(type) {
  170. case *mysql.MySQLError:
  171. if nErr.Number == mySQLErrCodeDuplicateEntry {
  172. if err = s.handlerUpdateTelDuplicate(ut); err != nil {
  173. log.Error("fail to handlerInsertTelDuplicate userTel(%+v) error(%+v)", ut, err)
  174. return
  175. }
  176. err = nil
  177. return
  178. }
  179. }
  180. log.Error("fail to update user tel userTel(%+v) error(%+v)", ut, err)
  181. return
  182. }
  183. }
  184. }
  185. func (s *Service) fixUserMail(mid int64, action, tpe string) {
  186. var (
  187. origin *model.OriginAccount
  188. originInfo *model.OriginAccountInfo
  189. err error
  190. )
  191. if origin, err = s.d.QueryAccountByMid(context.Background(), mid); err != nil {
  192. log.Error("data fix query account by mid error,mid is %d,err is (%+v)", mid, err)
  193. return
  194. }
  195. log.Info("data fix user mail,mid is %d,action %s,type %s, origin(%+v)", mid, action, tpe, origin)
  196. om := strings.Trim(strings.ToLower(origin.Email), "")
  197. var emailByte []byte
  198. if emailByte, err = s.doEncrypt(om); err != nil {
  199. log.Error("data fix doEncrypt mail by mid error,mid is %d,err is (%+v)", mid, err)
  200. return
  201. }
  202. if insertAction == action && len(om) != 0 {
  203. userMail := &model.UserEmail{
  204. Mid: origin.Mid,
  205. Email: emailByte,
  206. MTime: origin.MTime,
  207. }
  208. if originInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  209. log.Error("fail to QueryAccountInfoByMid mid is (%+v) error(%+v)", mid, err)
  210. return
  211. }
  212. timestamp := originInfo.ActiveTime
  213. if originInfo.Spacesta >= 0 {
  214. userMail.Verified = 1
  215. userMail.EmailBindTime = timestamp
  216. }
  217. if _, err = s.d.InsertUserEmail(context.Background(), userMail); err != nil {
  218. switch nErr := errors.Cause(err).(type) {
  219. case *mysql.MySQLError:
  220. if nErr.Number == mySQLErrCodeDuplicateEntry {
  221. if err = s.handlerEmailInsertDuplicate(userMail); err != nil {
  222. log.Error("fail to handlerEmailInsertDuplicate userEmail(%+v) error(%+v)", userMail, err)
  223. return
  224. }
  225. err = nil
  226. return
  227. }
  228. }
  229. log.Error("fail to add user email userEmail(%+v) error(%+v)", userMail, err)
  230. return
  231. }
  232. }
  233. if updateAction == action {
  234. userMail := &model.UserEmail{
  235. Mid: origin.Mid,
  236. Email: emailByte,
  237. }
  238. if _, err = s.d.UpdateUserMail(context.Background(), userMail); err != nil {
  239. switch nErr := errors.Cause(err).(type) {
  240. case *mysql.MySQLError:
  241. if nErr.Number == mySQLErrCodeDuplicateEntry {
  242. if err = s.handlerEmailUpdateDuplicate(userMail); err != nil {
  243. log.Error("fail to handlerEmailDuplicate userEmail(%+v) error(%+v)", userMail, err)
  244. return
  245. }
  246. err = nil
  247. return
  248. }
  249. }
  250. log.Error("fail to update user email userEmail(%+v) error(%+v)", userMail, err)
  251. return
  252. }
  253. }
  254. }
  255. func (s *Service) fixSafeQuestion(mid int64, action, tpe string) {
  256. var (
  257. accountInfo *model.OriginAccountInfo
  258. err error
  259. )
  260. log.Info("data fix safe question,mid is %d,action %s,type %s", mid, action, tpe)
  261. if accountInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  262. log.Error("data fix query account info by mid error,mid is %d,err is (%+v)", mid, err)
  263. return
  264. }
  265. if insertAction == action && len(accountInfo.SafeAnswer) != 0 {
  266. usq := &model.UserSafeQuestion{
  267. Mid: accountInfo.Mid,
  268. SafeQuestion: accountInfo.SafeQuestion,
  269. SafeAnswer: s.doHash(accountInfo.SafeAnswer),
  270. SafeBindTime: time.Now().Unix(),
  271. }
  272. if _, err = s.d.InsertUserSafeQuestion(context.Background(), usq); err != nil {
  273. log.Error("data fix s.d.InsertUserSafeQuestion error,mid is %d,err is (%+v)", mid, err)
  274. return
  275. }
  276. }
  277. if updateAction == action {
  278. usq := &model.UserSafeQuestion{
  279. Mid: accountInfo.Mid,
  280. SafeQuestion: accountInfo.SafeQuestion,
  281. SafeAnswer: s.doHash(accountInfo.SafeAnswer),
  282. }
  283. if _, err = s.d.UpdateUserSafeQuestion(context.Background(), usq); err != nil {
  284. log.Error("data fix s.d.UpdateUserSafeQuestion error,mid is %d,err is (%+v)", mid, err)
  285. return
  286. }
  287. }
  288. }
  289. func (s *Service) fixUserSns(mid, errType int64, action, tpe string) {
  290. var (
  291. accountSns *model.OriginAccountSns
  292. err error
  293. )
  294. log.Info("data fix third bind ,mid is %d,action %s,type %s", mid, action, tpe)
  295. if accountSns, err = s.d.QueryAccountSnsByMid(context.Background(), mid); err != nil {
  296. log.Error("data fix query account sns by mid error,mid is %d,err is (%+v)", mid, err)
  297. return
  298. }
  299. if insertAction == action {
  300. if len(accountSns.SinaAccessToken) != 0 {
  301. sina := &model.UserThirdBind{
  302. Mid: accountSns.Mid,
  303. PlatForm: platformSina,
  304. OpenID: string(accountSns.SinaUID),
  305. Token: accountSns.SinaAccessToken,
  306. Expires: accountSns.SinaAccessExpires,
  307. }
  308. if _, err = s.d.InsertUserThirdBind(context.Background(), sina); err != nil {
  309. log.Error("data fix s.d.InsertUserThirdBind by mid error,mid is %d,err is (%+v)", mid, err)
  310. return
  311. }
  312. }
  313. if len(accountSns.QQAccessToken) != 0 {
  314. qq := &model.UserThirdBind{
  315. Mid: accountSns.Mid,
  316. PlatForm: platformQQ,
  317. OpenID: accountSns.QQOpenid,
  318. Token: accountSns.QQAccessToken,
  319. Expires: accountSns.QQAccessExpires,
  320. }
  321. if _, err = s.d.InsertUserThirdBind(context.Background(), qq); err != nil {
  322. log.Error("data fix s.d.UpdateUserThirdBind by mid error,mid is %d,err is (%+v)", mid, err)
  323. return
  324. }
  325. }
  326. }
  327. if updateAction == action {
  328. if sinaErrorType == errType {
  329. sns := &model.UserThirdBind{
  330. Mid: accountSns.Mid,
  331. PlatForm: platformSina,
  332. OpenID: string(accountSns.SinaUID),
  333. Token: accountSns.SinaAccessToken,
  334. }
  335. if _, err = s.d.UpdateUserThirdBind(context.Background(), sns); err != nil {
  336. log.Error("data fix s.d.UpdateUserThirdBind by mid error,mid is %d,err is (%+v)", mid, err)
  337. return
  338. }
  339. }
  340. if qqErrorType == errType {
  341. sns := &model.UserThirdBind{
  342. Mid: accountSns.Mid,
  343. PlatForm: platformQQ,
  344. OpenID: accountSns.QQOpenid,
  345. Token: accountSns.QQAccessToken,
  346. }
  347. if _, err = s.d.UpdateUserThirdBind(context.Background(), sns); err != nil {
  348. log.Error("data fix s.d.UpdateUserThirdBind by mid error,mid is %d,err is (%+v)", mid, err)
  349. return
  350. }
  351. }
  352. }
  353. }
  354. func (s *Service) fixUserRegOrigin(mid, errType int64, action, tpe string) {
  355. var (
  356. accountInfo *model.OriginAccountInfo
  357. accountReg *model.OriginAccountReg
  358. affected int64
  359. err error
  360. )
  361. log.Info("data fix user reg ,mid is %d,action %s,type %s", mid, action, tpe)
  362. if accountInfo, err = s.d.QueryAccountInfoByMid(context.Background(), mid); err != nil {
  363. log.Error("data fix query account info by mid error,mid is %d,err is (%+v)", mid, err)
  364. return
  365. }
  366. uro := &model.UserRegOrigin{
  367. Mid: accountInfo.Mid,
  368. JoinTime: accountInfo.JoinTime,
  369. JoinIP: InetAtoN(accountInfo.JoinIP),
  370. MTime: accountInfo.MTime,
  371. CTime: accountInfo.MTime,
  372. }
  373. if mid >= 250531100 {
  374. if accountReg, err = s.d.QueryAccountRegByMid(context.Background(), mid); err != nil {
  375. log.Error("data fix query account reg by mid error,mid is %d,err is (%+v)", mid, err)
  376. return
  377. }
  378. if accountReg != nil {
  379. uro.RegType = accountReg.RegType
  380. uro.Origin = accountReg.OriginType
  381. uro.MTime = accountReg.MTime
  382. uro.CTime = accountReg.CTime
  383. uro.AppID = accountReg.AppID
  384. }
  385. }
  386. if insertAction == action || updateAction == action {
  387. if affected, err = s.d.InsertUpdateUserRegOriginType(context.Background(), uro); err != nil {
  388. log.Error("data fix InsertUpdateUserRegOrigin by mid error,mid is %d,err is (%+v)", mid, err)
  389. return
  390. }
  391. if affected == 0 {
  392. log.Error("data fix InsertUpdateUserRegOrigin opt error,not affected ", mid, err)
  393. return
  394. }
  395. }
  396. }
  397. func (s *Service) handlerEmailInsertDuplicate(userEmail *model.UserEmail) (err error) {
  398. var (
  399. duplicateMid int64
  400. asoAccount *model.OriginAccount
  401. affected int64
  402. )
  403. if duplicateMid, err = s.d.GetMidByEmail(context.Background(), userEmail); err != nil {
  404. log.Error("handlerEmailInsertDuplicate fail to get mid by email userEmail(%+v) error(%+v)", userEmail, err)
  405. return
  406. }
  407. if asoAccount, err = s.d.QueryAccountByMid(context.Background(), duplicateMid); err != nil {
  408. log.Error("handlerEmailInsertDuplicate fail to get asoAccount by mid(%d) error(%+v)", duplicateMid, err)
  409. return
  410. }
  411. // 3. 将冲突的Email设置为NULL
  412. dunplicateUserMailToNil := &model.UserEmail{
  413. Mid: duplicateMid,
  414. }
  415. if affected, err = s.d.UpdateUserMail(context.Background(), dunplicateUserMailToNil); err != nil || affected == 0 {
  416. log.Error("handlerEmailInsertDuplicate s.d.UpdateUserMail to nil error.")
  417. }
  418. // 4. 插入新的email
  419. if affected, err = s.d.InsertUserEmail(context.Background(), userEmail); err != nil {
  420. log.Error("handlerEmailInsertDuplicate s.d.InsertUserEmail. userMail is (%+v),affect is %d", userEmail, affected)
  421. }
  422. // 5. 更新的tel
  423. om := strings.Trim(strings.ToLower(asoAccount.Email), "")
  424. var emailByte []byte
  425. if emailByte, err = s.doEncrypt(om); err != nil {
  426. log.Error("handlerEmailInsertDuplicate data fix doEncrypt mail by mid error,mid is %d,err is (%+v)", asoAccount.Mid, err)
  427. return
  428. }
  429. dunplicateUserEmail := &model.UserEmail{
  430. Mid: asoAccount.Mid,
  431. Email: emailByte,
  432. }
  433. if affected, err = s.d.UpdateUserMail(context.Background(), dunplicateUserEmail); err != nil {
  434. log.Error("handlerEmailInsertDuplicate s.d.UpdateUserMail to right. userMail is (%+v),affected is %d", dunplicateUserEmail, affected)
  435. }
  436. return
  437. }
  438. func (s *Service) handlerEmailUpdateDuplicate(userEmail *model.UserEmail) (err error) {
  439. var (
  440. duplicateMid int64
  441. asoAccount *model.OriginAccount
  442. affected int64
  443. )
  444. if duplicateMid, err = s.d.GetMidByEmail(context.Background(), userEmail); err != nil {
  445. log.Error("handlerEmailInsertDuplicate fail to get mid by email userEmail(%+v) error(%+v)", userEmail, err)
  446. return
  447. }
  448. if asoAccount, err = s.d.QueryAccountByMid(context.Background(), duplicateMid); err != nil {
  449. log.Error("handlerEmailInsertDuplicate fail to get asoAccount by mid(%d) error(%+v)", duplicateMid, err)
  450. return
  451. }
  452. // 3. 将冲突的Email设置为NULL
  453. duplicateUserMailToNil := &model.UserEmail{
  454. Mid: duplicateMid,
  455. }
  456. if affected, err = s.d.UpdateUserMail(context.Background(), duplicateUserMailToNil); err != nil || affected == 0 {
  457. log.Error("handlerEmailInsertDuplicate s.d.UpdateUserMail to nil error.")
  458. }
  459. // 4. 插入新的email
  460. if affected, err = s.d.UpdateUserMail(context.Background(), userEmail); err != nil {
  461. log.Error("handlerEmailInsertDuplicate s.d.InsertUserEmail. userMail is (%+v),affected %d", userEmail, affected)
  462. }
  463. // 5. 更新的tel
  464. om := strings.Trim(strings.ToLower(asoAccount.Email), "")
  465. var emailByte []byte
  466. if emailByte, err = s.doEncrypt(om); err != nil {
  467. log.Error("handlerEmailInsertDuplicate data fix doEncrypt mail by mid error,mid is %d,err is (%+v)", asoAccount.Mid, err)
  468. return
  469. }
  470. duplicateUserEmail := &model.UserEmail{
  471. Mid: asoAccount.Mid,
  472. Email: emailByte,
  473. }
  474. if affected, err = s.d.UpdateUserMail(context.Background(), duplicateUserEmail); err != nil {
  475. log.Error("handlerEmailInsertDuplicate s.d.UpdateUserMail to right. userMail is (%+v),affected %d", duplicateUserEmail, affected)
  476. }
  477. return
  478. }
  479. func (s *Service) handlerInsertTelDuplicate(userTel *model.UserTel) (err error) {
  480. var (
  481. duplicateMid int64
  482. duplicateAsoAccount *model.OriginAccount
  483. affected int64
  484. )
  485. // 1. 查询duplicateMid
  486. if duplicateMid, err = s.d.GetMidByTel(context.Background(), userTel); err != nil {
  487. log.Error("handlerInsertTelDuplicate to get mid by tel userTel(%+v) error(%+v)", userTel, err)
  488. return
  489. }
  490. // 2. 查询冲突的mid
  491. if duplicateAsoAccount, err = s.d.QueryAccountByMid(context.Background(), duplicateMid); err != nil {
  492. log.Error("handlerInsertTelDuplicate to get asoAccount by mid(%d) error(%+v)", duplicateMid, err)
  493. return
  494. }
  495. // 3. 将冲突的tel设置为NULL
  496. duplicateUserTelToNil := &model.UserTel{
  497. Mid: duplicateMid,
  498. }
  499. if affected, err = s.d.UpdateUserTel(context.Background(), duplicateUserTelToNil); err != nil || affected == 0 {
  500. log.Error("handlerInsertTelDuplicate s.d.UpdateUserTel to nil error.")
  501. }
  502. // 4. 插入新的tel
  503. if affected, err = s.d.InsertUserTel(context.Background(), userTel); err != nil {
  504. log.Error("handlerInsertTelDuplicate s.d.InsertUserTel.userTel is (%+v),affected %d", userTel, affected)
  505. }
  506. // 5. 更新的tel
  507. ot := strings.Trim(strings.ToLower(duplicateAsoAccount.Tel), "")
  508. var telByte []byte
  509. if telByte, err = s.doEncrypt(ot); err != nil {
  510. log.Error("data fix doEncrypt tel by mid error,mid is %d,err is (%+v)", duplicateAsoAccount.Mid, err)
  511. return
  512. }
  513. duplicateUserTel := &model.UserTel{
  514. Mid: duplicateAsoAccount.Mid,
  515. Cid: s.countryMap[duplicateAsoAccount.CountryID],
  516. Tel: telByte,
  517. }
  518. if affected, err = s.d.UpdateUserTel(context.Background(), duplicateUserTel); err != nil {
  519. log.Error("handlerInsertTelDuplicate s.d.UpdateUserTel to right. userTel is (%+v),affected %d", userTel, affected)
  520. }
  521. return
  522. }
  523. func (s *Service) handlerUpdateTelDuplicate(userTel *model.UserTel) (err error) {
  524. var (
  525. duplicateMid int64
  526. duplicateAsoAccount *model.OriginAccount
  527. affected int64
  528. )
  529. // 1. 查询duplicateMid
  530. if duplicateMid, err = s.d.GetMidByTel(context.Background(), userTel); err != nil {
  531. log.Error("handlerUpdateTelDuplicate to get mid by tel userTel(%+v) error(%+v)", userTel, err)
  532. return
  533. }
  534. // 2. 查询冲突的mid
  535. if duplicateAsoAccount, err = s.d.QueryAccountByMid(context.Background(), duplicateMid); err != nil {
  536. log.Error("handlerUpdateTelDuplicate to get asoAccount by mid(%d) error(%+v)", duplicateMid, err)
  537. return
  538. }
  539. duplicateUserTelToNil := &model.UserTel{
  540. Mid: duplicateMid,
  541. }
  542. // 3. 冲突的Tel设置为NULL
  543. if affected, err = s.d.UpdateUserTel(context.Background(), duplicateUserTelToNil); err != nil || affected == 0 {
  544. log.Error("handlerUpdateTelDuplicate s.d.UpdateUserTel to nil error.")
  545. }
  546. // 4. update tel
  547. if affected, err = s.d.UpdateUserTel(context.Background(), userTel); err != nil {
  548. log.Error("handlerUpdateTelDuplicate s.d.UpdateUserTel.userTel is (%+v),affected %d", userTel, affected)
  549. }
  550. // 5. 设置冲突的tel
  551. ot := strings.Trim(strings.ToLower(duplicateAsoAccount.Tel), "")
  552. var telByte []byte
  553. if telByte, err = s.doEncrypt(ot); err != nil {
  554. log.Error("handlerUpdateTelDuplicate data fix doEncrypt tel by mid error,mid is %d,err is (%+v)", duplicateAsoAccount.Mid, err)
  555. return
  556. }
  557. duplicateUserTel := &model.UserTel{
  558. Mid: duplicateAsoAccount.Mid,
  559. Cid: s.countryMap[duplicateAsoAccount.CountryID],
  560. Tel: telByte,
  561. }
  562. if affected, err = s.d.UpdateUserTel(context.Background(), duplicateUserTel); err != nil {
  563. log.Error("handlerUpdateTelDuplicate s.d.UpdateUserTel to right. userTel is (%+v),affected %d", userTel, affected)
  564. }
  565. return
  566. }
  567. func (s *Service) fixEmailVerified() (err error) {
  568. var (
  569. res []*model.UserEmail
  570. originInfo *model.OriginAccountInfo
  571. start = int64(0)
  572. )
  573. for {
  574. log.Info("GetUnverifiedEmail, start %d", start)
  575. if res, err = s.d.GetUnverifiedEmail(context.Background(), start); err != nil {
  576. log.Error("fail to get UserTel error(%+v)", err)
  577. time.Sleep(100 * time.Millisecond)
  578. continue
  579. }
  580. if len(res) == 0 {
  581. log.Info("fix email verified finished!")
  582. break
  583. }
  584. for _, a := range res {
  585. for {
  586. if originInfo, err = s.d.QueryAccountInfoByMid(context.Background(), a.Mid); err != nil {
  587. log.Error("fail to QueryAccountInfoByMid mid is (%+v) error(%+v)", a.Mid, err)
  588. continue
  589. }
  590. break
  591. }
  592. if originInfo.Spacesta >= 0 {
  593. a.Verified = 1
  594. _, err = s.d.UpdateUserMailVerified(context.Background(), a)
  595. }
  596. }
  597. start = res[len(res)-1].Mid
  598. }
  599. return
  600. }