ImportApplicationBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Abp.Json;
  2. using Mapster;
  3. using SnapshotWinFormsApp.Application.Dtos;
  4. using SnapshotWinFormsApp.Entities.NewHotline;
  5. using SnapshotWinFormsApp.Entities.OldHotline;
  6. using SnapshotWinFormsApp.Repository;
  7. using SnapshotWinFormsApp.Repository.Interfaces;
  8. using SnapshotWinFormsApp.Tools;
  9. using SqlSugar;
  10. using System.Reflection;
  11. namespace SnapshotWinFormsApp.Application.Interfaces;
  12. public class ImportApplicationBase<TSource, TEntity, TKey> : ImportApplicationBase<TSource, TEntity, TKey, TSource>
  13. where TSource : OldBaseEntity<TKey>, new()
  14. where TEntity : OldIdEntity, new()
  15. {
  16. public ImportApplicationBase(CreateInstanceInDto inDto) : base(inDto)
  17. {
  18. }
  19. }
  20. public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplication
  21. where TSource : OldBaseEntity<TKey>, new()
  22. where TEntity : OldIdEntity, new()
  23. where TMix : OldBaseEntity<TKey>, new()
  24. {
  25. public readonly ISourceRepository<TMix, TKey> _sourceRepo;
  26. public readonly ITargetRepository<TEntity> _targetRepo;
  27. public readonly SqlSugarClient _sugarClient;
  28. public readonly CreateInstanceInDto _instance;
  29. public ImportApplicationBase(CreateInstanceInDto inDto)
  30. {
  31. _instance = inDto;
  32. _sourceRepo = new SourceRepository<TMix, TKey>(inDto);
  33. _targetRepo = new TargetRepository<TEntity>(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. target.OldId = item.Id.ToString();
  49. target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
  50. log($"{i + 1}/{items.Count} 插入数据: {item.Id} {target.Id} {target.ToJson().Substring(0, 100)}");
  51. await InsertAfterAsync(log, item, target, token);
  52. }
  53. }
  54. /// <summary>
  55. /// 插入数据以后执行的动作
  56. /// </summary>
  57. /// <param name="item"></param>
  58. /// <param name="target"></param>
  59. /// <param name="token"></param>
  60. /// <returns></returns>
  61. /// <exception cref="NotImplementedException"></exception>
  62. public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, CancellationToken token)
  63. {
  64. await Task.FromResult(0);
  65. }
  66. /// <summary>
  67. /// 获取原始数据集合
  68. /// </summary>
  69. /// <param name="inDto"></param>
  70. /// <returns></returns>
  71. public virtual ISugarQueryable<TMix> GetSourceList(CreateInstanceInDto inDto)
  72. {
  73. return _sourceRepo.Queryable();
  74. }
  75. /// <summary>
  76. /// 获取原始数据集合
  77. /// </summary>
  78. /// <returns></returns>
  79. /// <exception cref="NotImplementedException"></exception>
  80. public virtual ISugarQueryable<TMix> GetSourceList()
  81. {
  82. throw new NotImplementedException();
  83. }
  84. /// <summary>
  85. /// 根据原始数据获取目标数据
  86. /// </summary>
  87. /// <param name="source"></param>
  88. /// <returns></returns>
  89. public virtual async Task<TEntity> GetTargetAsync(TMix source, CancellationToken token)
  90. {
  91. return await Task.FromResult(source.Adapt<TEntity>());
  92. }
  93. /// <summary>
  94. /// 验证是否已存在
  95. /// </summary>
  96. /// <param name="tableName"></param>
  97. /// <param name="item"></param>
  98. /// <returns></returns>
  99. public virtual async Task<bool> HasOldDataAsync(string tableName, TMix item, CancellationToken token)
  100. {
  101. return await _targetRepo.Queryable().AnyAsync(m => item.Id.ToString() == m.OldId, token);
  102. }
  103. }