123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using Abp.Json;
- using Mapster;
- using SnapshotWinFormsApp.Application.Dtos;
- using SnapshotWinFormsApp.Entities.NewHotline;
- using SnapshotWinFormsApp.Entities.OldHotline;
- using SnapshotWinFormsApp.Repository;
- using SnapshotWinFormsApp.Repository.Interfaces;
- using SnapshotWinFormsApp.Tools;
- using SqlSugar;
- using System.Reflection;
- namespace SnapshotWinFormsApp.Application.Interfaces;
- public class ImportApplicationBase<TSource, TEntity, TKey> : ImportApplicationBase<TSource, TEntity, TKey, TSource>
- where TSource : OldBaseEntity<TKey>, new()
- where TEntity : OldIdEntity, new()
- {
- public ImportApplicationBase(CreateInstanceInDto inDto) : base(inDto)
- {
- }
- }
- public class ImportApplicationBase<TSource, TEntity, TKey, TMix> : IImportApplication
- where TSource : OldBaseEntity<TKey>, new()
- where TEntity : OldIdEntity, new()
- where TMix : OldBaseEntity<TKey>, new()
- {
- 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<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 sql = GetSourceList().ToSqlString();
- Sources = await GetSourceList().ToListAsync(token);
- 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 = 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 == 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>
- /// 插入数据以后执行的动作
- /// </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, bool isEnd, CancellationToken token)
- {
- await Task.FromResult(0);
- }
- /// <summary>
- /// 获取原始数据集合
- /// </summary>
- /// <param name="inDto"></param>
- /// <returns></returns>
- public virtual ISugarQueryable<TMix> GetSourceList()
- {
- return _sourceRepo.Queryable().Select(m => new TMix(), true);
- }
- /// <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(TMix item, CancellationToken token)
- {
- return await _targetRepo.Queryable().AnyAsync(m => item.Id.ToString() == m.OldId, token);
- }
- }
|