LeaderApplyCouponsHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if(PosCouponPrizeService.Instance.CheckPosCouponLeaderFlag(db, parentUser.Id, count, codes) > 0)
  120. {
  121. BigLeaderFlag = true;
  122. }
  123. }
  124. else if(parentUser.LeaderLevel == 2 && !BigLeaderFlag)
  125. {
  126. for (int i = 0; i < ApplyList.Count; i++)
  127. {
  128. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  129. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  130. int Step = 0;
  131. if(Kind == 1)
  132. {
  133. Step = 3;
  134. }
  135. else
  136. {
  137. Step = 2;
  138. }
  139. int result = PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / Step, couponIds);
  140. if(result > 0) BigLeaderFlag = true;
  141. }
  142. }
  143. if(parentUser.OperateLevel > 0)
  144. {
  145. int groupCount = 0;
  146. for (int i = 0; i < ApplyList.Count; i++)
  147. {
  148. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  149. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  150. if(Kind == 1)
  151. {
  152. groupCount += num / 3;
  153. }
  154. else
  155. {
  156. groupCount += num / 2;
  157. }
  158. }
  159. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  160. {
  161. //购机奖
  162. decimal CurBuyPrize = GetBuyPrize(parentUser, query.UserId);
  163. decimal GetPrize = CurBuyPrize - BuyPrize;
  164. int Kind = 0;
  165. if(query.Remark == "兑换机具券")
  166. {
  167. Kind = 101;
  168. }
  169. if(GetPrize > 0) PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  170. BuyPrize = CurBuyPrize;
  171. }
  172. }
  173. }
  174. }
  175. db.Dispose();
  176. opdb.Dispose();
  177. }
  178. public void addprize(int Id)
  179. {
  180. WebCMSEntities db = new WebCMSEntities();
  181. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  182. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  183. if(query != null)
  184. {
  185. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  186. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  187. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  188. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  189. Array.Reverse(ParentNavList);
  190. int index = 0;
  191. bool BigLeaderFlag = false; //大盟主标记
  192. decimal BuyPrize = 0; //购机奖励
  193. foreach(string ParentId in ParentNavList)
  194. {
  195. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  196. index += 1;
  197. if(parentUser.OperateLevel > 0)
  198. {
  199. int groupCount = 0;
  200. for (int i = 0; i < ApplyList.Count; i++)
  201. {
  202. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  203. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  204. if(Kind == 1)
  205. {
  206. groupCount += num / 3;
  207. }
  208. else
  209. {
  210. groupCount += num / 2;
  211. }
  212. }
  213. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  214. {
  215. //购机奖
  216. decimal CurBuyPrize = GetBuyPrize(parentUser, query.UserId);
  217. decimal GetPrize = CurBuyPrize - BuyPrize;
  218. int Kind = 0;
  219. if(query.Remark == "兑换机具券")
  220. {
  221. Kind = 101;
  222. }
  223. if(GetPrize > 0 && !db.UserAccountRecord.Any(m => m.QueryCount == Id && m.ChangeType == 128))
  224. {
  225. PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  226. }
  227. BuyPrize = CurBuyPrize;
  228. }
  229. }
  230. }
  231. }
  232. db.Dispose();
  233. opdb.Dispose();
  234. }
  235. /// <summary>
  236. /// 获取购机奖金额
  237. /// </summary>
  238. /// <param name="user"></param>
  239. /// <returns></returns>
  240. public decimal GetBuyPrize(UserRankItem user, int UserId = 0)
  241. {
  242. if(user.OperateLevel == 2)
  243. {
  244. if(PosCouponPrizeService.Instance.TradeTeamCheck2(user.Id))
  245. {
  246. return 60;
  247. }
  248. if(!PosCouponPrizeService.Instance.TradeTeamCheck(UserId))
  249. {
  250. return 0;
  251. }
  252. return 60;
  253. }
  254. if(user.OperateLevel == 3) return 80;
  255. if(user.OperateLevel == 4) return 100;
  256. return 0;
  257. }
  258. }
  259. }