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

BidRepository.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.ProcessFlow;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Helpers;
  8. using UnivateProperties_API.Model;
  9. using UnivateProperties_API.Model.ProcessFlow;
  10. using UnivateProperties_API.Repository.Communication;
  11. using UnivateProperties_API.Repository.Timeshare;
  12. namespace UnivateProperties_API.Repository.ProccessFlow
  13. {
  14. public class BidRepository : IBidRepository
  15. {
  16. private readonly DataContext _dbContext;
  17. public BidRepository(DataContext dbContext)
  18. {
  19. _dbContext = dbContext;
  20. }
  21. public List<BidItem> Get(Func<BidItem, bool> where)
  22. {
  23. return _dbContext.BidItems.Where(where).ToList();
  24. }
  25. public List<BidItem> GetAll()
  26. {
  27. return _dbContext.BidItems.ToList();
  28. }
  29. public BidItem GetDetailed(Func<BidItem, bool> first)
  30. {
  31. var item = _dbContext.BidItems.FirstOrDefault(first);
  32. return item;
  33. }
  34. public List<BidItem> GetDetailedAll()
  35. {
  36. return GetAll();
  37. }
  38. public List<BidItemDisplay> GetAllBid()
  39. {
  40. List<BidItem> bids = _dbContext.BidItems
  41. .Include("Property")
  42. .Include("TimeshareWeek")
  43. .Include("BidMaker")
  44. .Include("Status")
  45. .Include("Property.Owner")
  46. .Include("Property.Agent")
  47. .ToList();
  48. return LoadDisplay(bids);
  49. }
  50. public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
  51. {
  52. List<BidItem> bids = _dbContext.BidItems
  53. .Include("Property")
  54. .Include("TimeshareWeek")
  55. .Include("BidMaker")
  56. .Include("Status")
  57. .Where(where)
  58. .ToList();
  59. return LoadDisplay(bids);
  60. }
  61. public List<BidItemDisplay> GetAllBidBy(string name)
  62. {
  63. /*
  64. * Agency - can see all its agents' listings
  65. * Agent - only see own listings
  66. * Private User - only see own listings
  67. * Super Admin - can see all
  68. */
  69. var user = _dbContext.Users.Where(u => u.Username == name).FirstOrDefault();
  70. if (user != null)
  71. {
  72. if (user.Role.ToUpper() == "AGENCY" || user.Role.ToUpper() == "AGENT")
  73. {
  74. var agent = _dbContext.Agents.Where(a => a.UserId == user.Id).FirstOrDefault();
  75. if (user.Role.ToUpper() == "AGENCY")
  76. {
  77. List<BidItem> bids = _dbContext.BidItems
  78. .Include("Property")
  79. .Include("TimeshareWeek")
  80. .Include("BidMaker")
  81. .Include("Status")
  82. .Where(x => x.TimeshareWeek.AgencyId == agent.AgencyId || x.Property.AgencyId == agent.AgencyId)
  83. .ToList();
  84. return LoadDisplay(bids);
  85. }
  86. else
  87. {
  88. List<BidItem> bids = _dbContext.BidItems
  89. .Include("Property")
  90. .Include("TimeshareWeek")
  91. .Include("BidMaker")
  92. .Include("Status")
  93. .Where(x => x.TimeshareWeek.AgentId == agent.Id || x.Property.AgentId == agent.Id)
  94. .ToList();
  95. return LoadDisplay(bids);
  96. }
  97. }
  98. if (user.Role.ToUpper() == "PRIVATE USER")
  99. {
  100. var individual = _dbContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
  101. List<BidItem> bids = _dbContext.BidItems
  102. .Include("Property")
  103. .Include("TimeshareWeek")
  104. .Include("BidMaker")
  105. .Include("Status")
  106. .Where(x => x.TimeshareWeek.OwnerId == individual.Id || x.Property.OwnerId == individual.Id)
  107. .ToList();
  108. return LoadDisplay(bids);
  109. }
  110. if (user.Role.ToUpper() == "SUPER ADMIN")
  111. return GetAllBid();
  112. }
  113. return null;
  114. }
  115. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  116. {
  117. List<BidItemDisplay> list = new List<BidItemDisplay>();
  118. foreach (BidItem item in bids)
  119. {
  120. BidItemDisplay bid = BidItemDisplay(item);
  121. list.Add(bid);
  122. }
  123. return list;
  124. }
  125. public void Insert(BidItem item)
  126. {
  127. var week = _dbContext.Weeks.Find(item.TimeshareWeekId);
  128. if (!(item.TimeshareWeek == null && week == null))
  129. {
  130. if (week == null && item.TimeshareWeek != null)
  131. {
  132. MyCommon.PostToConsoft(item.TimeshareWeek);
  133. WeekRepository weekRepository = new WeekRepository(_dbContext);
  134. weekRepository.Insert(item.TimeshareWeek);
  135. }
  136. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  137. if (status != null)
  138. {
  139. List<BaseEntity> list = new List<BaseEntity>() { item, week, week.Owner };
  140. item.StatusId = status.Id;
  141. if (item.TimeshareWeekId != null)
  142. {
  143. if (week != null)
  144. {
  145. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  146. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  147. if (template != null)
  148. {
  149. templateRepository.SendEmailTemplate(template, week.Owner, list);
  150. }
  151. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  152. if (template != null)
  153. {
  154. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
  155. if (bidMaker != null)
  156. {
  157. templateRepository.SendEmailTemplate(template, bidMaker, list);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. _dbContext.Add(item);
  164. Save();
  165. }
  166. }
  167. public void InsertNew(BidItemNew item)
  168. {
  169. var week = _dbContext.Weeks.Include("Owner").Where(x => x.Id == item.TimeshareWeekId).FirstOrDefault();
  170. var property = _dbContext.Properties.Include("Owner").Where(x => x.Id == item.PropertyId).FirstOrDefault();
  171. BidItem bid = new BidItem();
  172. foreach (string prop in bid.GetAllProperties())
  173. {
  174. if (prop != "Item" && prop != "Display")
  175. bid[prop] = item[prop];
  176. }
  177. bid.Id = _dbContext.GetMaxID("BidItems") + 1;
  178. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  179. if (status != null)
  180. bid.StatusId = status.Id;
  181. var individual = _dbContext.Individuals.Where(i => i.UserId == item.UserId).FirstOrDefault();
  182. if (individual != null)
  183. bid.BidMakerId = individual.Id;
  184. if (!(bid.TimeshareWeek == null && week == null))
  185. {
  186. if (week == null && bid.TimeshareWeek != null)
  187. {
  188. MyCommon.PostToConsoft(bid.TimeshareWeek);
  189. WeekRepository weekRepository = new WeekRepository(_dbContext);
  190. weekRepository.Insert(bid.TimeshareWeek);
  191. }
  192. List<BaseEntity> list = new List<BaseEntity>() { bid, week, week.Owner };
  193. if (bid.TimeshareWeekId != null)
  194. {
  195. if (week != null)
  196. {
  197. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  198. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  199. if (template != null)
  200. {
  201. templateRepository.SendEmailTemplate(template, week.Owner, list);
  202. }
  203. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  204. if (template != null)
  205. {
  206. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == bid.BidMakerId);
  207. if (bidMaker != null)
  208. {
  209. templateRepository.SendEmailTemplate(template, bidMaker, list);
  210. }
  211. }
  212. }
  213. }
  214. _dbContext.Add(bid);
  215. Save();
  216. }
  217. else
  218. {
  219. List<BaseEntity> list = new List<BaseEntity>() { bid, property, property.Owner };
  220. if (bid.PropertyId != null)
  221. {
  222. if (property != null)
  223. {
  224. var propertyStatus = _dbContext.Status.Where(s => s.Code == "P4").FirstOrDefault();
  225. if (propertyStatus != null)
  226. {
  227. property.StatusId = propertyStatus.Id;
  228. _dbContext.Update(property);
  229. }
  230. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  231. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-Owner");
  232. if (template != null)
  233. {
  234. templateRepository.SendEmailTemplate(template, property.Owner, list);
  235. }
  236. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-User");
  237. if (template != null)
  238. {
  239. if (individual != null)
  240. {
  241. templateRepository.SendEmailTemplate(template, individual, list);
  242. }
  243. }
  244. }
  245. }
  246. _dbContext.Add(bid);
  247. Save();
  248. }
  249. item.Id = bid.Id;
  250. }
  251. public void Insert(IEnumerable<BidItem> items)
  252. {
  253. foreach (var item in items)
  254. {
  255. _dbContext.Add(item);
  256. }
  257. Save();
  258. }
  259. public void Remove(BidItem item)
  260. {
  261. var i = _dbContext.BidItems.Find(item);
  262. _dbContext.BidItems.Remove(i);
  263. Save();
  264. }
  265. public void Remove(IEnumerable<BidItem> items)
  266. {
  267. foreach (var item in items)
  268. {
  269. var i = _dbContext.BidItems.Find(item);
  270. _dbContext.BidItems.Remove(i);
  271. }
  272. Save();
  273. }
  274. public void RemoveAtId(int item)
  275. {
  276. var i = _dbContext.BidItems.Find(item);
  277. _dbContext.BidItems.Remove(i);
  278. Save();
  279. }
  280. public void Save()
  281. {
  282. _dbContext.SaveChanges();
  283. }
  284. public void Update(BidItem item)
  285. {
  286. _dbContext.Entry(item).State = EntityState.Modified;
  287. Save();
  288. }
  289. public BidItemDisplay AcceptBid(int id)
  290. {
  291. var item = _dbContext.BidItems
  292. .Include("Property")
  293. .Include("TimeshareWeek")
  294. .Include("BidMaker")
  295. .Include("Status")
  296. .Where(x => x.Id == id).FirstOrDefault();
  297. var status = (from s in _dbContext.Status
  298. where s.Code == "E2"
  299. select s).FirstOrDefault();
  300. if (status != null)
  301. {
  302. item.StatusId = status.Id;
  303. }
  304. if (item.Property != null)
  305. {
  306. var propertyStatus = _dbContext.Status.Where(p => p.Code == "P5").FirstOrDefault();
  307. if (propertyStatus != null)
  308. {
  309. item.Property.StatusId = propertyStatus.Id;
  310. item.Property.Published = false;
  311. }
  312. }
  313. _dbContext.Entry(item).State = EntityState.Modified;
  314. Save();
  315. List<BidItem> bids = new List<BidItem>() { item };
  316. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  317. }
  318. public BidItemDisplay DecineBid(BitItemDecline item)
  319. {
  320. var bid = _dbContext.BidItems
  321. .Include("Property")
  322. .Include("TimeshareWeek")
  323. .Include("BidMaker")
  324. .Include("Status")
  325. .Where(x => x.Id == item.Id).FirstOrDefault();
  326. var status = (from s in _dbContext.Status
  327. where s.Code == "E3"
  328. select s).FirstOrDefault();
  329. if (status != null)
  330. {
  331. bid.StatusId = status.Id;
  332. }
  333. bid.DeclinedReason = item.Comment;
  334. if (bid.Property != null)
  335. {
  336. var propertyStatus = _dbContext.Status.Where(p => p.Code == "P3").FirstOrDefault();
  337. if (propertyStatus != null)
  338. {
  339. bid.Property.StatusId = propertyStatus.Id;
  340. }
  341. }
  342. _dbContext.Entry(bid).State = EntityState.Modified;
  343. Save();
  344. List<BidItem> bids = new List<BidItem>() { bid };
  345. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  346. }
  347. public int NewId()
  348. {
  349. // Not sure if properties need it
  350. return 0;
  351. }
  352. public BidItemDisplay GetDispaly(int id)
  353. {
  354. var bid = _dbContext.BidItems
  355. .Include("Property")
  356. .Include("TimeshareWeek")
  357. .Include("BidMaker")
  358. .Include("Status")
  359. .Where(x => x.Id == id).FirstOrDefault();
  360. return BidItemDisplay(bid);
  361. }
  362. private BidItemDisplay BidItemDisplay(BidItem item)
  363. {
  364. if (item == null)
  365. return null;
  366. BidItemDisplay bid = new BidItemDisplay()
  367. {
  368. Id = item.Id,
  369. Offer = (decimal)item.Amount,
  370. Comment = item.Comment,
  371. DeclineReason = item.DeclinedReason
  372. };
  373. if (item.PropertyId != null)
  374. {
  375. bid.Type = "Property";
  376. bid.ShortDescription = item.Property.ShortDescription;
  377. bid.Description = item.Property.Description;
  378. bid.Price = item.Property.Price;
  379. }
  380. if (item.TimeshareWeekId != null)
  381. {
  382. bid.Type = "Timeshare";
  383. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  384. bid.SellPrice = (decimal)item.TimeshareWeek.SellPrice;
  385. bid.Resort = item.TimeshareWeek.ResortName;
  386. bid.UnitNumber = item.TimeshareWeek.UnitNumber;
  387. bid.WeekNumber = item.TimeshareWeek.Module;
  388. }
  389. if (item.Status != null)
  390. {
  391. bid.StatusCode = item.Status.Code;
  392. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  393. }
  394. if (item.BidMaker != null)
  395. bid.MadeBy = (item.BidMaker.Name + " " + item.BidMaker.Surname).Trim();
  396. bid.Date = item.Created;
  397. return bid;
  398. }
  399. }
  400. }