UserTradeStatDbconn.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using System.Linq;
  5. using MySystem.MainModels;
  6. namespace MySystem
  7. {
  8. public class UserTradeStatDbconn
  9. {
  10. public readonly static UserTradeStatDbconn Instance = new UserTradeStatDbconn();
  11. public UserTradeStatDbconn()
  12. { }
  13. #region 获取单个字段
  14. //个人业绩
  15. public UserTradeData GetTradeByDate(int UserId, string TradeDate, string kind = "self")
  16. {
  17. string key = "UserTradeAmt:" + UserId + ":" + TradeDate + ":" + kind; //总交易
  18. if (kind == "team")
  19. {
  20. key = "Team" + key;
  21. }
  22. UserTradeData obj = RedisDbconn.Instance.Get<UserTradeData>(key);
  23. if (obj != null)
  24. {
  25. return obj;
  26. }
  27. obj = new UserTradeData();
  28. WebCMSEntities db = new WebCMSEntities();
  29. bool check = db.UserTradeDaySummary.Any(m => m.UserId == UserId && m.SeoTitle == kind);
  30. if (check)
  31. {
  32. List<UserTradeDaySummary> data = db.UserTradeDaySummary.Where(m => m.UserId == UserId && m.TradeDate == TradeDate && m.SeoTitle == kind).ToList();
  33. if (kind == "self")
  34. {
  35. obj.TotalAmt = data.Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  36. if (data.Where(m => m.QueryCount == 0).Count() > 0)
  37. {
  38. obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  39. }
  40. else if (data.Where(m => m.QueryCount == 1).Count() > 0)
  41. {
  42. obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  43. }
  44. obj.Brands = new List<Brand>();
  45. foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
  46. {
  47. if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
  48. {
  49. obj.Brands.Add(new Brand()
  50. {
  51. Id = sub.BrandId,
  52. Amt = sub.DirectTradeAmt + sub.DirectDebitTradeAmt,
  53. });
  54. }
  55. else
  56. {
  57. obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.DirectTradeAmt + sub.DirectDebitTradeAmt;
  58. }
  59. }
  60. }
  61. else if (kind == "team")
  62. {
  63. obj.TotalAmt = data.Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  64. if (data.Where(m => m.QueryCount == 0).Count() > 0)
  65. {
  66. obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  67. }
  68. else if (data.Where(m => m.QueryCount == 1).Count() > 0)
  69. {
  70. obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  71. }
  72. obj.Brands = new List<Brand>();
  73. foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
  74. {
  75. if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
  76. {
  77. obj.Brands.Add(new Brand()
  78. {
  79. Id = sub.BrandId,
  80. Amt = sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt,
  81. });
  82. }
  83. else
  84. {
  85. obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt;
  86. }
  87. }
  88. }
  89. }
  90. RedisDbconn.Instance.Set(key, obj);
  91. db.Dispose();
  92. return obj;
  93. }
  94. public UserTradeData GetTradeByMonth(int UserId, string TradeMonth, string kind = "self")
  95. {
  96. string key = "UserTradeAmt:" + UserId + ":" + TradeMonth + ":" + kind; //总交易
  97. if (kind == "team")
  98. {
  99. key = "Team" + key;
  100. }
  101. UserTradeData obj = RedisDbconn.Instance.Get<UserTradeData>(key);
  102. if (obj != null)
  103. {
  104. return obj;
  105. }
  106. obj = new UserTradeData();
  107. WebCMSEntities db = new WebCMSEntities();
  108. bool check = db.UserTradeDaySummary.Any(m => m.UserId == UserId && m.SeoTitle == kind);
  109. if (check)
  110. {
  111. List<UserTradeDaySummary> data = db.UserTradeDaySummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).ToList();
  112. if (kind == "self")
  113. {
  114. obj.TotalAmt = data.Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  115. if (data.Where(m => m.QueryCount == 0).Count() > 0)
  116. {
  117. obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  118. }
  119. else if (data.Where(m => m.QueryCount == 1).Count() > 0)
  120. {
  121. obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
  122. }
  123. obj.Brands = new List<Brand>();
  124. foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
  125. {
  126. if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
  127. {
  128. obj.Brands.Add(new Brand()
  129. {
  130. Id = sub.BrandId,
  131. Amt = sub.DirectTradeAmt + sub.DirectDebitTradeAmt,
  132. });
  133. }
  134. else
  135. {
  136. obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.DirectTradeAmt + sub.DirectDebitTradeAmt;
  137. }
  138. }
  139. }
  140. else if (kind == "team")
  141. {
  142. obj.TotalAmt = data.Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  143. if (data.Where(m => m.QueryCount == 0).Count() > 0)
  144. {
  145. obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  146. }
  147. else if (data.Where(m => m.QueryCount == 1).Count() > 0)
  148. {
  149. obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
  150. }
  151. obj.Brands = new List<Brand>();
  152. foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
  153. {
  154. if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
  155. {
  156. obj.Brands.Add(new Brand()
  157. {
  158. Id = sub.BrandId,
  159. Amt = sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt,
  160. });
  161. }
  162. else
  163. {
  164. obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt;
  165. }
  166. }
  167. }
  168. }
  169. RedisDbconn.Instance.Set(key, obj);
  170. db.Dispose();
  171. return obj;
  172. }
  173. #endregion
  174. }
  175. }