API
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PropertyRepository.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.Property;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Model.Properties;
  8. namespace UnivateProperties_API.Repository.Properties
  9. {
  10. public class PropertyRepository : IPropertyRepository
  11. {
  12. private readonly DataContext dBContext;
  13. public PropertyRepository(DataContext _dBContext)
  14. {
  15. dBContext = _dBContext;
  16. }
  17. public List<Property> Get(Func<Property, bool> where)
  18. {
  19. return dBContext.Properties
  20. .Include("PropertyType")
  21. .Include("Province")
  22. .Include("City")
  23. .Include("Suburb")
  24. .Where(where).ToList();
  25. }
  26. public List<Property> GetAll()
  27. {
  28. var properties = dBContext.Properties.ToList();
  29. return properties;
  30. }
  31. public Property GetDetailed(Func<Property, bool> first)
  32. {
  33. var property = dBContext.Properties.FirstOrDefault(first);
  34. if (property != null)
  35. {
  36. GetDetail(ref property);
  37. }
  38. return property;
  39. }
  40. public List<Property> GetDetailedAll()
  41. {
  42. var properties = dBContext.Properties.ToList();
  43. return properties;
  44. }
  45. private void GetDetail(ref Property property)
  46. {
  47. int propID = property.Id;
  48. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  49. property.Province = dBContext.Provinces.Find(property.ProvinceId);
  50. property.City = dBContext.Cities.Find(property.CityId);
  51. property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
  52. property.DisplayData = new List<PropertyDetailGroup>();
  53. var groups = (from g in dBContext.UserDefinedGroups
  54. where g.UsageType == propertyType.UsageType
  55. || g.UsageType == PropertyUsageType.Both
  56. orderby g.Rank
  57. select g).ToList();
  58. foreach (UserDefinedGroup uGroup in groups)
  59. {
  60. var groupFields = (from f in dBContext.PropertyUserFields
  61. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  62. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  63. where f.PropertyId == propID
  64. && g.Id == uGroup.Id
  65. orderby g.Rank, uf.Rank
  66. select new { uf.FieldName, f.Value, f.Description }).ToList();
  67. if (groupFields.Count > 0)
  68. {
  69. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  70. {
  71. GroupName = uGroup.Description,
  72. Values = new List<PropertyDetail>()
  73. };
  74. if (uGroup.Description == "Property Overview")
  75. {
  76. detailGroup.Values.Add(new PropertyDetail()
  77. {
  78. Name = "Property Type",
  79. Value = property.PropertyType.Description
  80. });
  81. }
  82. foreach (var val in groupFields)
  83. {
  84. var irem = new PropertyDetail()
  85. {
  86. Name = val.FieldName,
  87. Description = val.Description
  88. };
  89. detailGroup.Values.Add(irem);
  90. if ((val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
  91. {
  92. irem.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
  93. }
  94. else
  95. irem.Value = val.Value;
  96. }
  97. property.DisplayData.Add(detailGroup);
  98. }
  99. }
  100. }
  101. public void Insert(Property item)
  102. {
  103. PropertyType pt = dBContext.PropertyTypes.Find(item.PropertyTypeId);
  104. if (pt != null)
  105. {
  106. if (pt.UsageType == PropertyUsageType.Residential)
  107. {
  108. string type = dBContext.PropertyTypes.Find(item.PropertyTypeId).Description;
  109. if (item.PropertyUserFields.Count > 0)
  110. {
  111. string shortDesc = "{0} {1} {2}";
  112. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  113. var udValue = item.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  114. if (udValue != null)
  115. item.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  116. else
  117. item.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  118. }
  119. else
  120. {
  121. item.ShortDescription = type;
  122. }
  123. }
  124. else
  125. {
  126. item.ShortDescription = pt.Description;
  127. }
  128. }
  129. dBContext.Properties.Add(item);
  130. Save();
  131. }
  132. public void Insert(IEnumerable<Property> items)
  133. {
  134. foreach (var item in items)
  135. {
  136. dBContext.Properties.Add(item);
  137. }
  138. Save();
  139. }
  140. public void Remove(Property item)
  141. {
  142. dBContext.Properties.Remove(item);
  143. Save();
  144. }
  145. public void Remove(IEnumerable<Property> items)
  146. {
  147. foreach (var item in items)
  148. {
  149. dBContext.Properties.Remove(item);
  150. }
  151. Save();
  152. }
  153. public void RemoveAtId(int item)
  154. {
  155. var property = Get(x => x.Id == item).FirstOrDefault();
  156. if (property != null)
  157. {
  158. dBContext.Properties.Remove(property);
  159. Save();
  160. }
  161. }
  162. public void Save()
  163. {
  164. dBContext.SaveChanges();
  165. }
  166. public void Update(Property item)
  167. {
  168. dBContext.Entry(item).State = EntityState.Modified;
  169. Save();
  170. }
  171. public List<PropertyDisplay> GetDisplay()
  172. {
  173. List<Property> props = GetAll();
  174. return GetDisplayDetails(props);
  175. }
  176. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  177. {
  178. List<Property> props;
  179. if (where != null)
  180. props = Get(where);
  181. else
  182. props = GetAll();
  183. return GetDisplayDetails(props);
  184. }
  185. public List<PropertyDisplay> GetDisplay(string Keyword)
  186. {
  187. Keyword = Keyword.ToLower();
  188. List<Property> props = (from p in dBContext.Properties
  189. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  190. join c in dBContext.Cities on p.CityId equals c.Id
  191. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  192. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  193. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{Keyword}%")
  194. || EF.Functions.Like(pr.Description.ToLower(), $"%{Keyword}%")
  195. || EF.Functions.Like(c.Description.ToLower(), $"%{Keyword}%")
  196. || EF.Functions.Like(s.Description.ToLower(), $"%{Keyword}%")
  197. || EF.Functions.Like(pt.Description.ToLower(), $"%{Keyword}%")
  198. select p).ToList();
  199. return GetDisplayDetails(props);
  200. }
  201. public List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType)
  202. {
  203. List<Property> props;
  204. PropertyUsageType uType = PropertyUsageType.Both;
  205. if (propertyType != "" && propertyType != "undefined")
  206. {
  207. if (propertyType.ToUpper() == "COMMERCIAL")
  208. uType = PropertyUsageType.Commercial;
  209. else
  210. uType = PropertyUsageType.Residential;
  211. }
  212. props = (from p in dBContext.Properties
  213. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  214. where pt.UsageType == uType
  215. select p).ToList();
  216. if (type != "" && type != "undefined")
  217. {
  218. if (type.ToUpper() == "SALE")
  219. props = props.Where(p => p.IsSale).ToList();
  220. else
  221. props = props.Where(p => !p.IsSale).ToList();
  222. }
  223. if (province != "" && province != "undefined" && province.ToUpper() != "ALL")
  224. {
  225. props = (from p in props
  226. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  227. where pp.Description.ToUpper() == province.ToUpper()
  228. select p).ToList();
  229. }
  230. if (city != "" && city != "undefined" && city.ToUpper() != "ALL")
  231. {
  232. props = (from p in props
  233. join c in dBContext.Cities on p.CityId equals c.Id
  234. where c.Description.ToUpper() == city.ToUpper()
  235. select p).ToList();
  236. }
  237. if (suburb != "" && suburb != "undefined" && suburb.ToUpper() != "ALL")
  238. {
  239. props = (from p in props
  240. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  241. where s.Description.ToUpper() == suburb.ToUpper()
  242. select p).ToList();
  243. }
  244. if (propType != "" && propType != "Undefined" && propType.ToUpper() != "ALL")
  245. {
  246. var pType = dBContext.PropertyTypes.Where(t => t.Description == propType).FirstOrDefault();
  247. if (pType != null)
  248. {
  249. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  250. }
  251. }
  252. return GetDisplayDetails(props);
  253. }
  254. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  255. {
  256. var properties = new List<PropertyDisplay>();
  257. foreach (var item in props)
  258. {
  259. PropertyDisplay display = new PropertyDisplay
  260. {
  261. Id = item.Id,
  262. ShortDescription = item.ShortDescription,
  263. IsSale = item.IsSale,
  264. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  265. DisplayImage = (from i in dBContext.PropertyImages
  266. where i.PropertyId == item.Id
  267. && i.IsDefault
  268. select i.Image).FirstOrDefault(),
  269. Area = (from u in dBContext.PropertyUserFields
  270. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  271. where u.PropertyId == item.Id
  272. && f.FieldName == "Floor Size"
  273. select u.Value).FirstOrDefault(),
  274. Beds = (from u in dBContext.PropertyUserFields
  275. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  276. where u.PropertyId == item.Id
  277. && f.FieldName == "Bedrooms"
  278. select u.Value).FirstOrDefault(),
  279. Baths = (from u in dBContext.PropertyUserFields
  280. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  281. where u.PropertyId == item.Id
  282. && f.FieldName == "Bathrooms"
  283. select u.Value).FirstOrDefault(),
  284. Garages = (from u in dBContext.PropertyUserFields
  285. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  286. where u.PropertyId == item.Id
  287. && f.FieldName == "Garages"
  288. select u.Value).FirstOrDefault(),
  289. Province = (from p in dBContext.Provinces
  290. where p.Id == item.ProvinceId
  291. select p.Description).FirstOrDefault(),
  292. City = (from c in dBContext.Cities
  293. where c.Id == item.CityId
  294. select c.Description).FirstOrDefault(),
  295. Suburb = (from s in dBContext.Suburbs
  296. where s.Id == item.SuburbId
  297. select s.Description).FirstOrDefault(),
  298. Price = item.Price,
  299. DateCreated = item.Created
  300. };
  301. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  302. {
  303. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  304. }
  305. properties.Add(display);
  306. }
  307. return properties;
  308. }
  309. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  310. {
  311. List<PropertyType> list;
  312. if (where != null)
  313. list = dBContext.PropertyTypes.Where(where).ToList();
  314. else
  315. list = dBContext.PropertyTypes.ToList();
  316. return list;
  317. }
  318. public List<PropertyDisplay> GetLatestDisplay()
  319. {
  320. List<Property> props = GetAll().OrderBy(x => x.Created).Take(3).ToList();
  321. return GetDisplayDetails(props);
  322. }
  323. public List<PropertyList> GetPropertyList(Func<Property, bool> where)
  324. {
  325. var properties = Get(where);
  326. List<PropertyList> list = new List<PropertyList>();
  327. foreach (Property p in properties)
  328. {
  329. var prop = new PropertyList()
  330. {
  331. Id = p.Id,
  332. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  333. Price = string.Format("R {0:n}", p.Price),
  334. Publish = p.Published.ToString(),
  335. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description
  336. };
  337. prop.Size = (from u in dBContext.PropertyUserFields
  338. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  339. where u.PropertyId == p.Id
  340. && f.FieldName == "Floor Size"
  341. select u.Value).FirstOrDefault();
  342. if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  343. {
  344. prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  345. }
  346. list.Add(prop);
  347. }
  348. return list;
  349. }
  350. }
  351. }