SnapshotUserInfoApplication.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Abp.Extensions;
  2. using Mapster;
  3. using SnapshotWinFormsApp.Application.Dtos;
  4. using SnapshotWinFormsApp.Entities.NewHotline;
  5. using SnapshotWinFormsApp.Entities.OldHotline;
  6. using SnapshotWinFormsApp.Repository;
  7. using SnapshotWinFormsApp.Repository.Interfaces;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace SnapshotWinFormsApp.Application;
  14. public class SnapshotUserInfoApplication
  15. {
  16. private readonly IBaseRepository<ThirdAccount> _thirdAccountRepo;
  17. private readonly IRepository<WeChatUserEntity> _weChatRepo;
  18. private readonly IRepository<WeChatFollowUserEntity> _weChatFlowRepo;
  19. private readonly IBaseRepository<SnapshotUserInfo> _userRepo;
  20. public SnapshotUserInfoApplication(DbSqlServer sqlServerDB)
  21. {
  22. _thirdAccountRepo = new BaseRepository<ThirdAccount>(sqlServerDB, "自贡");
  23. _weChatRepo = new Repository<WeChatUserEntity>(sqlServerDB, "自贡");
  24. _weChatFlowRepo = new Repository<WeChatFollowUserEntity>(sqlServerDB, "自贡");
  25. _userRepo = new BaseRepository<SnapshotUserInfo>(sqlServerDB, "自贡");
  26. }
  27. public async Task ImportSnapshotUserInfoAsync(Action<string> log, CancellationToken token)
  28. {
  29. log($"正在查询旧数据...");
  30. var items = await _weChatRepo.Queryable()
  31. .LeftJoin<WeChatFollowUserEntity>((w, f) => w.WUR_unionid == f.unionid)
  32. .Select((w, f) => new WeChatUserDto(), true)
  33. .ToListAsync(token);
  34. log($"共查询到{items.Count}条数据");
  35. var totalCount = items.Count;
  36. var index = 0;
  37. foreach (var item in items)
  38. {
  39. index++;
  40. if (token.IsCancellationRequested)
  41. {
  42. log("任务已取消");
  43. return;
  44. }
  45. var userId = await _userRepo.Queryable()
  46. .Where(m => m.PhoneNumber == item.WUR_PhoneNum)
  47. .Select(m => m.Id)
  48. .FirstAsync(token);
  49. if (userId.IsNullOrEmpty())
  50. {
  51. var userInfo = item.Adapt<SnapshotUserInfo>();
  52. userId = await _userRepo.InsertAsync(userInfo, token);
  53. log($"{index}/{totalCount} 插入用户信息: {userId}, {userInfo.Name}, {userInfo.PhoneNumber}");
  54. }
  55. var thirdId = await _thirdAccountRepo.Queryable()
  56. .Where(m => m.OpenId == item.openid && m.UnIonId == item.WUR_unionid)
  57. .Select(m => m.Id)
  58. .FirstAsync(token);
  59. if (thirdId.IsNullOrEmpty())
  60. {
  61. var thirdAccount = item.Adapt<ThirdAccount>();
  62. thirdAccount.ExternalId = userId;
  63. thirdAccount.Id = await _thirdAccountRepo.InsertAsync(thirdAccount, token);
  64. log($"{index}/{totalCount} 插入第三方账号信息: {thirdAccount.Id}, {thirdAccount.OpenId}, {thirdAccount.UserName}, {thirdAccount.PhoneNumber}, {thirdAccount.ExternalId}");
  65. }
  66. }
  67. }
  68. }