ly/Service/PositionService.cs

169 lines
6.2 KiB
C#
Raw Permalink Normal View History

2025-03-22 12:16:22 +00:00
using LY.App.Common;
2025-04-22 13:06:17 +00:00
using LY.App.Common.Redis;
2025-03-22 12:16:22 +00:00
using LY.App.Extensions.DI;
using LY.App.Model;
using Mapster;
using Microsoft.AspNetCore.Http;
using NetTopologySuite.Geometries;
using NetTopologySuite.GeometriesGraph;
2025-04-25 17:13:29 +00:00
using NetTopologySuite.Index.HPRtree;
2025-03-22 12:16:22 +00:00
using SqlSugar;
using System.Xml.Schema;
namespace LY.App.Service
{
/// <summary>
/// 阵地服务
/// </summary>
[ServiceInjection(InjectionType.Transient)]
public class PositionService
{
private readonly SqlSugarClient _db;
2025-04-22 13:06:17 +00:00
private readonly RedisService _redisService;
public PositionService(SqlSugarClient db, RedisService redisService)
2025-03-22 12:16:22 +00:00
{
_db = db;
2025-04-22 13:06:17 +00:00
_redisService = redisService;
2025-03-22 12:16:22 +00:00
}
/// <summary>
/// 添加
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<ApiResult> MainAdd(AddPosition input)
{
var entity = input.Adapt<PositionInfo>();
entity.CreateTime = DateTime.Now;
//名称重复判断
var isNameExist = await _db.Queryable<PositionInfo>().CountAsync(a => a.Name == input.Name && a.IsDeleted != true);
if (isNameExist > 0)
{
return new ApiResult(false, "名称已存在");
}
if (!string.IsNullOrWhiteSpace(entity.RegionJson))
{
if (GeoJsonHelper.TryGetGeomWKTFromGeoJson(entity.RegionJson, out MultiPolygon geo))
{
entity.SetRegion(geo);
}
else
{
return new ApiResult(false, "空间数据无效");
}
}
// 位置
await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
2025-04-25 17:13:29 +00:00
var key = RedisKeyList.PositioinRegion(entity.Id);
await _redisService.SetAsync(key, entity);
2025-03-22 12:16:22 +00:00
return new ApiResult(true, "添加成功");
}
public async Task<ApiResult> Update(UpdatePosition input)
{
var entity = input.Adapt<PositionInfo>();
await _db.Queryable<PositionInfo>().FirstAsync(a => a.Id == input.Id && a.IsDeleted != true);
if (entity != null)
{
if (!string.IsNullOrWhiteSpace(entity.RegionJson))
{
if (GeoJsonHelper.TryGetGeomWKTFromGeoJson(entity.RegionJson, out MultiPolygon geo))
{
entity.SetRegion(geo);
}
else
{
return new ApiResult(false, "空间数据无效");
}
}
await _db.Updateable(entity).ExecuteCommandAsync();
2025-04-25 17:13:29 +00:00
var key = RedisKeyList.PositioinRegion(entity.Id);
await _redisService.SetAsync(key, entity);
2025-03-22 12:16:22 +00:00
return new ApiResult(true, "添加成功");
}
return new ApiResult(false, "未找到要更新的对象");
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult> Delete(long id)
{
var entity = await _db.Queryable<PositionInfo>().FirstAsync(a => a.Id == id && a.IsDeleted != true);
if (entity != null)
{
entity.IsDeleted = true;
await _db.Updateable(entity).ExecuteCommandAsync();
2025-04-25 17:13:29 +00:00
var key = RedisKeyList.PositioinRegion(entity.Id);
await _redisService.DeleteAsync(key);
2025-03-22 12:16:22 +00:00
return new ApiResult(true, "删除成功");
}
return new ApiResult(false, "未找到要删除的对象");
}
/// <summary>
/// 获取
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiResult> Get(int id)
{
var entity = await _db.Queryable<PositionInfo>().FirstAsync(a => a.Id == id && a.IsDeleted != true);
if (entity != null)
{
return new ApiResult() { data = entity };
}
return new ApiResult(false, "未找到要获取的对象");
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<ApiResult> GetList(PositionQueryInput input)
{
var query = _db.Queryable<PositionInfo>()
.Where(a => a.IsDeleted != true)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), a => a.Name.Contains(input.Name))
.OrderBy(a => a.Id, OrderByType.Desc);
var result = await query.ToPageListAsync(input.pageNum, input.pageSize);
2025-03-28 15:36:56 +00:00
result.ForEach(a =>
{
a.SetRegionJson();
});
2025-03-22 12:16:22 +00:00
return new ApiResult()
{
data = new
{
items = result,
totalCount = query.Count()
}
};
}
/// <summary>
/// 首页数据
/// </summary>
/// <returns></returns>
public async Task<List<PositionDeviceDto>> Index()
{
var query = await _db.Queryable<PositionInfo>()
.Where(a => a.IsDeleted != true).ToListAsync();
var positionIds = query.Select(s => s.Id).ToList();
var deviceList = await _db.Queryable<DeviceEntity>()
.Where(s => positionIds.Contains(s.PositionId))
.Where(s => s.IsDeleted == false)
.ToListAsync();
2025-03-27 16:34:20 +00:00
List<PositionDeviceDto> result = query.Adapt<List<PositionDeviceDto>>();
foreach (var item in result)
2025-03-22 12:16:22 +00:00
{
2025-03-28 15:36:56 +00:00
item.SetRegionJson();
2025-03-27 16:34:20 +00:00
item.Devices = deviceList.Where(s => s.PositionId == item.Id).ToList()?.Adapt<List<DeviceItem>>() ?? new List<DeviceItem>();
2025-04-22 13:06:17 +00:00
item.Devices.ForEach(async s =>
{
s.IsOnline = await _redisService.ExistsAsync(RedisKeyList.DeviceStatus(s.DeviceSN));
});
2025-03-22 12:16:22 +00:00
}
return result;
}
}
}