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

BidRepository.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Model;
  8. using UnivateProperties_API.Model.Communication;
  9. using UnivateProperties_API.Model.ProcessFlow;
  10. using UnivateProperties_API.Repository.Communication;
  11. namespace UnivateProperties_API.Repository.ProccessFlow
  12. {
  13. public class BidRepository : IBidRepository
  14. {
  15. private readonly DataContext _dbContext;
  16. public BidRepository(DataContext dbContext)
  17. {
  18. _dbContext = dbContext;
  19. }
  20. public List<BidItem> Get(Func<BidItem, bool> where)
  21. {
  22. return _dbContext.BidItems.Where(where).ToList();
  23. }
  24. public List<BidItem> GetAll()
  25. {
  26. return _dbContext.BidItems.ToList();
  27. }
  28. public BidItem GetDetailed(Func<BidItem, bool> first)
  29. {
  30. var item = _dbContext.BidItems.FirstOrDefault(first);
  31. return item;
  32. }
  33. public List<BidItem> GetDetailedAll()
  34. {
  35. return GetAll();
  36. }
  37. public List<BidItemDisplay> GetAllBid()
  38. {
  39. List<BidItem> bids = _dbContext.BidItems
  40. .Include("Property")
  41. .Include("TimeshareWeek")
  42. .Include("BidMaker")
  43. .Include("Status")
  44. .Include("Property.Owner")
  45. .Include("Property.Agent")
  46. .ToList();
  47. return LoadDisplay(bids);
  48. }
  49. public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
  50. {
  51. List<BidItem> bids = _dbContext.BidItems
  52. .Include("Property")
  53. .Include("TimeshareWeek")
  54. .Include("BidMaker")
  55. .Include("Status")
  56. .Where(where)
  57. .ToList();
  58. return LoadDisplay(bids);
  59. }
  60. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  61. {
  62. List<BidItemDisplay> list = new List<BidItemDisplay>();
  63. foreach (BidItem item in bids)
  64. {
  65. BidItemDisplay bid = new BidItemDisplay()
  66. {
  67. Id = item.Id,
  68. Offer = (decimal)item.Amount,
  69. Comment = item.Comment,
  70. DeclineReason = item.DeclinedReason
  71. };
  72. if (item.PropertyId != null)
  73. {
  74. bid.Type = "Property";
  75. bid.ShortDescription = item.Property.ShortDescription;
  76. bid.Description = item.Property.Description;
  77. bid.Price = item.Property.Price;
  78. }
  79. if (item.TimeshareWeekId != null)
  80. {
  81. bid.Type = "Timeshare";
  82. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  83. bid.Price = (decimal)item.TimeshareWeek.SellPrice;
  84. bid.Resort = item.TimeshareWeek.ResortName;
  85. bid.Unit = item.TimeshareWeek.UnitNumber;
  86. bid.Module = item.TimeshareWeek.Module;
  87. }
  88. if (item.Status != null)
  89. {
  90. bid.StatusCode = item.Status.Code;
  91. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  92. }
  93. if (item.BidMaker != null)
  94. bid.MadeBy = item.BidMaker.Name + " " + item.BidMaker.Surname;
  95. bid.MadeBy = "Bob";
  96. list.Add(bid);
  97. }
  98. return list;
  99. }
  100. public void Insert(BidItem item)
  101. {
  102. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  103. if (status != null)
  104. {
  105. List<BaseEntity> list = new List<BaseEntity>() { item };
  106. item.StatusId = status.Id;
  107. if (item.TimeshareWeekId != null)
  108. {
  109. var week = _dbContext.Weeks.FirstOrDefault(x => x.Id == item.TimeshareWeekId);
  110. if (week != null)
  111. {
  112. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  113. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  114. if (template != null)
  115. {
  116. templateRepository.SendEmailTemplate(template, week, week.Owner, week.Owner, list);
  117. }
  118. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  119. if (template != null)
  120. {
  121. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
  122. if(bidMaker != null)
  123. {
  124. templateRepository.SendEmailTemplate(template, week, week.Owner, bidMaker, list);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. _dbContext.Add(item);
  131. Save();
  132. }
  133. public void Insert(IEnumerable<BidItem> items)
  134. {
  135. foreach (var item in items)
  136. {
  137. _dbContext.Add(item);
  138. }
  139. Save();
  140. }
  141. public void Remove(BidItem item)
  142. {
  143. var i = _dbContext.BidItems.Find(item);
  144. _dbContext.BidItems.Remove(i);
  145. Save();
  146. }
  147. public void Remove(IEnumerable<BidItem> items)
  148. {
  149. foreach (var item in items)
  150. {
  151. var i = _dbContext.BidItems.Find(item);
  152. _dbContext.BidItems.Remove(i);
  153. }
  154. Save();
  155. }
  156. public void RemoveAtId(int item)
  157. {
  158. var i = _dbContext.BidItems.Find(item);
  159. _dbContext.BidItems.Remove(i);
  160. Save();
  161. }
  162. public void Save()
  163. {
  164. _dbContext.SaveChanges();
  165. }
  166. public void Update(BidItem item)
  167. {
  168. _dbContext.Entry(item).State = EntityState.Modified;
  169. Save();
  170. }
  171. public BidItemDisplay AcceptBid(int id)
  172. {
  173. var item = _dbContext.BidItems
  174. .Include("Property")
  175. .Include("TimeshareWeek")
  176. .Include("BidMaker")
  177. .Include("Status")
  178. .Where(x => x.Id == id).FirstOrDefault();
  179. var status = (from s in _dbContext.Status
  180. where s.Code == "E2"
  181. select s).FirstOrDefault();
  182. if (status != null)
  183. {
  184. item.StatusId = status.Id;
  185. }
  186. _dbContext.Entry(item).State = EntityState.Modified;
  187. Save();
  188. List<BidItem> bids = new List<BidItem>() { item };
  189. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  190. }
  191. public BidItemDisplay DecineBid(BitItemDecline item)
  192. {
  193. var bid = _dbContext.BidItems
  194. .Include("Property")
  195. .Include("TimeshareWeek")
  196. .Include("BidMaker")
  197. .Include("Status")
  198. .Where(x => x.Id == item.Id).FirstOrDefault();
  199. var status = (from s in _dbContext.Status
  200. where s.Code == "E3"
  201. select s).FirstOrDefault();
  202. if (status != null)
  203. {
  204. bid.StatusId = status.Id;
  205. }
  206. bid.DeclinedReason = item.Comment;
  207. _dbContext.Entry(bid).State = EntityState.Modified;
  208. Save();
  209. List<BidItem> bids = new List<BidItem>() { bid };
  210. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  211. }
  212. public int NewId()
  213. {
  214. // Not sure if properties need it
  215. return 0;
  216. }
  217. }
  218. }