Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. private void GetUpdateCurrentVechileObject(VehicleObject obj, List<UpdateCurrentVechileObject> routes)
  2. {
  3. var dbAllEnumRouteStatus = m_vehicleService.AllEnumRouteStatus();
  4.  
  5. string RouteCurrentStatusIsAwating = "", RouteCurrentStatusIsNew = "", RouteCurrentStatusIsDelivered = "", MapLocationName = "london";
  6. Guid? id = obj.Guid;
  7. var availableSequences = m_vehicleService.GetAvailableSequences(id.Value);
  8. var origin = obj.Route.RoutePoints.OrderBy(x => x.Sequence).FirstOrDefault();
  9. var sourceId = origin.Id;
  10. var destination = obj.Route.RoutePoints.OrderByDescending(x => x.Sequence).FirstOrDefault();
  11. var destinationId = destination.Id;
  12. var lastHubSequence = destination.Sequence;
  13. /* 0 - Origin Address
  14. 1 - Origin Store
  15. 2 - Origin HUb
  16.  
  17. Last 3 Destination
  18. Destination HUb
  19. Destination Store
  20. Destination Address
  21. */
  22. int totalRoute = obj.Route.RoutePoints.Count;
  23. int endHubCount = totalRoute > 2 ? (totalRoute - 3) : 0;
  24.  
  25. obj.Route.RoutePoints.ForEach(x =>
  26. {
  27.  
  28. var d = new UpdateCurrentVechileObject()
  29. {
  30. Id = x.Id,
  31. Location = x.Location,
  32. Sequence = x.Sequence,
  33. SequenceName = "",
  34. IsOrigin = false,
  35. IsDestination = false,
  36. DestinationSequence = lastHubSequence,
  37. EnableButton = availableSequences.FirstOrDefault(s => s == x.Sequence) != 0,
  38. AvailableStatus = new List<KeyValue>(),
  39. IsCurrent = x.IsCurrent,
  40. ShowAddHubButton = false,
  41. HubAvailableSequence = new List<KeyValue>()
  42. };
  43. if (x.Sequence == 0)
  44. {
  45. d.SequenceName = "Origin";
  46. d.IsOrigin = true;
  47. }
  48. else if (x.Sequence == 1)
  49. {
  50. d.SequenceName = "Origin Store";
  51. }
  52. else if (x.Sequence == totalRoute - 1)
  53. {
  54. d.SequenceName = "Destination";
  55. d.IsDestination = true;
  56. }
  57. else if (x.Sequence == totalRoute - 2)
  58. {
  59. d.SequenceName = "Destination Store";
  60. }
  61. else
  62. {
  63. d.SequenceName = "Hub";
  64. }
  65. if (x.Sequence >= 2 && x.Sequence < endHubCount)
  66. {
  67. d.ShowAddHubButton = true;
  68. for (int i = x.Sequence; i < endHubCount; i++)
  69. {
  70. d.HubAvailableSequence.Add(new KeyValue()
  71. {
  72. Key = (i + 1).ToString(),
  73. Value = $"{AddOrdinal(i)} Hub"
  74. });
  75. }
  76. }
  77. if (d.EnableButton)
  78. {
  79. m_vehicleService.GetAvailableStatus(id.Value, x.Sequence).ForEach(sq =>
  80. {
  81. //Skip , New, Awaiting Pickup , delivered
  82. if (sq != EnumRouteStatus.New && sq != EnumRouteStatus.AwaitingPickup && sq != EnumRouteStatus.Delivered)
  83. {
  84. d.AvailableStatus.Add(new KeyValue()
  85. {
  86. Key = ((int)sq).ToString(),
  87. Value = dbAllEnumRouteStatus.FirstOrDefault(rs => rs.Id == (int)sq).Desc
  88. });
  89. }
  90. });
  91. d.AvailableStatus = d.AvailableStatus.OrderBy(avs => avs.Value).ToList();
  92.  
  93. }
  94.  
  95. if (d.IsOrigin)
  96. {
  97. /*if RouteStaus is "New", the button should be disabled. */
  98. if (obj.Route.RouteStatus == EnumRouteStatus.New)
  99. {
  100.  
  101. d.EnableButton = false;
  102.  
  103. }
  104. else
  105. {
  106. d.EnableButton = true;
  107. }
  108. }
  109.  
  110. routes.Add(d);
  111. });
  112. //origin will be current location if RouteStaus is New
  113. var r = routes.FirstOrDefault(x => x.IsOrigin && obj.Route.RouteStatus == EnumRouteStatus.New);
  114. if (r != null)
  115. {
  116. routes.ForEach(x =>
  117. {
  118. if (x.IsOrigin)
  119. {
  120. x.IsCurrent = true;
  121. }
  122. else
  123. {
  124. x.IsCurrent = false;
  125. }
  126. });
  127. }
  128. if (obj.Route != null)
  129. {
  130.  
  131. if (obj.Route.RouteStatus.HasValue)
  132. {
  133. ViewBag.CurrentRouteStatus = dbAllEnumRouteStatus.FirstOrDefault(x => x.Id == (int)obj?.Route?.RouteStatus)?.Desc;
  134. RouteCurrentStatusIsAwating = obj.Route.RouteStatus == EnumRouteStatus.AwaitingPickup ? "true" : "false";
  135. RouteCurrentStatusIsNew = obj.Route.RouteStatus == EnumRouteStatus.New ? "true" : "false";
  136. RouteCurrentStatusIsDelivered = obj.Route.RouteStatus == EnumRouteStatus.Delivered ? "true" : "false";
  137. }
  138. ViewBag.RouteCurrentSequence = obj.Route.CurrentSequence;
  139. //if current status is "AwaitingPickup" and current sequence=2, show the current location to the first address not the hub
  140. if (ViewBag.RouteCurrentSequence == 2 && RouteCurrentStatusIsAwating == "true")
  141. {
  142. if (origin != null)
  143. {
  144. MapLocationName = origin.Location.Name;
  145. }
  146. }
  147. //if current status is "new" show the current location to the first address not the hub
  148. if (RouteCurrentStatusIsNew == "true")
  149. {
  150. if (origin != null)
  151. {
  152. MapLocationName = origin.Location.Name;
  153. }
  154. }
  155. }
  156. if (MapLocationName == "london")
  157. {
  158. if (obj.CurrentLocation != null)
  159. {
  160. MapLocationName = obj.CurrentLocation.Name;
  161. }
  162. }
  163. // string RouteCurrentStatusIsAwating = "", RouteCurrentStatusIsNew = "", RouteCurrentStatusIsDelivered = "", MapLocationName= "london";
  164. ViewBag.MapLocationName = MapLocationName;
  165. ViewBag.RouteCurrentStatusIsDelivered = RouteCurrentStatusIsDelivered;
  166. ViewBag.RouteCurrentStatusIsNew = RouteCurrentStatusIsNew;
  167. ViewBag.RouteCurrentStatusIsAwating = RouteCurrentStatusIsAwating;
  168. ViewBag.CurrentLocationName = obj.CurrentLocation?.Name;
  169. }
  170.  
  171. private string AddOrdinal(int num)
  172. {
  173. if (num <= 0) return num.ToString();
  174.  
  175. switch (num % 100)
  176. {
  177. case 11:
  178. case 12:
  179. case 13:
  180. return num + "th";
  181. }
  182.  
  183. switch (num % 10)
  184. {
  185. case 1:
  186. return num + "st";
  187. case 2:
  188. return num + "nd";
  189. case 3:
  190. return num + "rd";
  191. default:
  192. return num + "th";
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement