SnapshotUserInfoApplication.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Abp.Extensions;
  2. using DataTransmission.Enum;
  3. using Mapster;
  4. using SnapshotWinFormsApp.Application.Dtos;
  5. using SnapshotWinFormsApp.Application.Interfaces;
  6. using SnapshotWinFormsApp.Entities.NewHotline;
  7. using SnapshotWinFormsApp.Entities.OldHotline;
  8. using SnapshotWinFormsApp.Repository;
  9. using SnapshotWinFormsApp.Repository.Enum;
  10. using SnapshotWinFormsApp.Repository.Interfaces;
  11. using SnapshotWinFormsApp.Tools;
  12. using SqlSugar;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Linq;
  17. using System.Reflection;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Xml.Linq;
  21. namespace SnapshotWinFormsApp.Application;
  22. [Description("微信用户和网格员")]
  23. public class SnapshotUserInfoApplication : ImportApplicationBase<WeChatUserEntity, Citizen>, IImportApplication
  24. {
  25. private readonly ITargetRepository<ThirdAccount> _thirdAccountRepo;
  26. private readonly ITargetRepository<Citizen> _userRepo;
  27. private readonly IList<Citizen> Users;
  28. private readonly IList<Flow03_PushContentDto> Guiders;
  29. private readonly IList<SSP_InviteLogEntity> InviteLogs;
  30. private readonly ITargetRepository<InviteCodeRecord> _inviteCodeRecordRepository;
  31. public SnapshotUserInfoApplication(CreateInstanceInDto inDto) : base(inDto)
  32. {
  33. _inviteCodeRecordRepository = new TargetRepository<InviteCodeRecord>(inDto);
  34. _thirdAccountRepo = new TargetRepository<ThirdAccount>(inDto);
  35. _userRepo = new TargetRepository<Citizen>(inDto);
  36. Users = _userRepo.GetAll();
  37. Guiders = new SourceRepository<Flow03_PushContent>(inDto).Queryable()
  38. .Where(m => m.MemberMobile != null && m.MemberName != null)
  39. .Select(m => new Flow03_PushContentDto(), true)
  40. .Distinct().ToList();
  41. InviteLogs = new SourceRepository<SSP_InviteLogEntity>(inDto).Queryable()
  42. .ToList();
  43. }
  44. public override async Task<Citizen> GetTargetAsync(WeChatUserEntity source, CancellationToken token)
  45. {
  46. var target = source.Adapt<Citizen>();
  47. target.InitId();
  48. target.OldId = source.Id;
  49. var guider = Guiders.Where(m => m.MemberMobile == target.PhoneNumber).FirstOrDefault();
  50. target.CitizenType = EReadPackUserType.Citizen;
  51. if (guider != null)
  52. {
  53. target.CitizenType = EReadPackUserType.Guider;
  54. target.LastModificationName = guider.MemberName;
  55. }
  56. var code = InviteLogs.Where(m => m.SSPI_Openid == source.WUR_Openid).FirstOrDefault();
  57. if (code != null)
  58. target.InvitationCode = code.SSPI_Code;
  59. return target;
  60. }
  61. public override ISugarQueryable<WeChatUserEntity> GetSourceList(string key = "")
  62. {
  63. return _sourceRepo.Queryable()
  64. .Where(m => m.WUR_UserType == "ssp")
  65. .WhereIF(_instance.Keyword.NotNullOrEmpty(), m => m.WUR_PhoneNum == _instance.Keyword)
  66. .WhereIF(key.NotNullOrEmpty(), m => m.WUR_Openid == key);
  67. }
  68. public override async Task<bool> HasOldDataAsync(WeChatUserEntity item, CancellationToken token)
  69. {
  70. var userId = Users
  71. .Where(m => m.PhoneNumber == item.WUR_PhoneNum)
  72. .Select(m => m.Id).FirstOrDefault();
  73. return userId.NotNullOrEmpty();
  74. }
  75. public override async Task<ThirdAccount> InsertAfterAsync(Action<string> log, WeChatUserEntity item, Citizen target, bool isEnd, CancellationToken token)
  76. {
  77. var thirdId = await _thirdAccountRepo.Queryable()
  78. .Where(m => m.OpenId == item.WUR_Openid && m.UnIonId == item.WUR_unionid)
  79. .Select(m => m.Id)
  80. .FirstAsync(token);
  81. if (thirdId.IsNullOrEmpty())
  82. {
  83. var thirdAccount = item.Adapt<ThirdAccount>();
  84. thirdAccount.InitId();
  85. thirdAccount.ExternalId = target.Id;
  86. if (thirdAccount.OpenId.IsNullOrEmpty())
  87. { log?.Invoke("openid为空"); return null; }
  88. thirdAccount.Id = _thirdAccountRepo.InsertBulk(thirdAccount, isEnd);
  89. return thirdAccount;
  90. }
  91. return null;
  92. }
  93. public async Task<IList<ThirdAccount>> InsertByOpenIdAsync(Action<string> log, string key, CancellationToken token)
  94. {
  95. var thirdAccountList = new List<ThirdAccount>();
  96. var oldUser = await GetSourceList(key).ToListAsync(token);
  97. for (int i = 0;i < oldUser.Count;i++)
  98. {
  99. var user = oldUser[i];
  100. var newUser = await GetTargetAsync(user, token);
  101. newUser.InitId();
  102. _targetRepo.InsertBulk(newUser, true);
  103. var th= await InsertAfterAsync(log, user, newUser, true, token);
  104. if (th != null) thirdAccountList.Add(th);
  105. }
  106. return thirdAccountList;
  107. }
  108. }