MapController.cs
14.3 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rcs.Application.Common;
using Rcs.Application.DTOs;
using MassTransit.Mediator;
using Rcs.Application.MessageBus.Commands;
using Rcs.Application.MessageBus.Responses;
using StatusCodes = Microsoft.AspNetCore.Http.StatusCodes;
using Rcs.Application.Dtos;
using Rcs.Domain.Repositories;
namespace Rcs.Api.Controllers
{
/// <summary>
/// 地图管理控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class MapController : ControllerBase
{
private readonly ILogger<MapController> _logger;
private readonly IMediator _mediator;
private readonly IMapFileRepository _mapFileRepository;
public MapController(
ILogger<MapController> logger,
IMediator mediator,
IMapFileRepository mapFileRepository)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_mediator = mediator;
_mapFileRepository = mapFileRepository;
}
/// <summary>
/// 获取地图列表
/// </summary>
/// <returns>地图列表</returns>
[HttpGet("list")]
public async Task<IActionResult> GetMapList([FromQuery] GetMapsQuery query, CancellationToken cancellationToken = default)
{
var client = _mediator.CreateRequestClient<GetMapsQuery>();
var response = await client.GetResponse<PagedResponse<MapListItemDto>>(query, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 根据ID获取地图数据
/// </summary>
/// <param name="id">地图ID</param>
/// <returns>地图详情</returns>
[HttpGet("{id}")]
public async Task<IActionResult> GetMapById(string id, CancellationToken cancellationToken = default)
{
if(!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<GetMapQuery>();
var response = await client.GetResponse<ApiResponse<MapDto>>(new GetMapQuery()
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 根据ID获取地图详情
/// </summary>
/// <param name="id">地图ID</param>
/// <returns>地图详情</returns>
[HttpGet("{id}/details")]
public async Task<IActionResult> GetMapDetailsById(string id, CancellationToken cancellationToken = default)
{
if(!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<GetMapDetailsQuery>();
var response = await client.GetResponse<ApiResponse<MapDto>>(new GetMapDetailsQuery()
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 创建地图
/// </summary>
/// <param name="dto">创建地图DTO</param>
/// <returns>创建的地图信息</returns>
[HttpPost]
public async Task<IActionResult> CreateMap([FromBody] CreateOrUpdateMapCommand command)
{
/*
test model
{
"mapCode": "MAP001",
"mapName": "主地图",
"mapType": 1,
"version": "1.0.0",
"description": "主要生产区域地图",
"active": true
}
*/
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse.Failed("请求数据验证失败", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<CreateOrUpdateMapCommand>();
var response = await client.GetResponse<ApiResponse>(command);
return Ok(response.Message);
}
/// <summary>
/// 删除地图
/// </summary>
/// <param name="id">地图ID</param>
/// <returns>删除结果</returns>
[HttpPost("{id}")]
public async Task<IActionResult> DeleteMap(string id, CancellationToken cancellationToken = default)
{
if(!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<DeleteMapCommand>();
var response = await client.GetResponse<ApiResponse>(new DeleteMapCommand()
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
[HttpPost("Canvas")]
public async Task<IActionResult> CanvasMap([FromBody] ManageMapCommand command, CancellationToken cancellationToken = default)
{
var client = _mediator.CreateRequestClient<ManageMapCommand>();
var response = await client.GetResponse<ApiResponse>(command, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 复制地图
/// </summary>
/// <param name="id">地图ID</param>
/// <returns>复制结果</returns>
[HttpPost("{id}/copy")]
public async Task<IActionResult> CopyMap(string id, CancellationToken cancellationToken = default)
{
if(!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<CopyMapCommand>();
var response = await client.GetResponse<ApiResponse>(new CopyMapCommand()
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 同步地图资源
/// @author zzy
/// </summary>
/// <param name="id">地图ID</param>
/// <param name="type">同步类型:resource或points</param>
/// <returns>同步结果</returns>
[HttpPost("{id}/sync/resource")]
public async Task<IActionResult> SyncMapResource(string id, CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<SyncMapResourceCommand>();
var response = await client.GetResponse<ApiResponse>(new SyncMapResourceCommand
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 同步地图节点资源
/// @author zzy
/// </summary>
/// <param name="id">地图ID</param>
/// <returns>同步结果</returns>
[HttpPost("{id}/sync/points")]
public async Task<IActionResult> SyncMapRPoints(string id, CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<SyncMapPointsCommand>();
var response = await client.GetResponse<ApiResponse>(new SyncMapPointsCommand
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 更新地图背景图设置
/// @author zzy
/// </summary>
/// <param name="id">地图ID</param>
/// <param name="opacity">透明度(0-1)</param>
/// <param name="scale">缩放比例</param>
/// <param name="rotation">旋转角度(-180到180)</param>
/// <param name="offsetX">地图左下角偏移量X</param>
/// <param name="offsetY">地图左下角偏移量Y</param>
/// <returns>更新结果</returns>
[HttpPost("{id}/settings")]
public async Task<IActionResult> UpdateMapFileSettings(
string id,
[FromForm] decimal opacity = 0.60m,
[FromForm] decimal scale = 1.00m,
[FromForm] decimal rotation = 0.00m,
[FromForm] decimal offsetX = 0.00m,
[FromForm] decimal offsetY = 0.00m,
CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<UpdateMapFileSettingsCommand>();
var response = await client.GetResponse<ApiResponse>(new UpdateMapFileSettingsCommand
{
MapId = mapId,
Opacity = opacity,
Scale = scale,
Rotation = rotation,
OffsetX = offsetX,
OffsetY = offsetY
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 上传地图文件
/// @author zzy
/// </summary>
/// <param name="id">地图ID</param>
/// <param name="file">地图文件</param>
/// <param name="opacity">透明度(0-1)</param>
/// <param name="scale">缩放比例</param>
/// <param name="rotation">旋转角度(-180到180)</param>
/// <param name="offsetX">地图左下角偏移量X</param>
/// <param name="offsetY">地图左下角偏移量Y</param>
/// <returns>上传结果</returns>
[HttpPost("{id}/upload")]
public async Task<IActionResult> UploadMapFile(
string id,
IFormFile file,
[FromForm] decimal opacity = 0.60m,
[FromForm] decimal scale = 1.00m,
[FromForm] decimal rotation = 0.00m,
[FromForm] decimal offsetX = 0.00m,
[FromForm] decimal offsetY = 0.00m,
CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
if (file == null || file.Length == 0)
{
return BadRequest(ApiResponse.Failed("请选择要上传的文件", StatusCodes.Status400BadRequest));
}
// 将IFormFile转换为Stream
using var stream = new MemoryStream();
await file.CopyToAsync(stream, cancellationToken);
var client = _mediator.CreateRequestClient<UploadMapFileCommand>();
var response = await client.GetResponse<ApiResponse>(new UploadMapFileCommand
{
MapId = id,
FileStream = stream,
FileName = file.FileName,
FileSize = file.Length,
Opacity = opacity,
Scale = scale,
Rotation = rotation,
OffsetX = offsetX,
OffsetY = offsetY
}, cancellationToken);
return Ok(response.Message);
}
/// <summary>
/// 获取地图文件信息
/// @author zzy
/// </summary>
/// <param name="id">地图ID</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("{id}/file")]
public async Task<IActionResult> GetMapFile(string id, CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var mapFiles = await _mapFileRepository.FindAsync(
x => x.MapId == mapId && x.Active,
cancellationToken);
var mapFile = mapFiles.FirstOrDefault();
if (mapFile == null)
{
return Ok(ApiResponse<object>.Successful(null, "未找到地图文件"));
}
// 构建完整的文件访问URL
var scheme = Request.Scheme; // http 或 https
var host = Request.Host.Value; // localhost:5000
var relativePath = mapFile.FilePath.Replace("\\", "/");
var fileUrl = $"{scheme}://{host}/uploads/{relativePath}";
var result = new
{
fileName = mapFile.FileName,
filePath = relativePath, // 保留相对路径
fileUrl = fileUrl, // 完整访问URL
fileSize = mapFile.FileSize,
opacity = mapFile.Opacity,
scale = mapFile.Scale,
rotation = mapFile.Rotation,
offsetX = mapFile.OffsetX,
offsetY = mapFile.OffsetY,
uploadTime = mapFile.UploadTime
};
return Ok(ApiResponse<object>.Successful(result, "获取成功"));
}
/// <summary>
/// 删除地图背景图文件
/// </summary>
/// <param name="id">地图ID</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("{id}/file")]
public async Task<IActionResult> DeleteMapFile(string id, CancellationToken cancellationToken = default)
{
if (!Guid.TryParse(id, out Guid mapId))
{
return BadRequest(ApiResponse.Failed("无效的地图ID格式", StatusCodes.Status400BadRequest));
}
var client = _mediator.CreateRequestClient<DeleteMapFileCommand>();
var response = await client.GetResponse<ApiResponse>(new DeleteMapFileCommand
{
MapId = mapId
}, cancellationToken);
return Ok(response.Message);
}
}
}