SiteDataModel.cs 6.6 KB

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