DepOrServicePointService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using YLShipBuildLandMap.Entity;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Z.EntityFramework.Plus;
  10. using YLShipBuildLandMap.Entity.Extensions;
  11. using Bowin.Common.Linq.Entity;
  12. using YLShipBuildLandMap.Entity.ViewModel.Build;
  13. using Bowin.Common.Linq;
  14. using Bowin.Common.Linq.Entity;
  15. using YLShipBuildLandMap.Entity.ViewModel;
  16. namespace YLShipBuildLandMap.Services.Build
  17. {
  18. public class DepOrServicePointService : IDepOrServicePointService
  19. {
  20. private YLShipBuildLandMapContext DbContext { get; set; }
  21. public DepOrServicePointService(YLShipBuildLandMapContext dbContext)
  22. {
  23. DbContext = dbContext;
  24. }
  25. private IQueryable<DepOrServicePointView> GetDepOrServicePointQuery(Expression<Func<BdDepartmentOrServicePoint, bool>> exp)
  26. {
  27. var query =
  28. from ds in DbContext.BdDepartmentOrServicePoint.Where(exp)
  29. /* join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on ds.DepartmentOrServicePointId equals rds.DepartmentOrServicePointId into dbBdDepartmentOrServicePointBmBuildingFloorRoom
  30. from rds in dbBdDepartmentOrServicePointBmBuildingFloorRoom.DefaultIfEmpty()
  31. join r in DbContext.BdBuildingFloorRoom on rds.BuildingFloorRoomId equals r.BuildingFloorRoomId into dbRoom
  32. from r in dbRoom.DefaultIfEmpty()
  33. join f in DbContext.BdBuildingFloor on r.BuildingFloorId equals f.BuildingFloorId into dbFloor
  34. from f in dbFloor.DefaultIfEmpty()
  35. join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId into dbBuild
  36. from b in dbBuild.DefaultIfEmpty()*/
  37. join room in (
  38. from b in DbContext.BdBuilding
  39. join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId
  40. join r in DbContext.BdBuildingFloorRoom on f.BuildingFloorId equals r.BuildingFloorId
  41. join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on r.BuildingFloorRoomId equals rds.BuildingFloorRoomId
  42. select new
  43. {
  44. rds.DepartmentOrServicePointId,
  45. f.BuildingFloorId,
  46. FloorName = f.Name,
  47. b.BuildingId,
  48. BuildingName = b.Name,
  49. RoomName = r.Name,
  50. RoomSort = r.Sort,
  51. FloorSort = f.Sort
  52. }
  53. ) on ds.DepartmentOrServicePointId equals room.DepartmentOrServicePointId into dbBdDepartmentOrServicePointBmBuildingFloorRoom
  54. from rds in dbBdDepartmentOrServicePointBmBuildingFloorRoom.DefaultIfEmpty()
  55. group new { rds.RoomName, rds.RoomSort } by new
  56. {
  57. ds.DepartmentOrServicePointId,
  58. ds.Name,
  59. ds.OpenTime,
  60. ds.ContactType,
  61. ds.Description,
  62. ds.RecordStatus,
  63. ds.CreateUserId,
  64. ds.CreateTime,
  65. ds.ModifyTime,
  66. ds.ModifyUserId,
  67. rds.FloorName,
  68. rds.BuildingName,
  69. rds.BuildingId,
  70. rds.BuildingFloorId,
  71. rds.FloorSort
  72. } into g
  73. select new DepOrServicePointView
  74. {
  75. DepartmentOrServicePointId = g.Key.DepartmentOrServicePointId,
  76. Name = g.Key.Name,
  77. OpenTime = g.Key.OpenTime,
  78. ContactType = g.Key.ContactType,
  79. Description = g.Key.Description,
  80. RecordStatus = g.Key.RecordStatus,
  81. CreateUserId = g.Key.CreateUserId,
  82. CreateTime = g.Key.CreateTime,
  83. ModifyTime = g.Key.ModifyTime,
  84. ModifyUserId = g.Key.ModifyUserId,
  85. BuildingName = g.Key.BuildingName,
  86. BuildingId = g.Key.BuildingId,
  87. FloorName = g.Key.FloorName,
  88. FloorId = g.Key.BuildingFloorId,
  89. /* RoomName = string.Join("、", g.Select(s => s.RoomName).ToList())*/
  90. Sort = g.Min(s => s.RoomSort),
  91. FloorSort = g.Key.FloorSort
  92. };
  93. return query;
  94. }
  95. private IQueryable<DepOrServicePointRoomView> GetDepOrServicePointRoomQuery(Expression<Func<BdDepartmentOrServicePoint, bool>> exp)
  96. {
  97. var query =
  98. from ds in DbContext.BdDepartmentOrServicePoint
  99. join rds in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on ds.DepartmentOrServicePointId equals rds.DepartmentOrServicePointId
  100. join r in DbContext.BdBuildingFloorRoom on rds.BuildingFloorRoomId equals r.BuildingFloorRoomId
  101. join f in DbContext.BdBuildingFloor on r.BuildingFloorId equals f.BuildingFloorId
  102. join b in DbContext.BdBuilding on f.BuildingId equals b.BuildingId
  103. select new DepOrServicePointRoomView
  104. {
  105. DepartmentOrServicePointId = ds.DepartmentOrServicePointId,
  106. FloorId = f.BuildingFloorId,
  107. RoomName = r.Name,
  108. RoomSort = r.Sort
  109. };
  110. return query;
  111. }
  112. public IGridResultSet<DepOrServicePointView> GetDepOrServicePointList(int pageIndex, int pageSize, Guid? buildingId, Guid? floorId, string name)
  113. {
  114. Expression<Func<BdDepartmentOrServicePoint, bool>> exp = e => true;
  115. Expression<Func<BdBuilding, bool>> expBuild = e => true;
  116. Expression<Func<BdBuildingFloor, bool>> expFloor = e => true;
  117. exp = exp.AndIf(!string.IsNullOrEmpty(name), e => e.Name.Contains(name.Trim()));
  118. expFloor = expFloor.AndIf(floorId.HasValue, e => e.BuildingFloorId == floorId);
  119. expBuild = expBuild.AndIf(buildingId.HasValue, e => e.BuildingId == buildingId);
  120. var query = GetDepOrServicePointQuery(exp)
  121. .WhereIf(floorId.HasValue, e => e.FloorId == floorId)
  122. .WhereIf(buildingId.HasValue, e => e.BuildingId == buildingId)
  123. .OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Sort).ThenBy(e => e.Name);
  124. var dataResult = query.ToGridResultSet(pageIndex, pageSize);
  125. var depOrServicePointIdList = dataResult.rows.Select(s => s.DepartmentOrServicePointId).ToList();
  126. var roomList = GetDepOrServicePointRoomQuery(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).ToList();
  127. var fileList = this.DbContext.SysAttachment.Where(e => depOrServicePointIdList.Contains((Guid)e.ReferenceId) && e.Type == 1).ToList();
  128. dataResult.rows.ForEach(item =>
  129. {
  130. 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());
  131. item.ImgUrl = fileList.Where(e => e.ReferenceId == item.DepartmentOrServicePointId).FirstOrDefault()?.Url;
  132. });
  133. return dataResult;
  134. }
  135. public Task<DepOrServicePointView> GetDepOrServicePointById(Guid? depOrServicePointId)
  136. {
  137. if (depOrServicePointId.HasValue)
  138. {
  139. return Task.FromResult(GetDepOrServicePointQuery(e => e.DepartmentOrServicePointId == depOrServicePointId).FirstOrDefault());
  140. }
  141. else
  142. {
  143. return Task.FromResult(new DepOrServicePointView { DepartmentOrServicePointId = Guid.NewGuid() });
  144. }
  145. }
  146. public async Task<int> Save(DepOrServicePointView data, List<Guid> roomIdList, Guid? userId)
  147. {
  148. BdDepartmentOrServicePoint dbData = DbContext.BdDepartmentOrServicePoint.FirstOrDefault(e => e.DepartmentOrServicePointId == data.DepartmentOrServicePointId);
  149. if (dbData != null)
  150. {
  151. dbData.Name = data.Name;
  152. dbData.Description = data.Description;
  153. dbData.OpenTime = data.OpenTime;
  154. dbData.ModifyTime = DateTime.Now;
  155. dbData.ModifyUserId = userId;
  156. dbData.ContactType = data.ContactType;
  157. DbContext.BdDepartmentOrServicePoint.Update(dbData);
  158. }
  159. else
  160. {
  161. dbData = new BdDepartmentOrServicePoint
  162. {
  163. DepartmentOrServicePointId = data.DepartmentOrServicePointId,
  164. Name = data.Name,
  165. OpenTime = data.OpenTime,
  166. Description = data.Description,
  167. CreateTime = DateTime.Now,
  168. CreateUserId = userId,
  169. RecordStatus = 1,
  170. ContactType = data.ContactType
  171. };
  172. this.DbContext.Add(dbData);
  173. }
  174. await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => dbData.DepartmentOrServicePointId == e.DepartmentOrServicePointId).DeleteAsync();
  175. this.DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.AddRange(roomIdList.Select(s => new BdDepartmentOrServicePointBmBuildingFloorRoom
  176. {
  177. BuildingFloorRoomId = s,
  178. DepartmentOrServicePointId = dbData.DepartmentOrServicePointId
  179. }).ToList());
  180. return await this.DbContext.SaveChangesAsync();
  181. }
  182. public async Task<int> Delete(List<Guid> depOrServicePointIdList)
  183. {
  184. await DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom.Where(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).DeleteAsync();
  185. await DbContext.BdDepartmentOrServicePoint.Where(e => depOrServicePointIdList.Contains(e.DepartmentOrServicePointId)).DeleteAsync();
  186. return await this.DbContext.SaveChangesAsync();
  187. }
  188. public Task<List<RoomView>> GetRoomList(Guid depOrServicePointId)
  189. {
  190. var query = from b in DbContext.BdBuilding
  191. join f in DbContext.BdBuildingFloor on b.BuildingId equals f.BuildingId
  192. join r in DbContext.BdBuildingFloorRoom on f.BuildingFloorId equals r.BuildingFloorId
  193. join deppoint in DbContext.BdDepartmentOrServicePointBmBuildingFloorRoom on r.BuildingFloorRoomId equals deppoint.BuildingFloorRoomId
  194. where deppoint.DepartmentOrServicePointId == depOrServicePointId
  195. select new RoomView
  196. {
  197. BuildingFloorRoomId = r.BuildingFloorRoomId,
  198. BuildingFloorId = r.BuildingFloorId,
  199. BuildingId = b.BuildingId,
  200. Name = r.Name,
  201. CreateTime = r.CreateTime,
  202. CreateUserId = r.CreateUserId,
  203. ModifyTime = r.ModifyTime,
  204. ModifyUserId = r.ModifyUserId,
  205. RecordStatus = r.RecordStatus,
  206. Sort = r.Sort,
  207. BuildingName = b.Name,
  208. FloorName = f.Name,
  209. FloorSort = f.Sort
  210. };
  211. return Task.FromResult(query.OrderBy(e => e.BuildingName).ThenBy(e => e.FloorSort).ThenBy(e => e.Name).ToList());
  212. }
  213. }
  214. }