SiteDataModel.cs 6.3 KB

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