AutoDispatching/AutoDispathingWork/Controllers/UserController.cs
2023-11-19 02:18:27 +08:00

198 lines
6.9 KiB
C#

using AutoDispathingWork.Utils;
using LiteDB;
using Microsoft.AspNetCore.Mvc;
using WorkerService1.Domains;
using WorkerService1.Dto;
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<IActionResult> GetPageList([FromQuery] UserQuery request,
[FromServices] SpiderServices spiderServices,
[FromServices] LiteDatabase liteDatabase)
{
var res = await spiderServices.GetUsers(request);
return Ok(res);
}
[HttpGet("/api/Polygon/Pages")]
public async Task<SpiderResponse<List<Polygon>>> GetPolygon([FromQuery] PageRequest request,
[FromServices] LiteDatabase db, [FromServices] SpiderServices spiderServices)
{
var polygon = db.GetCollection<Polygon>();
var result = polygon.FindAll().ToList();
var cameras = await spiderServices.GetCameras();
foreach (var item in result)
{
// item.RangeCameras ??= new List<string>();
foreach (var x in cameras.Result?.Records.Select(x =>
{
var tempGps = GpsUtil.Bd09ToGcj02(x.lon, x.lat);
return (x.name, new Points(tempGps[0], tempGps[1]));
}).ToList()!.Where(x => x.Item2.IsPointInsidePolygon(item))!)
{
item.RangeCameras ??= new List<string>();
item.RangeCameras?.Add(x.name);
}
}
return new SpiderResponse<List<Polygon>>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "", Result = result
};
}
[HttpPost("/api/Polygon")]
public SpiderResponse<bool> CreateOrUpdatePolygon([FromBody] Polygon request, [FromServices] LiteDatabase db)
{
var polygon = db.GetCollection<Polygon>();
if (request.PolygonId == null)
{
request.PolygonId = Guid.NewGuid();
var result = polygon.Insert(request);
return new SpiderResponse<bool>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "保存成功", Result = true
};
}
else
{
var old = polygon.FindOne(x => x.PolygonId == request.PolygonId);
old.Points = request.Points;
old.Name = request.Name;
polygon.Update(old);
return new SpiderResponse<bool>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新成功", Result = true
};
}
}
[HttpDelete("/api/Polygon/{polygonId}")]
public SpiderResponse<bool> DeletePolygon([FromRoute] Guid polygonId, [FromServices] LiteDatabase db)
{
var polygon = db.GetCollection<Polygon>();
var result = polygon.FindById(polygonId);
if (result == null)
{
return new SpiderResponse<bool>()
{
IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = false
};
}
polygon.Delete(polygonId);
return new SpiderResponse<bool>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "删除区域信息成功", Result = true
};
}
[HttpGet("/api/Polygon/{polygonId}")]
public SpiderResponse<Polygon> GetPolygon([FromRoute] Guid polygonId, [FromServices] LiteDatabase db)
{
var polygon = db.GetCollection<Polygon>();
var result = polygon.FindById(polygonId);
if (result == null)
{
return new SpiderResponse<Polygon>()
{
IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = null
};
}
return new SpiderResponse<Polygon>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = result
};
}
[HttpPut("/api/Polygon/{polygonId}/UserId/{userId}")]
public SpiderResponse<bool> BindPolygonUserId([FromRoute] Guid polygonId, [FromRoute] string userId,
[FromServices] LiteDatabase db)
{
var polygon = db.GetCollection<Polygon>();
var result = polygon.FindOne(x => x.PolygonId == polygonId);
if (result == null)
{
return new SpiderResponse<bool>()
{
IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = false
};
}
result.UserId = userId;
polygon.Update(result);
return new SpiderResponse<bool>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新区域信息成功", Result = true
};
}
[HttpGet("/api/Polygon/others")]
public SpiderResponse<List<List<List<Points>>>> GetAllPoints([FromQuery] Guid? polygonId,
[FromServices] LiteDatabase db)
{
var polygon = db.GetCollection<Polygon>();
if (polygonId.HasValue)
{
var result = polygon.Find(x => x.PolygonId != polygonId).ToList();
var points = result.Select(x => x.Points).ToList();
return new SpiderResponse<List<List<List<Points>>>>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = points
};
}
else
{
var result = polygon.FindAll().ToList();
var points = result.Select(x => x.Points).ToList();
return new SpiderResponse<List<List<List<Points>>>>()
{
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取区域信息成功", Result = points
};
}
}
[HttpGet("/api/Polygon/{polygonId}/InRangeCameras")]
public async IAsyncEnumerable<string> IsRangeInPolygon([FromRoute] Guid polygonId,
[FromServices] LiteDatabase db, [FromServices] SpiderServices spiderServices)
{
var polygon = db.GetCollection<Polygon>();
var result = polygon.FindOne(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<SpiderResponse<PageData<CameraItem>>> GetCameras(
[FromServices] SpiderServices spiderServices)
{
return await spiderServices.GetCameras();
}
}