157 lines
5.5 KiB
C#
157 lines
5.5 KiB
C#
using LY.App.Common;
|
|
using LY.App.Extensions.DI;
|
|
using LY.App.Model;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Http;
|
|
using NetTopologySuite.Geometries;
|
|
using NetTopologySuite.GeometriesGraph;
|
|
using SqlSugar;
|
|
using System.Xml.Schema;
|
|
|
|
namespace LY.App.Service
|
|
{
|
|
/// <summary>
|
|
/// 阵地服务
|
|
/// </summary>
|
|
[ServiceInjection(InjectionType.Transient)]
|
|
public class PositionService
|
|
{
|
|
private readonly SqlSugarClient _db;
|
|
public PositionService(SqlSugarClient db)
|
|
{
|
|
_db = db;
|
|
}
|
|
/// <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, "空间数据无效");
|
|
}
|
|
}
|
|
// 位置
|
|
entity.SetLocation();
|
|
await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
|
|
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, "空间数据无效");
|
|
}
|
|
}
|
|
// 位置
|
|
entity.SetLocation();
|
|
await _db.Updateable(entity).ExecuteCommandAsync();
|
|
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();
|
|
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);
|
|
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();
|
|
List<PositionDeviceDto> result = new List<PositionDeviceDto>();
|
|
foreach (var item in query)
|
|
{
|
|
result.Add(new PositionDeviceDto()
|
|
{
|
|
position = item,
|
|
Devices = deviceList.Where(s => s.PositionId == item.Id).ToList().Adapt<List<DeviceItem>>()
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|