123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Mapster;
- using SnapshotWinFormsApp.Application.Dtos;
- using SnapshotWinFormsApp.Entities.NewHotline;
- using SnapshotWinFormsApp.Entities.OldHotline;
- using SnapshotWinFormsApp.Repository;
- using SnapshotWinFormsApp.Repository.Interfaces;
- 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 : Entity, new()
- {
- public ImportApplicationBase(CreateInstanceInDto inDto) : base(inDto)
- {
- }
- }
- public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplication
- where TSource : OldBaseEntity<TKey>, new()
- where TEntity : Entity, new()
- where TMix : OldBaseEntity<TKey>, new()
- {
- public readonly IRepository<TMix, TKey> _sourceRepo;
- public readonly IBaseRepository<TEntity> _targetRepo;
- public readonly IBaseRepository<OldDataId> _oldDataIdRepo;
- public readonly SqlSugarClient _sugarClient;
- public readonly CreateInstanceInDto _instance;
- public ImportApplicationBase(CreateInstanceInDto inDto)
- {
- _instance = inDto;
- _sourceRepo = new Repository<TMix, TKey>(inDto);
- _targetRepo = new BaseRepository<TEntity>(inDto);
- _oldDataIdRepo = new BaseRepository<OldDataId>(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);
- await _targetRepo.db.Ado.BeginTranAsync();
- target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
- _oldDataIdRepo.InsertBulk(new OldDataId { OldId = item.Id.ToString(), TableName = tableName, NewId = target.Id }, i + 1 == items.Count);
- await _targetRepo.db.Ado.CommitTranAsync();
- log($"{i}/{items.Count} 插入数据: {item.Id} {target.Id}");
- 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 _oldDataIdRepo.Queryable().AnyAsync(m => m.TableName == tableName && item.Id.ToString() == m.OldId, token);
- }
- }
|