275 lines
9.7 KiB
C#
275 lines
9.7 KiB
C#
using AutoDispathingWork.Utils;
|
|
using LiteDB;
|
|
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<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] 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<string>();
|
|
item.RangeCameras?.Add(camera);
|
|
}
|
|
}
|
|
|
|
return new SpiderResponse<List<Polygon>>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "", Result = result
|
|
};
|
|
}
|
|
|
|
|
|
[HttpPost("/api/Polygon")]
|
|
public async Task<SpiderResponse<bool>> 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<bool>
|
|
{
|
|
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<bool>
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新成功", Result = true
|
|
};
|
|
}
|
|
|
|
|
|
[HttpDelete("/api/Polygon/{polygonId}")]
|
|
public async Task<SpiderResponse<bool>> 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<bool>()
|
|
{
|
|
IsSuccess = false, Code = SpiderResponseCode.Fail, Message = "未找到该区域", Result = false
|
|
};
|
|
}
|
|
|
|
polygon.Remove(result);
|
|
await db.SaveChangesAsync();
|
|
return new SpiderResponse<bool>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "删除区域信息成功", Result = true
|
|
};
|
|
}
|
|
|
|
[HttpGet("/api/Polygon/{polygonId}")]
|
|
public async Task<SpiderResponse<Polygon>> 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<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 async Task<SpiderResponse<bool>> 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<bool>()
|
|
{
|
|
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<bool>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "更新区域信息成功", Result = true
|
|
};
|
|
}
|
|
|
|
[HttpGet("/api/Polygon/others")]
|
|
public async Task<SpiderResponse<List<List<List<Points>>>>> 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<List<List<List<Points>>>>()
|
|
{
|
|
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<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] 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<SpiderResponse<PageData<CameraItem>>> GetCameras(
|
|
[FromServices] SpiderServices spiderServices)
|
|
{
|
|
return await spiderServices.GetCameras();
|
|
}
|
|
|
|
[HttpGet("/api/Setting")]
|
|
public SpiderResponse<ClientOptions> GetClientOptions([FromServices] SettingServices settingServices)
|
|
{
|
|
return new SpiderResponse<ClientOptions>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取配置成功",
|
|
Result = settingServices.GetClientOptions()
|
|
};
|
|
}
|
|
|
|
[HttpPost("/api/Setting")]
|
|
public SpiderResponse<ClientOptions> SettingClientOptions([FromBody] ClientOptionsReq options,
|
|
[FromServices] SettingServices settingServices)
|
|
{
|
|
var setting = 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<ClientOptions>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取配置成功",
|
|
Result = settingServices.SettingClientOptions(resp)
|
|
};
|
|
}
|
|
|
|
[HttpGet("/api/Log")]
|
|
public SpiderResponse<PageResponse<LogInfo>> 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<PageResponse<LogInfo>>()
|
|
{
|
|
IsSuccess = true, Code = SpiderResponseCode.Success, Message = "获取日志成功",
|
|
Result = new PageResponse<LogInfo>()
|
|
{
|
|
Data = logs.ToList(),
|
|
Page = request.Page,
|
|
PageSize = request.PageSize,
|
|
Total = total
|
|
}
|
|
};
|
|
}
|
|
} |