SiteDataModel.cs 6.9 KB

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