|
@@ -1,32 +1,36 @@
|
|
|
-using DataTransmission.Enum;
|
|
|
-using Mapster;
|
|
|
+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;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
using System.Reflection;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
|
|
|
namespace SnapshotWinFormsApp.Application.Interfaces;
|
|
|
-public class ImportApplicationBase<TSource, TEntity, TKey> : IImportApplication
|
|
|
+public class ImportApplicationBase<TSource, TEntity, TKey> : ImportApplicationBase<TSource, TEntity, TKey, TSource>
|
|
|
where TSource : OldBaseEntity<TKey>, new()
|
|
|
- where TEntity : Entity, new()
|
|
|
+ where TEntity : Entity, new()
|
|
|
{
|
|
|
- private readonly IRepository<TSource, TKey> _sourceRepo;
|
|
|
- private readonly IBaseRepository<TEntity> _targetRepo;
|
|
|
- private readonly IBaseRepository<OldDataId> _oldDataIdRepo;
|
|
|
- private readonly SqlSugarClient _sugarClient;
|
|
|
- private readonly CreateInstanceInDto _instance;
|
|
|
+ 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<TSource, TKey>(inDto);
|
|
|
+ _sourceRepo = new Repository<TMix, TKey>(inDto);
|
|
|
_targetRepo = new BaseRepository<TEntity>(inDto);
|
|
|
_oldDataIdRepo = new BaseRepository<OldDataId>(inDto);
|
|
|
}
|
|
@@ -35,6 +39,7 @@ public class ImportApplicationBase<TSource, TEntity, TKey> : IImportApplication
|
|
|
{
|
|
|
log($"正在查询旧数据...");
|
|
|
var items = await GetSourceList(_instance).ToListAsync(token);
|
|
|
+
|
|
|
log($"共查询到{items.Count}条数据");
|
|
|
var tableName = typeof(TSource).GetCustomAttribute<SugarTable>()?.TableName ?? throw new ArgumentNullException("老数据表名不能为空, 设置[SugarTable()]");
|
|
|
|
|
@@ -42,26 +47,71 @@ public class ImportApplicationBase<TSource, TEntity, TKey> : IImportApplication
|
|
|
{
|
|
|
await Task.Run(() => MainForm._pauseEvent.WaitHandle.WaitOne(), token);
|
|
|
var item = items[i];
|
|
|
- var has = HasOldData(tableName, item);
|
|
|
+ var has = await HasOldDataAsync(tableName, item, token);
|
|
|
if (has) continue;
|
|
|
- var target = item.Adapt<TEntity>();
|
|
|
+ var target = await GetTargetAsync(item, token);
|
|
|
|
|
|
await _targetRepo.db.Ado.BeginTranAsync();
|
|
|
target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
|
|
|
- await _oldDataIdRepo.InsertAsync(new OldDataId { OldId = item.Id.ToString(), TableName = tableName, NewId = target.Id }, token);
|
|
|
+ _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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public virtual ISugarQueryable<TSource> GetSourceList(CreateInstanceInDto inDto)
|
|
|
+ /// <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();
|
|
|
}
|
|
|
|
|
|
- public virtual bool HasOldData(string tableName, TSource item)
|
|
|
+ /// <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 _oldDataIdRepo.Queryable().Any(m => m.TableName == tableName && item.Id.ToString() == m.OldId);
|
|
|
+ return await _oldDataIdRepo.Queryable().AnyAsync(m => m.TableName == tableName && item.Id.ToString() == m.OldId, token);
|
|
|
}
|
|
|
|
|
|
}
|