BuildingService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.ViewModel;
  14. using YLShipBuildLandMap.Entity.Extensions;
  15. namespace YLShipBuildLandMap.Services.Build
  16. {
  17. public class BuildingService : IBuildingService
  18. {
  19. private YLShipBuildLandMapContext DbContext { get; set; }
  20. public BuildingService(YLShipBuildLandMapContext dbContext)
  21. {
  22. DbContext = dbContext;
  23. }
  24. private IQueryable<BuildingView> GetBuildingQuery(Expression<Func<BdBuilding, bool>> exp)
  25. {
  26. var query = from b in DbContext.BdBuilding.Where(exp)
  27. /*join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId into dbFloor
  28. from f in dbFloor.DefaultIfEmpty()
  29. group new { floorName = f.Name } by new { b } into g
  30. select new BuildingView
  31. {
  32. BuildingId = g.Key.b.BuildingId,
  33. Name = g.Key.b.Name,
  34. RecordStatus = g.Key.b.RecordStatus,
  35. CreateUserId = g.Key.b.CreateUserId,
  36. CreateTime = g.Key.b.CreateTime,
  37. ModifyTime = g.Key.b.ModifyTime,
  38. ModifyUserId = g.Key.b.ModifyUserId,
  39. FloorList = string.Join(",", g.Select(s => s.floorName)),
  40. };*/
  41. select new BuildingView
  42. {
  43. BuildingId = b.BuildingId,
  44. Name = b.Name,
  45. RecordStatus = b.RecordStatus,
  46. CreateUserId = b.CreateUserId,
  47. CreateTime = b.CreateTime,
  48. ModifyTime = b.ModifyTime,
  49. ModifyUserId = b.ModifyUserId,
  50. FloorList = string.Join("、", b.BdBuildingFloor.OrderBy(f => f.Sort).Select(s => s.Name))
  51. };
  52. return query;
  53. }
  54. public Task<List<BuildingView>> GetBuildingList()
  55. {
  56. return Task.FromResult(GetBuildingQuery(e => true).OrderBy(e => e.Name).ToList());
  57. }
  58. public Task<List<FloorView>> GetFloorList(Guid? buildingId)
  59. {
  60. Expression<Func<BdBuildingFloor, bool>> exp = e => true;
  61. exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId);
  62. var query = from f in DbContext.BdBuildingFloor.Where(exp)
  63. join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId
  64. select new FloorView
  65. {
  66. BuildingFloorId = f.BuildingFloorId,
  67. Name = f.Name,
  68. BuildingName = b.Name,
  69. Sort = f.Sort,
  70. RecordStatus = f.RecordStatus,
  71. BuildingId = f.BuildingId
  72. };
  73. return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.Sort).ThenBy(e => e.Name).ToList());
  74. }
  75. public Task<BuildingView> GetBuildingById(Guid? buildingId)
  76. {
  77. if (!buildingId.HasValue)
  78. return Task.FromResult(new BuildingView { BuildingId = Guid.NewGuid() });
  79. return Task.FromResult(GetBuildingQuery(e => e.BuildingId == buildingId).FirstOrDefault());
  80. }
  81. public async Task<int> DeleteBuilding(List<Guid> buildingIdList)
  82. {
  83. var floorIdList = DbContext.BdBuildingFloor.Where(e => buildingIdList.Contains((Guid)e.BuildingId)).Select(s => s.BuildingFloorId).ToList();
  84. var roomIdList = DbContext.BdBuildingFloorRoom.Where(e => floorIdList.Contains(e.BuildingFloorRoomId)).Select(s => s.BuildingFloorRoomId).ToList();
  85. await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  86. await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  87. await DbContext.BdBuildingFloor.Where(e => floorIdList.Contains(e.BuildingFloorId)).DeleteAsync();
  88. await DbContext.BdBuilding.Where(e => buildingIdList.Contains(e.BuildingId)).DeleteAsync();
  89. await DbContext.BdBuildingXyAxis.Where(e => buildingIdList.Contains((Guid)e.BuildingId)).DeleteAsync();
  90. return await this.DbContext.SaveChangesAsync();
  91. }
  92. public async Task<int> DeleteFloor(Guid buildingFloorId)
  93. {
  94. var roomIdList = DbContext.BdBuildingFloorRoom.Where(e => e.BuildingFloorId == buildingFloorId).Select(s => s.BuildingFloorRoomId).ToList();
  95. await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  96. await DbContext.BdBuildingFloorRoom.Where(e => roomIdList.Contains(e.BuildingFloorRoomId)).DeleteAsync();
  97. await DbContext.BdBuildingFloor.Where(e => e.BuildingFloorId == buildingFloorId).DeleteAsync();
  98. return await this.DbContext.SaveChangesAsync();
  99. }
  100. public async Task<int> SaveBuilding(BuildingView data, List<FloorView> floorList, Guid? userId)
  101. {
  102. BdBuilding dbData = DbContext.BdBuilding.FirstOrDefault(e => e.BuildingId == data.BuildingId);
  103. if (dbData != null)
  104. {
  105. dbData.Name = data.Name;
  106. dbData.ModifyTime = DateTime.Now;
  107. dbData.ModifyUserId = userId;
  108. DbContext.BdBuilding.Update(dbData);
  109. }
  110. else
  111. {
  112. dbData = new BdBuilding
  113. {
  114. BuildingId = data.BuildingId,
  115. Name = data.Name,
  116. CreateTime = DateTime.Now,
  117. CreateUserId = userId
  118. };
  119. this.DbContext.Add(dbData);
  120. }
  121. floorList.ForEach(floor =>
  122. {
  123. var dbFloor = this.DbContext.BdBuildingFloor.Where(e => e.BuildingFloorId == floor.BuildingFloorId).FirstOrDefault();
  124. if (dbFloor == null)
  125. {
  126. dbFloor = new BdBuildingFloor
  127. {
  128. BuildingFloorId = Guid.NewGuid(),
  129. BuildingId = dbData.BuildingId,
  130. Name = floor.Name,
  131. Sort = floor.Sort,
  132. CreateTime = DateTime.Now,
  133. CreateUserId = userId
  134. };
  135. this.DbContext.BdBuildingFloor.Add(dbFloor);
  136. }
  137. else
  138. {
  139. dbFloor.Sort = floor.Sort;
  140. dbFloor.Name = floor.Name;
  141. dbFloor.ModifyTime = DateTime.Now;
  142. dbFloor.ModifyUserId = userId;
  143. DbContext.BdBuildingFloor.Update(dbFloor);
  144. }
  145. });
  146. return await this.DbContext.SaveChangesAsync();
  147. }
  148. public Task<List<BuildingXyAxisView>> GetBuildingXYAxisList(Guid? buildingId)
  149. {
  150. Expression<Func<BdBuilding, bool>> exp = e => true;
  151. exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId);
  152. var query = from b in DbContext.BdBuilding.Where(exp)
  153. join xy in DbContext.BdBuildingXyAxis on b.BuildingId equals xy.BuildingId
  154. select new BuildingXyAxisView
  155. {
  156. BuildingId = b.BuildingId,
  157. XyaxisId = xy.XyaxisId,
  158. BuildingName = b.Name,
  159. Xaxis = xy.Xaxis,
  160. Yaxis = xy.Yaxis
  161. };
  162. return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.Xaxis).ThenBy(e => e.Yaxis).ToList());
  163. }
  164. public Task<List<BuildingXyAxisGroupView>> GetBuildingXyAxisGroupList(Guid? buildingId)
  165. {
  166. Expression<Func<BdBuilding, bool>> exp = e => true;
  167. exp = exp.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId);
  168. var query = from b in DbContext.BdBuilding.Where(exp)
  169. join xy in DbContext.BdBuildingXyAxis on b.BuildingId equals xy.BuildingId
  170. group new { xy } by new { b.BuildingId, b.Name } into g
  171. select new BuildingXyAxisGroupView
  172. {
  173. BuildingId = g.Key.BuildingId,
  174. BuildingName = g.Key.Name,
  175. MinXAxis = g.Min(e => e.xy.Xaxis),
  176. MaxXAxis = g.Max(e => e.xy.Xaxis),
  177. MinYAxis = g.Min(e => e.xy.Yaxis),
  178. MaxYAxis = g.Max(e => e.xy.Yaxis),
  179. };
  180. return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.MinXAxis).ThenBy(e => e.MinYAxis).ToList());
  181. }
  182. }
  183. }