LeaderApplyCouponsHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using MySystem.PxcModels;
  7. using LitJson;
  8. using System.Collections.Generic;
  9. namespace MySystem
  10. {
  11. public class LeaderApplyCouponsHelper
  12. {
  13. public readonly static LeaderApplyCouponsHelper Instance = new LeaderApplyCouponsHelper();
  14. private LeaderApplyCouponsHelper()
  15. {
  16. }
  17. public void Start()//启动
  18. {
  19. Thread thread = new Thread(doSomething);
  20. thread.IsBackground = true;
  21. thread.Start();
  22. }
  23. public void doSomething()
  24. {
  25. while (true)
  26. {
  27. string content = RedisDbconn.Instance.RPop<string>("LeaderApplyCouponsQueue");
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. try
  31. {
  32. var Id = int.Parse(content);
  33. WebCMSEntities db = new WebCMSEntities();
  34. var queryList = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id) ?? new LeaderReserveRecord();
  35. List<int> couponIds = new List<int>();
  36. JsonData ApplyList = JsonMapper.ToObject(queryList.SeoTitle);
  37. for (int i = 0; i < ApplyList.Count; i++)
  38. {
  39. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  40. int type = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  41. //电签
  42. if (type == 1)
  43. {
  44. var dposCoupons = db.PosCoupons.Where(m => m.QueryCount == 1 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.OpId == 0).Take(num).ToList();//电签券
  45. foreach (var item in dposCoupons)
  46. {
  47. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == item.Id) ?? new PosCoupons();
  48. posCoupon.UserId = queryList.UserId;
  49. db.UserSnDelayChange.Add(new UserSnDelayChange()
  50. {
  51. CreateDate = DateTime.Now,
  52. QueryCount = Id,
  53. SnNo = posCoupon.ExchangeCode,
  54. });
  55. couponIds.Add(item.Id);
  56. }
  57. }
  58. //大机
  59. if (type == 2)
  60. {
  61. var bposCoupons = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.OpId == 0).Take(num).ToList();//大机券
  62. foreach (var items in bposCoupons)
  63. {
  64. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == items.Id) ?? new PosCoupons();
  65. posCoupon.UserId = queryList.UserId;
  66. db.UserSnDelayChange.Add(new UserSnDelayChange()
  67. {
  68. CreateDate = DateTime.Now,
  69. QueryCount = Id,
  70. SnNo = posCoupon.ExchangeCode,
  71. });
  72. couponIds.Add(items.Id);
  73. }
  74. }
  75. db.SaveChanges();
  76. }
  77. db.SaveChanges();
  78. dosomething(Id, couponIds);
  79. }
  80. catch (Exception ex)
  81. {
  82. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "盟主兑换机具券异常");
  83. }
  84. }
  85. else
  86. {
  87. Thread.Sleep(5000);
  88. }
  89. }
  90. }
  91. public void dosomething(int Id, List<int> couponIds)
  92. {
  93. WebCMSEntities db = new WebCMSEntities();
  94. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  95. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  96. if(query != null)
  97. {
  98. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  99. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(db, query.UserId);
  100. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  101. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  102. Array.Reverse(ParentNavList);
  103. int index = 0;
  104. bool BigLeaderFlag = false; //大盟主标记
  105. decimal BuyPrize = 0; //购机奖励
  106. foreach(string ParentId in ParentNavList)
  107. {
  108. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(db, int.Parse(ParentId));
  109. index += 1;
  110. //给券打大盟主标记
  111. if(parentUser.LeaderLevel == 2 && !BigLeaderFlag)
  112. {
  113. for (int i = 0; i < ApplyList.Count; i++)
  114. {
  115. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  116. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  117. int Step = 0;
  118. if(Kind == 1)
  119. {
  120. Step = 3;
  121. }
  122. else
  123. {
  124. Step = 2;
  125. }
  126. int result = PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / Step, couponIds);
  127. if(result > 0) BigLeaderFlag = true;
  128. }
  129. }
  130. if(parentUser.OperateLevel > 0)
  131. {
  132. int groupCount = 0;
  133. for (int i = 0; i < ApplyList.Count; i++)
  134. {
  135. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  136. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  137. if(Kind == 1)
  138. {
  139. groupCount += num / 3;
  140. }
  141. else
  142. {
  143. groupCount += num / 2;
  144. }
  145. }
  146. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  147. {
  148. //购机奖
  149. decimal CurBuyPrize = GetBuyPrize(parentUser);
  150. decimal GetPrize = CurBuyPrize - BuyPrize;
  151. int Kind = 0;
  152. if(query.Remark == "兑换机具券")
  153. {
  154. Kind = 101;
  155. }
  156. if(GetPrize > 0) PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  157. BuyPrize = CurBuyPrize;
  158. }
  159. }
  160. }
  161. }
  162. db.Dispose();
  163. opdb.Dispose();
  164. }
  165. /// <summary>
  166. /// 获取购机奖金额
  167. /// </summary>
  168. /// <param name="user"></param>
  169. /// <returns></returns>
  170. public decimal GetBuyPrize(UserRankItem user)
  171. {
  172. if(user.OperateLevel == 2) return 60;
  173. if(user.OperateLevel == 3) return 80;
  174. if(user.OperateLevel == 4) return 100;
  175. return 0;
  176. }
  177. }
  178. }