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.Extensions; namespace YLShipBuildLandMap.Services.Build { public class RoomService : IRoomService { private YLShipBuildLandMapContext DbContext { get; set; } public RoomService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } private IQueryable GetRoomQuery(Expression> exp, Expression> expBuild) { var query = from b in DbContext.BdBuilding.Where(expBuild) join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId join r in DbContext.BdBuildingFloorRoom.Where(exp) on f.BuildingFloorId equals r.BuildingFloorId select new RoomView { BuildingFloorRoomId = r.BuildingFloorRoomId, BuildingFloorId = r.BuildingFloorId, BuildingId = b.BuildingId, Name = r.Name, CreateTime = r.CreateTime, CreateUserId = r.CreateUserId, ModifyTime = r.ModifyTime, ModifyUserId = r.ModifyUserId, RecordStatus = r.RecordStatus, Sort = r.Sort, BuildingName = b.Name, FloorName = f.Name, FloorSort = f.Sort }; return query; } public IGridResultSet GetRoomList(int pageIndex, int pageSize, Guid? buildingId, Guid? floorId) { Expression> exp = e => true; Expression> expBuild = e => true; exp = exp.AndIf(floorId.HasValue, e => e.BuildingFloorId == floorId); expBuild = expBuild.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId); var query = GetRoomQuery(exp, expBuild).OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Name); return query.ToGridResultSet(pageIndex, pageSize); } public Task GetRoomById(Guid? roomId) { if (roomId.HasValue) { return Task.FromResult(GetRoomQuery(e => e.BuildingFloorRoomId == roomId, e => true).FirstOrDefault()); } else { return Task.FromResult(new RoomView { BuildingFloorRoomId = Guid.NewGuid() }); } } public async Task Save(RoomView data, Guid? userId) { BdBuildingFloorRoom dbData = DbContext.BdBuildingFloorRoom.FirstOrDefault(e => e.BuildingFloorRoomId == data.BuildingFloorRoomId); if (dbData != null) { dbData.Name = data.Name; dbData.BuildingFloorId = data.BuildingFloorId; dbData.ModifyTime = DateTime.Now; dbData.ModifyUserId = userId; dbData.Sort = data.Sort; DbContext.BdBuildingFloorRoom.Update(dbData); } else { dbData = new BdBuildingFloorRoom { BuildingFloorRoomId = data.BuildingFloorRoomId, Name = data.Name, BuildingFloorId = data.BuildingFloorId, CreateTime = DateTime.Now, CreateUserId = userId, RecordStatus = 1, Sort = data.Sort }; this.DbContext.Add(dbData); } return await this.DbContext.SaveChangesAsync(); } public async Task Delete(List roomIdList) { await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync(); return await this.DbContext.SaveChangesAsync(); } public Task> GetRoomDepOrServiceList() { var query = from b in DbContext.BdBuilding join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId join r in DbContext.BdBuildingFloorRoom on f.BuildingFloorId equals r.BuildingFloorId join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on r.BuildingFloorRoomId equals rds.BuildingFloorRoomId join ds in DbContext.BdDepartmentOrServicePoint on rds.DepartmentOrServicePointId equals ds.DepartmentOrServicePointId select new RoomDepOrServiceView { BuildingId = b.BuildingId, BuildingName = b.Name, FloorName = f.Name, FloorSort = f.Sort, RoomName = r.Name, BuildingFloorId = r.BuildingFloorId, BuildingFloorRoomId = r.BuildingFloorRoomId, DepartmentOrServicePointId = ds.DepartmentOrServicePointId, DepartmentOrServiceName = ds.Name, Sort = r.Sort, OpenTime = ds.OpenTime, ContactType = ds.ContactType, Description = ds.Description }; var list = query.OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.FloorName).ThenBy(e => e.Sort).ThenBy(e => e.RoomName).ToList(); var depOrServicePointIdList = list.Select(s => s.DepartmentOrServicePointId).ToList(); var fileList = this.DbContext.SysAttachment.Where(e => depOrServicePointIdList.Contains((Guid)e.ReferenceId) && e.Type == 1).ToList(); list.ForEach(item => { item.ImgUrl = fileList.Where(e => e.ReferenceId == item.DepartmentOrServicePointId).FirstOrDefault()?.Url; }); return Task.FromResult(list); } } }