RoomService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Bowin.Common.Linq.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using YLShipBuildLandMap.Entity;
  7. using YLShipBuildLandMap.Entity.ViewModel.Build;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Z.EntityFramework.Plus;
  13. using YLShipBuildLandMap.Entity.Extensions;
  14. namespace YLShipBuildLandMap.Services.Build
  15. {
  16. public class RoomService : IRoomService
  17. {
  18. private YLShipBuildLandMapContext DbContext { get; set; }
  19. public RoomService(YLShipBuildLandMapContext dbContext)
  20. {
  21. DbContext = dbContext;
  22. }
  23. private IQueryable<RoomView> GetRoomQuery(Expression<Func<BdBuildingFloorRoom, bool>> exp, Expression<Func<BdBuilding, bool>> expBuild)
  24. {
  25. var query = from b in DbContext.BdBuilding.Where(expBuild)
  26. join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId
  27. join r in DbContext.BdBuildingFloorRoom.Where(exp) on f.BuildingFloorId equals r.BuildingFloorId
  28. select new RoomView
  29. {
  30. BuildingFloorRoomId = r.BuildingFloorRoomId,
  31. BuildingFloorId = r.BuildingFloorId,
  32. BuildingId = b.BuildingId,
  33. Name = r.Name,
  34. CreateTime = r.CreateTime,
  35. CreateUserId = r.CreateUserId,
  36. ModifyTime = r.ModifyTime,
  37. ModifyUserId = r.ModifyUserId,
  38. RecordStatus = r.RecordStatus,
  39. Sort = r.Sort,
  40. BuildingName = b.Name,
  41. FloorName = f.Name,
  42. FloorSort = f.Sort
  43. };
  44. return query;
  45. }
  46. public IGridResultSet<RoomView> GetRoomList(int pageIndex, int pageSize, Guid? buildingId, Guid? floorId)
  47. {
  48. Expression<Func<BdBuildingFloorRoom, bool>> exp = e => true;
  49. Expression<Func<BdBuilding, bool>> expBuild = e => true;
  50. exp = exp.AndIf(floorId.HasValue, e => e.BuildingFloorId == floorId);
  51. expBuild = expBuild.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId);
  52. var query = GetRoomQuery(exp, expBuild).OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Name);
  53. return query.ToGridResultSet(pageIndex, pageSize);
  54. }
  55. public Task<RoomView> GetRoomById(Guid? roomId)
  56. {
  57. if (roomId.HasValue)
  58. {
  59. return Task.FromResult(GetRoomQuery(e => e.BuildingFloorRoomId == roomId, e => true).FirstOrDefault());
  60. }
  61. else
  62. {
  63. return Task.FromResult(new RoomView { BuildingFloorRoomId = Guid.NewGuid() });
  64. }
  65. }
  66. public async Task<int> Save(RoomView data, Guid? userId)
  67. {
  68. BdBuildingFloorRoom dbData = DbContext.BdBuildingFloorRoom.FirstOrDefault(e => e.BuildingFloorRoomId == data.BuildingFloorRoomId);
  69. if (dbData != null)
  70. {
  71. dbData.Name = data.Name;
  72. dbData.BuildingFloorId = data.BuildingFloorId;
  73. dbData.ModifyTime = DateTime.Now;
  74. dbData.ModifyUserId = userId;
  75. dbData.Sort = data.Sort;
  76. DbContext.BdBuildingFloorRoom.Update(dbData);
  77. }
  78. else
  79. {
  80. dbData = new BdBuildingFloorRoom
  81. {
  82. BuildingFloorRoomId = data.BuildingFloorRoomId,
  83. Name = data.Name,
  84. BuildingFloorId = data.BuildingFloorId,
  85. CreateTime = DateTime.Now,
  86. CreateUserId = userId,
  87. RecordStatus = 1,
  88. Sort = data.Sort
  89. };
  90. this.DbContext.Add(dbData);
  91. }
  92. return await this.DbContext.SaveChangesAsync();
  93. }
  94. public async Task<int> Delete(List<Guid> roomIdList)
  95. {
  96. await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  97. await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  98. return await this.DbContext.SaveChangesAsync();
  99. }
  100. public Task<List<RoomDepOrServiceView>> GetRoomDepOrServiceList()
  101. {
  102. var query =
  103. from b in DbContext.BdBuilding
  104. join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId
  105. join r in DbContext.BdBuildingFloorRoom on f.BuildingFloorId equals r.BuildingFloorId
  106. join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on r.BuildingFloorRoomId equals rds.BuildingFloorRoomId
  107. join ds in DbContext.BdDepartmentOrServicePoint on rds.DepartmentOrServicePointId equals ds.DepartmentOrServicePointId
  108. select new RoomDepOrServiceView
  109. {
  110. BuildingId = b.BuildingId,
  111. BuildingName = b.Name,
  112. FloorName = f.Name,
  113. FloorSort = f.Sort,
  114. RoomName = r.Name,
  115. BuildingFloorId = r.BuildingFloorId,
  116. BuildingFloorRoomId = r.BuildingFloorRoomId,
  117. DepartmentOrServicePointId = ds.DepartmentOrServicePointId,
  118. DepartmentOrServiceName = ds.Name,
  119. Sort = r.Sort,
  120. OpenTime = ds.OpenTime,
  121. ContactType = ds.ContactType,
  122. Description = ds.Description
  123. };
  124. var list = query.OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.FloorName).ThenBy(e => e.Sort).ThenBy(e => e.RoomName).ToList();
  125. var depOrServicePointIdList = list.Select(s => s.DepartmentOrServicePointId).ToList();
  126. var fileList = this.DbContext.SysAttachment.Where(e => depOrServicePointIdList.Contains((Guid)e.ReferenceId) && e.Type == 1).ToList();
  127. list.ForEach(item =>
  128. {
  129. item.ImgUrl = fileList.Where(e => e.ReferenceId == item.DepartmentOrServicePointId).FirstOrDefault()?.Url;
  130. });
  131. return Task.FromResult(list);
  132. }
  133. }
  134. }