123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using Abp.Extensions;
- using Mapster;
- using SnapshotWinFormsApp.Application.Dtos;
- using SnapshotWinFormsApp.Entities.NewHotline;
- using SnapshotWinFormsApp.Entities.OldHotline;
- using SnapshotWinFormsApp.Repository;
- using SnapshotWinFormsApp.Repository.Interfaces;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SnapshotWinFormsApp.Application;
- public class SnapshotUserInfoApplication
- {
- private readonly IBaseRepository<ThirdAccount> _thirdAccountRepo;
- private readonly IRepository<WeChatUserEntity> _weChatRepo;
- private readonly IRepository<WeChatFollowUserEntity> _weChatFlowRepo;
- private readonly IBaseRepository<SnapshotUserInfo> _userRepo;
- public SnapshotUserInfoApplication(DbSqlServer sqlServerDB)
- {
- _thirdAccountRepo = new BaseRepository<ThirdAccount>(sqlServerDB, "自贡");
- _weChatRepo = new Repository<WeChatUserEntity>(sqlServerDB, "自贡");
- _weChatFlowRepo = new Repository<WeChatFollowUserEntity>(sqlServerDB, "自贡");
- _userRepo = new BaseRepository<SnapshotUserInfo>(sqlServerDB, "自贡");
- }
- public async Task ImportSnapshotUserInfoAsync(Action<string> log, CancellationToken token)
- {
- log($"正在查询旧数据...");
- var items = await _weChatRepo.Queryable()
- .LeftJoin<WeChatFollowUserEntity>((w, f) => w.WUR_unionid == f.unionid)
- .Select((w, f) => new WeChatUserDto(), true)
- .ToListAsync(token);
- log($"共查询到{items.Count}条数据");
- var totalCount = items.Count;
- var index = 0;
- foreach (var item in items)
- {
- index++;
- if (token.IsCancellationRequested)
- {
- log("任务已取消");
- return;
- }
- var userId = await _userRepo.Queryable()
- .Where(m => m.PhoneNumber == item.WUR_PhoneNum)
- .Select(m => m.Id)
- .FirstAsync(token);
- if (userId.IsNullOrEmpty())
- {
- var userInfo = item.Adapt<SnapshotUserInfo>();
- userId = await _userRepo.InsertAsync(userInfo, token);
- log($"{index}/{totalCount} 插入用户信息: {userId}, {userInfo.Name}, {userInfo.PhoneNumber}");
- }
- var thirdId = await _thirdAccountRepo.Queryable()
- .Where(m => m.OpenId == item.openid && m.UnIonId == item.WUR_unionid)
- .Select(m => m.Id)
- .FirstAsync(token);
- if (thirdId.IsNullOrEmpty())
- {
- var thirdAccount = item.Adapt<ThirdAccount>();
- thirdAccount.ExternalId = userId;
- thirdAccount.Id = await _thirdAccountRepo.InsertAsync(thirdAccount, token);
- log($"{index}/{totalCount} 插入第三方账号信息: {thirdAccount.Id}, {thirdAccount.OpenId}, {thirdAccount.UserName}, {thirdAccount.PhoneNumber}, {thirdAccount.ExternalId}");
- }
- }
- }
- }
|