SiteDataModel.cs 5.9 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
  49. {
  50. get
  51. {
  52. if (UserDescriptions == null)
  53. return String.Empty;
  54. else
  55. return String.Join(", ", UserDescriptions);
  56. }
  57. }
  58. public ICollection<SiteTreeDataModel> SiteTreeData { get; set; }
  59. public SiteDataModel()
  60. {
  61. DeviationValues = new List<int>();
  62. DeviationDescriptions = new List<string>();
  63. AppendixValues = new List<int>();
  64. AppendixDescriptions = new List<string>();
  65. UserValues = new List<int>();
  66. UserDescriptions = new List<string>();
  67. }
  68. public static SiteDataModel FromSite(Core.Domain.Site.Site siteEntity, bool newWhenIsNull)
  69. {
  70. if (siteEntity == null && newWhenIsNull)
  71. return new SiteDataModel
  72. {
  73. Id = -1,
  74. };
  75. if (siteEntity == null && !newWhenIsNull)
  76. throw new ArgumentNullException("siteEntity", "Cannot create SiteDataModel from NULL site entity.");
  77. return new SiteDataModel
  78. {
  79. Id = siteEntity.Id,
  80. CustomNumber = siteEntity.CustomNumber,
  81. Description = siteEntity.Description,
  82. Start = siteEntity.Start,
  83. End = siteEntity.End,
  84. Comment = siteEntity.Comment,
  85. DeviationValues =
  86. siteEntity.Deviations
  87. .Select(r => r.Id)
  88. .ToList(),
  89. DeviationDescriptions =
  90. siteEntity.Deviations
  91. .Select(r => r.Description)
  92. .ToList(),
  93. Deviations =
  94. siteEntity.Deviations
  95. .Select(r => DeviationDataModel.FromDeviation(r, false))
  96. .ToList(),
  97. DeviationValue =
  98. siteEntity.Deviations
  99. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  100. ? r.Value * r.Percentage.Value
  101. : 0) +
  102. siteEntity.Appendices
  103. .SelectMany(r => r.Deviations)
  104. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  105. ? r.Value * r.Percentage.Value
  106. : 0),
  107. AppendixValues =
  108. siteEntity.Appendices
  109. .Select(r => r.Id)
  110. .ToList(),
  111. AppendixDescriptions =
  112. siteEntity.Appendices
  113. .Select(r => r.CustomNumber)
  114. .ToList(),
  115. Appendices =
  116. siteEntity.Appendices
  117. .Select(r => AppendixDataModel.FromAppendix(r, false))
  118. .ToList(),
  119. UserValues =
  120. siteEntity.Users
  121. .Select(r => r.Id)
  122. .ToList(),
  123. UserDescriptions =
  124. siteEntity.Users
  125. .Select(r => new
  126. {
  127. LastName = r.Lastname,
  128. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  129. })
  130. .OrderBy(r => r.Roles)
  131. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  132. .ToList(),
  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. }