using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using WorkerService1.Domains; using WorkerService1.Dto; using WorkerService1.Dto.Configuration; using WorkerService1.Dto.QueryRequest; using WorkerService1.Dto.QueryResponse; using WorkerService1.Services; using WorkerService1.Utils; namespace WorkerService1.Controllers; [ApiController] [Route("api/[controller]")] public class UserController : ControllerBase { public UserController() { } [HttpGet("Pages")] public async Task GetPageList([FromQuery] UserQuery request, [FromServices] SpiderServices spiderServices) { var res = await spiderServices.GetUsers(request); return Ok(res); } [HttpGet("/api/Polygon/Pages")] public async Task>> GetPolygon([FromQuery] PageRequest request, [FromServices] ApplicationDbContext db, [FromServices] SpiderServices spiderServices) { var polygon = db.Polygons; var result = await polygon.ToListAsync(); var cameras = await spiderServices.GetCameras(); var cameraLocation = cameras.Result?.Records.Select(x => (x.name, Location: new Points(x.lon, x.lat))) .ToList(); foreach (var item in result) { if (cameraLocation == null) continue; foreach (var (camera, location) in cameraLocation) { if (!location.IsPointInsidePolygon(item)) continue; item.RangeCameras ??= new List(); item.RangeCameras?.Add(camera); } } return new SpiderResponse>() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "", Result = result }; } [HttpPost("/api/Polygon")] public async Task> CreateOrUpdatePolygon([FromBody] Polygon request, [FromServices] ApplicationDbContext db) { var polygon = db.Polygons; if (request.PolygonId == null) { request.PolygonId = Guid.NewGuid(); var result = await polygon.AddAsync(request); await db.SaveChangesAsync(); return new SpiderResponse { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "保存成功", Result = true }; } var old = await polygon.FirstOrDefaultAsync(x => x.PolygonId == request.PolygonId); old.Points = request.Points; old.Name = request.Name; polygon.Update(old); await db.SaveChangesAsync(); return new SpiderResponse { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新成功", Result = true }; } [HttpDelete("/api/Polygon/{polygonId}")] public async Task> DeletePolygon([FromRoute] Guid polygonId, [FromServices] ApplicationDbContext db) { var polygon = db.Polygons; var result = await polygon.FirstOrDefaultAsync(x => x.PolygonId == polygonId); if (result == null) { return new SpiderResponse() { IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = false }; } polygon.Remove(result); await db.SaveChangesAsync(); return new SpiderResponse() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "删除区域信息成功", Result = true }; } [HttpGet("/api/Polygon/{polygonId}")] public async Task> GetPolygon([FromRoute] Guid polygonId, [FromServices] ApplicationDbContext db) { var polygon = db.Polygons; var result = await polygon.FirstOrDefaultAsync(x => x.PolygonId == polygonId); if (result == null) { return new SpiderResponse() { IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = null }; } return new SpiderResponse() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = result }; } [HttpPut("/api/Polygon/{polygonId}/UserId/{userId}")] public async Task> BindPolygonUserId([FromRoute] Guid polygonId, [FromRoute] string userId , [FromServices] ApplicationDbContext db, [FromServices] SpiderServices spiderServices) { var polygon = db.Polygons; var result = await polygon.FirstOrDefaultAsync(x => x.PolygonId == polygonId); if (result == null) { return new SpiderResponse() { IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = false }; } var user = await spiderServices.GetUsers(new UserQuery() { Condition = { Id = userId } }); result.UserId = userId; var currentUser = user.Result?.Records.FirstOrDefault(); if (currentUser != null) { result.UserName = currentUser.UserRealName; result.PhoneNumber = currentUser.UserPhoneNumber; } polygon.Update(result); await db.SaveChangesAsync(); return new SpiderResponse() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新区域信息成功", Result = true }; } [HttpGet("/api/Polygon/others")] public async Task>>>> GetAllPoints([FromQuery] Guid? polygonId , [FromServices] ApplicationDbContext db) { var polygon = db.Polygons; if (polygonId.HasValue) { var result = await polygon.Where(x => x.PolygonId != polygonId).ToListAsync(); var points = result.Select(x => x.Points).ToList(); return new SpiderResponse>>>() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = points }; } else { var result = await polygon.ToListAsync(); var points = result.Select(x => x.Points).ToList(); return new SpiderResponse>>>() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = points }; } } [HttpGet("/api/Polygon/{polygonId}/InRangeCameras")] public async IAsyncEnumerable IsRangeInPolygon([FromRoute] Guid polygonId, [FromServices] ApplicationDbContext db, [FromServices] SpiderServices spiderServices) { var polygon = db.Polygons; var result = await polygon.FirstOrDefaultAsync(x => x.PolygonId == polygonId); if (result != null) { var cameras = await spiderServices.GetCameras(); foreach (var x in cameras.Result?.Records.Select(x => (x.name, new Points(x.lon, x.lat))).ToList()!) { if (x.Item2.IsPointInsidePolygon(result)) { yield return x.name; } } } } [HttpGet("/api/Camera/All")] public async Task>> GetCameras( [FromServices] SpiderServices spiderServices) { return await spiderServices.GetCameras(); } [HttpGet("/api/Setting")] public async Task> GetClientOptions([FromServices] SettingServices settingServices) { return new SpiderResponse() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取配置成功", Result = await settingServices.GetClientOptions() }; } [HttpPost("/api/Setting")] public async Task> SettingClientOptions([FromBody] ClientOptionsReq options, [FromServices] SettingServices settingServices) { var setting = await settingServices.GetClientOptions(); setting.Delay = options.Delay; setting.DispatchingRunning = options.DispatchingRunning; setting.CloseFileRunning = options.CloseFileRunning; setting.ApiGateway = options.ApiGateway; if (!string.IsNullOrWhiteSpace(options.UserName)) setting.UserName = options.UserName; if (!string.IsNullOrWhiteSpace(options.Password)) setting.Password = options.Password; var resp = settingServices.SettingClientOptions(setting); return new SpiderResponse() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取配置成功", Result = settingServices.SettingClientOptions(resp) }; } [HttpGet("/api/Log")] public SpiderResponse> GetLog([FromQuery] PageRequest request, [FromServices] ApplicationDbContext db) { var collection = db.LogInfos; var logs = collection .OrderByDescending(x => x.CreateTime) .Skip((request.Page - 1) * request.PageSize) .Take(request.PageSize) .ToList(); var total = collection.Count(); return new SpiderResponse>() { IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取日志成功", Result = new PageResponse() { Data = logs.ToList(), Page = request.Page, PageSize = request.PageSize, Total = total } }; } }