sunpengfei
3 天以前 b288e7effa0edc09baad6089433a38b2b29b29db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Aop.Api.Domain;
using EFCore.BulkExtensions;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.Core
{
    /// <summary>
    /// 同步数据库
    /// </summary>
    public class SyncDatabaseCommandHandler() :
        IRequestHandler<SyncDatabaseCommand, int>
    {
 
        /// <summary>
        /// 同步数据库
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(SyncDatabaseCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            count += await SyncData<DictionaryCategory>();
            count += await SyncTreeData<DictionaryData>();
            //count += await SyncData<ElectronSignSetting>();
            //count += await SyncData<Enterprise>();
            //count += await SyncData<EnterpriseAuth>();
            //count += await SyncData<EnterpriseCost>();
            //count += await SyncData<EnterpriseElectronSignSetting>();
            //count += await SyncData<EnterpriseWallet>();
            //count += await SyncData<EnterpriseWalletExpandindirectOrder>();
            //count += await SyncData<EnterpriseWalletExpandindirectOrderFile>();
            //count += await SyncData<User>();
            //count += await SyncData<UserAuth>();
            count += await SyncTreeData<Menu>();
            count += await SyncData<Resource>();
            count += await SyncData<Role>();
            count += await SyncData<RoleMenu>();
            count += await SyncData<RoleResource>();
            //count += await SyncData<UserRole>();
            //count += await SyncData<ContractTemplate>();
            //count += await SyncData<ContractTemplateValue>();
            return count;
        }
 
        private async Task<int> SyncData<TEntity>()
            where TEntity : CommonEntity, new()
        {
            var count = 0;
            var repTarget = Db.GetRepository<TEntity, SyncTargetDbContextLocator>();
            var repSource = Db.GetRepository<TEntity, MasterDbContextLocator>();
 
            var sources = await repSource.AsQueryable().AsNoTracking()
                .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
                .ToListAsync();
            var targets = await repTarget.AsQueryable().AsNoTracking()
                .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
                .ToListAsync();
 
            var adds = sources.Where(s => !targets.Any(t => s.Id == t.Id)).ToList();
            if (adds.IsNotNull())
            {
                await repTarget.Context.BulkInsertAsync(adds);
                count += adds.Count;
            }
 
            var updates = sources.Where(s => targets.Any(t => s.Id == t.Id)).ToList();
            if (updates.IsNotNull())
            {
                await repTarget.Context.BulkUpdateAsync(updates);
                count += updates.Count;
            }
 
            var deletes = targets.Where(s => sources.Any(t => s.Id == t.Id)).ToList();
            if (deletes.IsNotNull())
            {
                await repTarget.Context.BulkUpdateAsync(deletes);
                count += deletes.Count;
            }
 
            return count;
        }
 
        private async Task<int> SyncTreeData<TEntity>()
            where TEntity : CommonEntity, ITreeData<TEntity>, new()
        {
            var count = 0;
            var repTarget = Db.GetRepository<TEntity, SyncTargetDbContextLocator>();
            var repSource = Db.GetRepository<TEntity, MasterDbContextLocator>();
 
            var sourceData = await repSource.AsQueryable().AsNoTracking()
                .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
                .ToListAsync();
            var targetData = await repTarget.AsQueryable().AsNoTracking()
                .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
                .ToListAsync();
 
            var sources = new List<TEntity>();
            var sourceParents = sourceData.Where(it => it.ParentId == null).ToList();
            LoopTreeData(sources, sourceData, sourceParents);
 
            var targets = new List<TEntity>();
            var targetParents = targetData.Where(it => it.ParentId == null).ToList();
            LoopTreeData(targets, targetData, targetParents);
 
            var adds = sources.Where(s => !targets.Any(t => s.Id == t.Id)).ToList();
            if (adds.IsNotNull())
            {
                await repTarget.Context.BulkInsertAsync(adds);
                count += adds.Count;
            }
 
            var updates = sources.Where(s => targets.Any(t => s.Id == t.Id)).ToList();
            if (updates.IsNotNull())
            {
                await repTarget.Context.BulkUpdateAsync(updates);
                count += updates.Count;
            }
 
            var deletes = targets.Where(s => sources.Any(t => s.Id == t.Id)).ToList();
            if (deletes.IsNotNull())
            {
                await repTarget.Context.BulkUpdateAsync(deletes);
                count += deletes.Count;
            }
 
            return count;
        }
 
        private void LoopTreeData<TEntity>(List<TEntity> entities, List<TEntity> all, List<TEntity> parents)
            where TEntity : CommonEntity, ITreeData<TEntity>, new()
        {
            if (parents.IsNotNull())
            {
                foreach (var parent in parents)
                {
                    entities.Add(parent);
                    var children = all.Where(it => it.ParentId == parent.Id).ToList();
                    LoopTreeData(entities, all, children);
                }
            }
        }
    }
}