123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using Abp.Json;
- using Mapster;
- using SnapshotWinFormsApp.Application.Dtos;
- using SnapshotWinFormsApp.Entities.NewHotline;
- using SnapshotWinFormsApp.Entities.OldHotline;
- using SnapshotWinFormsApp.Repository;
- using SnapshotWinFormsApp.Repository.Interfaces;
- using SnapshotWinFormsApp.Tools;
- using SqlSugar;
- using System.Reflection;
- namespace SnapshotWinFormsApp.Application.Interfaces;
- public class ImportApplicationBase<TSource, TEntity, TKey> : ImportApplicationBase<TSource, TEntity, TKey, TSource>
- where TSource : OldBaseEntity<TKey>, new()
- where TEntity : OldIdEntity, new()
- {
- public ImportApplicationBase(CreateInstanceInDto inDto) : base(inDto)
- {
- }
- }
- public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplication
- where TSource : OldBaseEntity<TKey>, new()
- where TEntity : OldIdEntity, new()
- where TMix : OldBaseEntity<TKey>, new()
- {
- public readonly ISourceRepository<TMix, TKey> _sourceRepo;
- public readonly ITargetRepository<TEntity> _targetRepo;
- public readonly SqlSugarClient _sugarClient;
- public readonly CreateInstanceInDto _instance;
- public ImportApplicationBase(CreateInstanceInDto inDto)
- {
- _instance = inDto;
- _sourceRepo = new SourceRepository<TMix, TKey>(inDto);
- _targetRepo = new TargetRepository<TEntity>(inDto);
- }
- public async Task ImportAsync(Action<string> log, CancellationToken token)
- {
- log($"正在查询旧数据...");
- var items = await GetSourceList(_instance).ToListAsync(token);
-
- log($"共查询到{items.Count}条数据");
- var tableName = typeof(TSource).GetCustomAttribute<SugarTable>()?.TableName ?? throw new ArgumentNullException("老数据表名不能为空, 设置[SugarTable()]");
- for (int i = 0;i < items.Count;i++)
- {
- await Task.Run(() => MainForm._pauseEvent.WaitHandle.WaitOne(), token);
- var item = items[i];
- var has = await HasOldDataAsync(tableName, item, token);
- if (has) continue;
- var target = await GetTargetAsync(item, token);
- target.OldId = item.Id.ToString();
- target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
- log($"{i + 1}/{items.Count} 插入数据: {item.Id} {target.Id} {target.ToJson().Substring(0, 100)}");
- await InsertAfterAsync(log, item, target, token);
- }
- }
- /// <summary>
- /// 插入数据以后执行的动作
- /// </summary>
- /// <param name="item"></param>
- /// <param name="target"></param>
- /// <param name="token"></param>
- /// <returns></returns>
- /// <exception cref="NotImplementedException"></exception>
- public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, CancellationToken token)
- {
- await Task.FromResult(0);
- }
- /// <summary>
- /// 获取原始数据集合
- /// </summary>
- /// <param name="inDto"></param>
- /// <returns></returns>
- public virtual ISugarQueryable<TMix> GetSourceList(CreateInstanceInDto inDto)
- {
- return _sourceRepo.Queryable();
- }
- /// <summary>
- /// 获取原始数据集合
- /// </summary>
- /// <returns></returns>
- /// <exception cref="NotImplementedException"></exception>
- public virtual ISugarQueryable<TMix> GetSourceList()
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// 根据原始数据获取目标数据
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public virtual async Task<TEntity> GetTargetAsync(TMix source, CancellationToken token)
- {
- return await Task.FromResult(source.Adapt<TEntity>());
- }
- /// <summary>
- /// 验证是否已存在
- /// </summary>
- /// <param name="tableName"></param>
- /// <param name="item"></param>
- /// <returns></returns>
- public virtual async Task<bool> HasOldDataAsync(string tableName, TMix item, CancellationToken token)
- {
- return await _targetRepo.Queryable().AnyAsync(m => item.Id.ToString() == m.OldId, token);
- }
- }
|