using Bowin.Common.Linq.Entity; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel.Build; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Entity.Extensions; namespace YLShipBuildLandMap.Services.Build { public class BuildingService : IBuildingService { private YLShipBuildLandMapContext DbContext { get; set; } public BuildingService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } private IQueryable GetBuildingQuery(Expression> exp) { var query = from b in DbContext.BdBuilding.Where(exp) /*join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId into dbFloor from f in dbFloor.DefaultIfEmpty() group new { floorName = f.Name } by new { b } into g select new BuildingView { BuildingId = g.Key.b.BuildingId, Name = g.Key.b.Name, RecordStatus = g.Key.b.RecordStatus, CreateUserId = g.Key.b.CreateUserId, CreateTime = g.Key.b.CreateTime, ModifyTime = g.Key.b.ModifyTime, ModifyUserId = g.Key.b.ModifyUserId, FloorList = string.Join(",", g.Select(s => s.floorName)), };*/ select new BuildingView { BuildingId = b.BuildingId, Name = b.Name, RecordStatus = b.RecordStatus, CreateUserId = b.CreateUserId, CreateTime = b.CreateTime, ModifyTime = b.ModifyTime, ModifyUserId = b.ModifyUserId, FloorList = string.Join("、", b.BdBuildingFloor.OrderBy(f => f.Sort).Select(s => s.Name)) }; return query; } public Task> GetBuildingList() { return Task.FromResult(GetBuildingQuery(e => true).OrderBy(e => e.Name).ToList()); } public Task> GetFloorList(Guid? buildingId) { Expression> exp = e => true; exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId); var query = from f in DbContext.BdBuildingFloor.Where(exp) join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId select new FloorView { BuildingFloorId = f.BuildingFloorId, Name = f.Name, BuildingName = b.Name, Sort = f.Sort, RecordStatus = f.RecordStatus, BuildingId = f.BuildingId }; return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.Sort).ThenBy(e => e.Name).ToList()); } public Task GetBuildingById(Guid? buildingId) { if (!buildingId.HasValue) return Task.FromResult(new BuildingView { BuildingId = Guid.NewGuid() }); return Task.FromResult(GetBuildingQuery(e => e.BuildingId == buildingId).FirstOrDefault()); } public async Task DeleteBuilding(List buildingIdList) { var floorIdList = DbContext.BdBuildingFloor.Where(e => buildingIdList.Contains((Guid)e.BuildingId)).Select(s => s.BuildingFloorId).ToList(); var roomIdList = DbContext.BdBuildingFloorRoom.Where(e => floorIdList.Contains(e.BuildingFloorRoomId)).Select(s => s.BuildingFloorRoomId).ToList(); await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); await DbContext.BdBuildingFloor.Where(e => floorIdList.Contains(e.BuildingFloorId)).DeleteAsync(); await DbContext.BdBuilding.Where(e => buildingIdList.Contains(e.BuildingId)).DeleteAsync(); await DbContext.BdBuildingXyAxis.Where(e => buildingIdList.Contains((Guid)e.BuildingId)).DeleteAsync(); return await this.DbContext.SaveChangesAsync(); } public async Task DeleteFloor(Guid buildingFloorId) { var roomIdList = DbContext.BdBuildingFloorRoom.Where(e => e.BuildingFloorId == buildingFloorId).Select(s => s.BuildingFloorRoomId).ToList(); await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); await DbContext.BdBuildingFloor.Where(e => e.BuildingFloorId == buildingFloorId).DeleteAsync(); return await this.DbContext.SaveChangesAsync(); } public async Task SaveBuilding(BuildingView data, List floorList, Guid? userId) { BdBuilding dbData = DbContext.BdBuilding.FirstOrDefault(e => e.BuildingId == data.BuildingId); if (dbData != null) { dbData.Name = data.Name; dbData.ModifyTime = DateTime.Now; dbData.ModifyUserId = userId; DbContext.BdBuilding.Update(dbData); } else { dbData = new BdBuilding { BuildingId = data.BuildingId, Name = data.Name, CreateTime = DateTime.Now, CreateUserId = userId }; this.DbContext.Add(dbData); } floorList.ForEach(floor => { var dbFloor = this.DbContext.BdBuildingFloor.Where(e => e.BuildingFloorId == floor.BuildingFloorId).FirstOrDefault(); if (dbFloor == null) { dbFloor = new BdBuildingFloor { BuildingFloorId = Guid.NewGuid(), BuildingId = dbData.BuildingId, Name = floor.Name, Sort = floor.Sort, CreateTime = DateTime.Now, CreateUserId = userId }; this.DbContext.BdBuildingFloor.Add(dbFloor); } else { dbFloor.Sort = floor.Sort; dbFloor.Name = floor.Name; dbFloor.ModifyTime = DateTime.Now; dbFloor.ModifyUserId = userId; DbContext.BdBuildingFloor.Update(dbFloor); } }); return await this.DbContext.SaveChangesAsync(); } public Task> GetBuildingXYAxisList(Guid? buildingId) { Expression> exp = e => true; exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId); var query = from b in DbContext.BdBuilding.Where(exp) join xy in DbContext.BdBuildingXyAxis on b.BuildingId equals xy.BuildingId select new BuildingXyAxisView { BuildingId = b.BuildingId, XyaxisId = xy.XyaxisId, BuildingName = b.Name, Xaxis = xy.Xaxis, Yaxis = xy.Yaxis }; return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.Xaxis).ThenBy(e => e.Yaxis).ToList()); } public Task> GetBuildingXyAxisGroupList(Guid? buildingId) { Expression> exp = e => true; exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId); var query = from b in DbContext.BdBuilding.Where(exp) join xy in DbContext.BdBuildingXyAxis on b.BuildingId equals xy.BuildingId group new { xy } by new { b.BuildingId, b.Name } into g select new BuildingXyAxisGroupView { BuildingId = g.Key.BuildingId, BuildingName = g.Key.Name, MinXAxis = g.Min(e => e.xy.Xaxis), MaxXAxis = g.Max(e => e.xy.Xaxis), MinYAxis = g.Min(e => e.xy.Yaxis), MaxYAxis = g.Max(e => e.xy.Yaxis), }; return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.MinXAxis).ThenBy(e => e.MinYAxis).ToList()); } } }