MapResourceRepository.cs
4.42 KB
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
using Microsoft.EntityFrameworkCore;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Infrastructure.DB.MsSql;
namespace Rcs.Infrastructure.DB.Repositories
{
/// <summary>
/// 地图资源仓储实现
/// </summary>
public class MapResourceRepository : Repository<MapResource>, IMapResourceRepository
{
public MapResourceRepository(AppDbContext context) : base(context)
{
}
/// <summary>
/// 根据地图ID获取所有资源
/// </summary>
public async Task<IEnumerable<MapResource>> GetByMapIdAsync(
Guid mapId,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Where(r => r.MapId == mapId && r.Active)
.OrderBy(r => r.ResourceCode)
.ToListAsync(cancellationToken);
}
/// <summary>
/// 根据节点ID获取所有资源
/// </summary>
public async Task<IEnumerable<MapResource>> GetByNodeIdAsync(
Guid nodeId,
CancellationToken cancellationToken = default)
{
// 通过连接查询获取与指定节点关联的资源
return await _context.MapNodes
.Where(n => n.NodeId == nodeId)
.SelectMany(n => _context.MapResources
.Where(r => r.MapId == n.MapId && r.Active))
.Distinct()
.OrderBy(r => r.ResourceCode)
.ToListAsync(cancellationToken);
}
/// <summary>
/// 根据资源类型获取资源列表
/// </summary>
public async Task<IEnumerable<MapResource>> GetByTypeAsync(
MapResourcesTYPE type,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Where(r => r.Type == type && r.Active)
.OrderBy(r => r.ResourceCode)
.ToListAsync(cancellationToken);
}
/// <summary>
/// 根据地图ID和资源类型获取资源列表
/// </summary>
public async Task<IEnumerable<MapResource>> GetByMapIdAndTypeAsync(
Guid mapId,
MapResourcesTYPE type,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Where(r => r.MapId == mapId && r.Type == type && r.Active)
.OrderBy(r => r.ResourceCode)
.ToListAsync(cancellationToken);
}
/// <summary>
/// 根据资源名称获取资源
/// </summary>
public async Task<MapResource?> GetByResourceNameAsync(
string resourceName,
CancellationToken cancellationToken = default)
{
return await _dbSet
.FirstOrDefaultAsync(r => r.ResourceName == resourceName && r.Active, cancellationToken);
}
/// <summary>
/// 根据资源编码获取资源
/// </summary>
public async Task<MapResource?> GetByResourceCodeAsync(
string resourceCode,
CancellationToken cancellationToken = default)
{
return await _dbSet
.FirstOrDefaultAsync(r => r.ResourceCode == resourceCode && r.Active, cancellationToken);
}
/// <summary>
/// 根据地图ID和资源编码获取资源
/// </summary>
public async Task<MapResource?> GetByMapIdAndResourceCodeAsync(
Guid mapId,
string resourceCode,
CancellationToken cancellationToken = default)
{
return await _dbSet
.FirstOrDefaultAsync(r => r.MapId == mapId && r.ResourceCode == resourceCode && r.Active, cancellationToken);
}
/// <summary>
/// 根据节点编码获取所有资源
/// </summary>
public async Task<IEnumerable<MapResource>> GetByNodeCodeAsync(
string nodeCode,
CancellationToken cancellationToken = default)
{
// 通过节点编码找到对应的地图,然后获取该地图的所有资源
return await _context.MapNodes
.Where(n => n.NodeCode == nodeCode)
.SelectMany(n => _context.MapResources
.Where(r => r.MapId == n.MapId && r.Active))
.Distinct()
.OrderBy(r => r.ResourceCode)
.ToListAsync(cancellationToken);
}
}
}