123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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<BuildingView> GetBuildingQuery(Expression<Func<BdBuilding, bool>> 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<List<BuildingView>> GetBuildingList()
- {
- return Task.FromResult(GetBuildingQuery(e => true).OrderBy(e => e.Name).ToList());
- }
- public Task<List<FloorView>> GetFloorList(Guid? buildingId)
- {
- Expression<Func<BdBuildingFloor, bool>> 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<BuildingView> 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<int> DeleteBuilding(List<Guid> 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<int> 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<int> SaveBuilding(BuildingView data, List<FloorView> 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<List<BuildingXyAxisView>> GetBuildingXYAxisList(Guid? buildingId)
- {
- Expression<Func<BdBuilding, bool>> 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<List<BuildingXyAxisGroupView>> GetBuildingXyAxisGroupList(Guid? buildingId)
- {
- Expression<Func<BdBuilding, bool>> 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());
- }
- }
- }
|