LeaderApplyCouponsHelper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. dosomething(Id);
  34. // WebCMSEntities db = new WebCMSEntities();
  35. // var LeaderUserId = 0;
  36. // var queryList = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id) ?? new LeaderReserveRecord();
  37. // var userInfo = db.Users.FirstOrDefault(m => m.Id == queryList.UserId) ?? new Users();
  38. // if (userInfo.LeaderLevel == 2)
  39. // {
  40. // LeaderUserId = queryList.UserId;
  41. // }
  42. // else
  43. // {
  44. // string[] ParentNavList = userInfo.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  45. // for (int j = ParentNavList.Length; j > 0; j--)
  46. // {
  47. // int userId = int.Parse(ParentNavList[j - 1]);
  48. // var user = db.Users.FirstOrDefault(m => m.Id == userId) ?? new Users();
  49. // //如果是小盟主,则查找该小盟主关联的大盟主储蓄金是否充足,是则给券打上关联大盟主标记
  50. // if (user.LeaderLevel == 2)
  51. // {
  52. // LeaderUserId = userId;
  53. // break;
  54. // }
  55. // else
  56. // {
  57. // continue;
  58. // }
  59. // }
  60. // }
  61. // JsonData ApplyList = JsonMapper.ToObject(queryList.SeoTitle);
  62. // var SnList = "";
  63. // for (int i = 0; i < ApplyList.Count; i++)
  64. // {
  65. // int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  66. // int type = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  67. // //电签
  68. // if (type == 1)
  69. // {
  70. // 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();//电签券
  71. // foreach (var item in dposCoupons)
  72. // {
  73. // var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == item.Id) ?? new PosCoupons();
  74. // posCoupon.UserId = queryList.UserId;
  75. // posCoupon.LeaderUserId = LeaderUserId;
  76. // // SnList += posCoupon.ExchangeCode + ",";
  77. // db.UserSnDelayChange.Add(new UserSnDelayChange()
  78. // {
  79. // CreateDate = DateTime.Now,
  80. // QueryCount = Id,
  81. // SnNo = posCoupon.ExchangeCode,
  82. // });
  83. // }
  84. // }
  85. // //大机
  86. // if (type == 2)
  87. // {
  88. // 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();//大机券
  89. // foreach (var items in bposCoupons)
  90. // {
  91. // var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == items.Id) ?? new PosCoupons();
  92. // posCoupon.UserId = queryList.UserId;
  93. // posCoupon.LeaderUserId = LeaderUserId;
  94. // // SnList += posCoupon.ExchangeCode + ",";
  95. // db.UserSnDelayChange.Add(new UserSnDelayChange()
  96. // {
  97. // CreateDate = DateTime.Now,
  98. // QueryCount = Id,
  99. // SnNo = posCoupon.ExchangeCode,
  100. // });
  101. // }
  102. // }
  103. // db.SaveChanges();
  104. // }
  105. // // queryList.SeoDescription = SnList.TrimEnd(',');
  106. // db.SaveChanges();
  107. }
  108. catch (Exception ex)
  109. {
  110. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "盟主兑换机具券异常");
  111. }
  112. }
  113. else
  114. {
  115. Thread.Sleep(5000);
  116. }
  117. }
  118. }
  119. private void dosomething(int Id)
  120. {
  121. WebCMSEntities db = new WebCMSEntities();
  122. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  123. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  124. if(query != null)
  125. {
  126. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  127. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(db, query.UserId);
  128. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  129. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  130. Array.Reverse(ParentNavList);
  131. int index = 0;
  132. bool BuyPrizeFlag = false; //购机奖励发放标识
  133. decimal BuyPrize = 0; //购机奖励
  134. foreach(string ParentId in ParentNavList)
  135. {
  136. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(db, int.Parse(ParentId));
  137. index += 1;
  138. //给券打大盟主标记
  139. if(parentUser.LeaderLevel == 2 && index == 1)
  140. {
  141. for (int i = 0; i < ApplyList.Count; i++)
  142. {
  143. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  144. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  145. List<int> couponIds = db.PosCoupons.Where(m => m.IsLock == 0 && m.IsUse == 0 && m.UserId == 0 && m.QueryCount == Kind).OrderBy(m => m.Id).Take(num).ToList().Select(m => m.Id).ToList();
  146. PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / 3, couponIds);
  147. }
  148. }
  149. if(parentUser.OperateLevel > 0 && !BuyPrizeFlag)
  150. {
  151. int groupCount = 0;
  152. for (int i = 0; i < ApplyList.Count; i++)
  153. {
  154. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  155. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  156. groupCount += num / 3;
  157. }
  158. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  159. {
  160. //购机奖
  161. decimal CurBuyPrize = GetBuyPrize(parentUser);
  162. PosCouponPrizeService.Instance.OpAccount(db, 0, parentUser.Id, CurBuyPrize - BuyPrize, groupCount);
  163. BuyPrizeFlag = true;
  164. BuyPrize = CurBuyPrize;
  165. }
  166. }
  167. }
  168. }
  169. db.Dispose();
  170. opdb.Dispose();
  171. }
  172. /// <summary>
  173. /// 获取购机奖金额
  174. /// </summary>
  175. /// <param name="user"></param>
  176. /// <returns></returns>
  177. public decimal GetBuyPrize(UserRankItem user)
  178. {
  179. if(user.OperateLevel == 2) return 60;
  180. if(user.OperateLevel == 3) return 80;
  181. if(user.OperateLevel == 4) return 100;
  182. return 0;
  183. }
  184. }
  185. }