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 _thirdAccountRepo; private readonly IRepository _weChatRepo; private readonly IRepository _weChatFlowRepo; private readonly IBaseRepository _userRepo; public SnapshotUserInfoApplication(DbSqlServer sqlServerDB) { _thirdAccountRepo = new BaseRepository(sqlServerDB, "自贡"); _weChatRepo = new Repository(sqlServerDB, "自贡"); _weChatFlowRepo = new Repository(sqlServerDB, "自贡"); _userRepo = new BaseRepository(sqlServerDB, "自贡"); } public async Task ImportSnapshotUserInfoAsync(Action log, CancellationToken token) { log($"正在查询旧数据..."); var items = await _weChatRepo.Queryable() .LeftJoin((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(); 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.ExternalId = userId; thirdAccount.Id = await _thirdAccountRepo.InsertAsync(thirdAccount, token); log($"{index}/{totalCount} 插入第三方账号信息: {thirdAccount.Id}, {thirdAccount.OpenId}, {thirdAccount.UserName}, {thirdAccount.PhoneNumber}, {thirdAccount.ExternalId}"); } } } }