LeaderApplyCouponsHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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(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(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, query.UserId);
  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. public void addprize(int Id)
  166. {
  167. WebCMSEntities db = new WebCMSEntities();
  168. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  169. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  170. if(query != null)
  171. {
  172. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  173. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  174. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  175. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  176. Array.Reverse(ParentNavList);
  177. int index = 0;
  178. bool BigLeaderFlag = false; //大盟主标记
  179. decimal BuyPrize = 0; //购机奖励
  180. foreach(string ParentId in ParentNavList)
  181. {
  182. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  183. index += 1;
  184. if(parentUser.OperateLevel > 0)
  185. {
  186. int groupCount = 0;
  187. for (int i = 0; i < ApplyList.Count; i++)
  188. {
  189. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  190. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  191. if(Kind == 1)
  192. {
  193. groupCount += num / 3;
  194. }
  195. else
  196. {
  197. groupCount += num / 2;
  198. }
  199. }
  200. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  201. {
  202. //购机奖
  203. decimal CurBuyPrize = GetBuyPrize(parentUser, query.UserId);
  204. decimal GetPrize = CurBuyPrize - BuyPrize;
  205. int Kind = 0;
  206. if(query.Remark == "兑换机具券")
  207. {
  208. Kind = 101;
  209. }
  210. if(GetPrize > 0 && !db.UserAccountRecord.Any(m => m.QueryCount == Id && m.ChangeType == 128))
  211. {
  212. PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  213. }
  214. BuyPrize = CurBuyPrize;
  215. }
  216. }
  217. }
  218. }
  219. db.Dispose();
  220. opdb.Dispose();
  221. }
  222. /// <summary>
  223. /// 获取购机奖金额
  224. /// </summary>
  225. /// <param name="user"></param>
  226. /// <returns></returns>
  227. public decimal GetBuyPrize(UserRankItem user, int UserId = 0)
  228. {
  229. if(user.OperateLevel == 2)
  230. {
  231. if(PosCouponPrizeService.Instance.TradeTeamCheck2(user.Id))
  232. {
  233. return 60;
  234. }
  235. if(!PosCouponPrizeService.Instance.TradeTeamCheck(UserId))
  236. {
  237. return 0;
  238. }
  239. return 60;
  240. }
  241. if(user.OperateLevel == 3) return 80;
  242. if(user.OperateLevel == 4) return 100;
  243. return 0;
  244. }
  245. }
  246. }