ImportApplicationBase.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Mapster;
  2. using SnapshotWinFormsApp.Application.Dtos;
  3. using SnapshotWinFormsApp.Entities.NewHotline;
  4. using SnapshotWinFormsApp.Entities.OldHotline;
  5. using SnapshotWinFormsApp.Repository;
  6. using SnapshotWinFormsApp.Repository.Interfaces;
  7. using SqlSugar;
  8. using System.Reflection;
  9. namespace SnapshotWinFormsApp.Application.Interfaces;
  10. public class ImportApplicationBase<TSource, TEntity, TKey> : ImportApplicationBase<TSource, TEntity, TKey, TSource>
  11. where TSource : OldBaseEntity<TKey>, new()
  12. where TEntity : Entity, new()
  13. {
  14. public ImportApplicationBase(CreateInstanceInDto inDto) : base(inDto)
  15. {
  16. }
  17. }
  18. public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplication
  19. where TSource : OldBaseEntity<TKey>, new()
  20. where TEntity : Entity, new()
  21. where TMix : OldBaseEntity<TKey>, new()
  22. {
  23. public readonly IRepository<TMix, TKey> _sourceRepo;
  24. public readonly IBaseRepository<TEntity> _targetRepo;
  25. public readonly IBaseRepository<OldDataId> _oldDataIdRepo;
  26. public readonly SqlSugarClient _sugarClient;
  27. public readonly CreateInstanceInDto _instance;
  28. public ImportApplicationBase(CreateInstanceInDto inDto)
  29. {
  30. _instance = inDto;
  31. _sourceRepo = new Repository<TMix, TKey>(inDto);
  32. _targetRepo = new BaseRepository<TEntity>(inDto);
  33. _oldDataIdRepo = new BaseRepository<OldDataId>(inDto);
  34. }
  35. public async Task ImportAsync(Action<string> log, CancellationToken token)
  36. {
  37. log($"正在查询旧数据...");
  38. var items = await GetSourceList(_instance).ToListAsync(token);
  39. log($"共查询到{items.Count}条数据");
  40. var tableName = typeof(TSource).GetCustomAttribute<SugarTable>()?.TableName ?? throw new ArgumentNullException("老数据表名不能为空, 设置[SugarTable()]");
  41. for (int i = 0;i < items.Count;i++)
  42. {
  43. await Task.Run(() => MainForm._pauseEvent.WaitHandle.WaitOne(), token);
  44. var item = items[i];
  45. var has = await HasOldDataAsync(tableName, item, token);
  46. if (has) continue;
  47. var target = await GetTargetAsync(item, token);
  48. await _targetRepo.db.Ado.BeginTranAsync();
  49. target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
  50. _oldDataIdRepo.InsertBulk(new OldDataId { OldId = item.Id.ToString(), TableName = tableName, NewId = target.Id }, i + 1 == items.Count);
  51. await _targetRepo.db.Ado.CommitTranAsync();
  52. log($"{i}/{items.Count} 插入数据: {item.Id} {target.Id}");
  53. await InsertAfterAsync(log, item, target, token);
  54. }
  55. }
  56. /// <summary>
  57. /// 插入数据以后执行的动作
  58. /// </summary>
  59. /// <param name="item"></param>
  60. /// <param name="target"></param>
  61. /// <param name="token"></param>
  62. /// <returns></returns>
  63. /// <exception cref="NotImplementedException"></exception>
  64. public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, CancellationToken token)
  65. {
  66. await Task.FromResult(0);
  67. }
  68. /// <summary>
  69. /// 获取原始数据集合
  70. /// </summary>
  71. /// <param name="inDto"></param>
  72. /// <returns></returns>
  73. public virtual ISugarQueryable<TMix> GetSourceList(CreateInstanceInDto inDto)
  74. {
  75. return _sourceRepo.Queryable();
  76. }
  77. /// <summary>
  78. /// 获取原始数据集合
  79. /// </summary>
  80. /// <returns></returns>
  81. /// <exception cref="NotImplementedException"></exception>
  82. public virtual ISugarQueryable<TMix> GetSourceList()
  83. {
  84. throw new NotImplementedException();
  85. }
  86. /// <summary>
  87. /// 根据原始数据获取目标数据
  88. /// </summary>
  89. /// <param name="source"></param>
  90. /// <returns></returns>
  91. public virtual async Task<TEntity> GetTargetAsync(TMix source, CancellationToken token)
  92. {
  93. return await Task.FromResult(source.Adapt<TEntity>());
  94. }
  95. /// <summary>
  96. /// 验证是否已存在
  97. /// </summary>
  98. /// <param name="tableName"></param>
  99. /// <param name="item"></param>
  100. /// <returns></returns>
  101. public virtual async Task<bool> HasOldDataAsync(string tableName, TMix item, CancellationToken token)
  102. {
  103. return await _oldDataIdRepo.Queryable().AnyAsync(m => m.TableName == tableName && item.Id.ToString() == m.OldId, token);
  104. }
  105. }