SiteDataModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. DeviationValues =
  85. siteEntity.Deviations
  86. .Select(r => r.Id)
  87. .ToList(),
  88. DeviationDescriptions =
  89. siteEntity.Deviations
  90. .Select(r => r.Description)
  91. .ToList(),
  92. Deviations =
  93. siteEntity.Deviations
  94. .Select(r => DeviationDataModel.FromDeviation(r, false))
  95. .ToList(),
  96. DeviationValue =
  97. siteEntity.Deviations
  98. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  99. ? r.Value * r.Percentage.Value
  100. : 0) +
  101. siteEntity.Appendices
  102. .SelectMany(r => r.Deviations)
  103. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  104. ? r.Value * r.Percentage.Value
  105. : 0),
  106. AppendixValues =
  107. siteEntity.Appendices
  108. .Select(r => r.Id)
  109. .ToList(),
  110. AppendixDescriptions =
  111. siteEntity.Appendices
  112. .Select(r => r.CustomNumber)
  113. .ToList(),
  114. Appendices =
  115. siteEntity.Appendices
  116. .Select(r => AppendixDataModel.FromAppendix(r, false))
  117. .ToList(),
  118. UserValues =
  119. siteEntity.Users
  120. .Select(r => r.Id)
  121. .ToList(),
  122. UserDescriptions =
  123. siteEntity.Users
  124. .Select(r => String.Format("{0} - ({1})", r.Lastname,
  125. String.Join(", ", r.Roles.Select(u => u.Description))))
  126. .ToList(),
  127. Users =
  128. siteEntity.Users
  129. .Select(r => UserDataModel.FromUser(r, false))
  130. .ToList(),
  131. };
  132. }
  133. public Core.Domain.Site.Site ToSite()
  134. {
  135. return new Core.Domain.Site.Site
  136. {
  137. Id = this.Id,
  138. CustomNumber = this.CustomNumber,
  139. Description = Description,
  140. Comment = this.Comment,
  141. Start = this.Start,
  142. End = this.End
  143. };
  144. }
  145. }
  146. }