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 { /// /// 阵地服务 /// [ServiceInjection(InjectionType.Transient)] public class PositionService { private readonly SqlSugarClient _db; public PositionService(SqlSugarClient db) { _db = db; } /// /// 添加 /// /// /// public async Task MainAdd(AddPosition input) { var entity = input.Adapt(); entity.CreateTime = DateTime.Now; //名称重复判断 var isNameExist = await _db.Queryable().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 Update(UpdatePosition input) { var entity = input.Adapt(); await _db.Queryable().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, "未找到要更新的对象"); } /// /// /// /// /// public async Task Delete(long id) { var entity = await _db.Queryable().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, "未找到要删除的对象"); } /// /// 获取 /// /// /// public async Task Get(int id) { var entity = await _db.Queryable().FirstAsync(a => a.Id == id && a.IsDeleted != true); if (entity != null) { return new ApiResult() { data = entity }; } return new ApiResult(false, "未找到要获取的对象"); } /// /// 获取列表 /// /// /// public async Task GetList(PositionQueryInput input) { var query = _db.Queryable() .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() } }; } /// /// 首页数据 /// /// public async Task> Index() { var query = await _db.Queryable() .Where(a => a.IsDeleted != true).ToListAsync(); var positionIds = query.Select(s => s.Id).ToList(); var deviceList = await _db.Queryable() .Where(s => positionIds.Contains(s.PositionId)) .Where(s => s.IsDeleted == false) .ToListAsync(); List result = query.Adapt>(); foreach (var item in result) { item.Devices = deviceList.Where(s => s.PositionId == item.Id).ToList()?.Adapt>() ?? new List(); } return result; } } }