Utils.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  5. namespace MySystem
  6. {
  7. public class Utils
  8. {
  9. public readonly static Utils Instance = new Utils();
  10. private Utils()
  11. { }
  12. /// <summary>
  13. /// 操作收支明细
  14. /// </summary>
  15. /// <param name="UserId"></param>
  16. /// <param name="Money"></param>
  17. /// <param name="ChangeType"></param>
  18. public void OpAccount(int UserId, decimal Money, int ChangeType, bool IsTotal = true)
  19. {
  20. WebCMSEntities db = new WebCMSEntities();
  21. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  22. if (account == null)
  23. {
  24. account = db.UserAccount.Add(new UserAccount()
  25. {
  26. Id = UserId,
  27. UserId = UserId,
  28. }).Entity;
  29. db.SaveChanges();
  30. }
  31. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  32. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  33. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  34. account.BalanceAmount += Money;
  35. if(IsTotal)
  36. {
  37. account.TotalAmount += Money;
  38. }
  39. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  40. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  41. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  42. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  43. {
  44. CreateDate = DateTime.Now,
  45. UpdateDate = DateTime.Now,
  46. UserId = UserId, //创客
  47. ChangeType = ChangeType, //变动类型
  48. ChangeAmount = Money, //变更金额
  49. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  50. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  51. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  52. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  53. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  54. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  55. }).Entity;
  56. db.SaveChanges();
  57. db.Dispose();
  58. }
  59. public void ToChargeAmount(int UserId, decimal Money)
  60. {
  61. WebCMSEntities db = new WebCMSEntities();
  62. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  63. if (account == null)
  64. {
  65. account = db.UserAccount.Add(new UserAccount()
  66. {
  67. Id = UserId,
  68. UserId = UserId,
  69. }).Entity;
  70. db.SaveChanges();
  71. }
  72. account.ToChargeAmount += Money;
  73. db.SaveChanges();
  74. db.Dispose();
  75. }
  76. /// <summary>
  77. /// 判断是否达标创客
  78. /// </summary>
  79. /// <param name="UserId"></param>
  80. /// <returns></returns>
  81. public bool IsStandardUser(int UserId)
  82. {
  83. WebCMSEntities db = new WebCMSEntities();
  84. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  85. if(user.UserType == 1)
  86. {
  87. return true;
  88. }
  89. if(user.LeaderLevel > 0)
  90. {
  91. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId) ?? new Leaders();
  92. if(leader.ExpiredDate > DateTime.Now)
  93. {
  94. return true;
  95. }
  96. }
  97. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  98. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  99. int couponCount = db.PosCoupons.Count(m => m.UserId == UserId && m.IsUse == 0); //1. 个人名下电签兑换券 + 大机兑换券 ≥ 3
  100. // TODO:待确认条件
  101. if(couponCount >= 3)
  102. {
  103. db.Dispose();
  104. mpdb.Dispose();
  105. mpdb2.Dispose();
  106. return true;
  107. }
  108. else
  109. {
  110. List<int> ids = new List<int>();
  111. ids.Add(10);
  112. ids.Add(11);
  113. ids.Add(77);
  114. ids.Add(78);
  115. ids.Add(79);
  116. bool orderCheck = db.Orders.Any(m => m.UserId == UserId && ids.Contains(m.ProductId)); //1. 商城下单过id为10、11、77、78、79的商品
  117. if(orderCheck)
  118. {
  119. db.Dispose();
  120. mpdb.Dispose();
  121. mpdb2.Dispose();
  122. return true;
  123. }
  124. else
  125. {
  126. //1. 激活POS机数量 + 激活来客吧商户数量 + 激活广电卡数量 ≥ 3
  127. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == UserId && m.ActivationState == 1);
  128. int ActMerchantCount = mpdb.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0) + mpdb2.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0);
  129. if(ActiveCount + ActMerchantCount >= 3)
  130. {
  131. db.Dispose();
  132. mpdb.Dispose();
  133. mpdb2.Dispose();
  134. return true;
  135. }
  136. }
  137. }
  138. db.Dispose();
  139. mpdb.Dispose();
  140. mpdb2.Dispose();
  141. return false;
  142. }
  143. /// <summary>
  144. /// 预设职级
  145. /// </summary>
  146. /// <param name="UserId"></param>
  147. /// <param name="LeaderKind"></param>
  148. public void LeaderPreUserLevel(int UserId, int UserLevel, DateTime ExpiredDate)
  149. {
  150. WebCMSEntities db = new WebCMSEntities();
  151. UserRankWhite rank = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
  152. if(rank == null)
  153. {
  154. rank = db.UserRankWhite.Add(new UserRankWhite()
  155. {
  156. CreateDate = DateTime.Now, //设置时间
  157. UserId = UserId, //用户
  158. Id = UserId,
  159. }).Entity;
  160. db.SaveChanges();
  161. }
  162. rank.Rank = UserLevel;
  163. rank.UpdateDate = ExpiredDate;
  164. db.SaveChanges();
  165. db.Dispose();
  166. }
  167. #region 获取创客职级
  168. public int GetUserLevel(int Id)
  169. {
  170. WebCMSEntities db = new WebCMSEntities();
  171. DateTime now = DateTime.Now;
  172. List<int> Levels = new List<int>();
  173. Users user = db.Users.FirstOrDefault(m => m.Id == Id) ?? new Users();
  174. UserRankWhite userRank = db.UserRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new UserRankWhite();
  175. LeaderRankWhite leaderRank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new LeaderRankWhite();
  176. OperatorRankWhite operatorRank = db.OperatorRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new OperatorRankWhite();
  177. Levels.Add(user.UserLevel);
  178. Levels.Add(userRank.Rank);
  179. Levels.Add(leaderRank.Rank);
  180. Levels.Add(operatorRank.Rank);
  181. db.Dispose();
  182. return Levels.Max();
  183. }
  184. #endregion
  185. }
  186. }