InviteApplication.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Abp.Collections.Extensions;
  2. using Hotline.Snapshot;
  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.Interfaces;
  10. using SnapshotWinFormsApp.Tools;
  11. using System.ComponentModel;
  12. using System.Configuration;
  13. namespace SnapshotWinFormsApp.Application;
  14. [Description("邀请码")]
  15. public class InviteApplication : IImportApplication
  16. {
  17. private readonly IRepository<SSP_InviteLogEntity, string> _inviteLogRepo;
  18. private readonly IRepository<SSP_InviteEntity, int> _inviteRepo;
  19. private readonly IBaseRepository<InviteCode> _newInviteCodeRepo;
  20. private readonly IBaseRepository<InviteCodeRecord> _newInviteLogRepo;
  21. private readonly IBaseRepository<SnapshotUserInfo> _userInfoRepo;
  22. private readonly IBaseRepository<ThirdAccount> _thirdAccountRepo;
  23. public InviteApplication(CreateInstanceInDto inDto)
  24. {
  25. _inviteLogRepo = new Repository<SSP_InviteLogEntity, string>(inDto);
  26. _inviteRepo = new Repository<SSP_InviteEntity, int>(inDto);
  27. _newInviteCodeRepo = new BaseRepository<InviteCode>(inDto);
  28. _newInviteLogRepo = new BaseRepository<InviteCodeRecord>(inDto);
  29. _userInfoRepo = new BaseRepository<SnapshotUserInfo>(inDto);
  30. _thirdAccountRepo = new BaseRepository<ThirdAccount>(inDto);
  31. }
  32. public async Task ImportAsync(Action<string> log, CancellationToken token)
  33. {
  34. log($"正在查询旧数据...");
  35. var items = await _inviteRepo.GetAllAsync(token);
  36. log($"共查询到{items.Count}条数据");
  37. for (int i = 0;i < items.Count;i++)
  38. {
  39. var inviteCode = items[i].Adapt<InviteCode>();
  40. var has = await _newInviteCodeRepo.Queryable().AnyAsync(m => m.OrgName == inviteCode.OrgName, token);
  41. if (has) continue;
  42. var url = ConfigurationManager.AppSettings["ZiGongFile"] + inviteCode.QRCodeUrl;
  43. var fileContent = await new FileTools().GetNetworkFileAsync(url, token);
  44. inviteCode.QRCodeUrl = fileContent.Path;
  45. await _newInviteCodeRepo.InsertAsync(inviteCode, token);
  46. log($"{i + 1}/{items.Count} 插入数据: {inviteCode.Id} {inviteCode.OrgName}");
  47. }
  48. await ImportInivteLogAsync(log, token);
  49. }
  50. private async Task ImportInivteLogAsync(Action<string> log, CancellationToken token)
  51. {
  52. log($"正在查询旧数据...");
  53. var items = await _inviteLogRepo.Queryable()
  54. .LeftJoin<SSP_InviteEntity>((log, invite) => log.SSPI_CodeID == invite.SIC_Byte)
  55. .LeftJoin<WeChatUserEntity>((log, invite, user) => user.WUR_Openid == log.SSPI_Openid)
  56. .Select((log, invite, user) => new InviteCodeRecord
  57. {
  58. Id = log.Id,
  59. Name = user.WUR_WebUserName,
  60. OrgName = invite.SIC_BMName,
  61. InviteCode = log.SSPI_Code,
  62. WXOpenId = log.SSPI_Openid,
  63. PhoneNumber = user.WUR_PhoneNum,
  64. })
  65. .ToListAsync(token);
  66. log($"共查询到{items.Count}条数据");
  67. var invities = await _newInviteCodeRepo.GetAllAsync(token);
  68. var thirdAccounts = await _thirdAccountRepo.GetAllAsync(token);
  69. for (int i = 0;i < items.Count;i++)
  70. {
  71. var inviteLog = items[i];
  72. var has = await _newInviteLogRepo.Queryable().AnyAsync(m => m.Id == inviteLog.Id, token);
  73. if (has) continue;
  74. inviteLog.OrgId = invities.FirstOrDefault(m => m.OrgName == inviteLog.OrgName)?.Id;
  75. await _newInviteLogRepo.InsertAsync(inviteLog, token);
  76. log($"{i + 1}/{items.Count} 插入数据: {inviteLog.Id} {inviteLog.OrgName} {inviteLog.Name} {inviteLog.InviteCode}");
  77. var userId = thirdAccounts
  78. .Where(m => m.OpenId == inviteLog.WXOpenId).Select(m => m.ExternalId).FirstOrDefault();
  79. if (userId.IsNullOrEmpty()) continue;
  80. log($"更新用户邀请码: {userId} {inviteLog.InviteCode}");
  81. await _userInfoRepo.Updateable()
  82. .SetColumns(m => m.InvitationCode, inviteLog.InviteCode)
  83. .Where(m => m.Id == userId)
  84. .ExecuteCommandAsync(token);
  85. }
  86. }
  87. }