SiteDataModel.cs 6.1 KB

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