|
| 1 | +using EFCore.BulkExtensions; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Secure.SecurityDatabaseSync.BLL.Interfaces; |
| 4 | +using Secure.SecurityDatabaseSync.DAL.Contexts; |
| 5 | +using Secure.SecurityDatabaseSync.DAL.Models; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Secure.SecurityDatabaseSync.BLL.Services |
| 12 | +{ |
| 13 | + /// <inheritdoc cref="ISyncTask"/> |
| 14 | + public class BulkSyncTask : ISyncTask, IDisposable |
| 15 | + { |
| 16 | + private readonly ApplicationContext _sourceContext; |
| 17 | + private readonly ApplicationContext _targetContext; |
| 18 | + private readonly string _code; |
| 19 | + |
| 20 | + private IList<Common> _sourceModels; |
| 21 | + private IList<Common> _targetModels; |
| 22 | + |
| 23 | + public BulkSyncTask( |
| 24 | + ApplicationContext sourceContext, |
| 25 | + ApplicationContext targetContext, |
| 26 | + string code) |
| 27 | + { |
| 28 | + _sourceContext = sourceContext ?? throw new ArgumentNullException(nameof(sourceContext)); |
| 29 | + _targetContext = targetContext ?? throw new ArgumentNullException(nameof(targetContext)); |
| 30 | + _code = code.ToUpper(); |
| 31 | + } |
| 32 | + |
| 33 | + public void Dispose() |
| 34 | + { |
| 35 | + _sourceContext.Dispose(); |
| 36 | + _targetContext.Dispose(); |
| 37 | + |
| 38 | + GC.SuppressFinalize(this); |
| 39 | + } |
| 40 | + |
| 41 | + public async Task RunAsync() |
| 42 | + { |
| 43 | + _sourceModels = await _sourceContext.Commons |
| 44 | + .AsNoTracking() |
| 45 | + .Where(model => model.Code == _code) |
| 46 | + .ToListAsync(); |
| 47 | + |
| 48 | + _targetModels = await _targetContext.Commons |
| 49 | + .AsNoTracking() |
| 50 | + .Where(model => model.Code == _code) |
| 51 | + .ToListAsync(); |
| 52 | + |
| 53 | + if (_targetModels.Any()) |
| 54 | + { |
| 55 | + await DeleteAsync(); |
| 56 | + } |
| 57 | + |
| 58 | + if (_sourceModels.Any()) |
| 59 | + { |
| 60 | + await AddAsync(); |
| 61 | + await UpdateAsync(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private async Task DeleteAsync() |
| 66 | + { |
| 67 | + var idsToDelete = _targetModels |
| 68 | + .Select(model => model.InternalNumber) |
| 69 | + .Except(_sourceModels |
| 70 | + .Select(model => model.InternalNumber)); |
| 71 | + |
| 72 | + if (idsToDelete.Any()) |
| 73 | + { |
| 74 | + List<Common> modelsToDelete = _targetModels |
| 75 | + .Where(model => idsToDelete.Contains(model.InternalNumber)) |
| 76 | + .ToList(); |
| 77 | + |
| 78 | + await _targetContext.BulkDeleteAsync(modelsToDelete); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private async Task AddAsync() |
| 83 | + { |
| 84 | + var idsToAdd = _sourceModels |
| 85 | + .Select(model => model.InternalNumber) |
| 86 | + .Except(_targetModels |
| 87 | + .Select(model => model.InternalNumber)); |
| 88 | + |
| 89 | + if (idsToAdd.Any()) |
| 90 | + { |
| 91 | + IEnumerable<Common> GetModelsToAdd() |
| 92 | + { |
| 93 | + var sourceModelsToAdd = _sourceModels |
| 94 | + .Where(model => idsToAdd.Contains(model.InternalNumber)); |
| 95 | + |
| 96 | + foreach (var sourceModel in sourceModelsToAdd) |
| 97 | + { |
| 98 | + yield return new Common |
| 99 | + { |
| 100 | + InternalNumber = sourceModel.InternalNumber, |
| 101 | + Code = sourceModel.Code, |
| 102 | + Name = sourceModel.Name, |
| 103 | + Updated = DateTime.Now, |
| 104 | + }; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + List<Common> modelsToAdd = GetModelsToAdd().ToList(); |
| 109 | + await _targetContext.BulkInsertAsync(modelsToAdd); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private async Task UpdateAsync() |
| 114 | + { |
| 115 | + IEnumerable<Common> GetModelsToUpdate() |
| 116 | + { |
| 117 | + foreach (var targetModel in _targetModels) |
| 118 | + { |
| 119 | + var sourceModel = _sourceModels |
| 120 | + .FirstOrDefault(model => model.InternalNumber == targetModel.InternalNumber); |
| 121 | + |
| 122 | + var isUpdated = false; |
| 123 | + |
| 124 | + if (targetModel.Name != sourceModel.Name) |
| 125 | + { |
| 126 | + isUpdated = true; |
| 127 | + targetModel.Name = sourceModel.Name; |
| 128 | + targetModel.Updated = DateTime.Now; |
| 129 | + } |
| 130 | + |
| 131 | + if (isUpdated) |
| 132 | + { |
| 133 | + yield return targetModel; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + List<Common> modelsToUpdate = GetModelsToUpdate().ToList(); |
| 139 | + if (modelsToUpdate.Any()) |
| 140 | + { |
| 141 | + await _targetContext.BulkUpdateAsync(modelsToUpdate); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments