ImportApplicationBase.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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<TSource, TKey> _sourceRepo;
  26. public readonly ITargetRepository<TEntity> _targetRepo;
  27. public readonly SqlSugarClient _sugarClient;
  28. public readonly CreateInstanceInDto _instance;
  29. public readonly ISelectRepository<SystemDicData> _systemDicDataRepo;
  30. public readonly IList<SystemDicData> JobType;
  31. public List<TMix> Sources = new List<TMix>();
  32. public ImportApplicationBase(CreateInstanceInDto inDto)
  33. {
  34. _instance = inDto;
  35. _sourceRepo = new SourceRepository<TSource, TKey>(inDto);
  36. _targetRepo = new TargetRepository<TEntity>(inDto);
  37. _systemDicDataRepo = new SelectRepository<SystemDicData>(inDto);
  38. JobType = _systemDicDataRepo.Queryable().Where(m => m.DicTypeCode == "JobType").ToList();
  39. }
  40. public async Task ImportAsync(Action<string> log, CancellationToken token)
  41. {
  42. log($"正在查询旧数据...");
  43. //var sql = GetSourceList().ToSqlString();
  44. Sources = await GetSourceList().ToListAsync(token);
  45. log($"共查询到{Sources.Count}条数据");
  46. //var tableName = typeof(TSource).GetCustomAttribute<SugarTable>()?.TableName ?? throw new ArgumentNullException("旧数据表名不能为空, 设置[SugarTable()]");
  47. for (int i = 0;i < Sources.Count;i++)
  48. {
  49. await Task.Run(() => MainForm._pauseEvent.WaitHandle.WaitOne(), token);
  50. var item = Sources[i];
  51. var has = true;
  52. try
  53. {
  54. has = await HasOldDataAsync(item, token);
  55. }
  56. catch (Exception e)
  57. {
  58. if (e.Message.Contains("column \"OldId\" does not exist"))
  59. {
  60. var tableName = new TEntity().GetTableName();
  61. log($"{tableName}表没有 OldId 字段");
  62. log($"ALTER TABLE \"public\".\"{tableName}\" ADD COLUMN \"OldId\" varchar(255);");
  63. return;
  64. }
  65. }
  66. if (has)
  67. {
  68. log($"{i + 1}/{Sources.Count} 跳过已存在");
  69. continue;
  70. }
  71. var target = await GetTargetAsync(item, token);
  72. target.OldId = item.Id.ToString();
  73. target.Id = _targetRepo.InsertBulk(target, i + 1 == Sources.Count);
  74. var msg = target.GetObjectValues();
  75. if (msg.Length > 88)
  76. msg = msg.Substring(0, 88);
  77. log($"{i + 1}/{Sources.Count} 新增: {item.Id} {target.Id} {msg}");
  78. await InsertAfterAsync(log, item, target, i + 1 == Sources.Count, token);
  79. }
  80. await EndTaskAsync(log, token);
  81. }
  82. public virtual async Task EndTaskAsync(Action<string> log, CancellationToken token)
  83. {
  84. await Task.FromResult(0);
  85. }
  86. /// <summary>
  87. /// 插入数据以后执行的动作
  88. /// </summary>
  89. /// <param name="item"></param>
  90. /// <param name="target"></param>
  91. /// <param name="token"></param>
  92. /// <returns></returns>
  93. /// <exception cref="NotImplementedException"></exception>
  94. public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, bool isEnd, CancellationToken token)
  95. {
  96. await Task.FromResult(0);
  97. }
  98. /// <summary>
  99. /// 获取原始数据集合
  100. /// </summary>
  101. /// <param name="inDto"></param>
  102. /// <returns></returns>
  103. public virtual ISugarQueryable<TMix> GetSourceList()
  104. {
  105. return _sourceRepo.Queryable().Select(m => new TMix(), true);
  106. }
  107. /// <summary>
  108. /// 根据原始数据获取目标数据
  109. /// </summary>
  110. /// <param name="source"></param>
  111. /// <returns></returns>
  112. public virtual async Task<TEntity> GetTargetAsync(TMix source, CancellationToken token)
  113. {
  114. return await Task.FromResult(source.Adapt<TEntity>());
  115. }
  116. /// <summary>
  117. /// 验证是否已存在
  118. /// </summary>
  119. /// <param name="tableName"></param>
  120. /// <param name="item"></param>
  121. /// <returns></returns>
  122. public virtual async Task<bool> HasOldDataAsync(TMix item, CancellationToken token)
  123. {
  124. return await _targetRepo.Queryable().AnyAsync(m => item.Id.ToString() == m.OldId, token);
  125. }
  126. }