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

BidRepository.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
  62. {
  63. List<BidItemDisplay> list = new List<BidItemDisplay>();
  64. foreach (BidItem item in bids)
  65. {
  66. BidItemDisplay bid = BidItemDisplay(item);
  67. list.Add(bid);
  68. }
  69. return list;
  70. }
  71. public void Insert(BidItem item)
  72. {
  73. var week = _dbContext.Weeks.Find(item.TimeshareWeekId);
  74. if (!(item.TimeshareWeek == null && week == null))
  75. {
  76. if (week == null && item.TimeshareWeek != null)
  77. {
  78. MyCommon.PostToConsoft(item.TimeshareWeek);
  79. WeekRepository weekRepository = new WeekRepository(_dbContext);
  80. weekRepository.Insert(item.TimeshareWeek);
  81. }
  82. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  83. if (status != null)
  84. {
  85. List<BaseEntity> list = new List<BaseEntity>() { item, week, week.Owner };
  86. item.StatusId = status.Id;
  87. if (item.TimeshareWeekId != null)
  88. {
  89. if (week != null)
  90. {
  91. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  92. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  93. if (template != null)
  94. {
  95. templateRepository.SendEmailTemplate(template, week.Owner, list);
  96. }
  97. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  98. if (template != null)
  99. {
  100. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
  101. if (bidMaker != null)
  102. {
  103. templateRepository.SendEmailTemplate(template, bidMaker, list);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. _dbContext.Add(item);
  110. Save();
  111. }
  112. }
  113. public void InsertNew(BidItemNew item)
  114. {
  115. var week = _dbContext.Weeks.Include("Owner").Where(x => x.Id == item.TimeshareWeekId).FirstOrDefault();
  116. var property = _dbContext.Properties.Include("Owner").Where(x => x.Id == item.PropertyId).FirstOrDefault();
  117. BidItem bid = new BidItem();
  118. foreach (string prop in bid.GetAllProperties())
  119. {
  120. if (prop != "Item" && prop != "Display")
  121. bid[prop] = item[prop];
  122. }
  123. var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
  124. if (status != null)
  125. bid.StatusId = status.Id;
  126. var individual = _dbContext.Individuals.Where(i => i.UserId == item.UserId).FirstOrDefault();
  127. if (individual != null)
  128. bid.BidMakerId = individual.Id;
  129. if (!(bid.TimeshareWeek == null && week == null))
  130. {
  131. if (week == null && bid.TimeshareWeek != null)
  132. {
  133. MyCommon.PostToConsoft(bid.TimeshareWeek);
  134. WeekRepository weekRepository = new WeekRepository(_dbContext);
  135. weekRepository.Insert(bid.TimeshareWeek);
  136. }
  137. List<BaseEntity> list = new List<BaseEntity>() { bid, week, week.Owner };
  138. if (bid.TimeshareWeekId != null)
  139. {
  140. if (week != null)
  141. {
  142. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  143. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
  144. if (template != null)
  145. {
  146. templateRepository.SendEmailTemplate(template, week.Owner, list);
  147. }
  148. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
  149. if (template != null)
  150. {
  151. var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == bid.BidMakerId);
  152. if (bidMaker != null)
  153. {
  154. templateRepository.SendEmailTemplate(template, bidMaker, list);
  155. }
  156. }
  157. }
  158. }
  159. _dbContext.Add(bid);
  160. Save();
  161. }
  162. else
  163. {
  164. List<BaseEntity> list = new List<BaseEntity>() { bid, property, property.Owner };
  165. if (bid.PropertyId != null)
  166. {
  167. if (property != null)
  168. {
  169. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  170. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-Owner");
  171. if (template != null)
  172. {
  173. templateRepository.SendEmailTemplate(template, property.Owner, list);
  174. }
  175. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "PropertyOfferMade-User");
  176. if (template != null)
  177. {
  178. if (individual != null)
  179. {
  180. templateRepository.SendEmailTemplate(template, individual, list);
  181. }
  182. }
  183. }
  184. }
  185. _dbContext.Add(bid);
  186. Save();
  187. }
  188. }
  189. public void Insert(IEnumerable<BidItem> items)
  190. {
  191. foreach (var item in items)
  192. {
  193. _dbContext.Add(item);
  194. }
  195. Save();
  196. }
  197. public void Remove(BidItem item)
  198. {
  199. var i = _dbContext.BidItems.Find(item);
  200. _dbContext.BidItems.Remove(i);
  201. Save();
  202. }
  203. public void Remove(IEnumerable<BidItem> items)
  204. {
  205. foreach (var item in items)
  206. {
  207. var i = _dbContext.BidItems.Find(item);
  208. _dbContext.BidItems.Remove(i);
  209. }
  210. Save();
  211. }
  212. public void RemoveAtId(int item)
  213. {
  214. var i = _dbContext.BidItems.Find(item);
  215. _dbContext.BidItems.Remove(i);
  216. Save();
  217. }
  218. public void Save()
  219. {
  220. _dbContext.SaveChanges();
  221. }
  222. public void Update(BidItem item)
  223. {
  224. _dbContext.Entry(item).State = EntityState.Modified;
  225. Save();
  226. }
  227. public BidItemDisplay AcceptBid(int id)
  228. {
  229. var item = _dbContext.BidItems
  230. .Include("Property")
  231. .Include("TimeshareWeek")
  232. .Include("BidMaker")
  233. .Include("Status")
  234. .Where(x => x.Id == id).FirstOrDefault();
  235. var status = (from s in _dbContext.Status
  236. where s.Code == "E2"
  237. select s).FirstOrDefault();
  238. if (status != null)
  239. {
  240. item.StatusId = status.Id;
  241. }
  242. _dbContext.Entry(item).State = EntityState.Modified;
  243. Save();
  244. List<BidItem> bids = new List<BidItem>() { item };
  245. return LoadDisplay(bids).Find(x => x.Id == item.Id);
  246. }
  247. public BidItemDisplay DecineBid(BitItemDecline item)
  248. {
  249. var bid = _dbContext.BidItems
  250. .Include("Property")
  251. .Include("TimeshareWeek")
  252. .Include("BidMaker")
  253. .Include("Status")
  254. .Where(x => x.Id == item.Id).FirstOrDefault();
  255. var status = (from s in _dbContext.Status
  256. where s.Code == "E3"
  257. select s).FirstOrDefault();
  258. if (status != null)
  259. {
  260. bid.StatusId = status.Id;
  261. }
  262. bid.DeclinedReason = item.Comment;
  263. _dbContext.Entry(bid).State = EntityState.Modified;
  264. Save();
  265. List<BidItem> bids = new List<BidItem>() { bid };
  266. return LoadDisplay(bids).Find(x => x.Id == bid.Id);
  267. }
  268. public int NewId()
  269. {
  270. // Not sure if properties need it
  271. return 0;
  272. }
  273. public BidItemDisplay GetDispaly(int id)
  274. {
  275. var bid = _dbContext.BidItems
  276. .Include("Property")
  277. .Include("TimeshareWeek")
  278. .Include("BidMaker")
  279. .Include("Status")
  280. .Where(x => x.Id == id).FirstOrDefault();
  281. return BidItemDisplay(bid);
  282. }
  283. private BidItemDisplay BidItemDisplay(BidItem item)
  284. {
  285. if (item == null)
  286. return null;
  287. BidItemDisplay bid = new BidItemDisplay()
  288. {
  289. Id = item.Id,
  290. Offer = (decimal)item.Amount,
  291. Comment = item.Comment,
  292. DeclineReason = item.DeclinedReason
  293. };
  294. if (item.PropertyId != null)
  295. {
  296. bid.Type = "Property";
  297. bid.ShortDescription = item.Property.ShortDescription;
  298. bid.Description = item.Property.Description;
  299. bid.Price = item.Property.Price;
  300. }
  301. if (item.TimeshareWeekId != null)
  302. {
  303. bid.Type = "Timeshare";
  304. bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
  305. bid.SellPrice = (decimal)item.TimeshareWeek.SellPrice;
  306. bid.Resort = item.TimeshareWeek.ResortName;
  307. bid.UnitNumber = item.TimeshareWeek.UnitNumber;
  308. bid.WeekNumber = item.TimeshareWeek.Module;
  309. }
  310. if (item.Status != null)
  311. {
  312. bid.StatusCode = item.Status.Code;
  313. bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
  314. }
  315. if (item.BidMaker != null)
  316. bid.MadeBy = (item.BidMaker.Name + " " + item.BidMaker.Surname).Trim();
  317. bid.Date = item.Created;
  318. return bid;
  319. }
  320. }
  321. }