LeaderApplyCouponsHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. List<string> codes = new List<string>();
  37. int count = 0;
  38. JsonData ApplyList = JsonMapper.ToObject(queryList.SeoTitle);
  39. for (int i = 0; i < ApplyList.Count; i++)
  40. {
  41. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  42. int type = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  43. //电签
  44. if (type == 1)
  45. {
  46. 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();//电签券
  47. foreach (var item in dposCoupons)
  48. {
  49. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == item.Id) ?? new PosCoupons();
  50. posCoupon.UserId = queryList.UserId;
  51. db.UserSnDelayChange.Add(new UserSnDelayChange()
  52. {
  53. CreateDate = DateTime.Now,
  54. QueryCount = Id,
  55. SnNo = posCoupon.ExchangeCode,
  56. });
  57. couponIds.Add(item.Id);
  58. codes.Add(item.ExchangeCode);
  59. }
  60. count += couponIds.Count / 3;
  61. }
  62. //大机
  63. if (type == 2)
  64. {
  65. 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();//大机券
  66. foreach (var items in bposCoupons)
  67. {
  68. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == items.Id) ?? new PosCoupons();
  69. posCoupon.UserId = queryList.UserId;
  70. db.UserSnDelayChange.Add(new UserSnDelayChange()
  71. {
  72. CreateDate = DateTime.Now,
  73. QueryCount = Id,
  74. SnNo = posCoupon.ExchangeCode,
  75. });
  76. couponIds.Add(items.Id);
  77. codes.Add(items.ExchangeCode);
  78. }
  79. count += couponIds.Count / 2;
  80. }
  81. db.SaveChanges();
  82. }
  83. db.SaveChanges();
  84. dosomething(Id, couponIds, codes, count);
  85. }
  86. catch (Exception ex)
  87. {
  88. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "盟主兑换机具券异常");
  89. }
  90. }
  91. else
  92. {
  93. Thread.Sleep(5000);
  94. }
  95. }
  96. }
  97. public void dosomething(int Id, List<int> couponIds, List<string> codes, int count = 0)
  98. {
  99. WebCMSEntities db = new WebCMSEntities();
  100. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  101. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  102. if(query != null)
  103. {
  104. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  105. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  106. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  107. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  108. Array.Reverse(ParentNavList);
  109. int index = 0;
  110. bool BigLeaderFlag = false; //大盟主标记
  111. decimal BuyPrize = 0; //购机奖励
  112. foreach(string ParentId in ParentNavList)
  113. {
  114. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  115. index += 1;
  116. //给券打大盟主标记
  117. if(parentUser.LeaderLevel == 1 && PosCouponPrizeService.Instance.CheckLeaderReserve(db, count, parentUser.Id) && !BigLeaderFlag)
  118. {
  119. BigLeaderFlag = PosCouponPrizeService.Instance.CheckPosCouponLeaderFlag(db, parentUser.Id, count, codes);
  120. }
  121. else if(parentUser.LeaderLevel == 2 && !BigLeaderFlag)
  122. {
  123. for (int i = 0; i < ApplyList.Count; i++)
  124. {
  125. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  126. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  127. int Step = 0;
  128. if(Kind == 1)
  129. {
  130. Step = 3;
  131. }
  132. else
  133. {
  134. Step = 2;
  135. }
  136. int result = PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / Step, couponIds);
  137. if(result > 0) BigLeaderFlag = true;
  138. }
  139. }
  140. if(parentUser.OperateLevel > 0)
  141. {
  142. int groupCount = 0;
  143. for (int i = 0; i < ApplyList.Count; i++)
  144. {
  145. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  146. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  147. if(Kind == 1)
  148. {
  149. groupCount += num / 3;
  150. }
  151. else
  152. {
  153. groupCount += num / 2;
  154. }
  155. }
  156. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  157. {
  158. //购机奖
  159. decimal CurBuyPrize = GetBuyPrize(parentUser);
  160. decimal GetPrize = CurBuyPrize - BuyPrize;
  161. int Kind = 0;
  162. if(query.Remark == "兑换机具券")
  163. {
  164. Kind = 101;
  165. }
  166. if(GetPrize > 0) PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  167. BuyPrize = CurBuyPrize;
  168. }
  169. }
  170. }
  171. }
  172. db.Dispose();
  173. opdb.Dispose();
  174. }
  175. public void addprize(int Id)
  176. {
  177. WebCMSEntities db = new WebCMSEntities();
  178. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  179. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  180. if(query != null)
  181. {
  182. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  183. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  184. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  185. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  186. Array.Reverse(ParentNavList);
  187. int index = 0;
  188. bool BigLeaderFlag = false; //大盟主标记
  189. decimal BuyPrize = 0; //购机奖励
  190. foreach(string ParentId in ParentNavList)
  191. {
  192. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  193. index += 1;
  194. if(parentUser.OperateLevel > 0)
  195. {
  196. int groupCount = 0;
  197. for (int i = 0; i < ApplyList.Count; i++)
  198. {
  199. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  200. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  201. if(Kind == 1)
  202. {
  203. groupCount += num / 3;
  204. }
  205. else
  206. {
  207. groupCount += num / 2;
  208. }
  209. }
  210. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  211. {
  212. //购机奖
  213. decimal CurBuyPrize = GetBuyPrize(parentUser);
  214. decimal GetPrize = CurBuyPrize - BuyPrize;
  215. int Kind = 0;
  216. if(query.Remark == "兑换机具券")
  217. {
  218. Kind = 101;
  219. }
  220. if(GetPrize > 0 && !db.UserAccountRecord.Any(m => m.QueryCount == Id && m.ChangeType == 128))
  221. {
  222. PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  223. }
  224. BuyPrize = CurBuyPrize;
  225. }
  226. }
  227. }
  228. }
  229. db.Dispose();
  230. opdb.Dispose();
  231. }
  232. /// <summary>
  233. /// 获取购机奖金额
  234. /// </summary>
  235. /// <param name="user"></param>
  236. /// <returns></returns>
  237. public decimal GetBuyPrize(UserRankItem user)
  238. {
  239. if(user.OperateLevel == 2) return 60;
  240. if(user.OperateLevel == 3) return 80;
  241. if(user.OperateLevel == 4) return 100;
  242. return 0;
  243. }
  244. }
  245. }