using System; using System.Collections.Generic; using System.Text; using YLShipBuildLandMap.Entity; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; using YLShipBuildLandMap.Entity.Extensions; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity.ViewModel.Build; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity.ViewModel; namespace YLShipBuildLandMap.Services.Build { public class DepOrServicePointService : IDepOrServicePointService { private YLShipBuildLandMapContext DbContext { get; set; } public DepOrServicePointService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } private IQueryable GetDepOrServicePointQuery(Expression> exp) { var query = from ds in DbContext.BdDepartmentOrServicePoint.Where(exp) /* join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on ds.DepartmentOrServicePointId equals rds.DepartmentOrServicePointId into dbBdDepartmentOrServicePointBmBuildingFloorRoom from rds in dbBdDepartmentOrServicePointBmBuildingFloorRoom.DefaultIfEmpty() join r in DbContext.BdBuildingFloorRoom on rds.BuildingFloorRoomId equals r.BuildingFloorRoomId into dbRoom from r in dbRoom.DefaultIfEmpty() join f in DbContext.BdBuildingFloor on r.BuildingFloorId equals f.BuildingFloorId into dbFloor from f in dbFloor.DefaultIfEmpty() join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId into dbBuild from b in dbBuild.DefaultIfEmpty()*/ join room in ( 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 select new { rds.DepartmentOrServicePointId, f.BuildingFloorId, FloorName = f.Name, b.BuildingId, BuildingName = b.Name, RoomName = r.Name, RoomSort = r.Sort, FloorSort = f.Sort } ) on ds.DepartmentOrServicePointId equals room.DepartmentOrServicePointId into dbBdDepartmentOrServicePointBmBuildingFloorRoom from rds in dbBdDepartmentOrServicePointBmBuildingFloorRoom.DefaultIfEmpty() group new { rds.RoomName, rds.RoomSort } by new { ds.DepartmentOrServicePointId, ds.Name, ds.OpenTime, ds.ContactType, ds.Description, ds.RecordStatus, ds.CreateUserId, ds.CreateTime, ds.ModifyTime, ds.ModifyUserId, rds.FloorName, rds.BuildingName, rds.BuildingId, rds.BuildingFloorId, rds.FloorSort } into g select new DepOrServicePointView { DepartmentOrServicePointId = g.Key.DepartmentOrServicePointId, Name = g.Key.Name, OpenTime = g.Key.OpenTime, ContactType = g.Key.ContactType, Description = g.Key.Description, RecordStatus = g.Key.RecordStatus, CreateUserId = g.Key.CreateUserId, CreateTime = g.Key.CreateTime, ModifyTime = g.Key.ModifyTime, ModifyUserId = g.Key.ModifyUserId, BuildingName = g.Key.BuildingName, BuildingId = g.Key.BuildingId, FloorName = g.Key.FloorName, FloorId = g.Key.BuildingFloorId, /* RoomName = string.Join("、", g.Select(s => s.RoomName).ToList())*/ Sort = g.Min(s => s.RoomSort), FloorSort = g.Key.FloorSort }; return query; } private IQueryable GetDepOrServicePointRoomQuery(Expression> exp) { var query = from ds in DbContext.BdDepartmentOrServicePoint join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on ds.DepartmentOrServicePointId equals rds.DepartmentOrServicePointId join r in DbContext.BdBuildingFloorRoom on rds.BuildingFloorRoomId equals r.BuildingFloorRoomId join f in DbContext.BdBuildingFloor on r.BuildingFloorId equals f.BuildingFloorId join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId select new DepOrServicePointRoomView { DepartmentOrServicePointId = ds.DepartmentOrServicePointId, FloorId = f.BuildingFloorId, RoomName = r.Name, RoomSort = r.Sort }; return query; } public IGridResultSet GetDepOrServicePointList(int pageIndex, int pageSize, Guid? buildingId, Guid? floorId, string name) { Expression> exp = e => true; Expression> expBuild = e => true; Expression> expFloor = e => true; exp = exp.AndIf(!string.IsNullOrEmpty(name), e => e.Name.Contains(name.Trim())); expFloor = expFloor.AndIf(floorId.HasValue, e => e.BuildingFloorId == floorId); expBuild = expBuild.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId); var query = GetDepOrServicePointQuery(exp) .WhereIf(floorId.HasValue, e => e.FloorId == floorId) .WhereIf(buildingId.HasValue, e => e.BuildingId == buildingId) .OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Sort).ThenBy(e => e.Name); var dataResult = query.ToGridResultSet(pageIndex, pageSize); var depOrServicePointIdList = dataResult.rows.Select(s => s.DepartmentOrServicePointId).ToList(); var roomList = GetDepOrServicePointRoomQuery(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).ToList(); var fileList = this.DbContext.SysAttachment.Where(e => depOrServicePointIdList.Contains((Guid)e.ReferenceId) && e.Type == 1).ToList(); dataResult.rows.ForEach(item => { item.RoomName = string.Join("、", roomList.Where(e => e.DepartmentOrServicePointId == item.DepartmentOrServicePointId && e.FloorId == item.FloorId).OrderBy(e => e.RoomSort).ThenBy(e => e.RoomName).Select(s => s.RoomName).ToList()); item.ImgUrl = fileList.Where(e => e.ReferenceId == item.DepartmentOrServicePointId).FirstOrDefault()?.Url; }); return dataResult; } public Task GetDepOrServicePointById(Guid? depOrServicePointId) { if (depOrServicePointId.HasValue) { return Task.FromResult(GetDepOrServicePointQuery(e => e.DepartmentOrServicePointId == depOrServicePointId).FirstOrDefault()); } else { return Task.FromResult(new DepOrServicePointView { DepartmentOrServicePointId = Guid.NewGuid() }); } } public async Task Save(DepOrServicePointView data, List roomIdList, Guid? userId) { BdDepartmentOrServicePoint dbData = DbContext.BdDepartmentOrServicePoint.FirstOrDefault(e => e.DepartmentOrServicePointId == data.DepartmentOrServicePointId); if (dbData != null) { dbData.Name = data.Name; dbData.Description = data.Description; dbData.OpenTime = data.OpenTime; dbData.ModifyTime = DateTime.Now; dbData.ModifyUserId = userId; dbData.ContactType = data.ContactType; DbContext.BdDepartmentOrServicePoint.Update(dbData); } else { dbData = new BdDepartmentOrServicePoint { DepartmentOrServicePointId = data.DepartmentOrServicePointId, Name = data.Name, OpenTime = data.OpenTime, Description = data.Description, CreateTime = DateTime.Now, CreateUserId = userId, RecordStatus = 1, ContactType = data.ContactType }; this.DbContext.Add(dbData); } await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => dbData.DepartmentOrServicePointId == e.DepartmentOrServicePointId).DeleteAsync(); this.DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.AddRange(roomIdList.Select(s => new BdDepartmentOrServicePointBmBuildingFloorRoom { BuildingFloorRoomId = s, DepartmentOrServicePointId = dbData.DepartmentOrServicePointId }).ToList()); return await this.DbContext.SaveChangesAsync(); } public async Task Delete(List depOrServicePointIdList) { await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).DeleteAsync(); await DbContext.BdDepartmentOrServicePoint.Where(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).DeleteAsync(); return await this.DbContext.SaveChangesAsync(); } public Task> GetRoomList(Guid depOrServicePointId) { 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 deppoint in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on r.BuildingFloorRoomId equals deppoint.BuildingFloorRoomId where deppoint.DepartmentOrServicePointId == depOrServicePointId 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 Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Name).ToList()); } } }