CommunityInfoApplication.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Mapster;
  2. using SnapshotWinFormsApp.Application.Dtos;
  3. using SnapshotWinFormsApp.Application.Interfaces;
  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 System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace SnapshotWinFormsApp.Application;
  16. [Description("导入社区信息")]
  17. public class CommunityInfoApplication : IImportApplication
  18. {
  19. private readonly IBaseRepository<CommunityInfo> _communityInfoRepo;
  20. private readonly IRepository<SSP_AreaEntity, int> _areaRepo;
  21. public CommunityInfoApplication(CreateInstanceInDto inDto)
  22. {
  23. _communityInfoRepo = new BaseRepository<CommunityInfo>(inDto);
  24. _areaRepo = new Repository<SSP_AreaEntity, int>(inDto);
  25. }
  26. public async Task ImportAsync(Action<string> log, CancellationToken token)
  27. {
  28. try
  29. {
  30. log($"正在查询旧数据...");
  31. var items = await _areaRepo.GetAllAsync(token);
  32. log($"共查询到{items.Count}条数据");
  33. for (int i = 0;i < items.Count;i++)
  34. {
  35. var item = items[i];
  36. var communitInfo = item.Adapt<CommunityInfo>();
  37. var has = await _communityInfoRepo.Queryable()
  38. .AnyAsync(m => m.UniqueKey == communitInfo.UniqueKey, token);
  39. if (has) continue;
  40. await _communityInfoRepo.InsertAsync(communitInfo, token);
  41. log($"{i}/{items.Count} 插入数据: {communitInfo.Id} {communitInfo.Name}");
  42. }
  43. }
  44. catch (Exception e)
  45. {
  46. var msg = e.Message;
  47. log($"导入数据异常: {e.Message}");
  48. }
  49. }
  50. }