|
@@ -24,38 +24,73 @@ public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplic
|
|
|
where TEntity : OldIdEntity, new()
|
|
|
where TMix : OldBaseEntity<TKey>, new()
|
|
|
{
|
|
|
- public readonly ISourceRepository<TMix, TKey> _sourceRepo;
|
|
|
+ public readonly ISourceRepository<TSource, TKey> _sourceRepo;
|
|
|
public readonly ITargetRepository<TEntity> _targetRepo;
|
|
|
public readonly SqlSugarClient _sugarClient;
|
|
|
public readonly CreateInstanceInDto _instance;
|
|
|
+ public readonly ISelectRepository<SystemDicData> _systemDicDataRepo;
|
|
|
+ public readonly IList<SystemDicData> JobType;
|
|
|
+ public List<TMix> Sources = new List<TMix>();
|
|
|
public ImportApplicationBase(CreateInstanceInDto inDto)
|
|
|
{
|
|
|
_instance = inDto;
|
|
|
- _sourceRepo = new SourceRepository<TMix, TKey>(inDto);
|
|
|
+ _sourceRepo = new SourceRepository<TSource, TKey>(inDto);
|
|
|
_targetRepo = new TargetRepository<TEntity>(inDto);
|
|
|
+ _systemDicDataRepo = new SelectRepository<SystemDicData>(inDto);
|
|
|
+ JobType = _systemDicDataRepo.Queryable().Where(m => m.DicTypeCode == "JobType").ToList();
|
|
|
}
|
|
|
|
|
|
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()]");
|
|
|
+ //var sql = GetSourceList().ToSqlString();
|
|
|
+ Sources = await GetSourceList().ToListAsync(token);
|
|
|
|
|
|
- for (int i = 0;i < items.Count;i++)
|
|
|
+ log($"共查询到{Sources.Count}条数据");
|
|
|
+ //var tableName = typeof(TSource).GetCustomAttribute<SugarTable>()?.TableName ?? throw new ArgumentNullException("旧数据表名不能为空, 设置[SugarTable()]");
|
|
|
+
|
|
|
+ for (int i = 0;i < Sources.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 item = Sources[i];
|
|
|
+ var has = true;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ has = await HasOldDataAsync(item, token);
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (e.Message.Contains("column \"OldId\" does not exist"))
|
|
|
+ {
|
|
|
+ var tableName = new TEntity().GetTableName();
|
|
|
+ log($"{tableName}表没有 OldId 字段");
|
|
|
+ log($"ALTER TABLE \"public\".\"{tableName}\" ADD COLUMN \"OldId\" varchar(255);");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (has)
|
|
|
+ {
|
|
|
+ log($"{i + 1}/{Sources.Count} 跳过已存在");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
var target = await GetTargetAsync(item, token);
|
|
|
target.OldId = item.Id.ToString();
|
|
|
|
|
|
- target.Id = _targetRepo.InsertBulk(target, i + 1 == items.Count);
|
|
|
- log($"{i + 1}/{items.Count} 插入数据: {item.Id} {target.Id} {target.ToJson().Substring(0, 100)}");
|
|
|
- await InsertAfterAsync(log, item, target, token);
|
|
|
+ target.Id = _targetRepo.InsertBulk(target, i + 1 == Sources.Count);
|
|
|
+ var msg = target.GetObjectValues();
|
|
|
+ if (msg.Length > 88)
|
|
|
+ msg = msg.Substring(0, 88);
|
|
|
+ log($"{i + 1}/{Sources.Count} 新增: {item.Id} {target.Id} {msg}");
|
|
|
+ await InsertAfterAsync(log, item, target, i + 1 == Sources.Count, token);
|
|
|
}
|
|
|
+ await EndTaskAsync(log, token);
|
|
|
+ }
|
|
|
+
|
|
|
+ public virtual async Task EndTaskAsync(Action<string> log, CancellationToken token)
|
|
|
+ {
|
|
|
+ await Task.FromResult(0);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -66,7 +101,7 @@ public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplic
|
|
|
/// <param name="token"></param>
|
|
|
/// <returns></returns>
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
- public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, CancellationToken token)
|
|
|
+ public virtual async Task InsertAfterAsync(Action<string> log, TMix item, TEntity target, bool isEnd, CancellationToken token)
|
|
|
{
|
|
|
await Task.FromResult(0);
|
|
|
}
|
|
@@ -76,19 +111,9 @@ public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplic
|
|
|
/// </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();
|
|
|
+ {
|
|
|
+ return _sourceRepo.Queryable().Select(m => new TMix(), true);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -107,7 +132,7 @@ public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplic
|
|
|
/// <param name="tableName"></param>
|
|
|
/// <param name="item"></param>
|
|
|
/// <returns></returns>
|
|
|
- public virtual async Task<bool> HasOldDataAsync(string tableName, TMix item, CancellationToken token)
|
|
|
+ public virtual async Task<bool> HasOldDataAsync(TMix item, CancellationToken token)
|
|
|
{
|
|
|
return await _targetRepo.Queryable().AnyAsync(m => item.Id.ToString() == m.OldId, token);
|
|
|
}
|