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

PropertyRepository.cs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using Microsoft.EntityFrameworkCore;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using UnivateProperties_API.Containers.Property;
  8. using UnivateProperties_API.Containers.Timeshare;
  9. using UnivateProperties_API.Context;
  10. using UnivateProperties_API.Model.Logging;
  11. using UnivateProperties_API.Model.Properties;
  12. namespace UnivateProperties_API.Repository.Properties
  13. {
  14. public class PropertyRepository : IPropertyRepository
  15. {
  16. private readonly DataContext dBContext;
  17. public PropertyRepository(DataContext _dBContext)
  18. {
  19. dBContext = _dBContext;
  20. }
  21. public List<Property> Get(Func<Property, bool> where)
  22. {
  23. return dBContext.Properties
  24. .Include("PropertyType")
  25. .Include("Province")
  26. .Include("City")
  27. .Include("Suburb")
  28. .Where(where).ToList();
  29. }
  30. public List<Property> GetAll()
  31. {
  32. var properties = dBContext.Properties.ToList();
  33. return properties;
  34. }
  35. public Property GetDetailed(Func<Property, bool> first)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. public PropertyContainer GetDetailed(int id, bool detailed)
  40. {
  41. var property = dBContext.Properties.Include("Status").Where(p => p.Id == id).FirstOrDefault();
  42. if (property != null)
  43. {
  44. return GetDetail(property, detailed);
  45. }
  46. return null;
  47. }
  48. public List<Property> GetDetailedAll()
  49. {
  50. var properties = dBContext.Properties.ToList();
  51. return properties;
  52. }
  53. private PropertyContainer GetDetail(Property property, bool detailed)
  54. {
  55. int propID = property.Id;
  56. var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
  57. property.Province = dBContext.Provinces.Find(property.ProvinceId);
  58. property.City = dBContext.Cities.Find(property.CityId);
  59. property.Suburb = dBContext.Suburbs.Find(property.SuburbId);
  60. property.DisplayData = new List<PropertyDetailGroup>();
  61. if (detailed)
  62. {
  63. var groups = (from g in dBContext.UserDefinedGroups
  64. where g.UsageType == propertyType.UsageType
  65. || g.UsageType == PropertyUsageType.Both
  66. orderby g.Rank
  67. select g).ToList();
  68. foreach (UserDefinedGroup uGroup in groups)
  69. {
  70. var groupFields = (from f in dBContext.PropertyUserFields
  71. join uf in dBContext.UserDefinedFields on f.UserDefinedFieldId equals uf.Id
  72. join g in dBContext.UserDefinedGroups on uf.GroupId equals g.Id
  73. where f.PropertyId == propID
  74. && g.Id == uGroup.Id
  75. orderby g.Rank, uf.Rank
  76. select new { uf.FieldName, f.Value, f.Description }).ToList();
  77. if (groupFields.Count > 0)
  78. {
  79. PropertyDetailGroup detailGroup = new PropertyDetailGroup()
  80. {
  81. GroupName = uGroup.Description,
  82. Values = new List<PropertyDetail>()
  83. };
  84. if (uGroup.Description == "Property Overview")
  85. {
  86. detailGroup.Values.Add(new PropertyDetail()
  87. {
  88. Name = "Property Type",
  89. Value = property.PropertyType.Description
  90. });
  91. }
  92. foreach (var val in groupFields)
  93. {
  94. var irem = new PropertyDetail()
  95. {
  96. Name = val.FieldName,
  97. Description = val.Description
  98. };
  99. detailGroup.Values.Add(irem);
  100. if ((val.FieldName == "Erf Size" || val.FieldName == "Floor Size") && val.Value.EndsWith("2"))
  101. {
  102. irem.Value = val.Value.Substring(0, val.Value.Length - 1) + "<sup>" + val.Value.Last() + "</sup>";
  103. }
  104. else
  105. irem.Value = val.Value;
  106. }
  107. property.DisplayData.Add(detailGroup);
  108. }
  109. }
  110. }
  111. else
  112. {
  113. if (!string.IsNullOrEmpty(property.Video))
  114. property.Video = string.Format("https://www.youtube.com/watch?v={0}", property.Video);
  115. }
  116. var propertyDetails = new PropertyContainer();
  117. foreach (string prop in property.GetAllProperties())
  118. {
  119. if (prop != "Item")
  120. propertyDetails[prop] = property[prop];
  121. }
  122. propertyDetails.PropertyUsageType = propertyType.UsageType == PropertyUsageType.Commercial ? "Commercial" : "Residential";
  123. if (property.OwnerId > 0)
  124. propertyDetails.UserId = (dBContext.Individuals.Where(p => p.Id == property.OwnerId).FirstOrDefault().UserId).Value;
  125. if (property.AgentId > 0)
  126. propertyDetails.UserId = (dBContext.Agents.Where(p => p.Id == property.AgentId).FirstOrDefault().UserId).Value;
  127. return propertyDetails;
  128. }
  129. public void Insert(Property item)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public void Insert(IEnumerable<Property> items)
  134. {
  135. foreach (var item in items)
  136. {
  137. dBContext.Properties.Add(item);
  138. }
  139. Save();
  140. }
  141. public void Remove(Property item)
  142. {
  143. item.IsDeleted = true;
  144. Save();
  145. }
  146. public void Remove(IEnumerable<Property> items)
  147. {
  148. foreach (var item in items)
  149. {
  150. item.IsDeleted = true;
  151. }
  152. Save();
  153. }
  154. public void RemoveAtId(int item)
  155. {
  156. var property = Get(x => x.Id == item).FirstOrDefault();
  157. if (property != null)
  158. {
  159. dBContext.Properties.Remove(property);
  160. Save();
  161. }
  162. }
  163. public void Save()
  164. {
  165. dBContext.SaveChanges();
  166. }
  167. public void Update(Property item)
  168. {
  169. if (item.Video.StartsWith("http"))
  170. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  171. dBContext.Entry(item).State = EntityState.Modified;
  172. Save();
  173. }
  174. public void Update(PropertyContainer item)
  175. {
  176. if (item.Video.StartsWith("http"))
  177. item.Video = item.Video.Replace("https://www.youtube.com/watch?v=", "");
  178. var property = new Property();
  179. foreach (string prop in property.GetAllProperties())
  180. {
  181. if (prop != "Item")
  182. property[prop] = item[prop];
  183. }
  184. dBContext.Entry(property).State = EntityState.Modified;
  185. Save();
  186. }
  187. public List<PropertyDisplay> GetDisplay()
  188. {
  189. List<Property> props = GetAll();
  190. return GetDisplayDetails(props);
  191. }
  192. public List<PropertyDisplay> GetDisplay(Func<Property, bool> where)
  193. {
  194. List<Property> props;
  195. if (where != null)
  196. props = Get(where);
  197. else
  198. props = GetAll();
  199. return GetDisplayDetails(props);
  200. }
  201. public List<PropertyDisplay> GetDisplay(PropertySearch search)
  202. {
  203. SearchObject obj = new SearchObject()
  204. {
  205. UserName = search.UserName,
  206. Type = "Property"
  207. };
  208. if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
  209. {
  210. string keyword = search.Keyword.ToLower();
  211. List<Property> props = (from p in dBContext.Properties
  212. join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
  213. join c in dBContext.Cities on p.CityId equals c.Id
  214. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  215. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  216. where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
  217. || EF.Functions.Like(pr.Description.ToLower(), $"%{keyword}%")
  218. || EF.Functions.Like(c.Description.ToLower(), $"%{keyword}%")
  219. || EF.Functions.Like(s.Description.ToLower(), $"%{keyword}%")
  220. || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
  221. select p).ToList();
  222. obj.Property = "Keyword";
  223. obj.Value = search.Keyword;
  224. SaveLog(obj);
  225. return GetDisplayDetails(props);
  226. }
  227. else
  228. {
  229. List<Property> props;
  230. PropertyUsageType uType = PropertyUsageType.Both;
  231. if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined" && search.PropertyUsageType.ToUpper() != "ALL")
  232. {
  233. if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
  234. uType = PropertyUsageType.Commercial;
  235. else
  236. uType = PropertyUsageType.Residential;
  237. }
  238. props = (from p in dBContext.Properties
  239. join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
  240. where pt.UsageType == uType
  241. select p).ToList();
  242. obj.Property = "PropertyUsageType";
  243. obj.Value = search.PropertyUsageType;
  244. SaveLog(obj);
  245. if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
  246. {
  247. if (search.SalesType.ToUpper() == "SALE")
  248. props = props.Where(p => p.IsSale).ToList();
  249. else
  250. props = props.Where(p => !p.IsSale).ToList();
  251. obj.Property = "SalesType";
  252. obj.Value = search.SalesType;
  253. SaveLog(obj);
  254. }
  255. if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
  256. {
  257. props = (from p in props
  258. join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
  259. where pp.Description.ToUpper() == search.Province.ToUpper()
  260. select p).ToList();
  261. obj.Property = "Province";
  262. obj.Value = search.Province;
  263. SaveLog(obj);
  264. }
  265. if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
  266. {
  267. props = (from p in props
  268. join c in dBContext.Cities on p.CityId equals c.Id
  269. where c.Description.ToUpper() == search.City.ToUpper()
  270. select p).ToList();
  271. obj.Property = "City";
  272. obj.Value = search.City;
  273. SaveLog(obj);
  274. }
  275. if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
  276. {
  277. props = (from p in props
  278. join s in dBContext.Suburbs on p.SuburbId equals s.Id
  279. where s.Description.ToUpper() == search.Suburb.ToUpper()
  280. select p).ToList();
  281. obj.Property = "Suburb";
  282. obj.Value = search.Suburb;
  283. SaveLog(obj);
  284. }
  285. if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
  286. {
  287. var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
  288. if (pType != null)
  289. {
  290. props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
  291. }
  292. obj.Property = "PropertyType";
  293. obj.Value = search.PropertyType;
  294. SaveLog(obj);
  295. }
  296. if (search.MinPrice > 0)
  297. {
  298. props = props.Where(p => p.Price >= search.MinPrice).ToList();
  299. obj.Property = "MinPrice";
  300. obj.Value = search.MinPrice.ToString();
  301. SaveLog(obj);
  302. }
  303. if (search.MaxPrice > 0)
  304. {
  305. props = props.Where(p => p.Price <= search.MaxPrice).ToList();
  306. obj.Property = "MaxPrice";
  307. obj.Value = search.MaxPrice.ToString();
  308. SaveLog(obj);
  309. }
  310. return GetDisplayDetails(props);
  311. }
  312. }
  313. private void SaveLog(SearchObject item)
  314. {
  315. var searchLog = new SearchLog
  316. {
  317. Type = item.Type,
  318. Search = JsonConvert.SerializeObject(item)
  319. };
  320. dBContext.SearchLogs.Add(searchLog);
  321. Save();
  322. }
  323. private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
  324. {
  325. var properties = new List<PropertyDisplay>();
  326. foreach (var item in props)
  327. {
  328. PropertyDisplay display = new PropertyDisplay
  329. {
  330. Id = item.Id,
  331. ShortDescription = item.ShortDescription,
  332. IsSale = item.IsSale,
  333. DisplayPrice = string.Format("R {0}", item.Price.ToString("N0")),
  334. DisplayImage = (from i in dBContext.PropertyImages
  335. where i.PropertyId == item.Id
  336. && i.IsDefault
  337. select i.Image).FirstOrDefault(),
  338. Area = (from u in dBContext.PropertyUserFields
  339. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  340. where u.PropertyId == item.Id
  341. && f.FieldName == "Floor Size"
  342. select u.Value).FirstOrDefault(),
  343. Beds = (from u in dBContext.PropertyUserFields
  344. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  345. where u.PropertyId == item.Id
  346. && f.FieldName == "Bedrooms"
  347. select u.Value).FirstOrDefault(),
  348. Baths = (from u in dBContext.PropertyUserFields
  349. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  350. where u.PropertyId == item.Id
  351. && f.FieldName == "Bathrooms"
  352. select u.Value).FirstOrDefault(),
  353. Garages = (from u in dBContext.PropertyUserFields
  354. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  355. where u.PropertyId == item.Id
  356. && f.FieldName == "Garages"
  357. select u.Value).FirstOrDefault(),
  358. Province = (from p in dBContext.Provinces
  359. where p.Id == item.ProvinceId
  360. select p.Description).FirstOrDefault(),
  361. City = (from c in dBContext.Cities
  362. where c.Id == item.CityId
  363. select c.Description).FirstOrDefault(),
  364. Suburb = (from s in dBContext.Suburbs
  365. where s.Id == item.SuburbId
  366. select s.Description).FirstOrDefault(),
  367. Price = item.Price,
  368. DateCreated = item.Created,
  369. PropertyUsageType = (from p in dBContext.PropertyTypes
  370. where p.Id == item.PropertyTypeId
  371. select p.UsageType.ToString()).FirstOrDefault()
  372. };
  373. if (display.DisplayImage != null && !display.DisplayImage.StartsWith("data:image"))
  374. {
  375. display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
  376. }
  377. if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
  378. {
  379. display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
  380. }
  381. properties.Add(display);
  382. }
  383. return properties;
  384. }
  385. public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
  386. {
  387. List<PropertyType> list;
  388. if (where != null)
  389. list = dBContext.PropertyTypes.Where(where).ToList();
  390. else
  391. list = dBContext.PropertyTypes.ToList();
  392. return list;
  393. }
  394. public List<PropertyDisplay> GetLatestDisplay()
  395. {
  396. List<Property> props = GetAll().OrderBy(x => x.Created).Take(3).ToList();
  397. return GetDisplayDetails(props);
  398. }
  399. public List<PropertyList> GetPropertyList(string Type, int By)
  400. {
  401. var individual = dBContext.Individuals.Where(x => x.UserId == By).FirstOrDefault();
  402. var agent = dBContext.Agents.Where(x => x.UserId == By).FirstOrDefault();
  403. List<Property> properties = new List<Property>();
  404. if (Type.ToUpper() == "MY")
  405. {
  406. if (individual != null)
  407. properties = Get(x => x.OwnerId == individual.Id);
  408. if (agent != null)
  409. properties = Get(x => x.AgentId == agent.Id);
  410. }
  411. else
  412. {
  413. if (individual != null)
  414. properties = Get(x => x.OwnerId == individual.Id);
  415. if (agent != null)
  416. properties = Get(x => x.AgencyId == agent.AgencyId);
  417. }
  418. List<PropertyList> list = new List<PropertyList>();
  419. foreach (Property p in properties)
  420. {
  421. var prop = new PropertyList()
  422. {
  423. Id = p.Id,
  424. Name = string.IsNullOrEmpty(p.PropertyName) ? p.ShortDescription : p.PropertyName,
  425. Price = string.Format("R {0:n}", p.Price),
  426. Publish = p.Published.ToString(),
  427. Type = dBContext.PropertyTypes.Find(p.PropertyTypeId)?.Description
  428. };
  429. prop.Size = (from u in dBContext.PropertyUserFields
  430. join f in dBContext.UserDefinedFields on u.UserDefinedFieldId equals f.Id
  431. where u.PropertyId == p.Id
  432. && f.FieldName == "Floor Size"
  433. select u.Value).FirstOrDefault();
  434. if (!string.IsNullOrEmpty(prop.Size) && prop.Size.EndsWith("2"))
  435. {
  436. prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
  437. }
  438. prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
  439. prop.SaleType = p.IsSale ? "Sale" : "Rental";
  440. list.Add(prop);
  441. }
  442. return list;
  443. }
  444. public int NewId()
  445. {
  446. // Not sure if properties need it
  447. return 0;
  448. }
  449. public void Insert(PropertyContainer items)
  450. {
  451. Property property = new Property();
  452. PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
  453. if (pt != null)
  454. {
  455. if (pt.UsageType == PropertyUsageType.Residential)
  456. {
  457. string type = dBContext.PropertyTypes.Find(items.PropertyTypeId).Description;
  458. if (items.PropertyUserFields.Count > 0)
  459. {
  460. string shortDesc = "{0} {1} {2}";
  461. UserDefinedField bedrooms = dBContext.UserDefinedFields.Where(u => u.FieldName == "Bedrooms").FirstOrDefault();
  462. var udValue = items.PropertyUserFields.Where(u => u.UserDefinedFieldId == bedrooms.Id).FirstOrDefault();
  463. if (udValue != null)
  464. items.ShortDescription = string.Format(shortDesc, udValue.Value, "Bedroom", pt.Description).Trim();
  465. else
  466. items.ShortDescription = string.Format(shortDesc, "", "", pt.Description).Trim();
  467. }
  468. else
  469. {
  470. items.ShortDescription = type;
  471. }
  472. }
  473. else
  474. {
  475. items.ShortDescription = pt.Description;
  476. }
  477. }
  478. var images = items.PropertyImages;
  479. var fields = items.PropertyUserFields;
  480. items.PropertyImages = null;
  481. items.PropertyUserFields = null;
  482. var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
  483. var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
  484. foreach( string prop in property.GetAllProperties())
  485. {
  486. if (prop != "Item")
  487. property[prop] = items[prop];
  488. }
  489. if (individual != null)
  490. property.OwnerId = individual.Id;
  491. if (agent != null)
  492. {
  493. property.AgencyId = agent.AgencyId;
  494. property.AgentId = agent.Id;
  495. }
  496. property.Video = property.Video.Replace("https://www.youtube.com/watch?v=", "");
  497. dBContext.Properties.Add(property);
  498. Save();
  499. if (images != null)
  500. {
  501. var lastID = (from p in dBContext.PropertyImages
  502. orderby p.Id descending
  503. select p.Id).FirstOrDefault();
  504. bool saveFiles = false;
  505. var loc = dBContext.Location.FirstOrDefault().PropertyImageLocation;
  506. if (!string.IsNullOrEmpty(loc))
  507. {
  508. saveFiles = true;
  509. loc += string.Format("\\{0}", property.Id);
  510. if (Directory.Exists(loc))
  511. {
  512. Directory.CreateDirectory(loc);
  513. }
  514. }
  515. foreach (PropertyImage image in images)
  516. {
  517. lastID++;
  518. image.Id = lastID;
  519. image.PropertyId = property.Id;
  520. if (saveFiles)
  521. {
  522. string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
  523. image.Image = path;
  524. }
  525. dBContext.PropertyImages.Add(image);
  526. Save();
  527. }
  528. }
  529. if (fields != null)
  530. {
  531. var lastID = (from p in dBContext.PropertyUserFields
  532. orderby p.Id descending
  533. select p.Id).FirstOrDefault();
  534. foreach (PropertyUserField field in fields)
  535. {
  536. lastID++;
  537. field.Id = lastID;
  538. field.PropertyId = property.Id;
  539. dBContext.PropertyUserFields.Add(field);
  540. Save();
  541. }
  542. }
  543. }
  544. }
  545. }