SiteDataModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using GreenTree.Nachtragsmanagement.Services.Configuration;
  2. using GreenTree.Nachtragsmanagement.Web.Models.Admin.User;
  3. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  4. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Web;
  9. namespace GreenTree.Nachtragsmanagement.Web.Models.Site
  10. {
  11. public class SiteDataModel
  12. {
  13. public int Id { get; set; }
  14. public string CustomNumber { get; set; }
  15. public string Description { get; set; }
  16. public DateTime? Start { get; set; }
  17. public DateTime? End { get; set; }
  18. public string Comment { get; set; }
  19. public ICollection<int> DeviationValues { get; set; }
  20. public ICollection<string> DeviationDescriptions { get; set; }
  21. public ICollection<DeviationDataModel> Deviations { get; set; }
  22. public string DeviationDescription
  23. {
  24. get
  25. {
  26. if (DeviationDescriptions == null)
  27. return String.Empty;
  28. else
  29. return String.Join(" - ", DeviationDescriptions);
  30. }
  31. }
  32. public string HtmlDeviationDescription
  33. {
  34. get
  35. {
  36. if (DeviationDescriptions == null)
  37. return String.Empty;
  38. else
  39. return String.Join("</br>--------------</br>",
  40. Deviations
  41. .Select(d => String.Format("{0} - {1}", d.CustomNumber, d.Description)));
  42. }
  43. }
  44. public decimal? DeviationValue { get; set; }
  45. public ICollection<int> AppendixValues { get; set; }
  46. public ICollection<string> AppendixDescriptions { get; set; }
  47. public ICollection<AppendixDataModel> Appendices { get; set; }
  48. public string AppendixDescription
  49. {
  50. get
  51. {
  52. if (AppendixDescriptions == null)
  53. return String.Empty;
  54. else
  55. return String.Join(", ", AppendixDescriptions);
  56. }
  57. }
  58. public ICollection<int> UserValues { get; set; }
  59. public ICollection<string> UserDescriptions { get; set; }
  60. public ICollection<UserDataModel> Users { get; set; }
  61. public string UserDescription { get; set; }
  62. public ICollection<SiteTreeDataModel> SiteTreeData { get; set; }
  63. public SiteDataModel()
  64. {
  65. DeviationValues = new List<int>();
  66. DeviationDescriptions = new List<string>();
  67. AppendixValues = new List<int>();
  68. AppendixDescriptions = new List<string>();
  69. UserValues = new List<int>();
  70. UserDescriptions = new List<string>();
  71. }
  72. public static SiteDataModel FromSite(Core.Domain.Site.Site siteEntity, bool newWhenIsNull,
  73. IConfigurationService configurationService)
  74. {
  75. if (siteEntity == null && newWhenIsNull)
  76. return new SiteDataModel
  77. {
  78. Id = -1,
  79. };
  80. if (siteEntity == null && !newWhenIsNull)
  81. throw new ArgumentNullException("siteEntity", "Cannot create SiteDataModel from NULL site entity.");
  82. return new SiteDataModel
  83. {
  84. Id = siteEntity.Id,
  85. CustomNumber = siteEntity.CustomNumber,
  86. Description = siteEntity.Description,
  87. Start = siteEntity.Start,
  88. End = siteEntity.End,
  89. Comment = siteEntity.Comment,
  90. DeviationValues =
  91. siteEntity.Deviations
  92. .Select(r => r.Id)
  93. .ToList(),
  94. DeviationDescriptions =
  95. siteEntity.Deviations
  96. .Select(r => r.Description)
  97. .ToList(),
  98. Deviations =
  99. siteEntity.Deviations
  100. .Select(r => DeviationDataModel.FromDeviation(r, false, configurationService))
  101. .ToList(),
  102. DeviationValue =
  103. siteEntity.Deviations
  104. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  105. ? r.Value * r.Percentage.Value
  106. : 0) +
  107. siteEntity.Appendices
  108. .SelectMany(r => r.Deviations)
  109. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  110. ? r.Value * r.Percentage.Value
  111. : 0),
  112. AppendixValues =
  113. siteEntity.Appendices
  114. .Select(r => r.Id)
  115. .ToList(),
  116. AppendixDescriptions =
  117. siteEntity.Appendices
  118. .Select(r => r.CustomNumber)
  119. .ToList(),
  120. Appendices =
  121. siteEntity.Appendices
  122. .Select(r => AppendixDataModel.FromAppendix(r, false))
  123. .ToList(),
  124. UserValues =
  125. siteEntity.Users
  126. .Select(r => r.Id)
  127. .ToList(),
  128. UserDescriptions =
  129. siteEntity.Users
  130. .Select(r => new
  131. {
  132. LastName = r.Lastname,
  133. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  134. })
  135. .OrderBy(r => r.Roles)
  136. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  137. .ToList(),
  138. UserDescription =
  139. String.Join(", ",
  140. siteEntity.Users
  141. .Select(r => new
  142. {
  143. LastName = r.Lastname,
  144. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  145. })
  146. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles ))),
  147. Users =
  148. siteEntity.Users
  149. .Select(r => UserDataModel.FromUser(r, false))
  150. .ToList(),
  151. };
  152. }
  153. public Core.Domain.Site.Site ToSite()
  154. {
  155. return new Core.Domain.Site.Site
  156. {
  157. Id = this.Id,
  158. CustomNumber = this.CustomNumber,
  159. Description = Description,
  160. Comment = this.Comment,
  161. Start = this.Start,
  162. End = this.End
  163. };
  164. }
  165. }
  166. }