Utils.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. UserRankItem user = PosCouponPrizeService.Instance.GetUserLevel(db, UserId);
  86. if(user.UserType == 1)
  87. {
  88. return true;
  89. }
  90. if(user.LeaderLevel > 0)
  91. {
  92. Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId) ?? new Leaders();
  93. if(leader.ExpiredDate > DateTime.Now)
  94. {
  95. return true;
  96. }
  97. }
  98. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  99. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  100. int couponCount = db.PosCoupons.Count(m => m.UserId == UserId && m.IsUse == 0); //1. 个人名下电签兑换券 + 大机兑换券 ≥ 3
  101. // TODO:待确认条件
  102. if(couponCount >= 3)
  103. {
  104. db.Dispose();
  105. mpdb.Dispose();
  106. mpdb2.Dispose();
  107. return true;
  108. }
  109. else
  110. {
  111. List<int> ids = new List<int>();
  112. ids.Add(10);
  113. ids.Add(11);
  114. ids.Add(77);
  115. ids.Add(78);
  116. ids.Add(79);
  117. bool orderCheck = db.Orders.Any(m => m.UserId == UserId && ids.Contains(m.ProductId)); //1. 商城下单过id为10、11、77、78、79的商品
  118. if(orderCheck)
  119. {
  120. db.Dispose();
  121. mpdb.Dispose();
  122. mpdb2.Dispose();
  123. return true;
  124. }
  125. else
  126. {
  127. //1. 激活POS机数量 + 激活来客吧商户数量 + 激活广电卡数量 ≥ 3
  128. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == UserId && m.ActivationState == 1);
  129. int ActMerchantCount = mpdb.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0) + mpdb2.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0);
  130. if(ActiveCount + ActMerchantCount >= 3)
  131. {
  132. db.Dispose();
  133. mpdb.Dispose();
  134. mpdb2.Dispose();
  135. return true;
  136. }
  137. }
  138. }
  139. db.Dispose();
  140. mpdb.Dispose();
  141. mpdb2.Dispose();
  142. return false;
  143. }
  144. /// <summary>
  145. /// 预设职级
  146. /// </summary>
  147. /// <param name="UserId"></param>
  148. /// <param name="LeaderKind"></param>
  149. public void LeaderPreUserLevel(int UserId, int UserLevel, DateTime ExpiredDate)
  150. {
  151. WebCMSEntities db = new WebCMSEntities();
  152. UserRankWhite rank = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
  153. if(rank == null)
  154. {
  155. rank = db.UserRankWhite.Add(new UserRankWhite()
  156. {
  157. CreateDate = DateTime.Now, //设置时间
  158. UserId = UserId, //用户
  159. Id = UserId,
  160. }).Entity;
  161. db.SaveChanges();
  162. }
  163. rank.Rank = UserLevel;
  164. rank.UpdateDate = ExpiredDate;
  165. db.SaveChanges();
  166. db.Dispose();
  167. }
  168. #region 获取创客职级
  169. public int GetUserLevel(int Id)
  170. {
  171. WebCMSEntities db = new WebCMSEntities();
  172. DateTime now = DateTime.Now;
  173. List<int> Levels = new List<int>();
  174. Users user = db.Users.FirstOrDefault(m => m.Id == Id) ?? new Users();
  175. UserRankWhite userRank = db.UserRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new UserRankWhite();
  176. LeaderRankWhite leaderRank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new LeaderRankWhite();
  177. OperatorRankWhite operatorRank = db.OperatorRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new OperatorRankWhite();
  178. Levels.Add(user.UserLevel);
  179. Levels.Add(userRank.Rank);
  180. Levels.Add(leaderRank.Rank);
  181. Levels.Add(operatorRank.Rank);
  182. db.Dispose();
  183. return Levels.Max();
  184. }
  185. #endregion
  186. }
  187. }