LeaderApplyCouponsHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. var user = db.Users.FirstOrDefault(m => m.Id == queryList.UserId) ?? new Users();
  36. List<int> couponIds = new List<int>();
  37. List<string> codes = new List<string>();
  38. int count = 0;
  39. int BuyCount = 0;
  40. JsonData ApplyList = JsonMapper.ToObject(queryList.SeoTitle);
  41. for (int i = 0; i < ApplyList.Count; i++)
  42. {
  43. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  44. int type = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  45. //电签
  46. if (type == 1)
  47. {
  48. var dposCoupons = db.PosCoupons.Where(m => m.QueryCount == 1 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.Sort == 0 && m.OpId == 0).Take(num).ToList();//电签券
  49. foreach (var item in dposCoupons)
  50. {
  51. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == item.Id) ?? new PosCoupons();
  52. posCoupon.UserId = queryList.UserId;
  53. posCoupon.Sort = 1;
  54. db.UserSnDelayChange.Add(new UserSnDelayChange()
  55. {
  56. CreateDate = DateTime.Now,
  57. QueryCount = Id,
  58. SnNo = posCoupon.ExchangeCode,
  59. });
  60. couponIds.Add(item.Id);
  61. codes.Add(item.ExchangeCode);
  62. }
  63. count += couponIds.Count / 3;
  64. BuyCount += num / 3;
  65. }
  66. //大机
  67. if (type == 2)
  68. {
  69. var bposCoupons = db.PosCoupons.Where(m => m.QueryCount == 2 && m.UserId == 0 && m.LeaderUserId == 0 && m.IsUse == 0 && m.IsLock == 0 && m.Sort == 0 && m.OpId == 0).Take(num).ToList();//大机券
  70. foreach (var items in bposCoupons)
  71. {
  72. var posCoupon = db.PosCoupons.FirstOrDefault(m => m.Id == items.Id) ?? new PosCoupons();
  73. posCoupon.UserId = queryList.UserId;
  74. posCoupon.Sort = 1;
  75. db.UserSnDelayChange.Add(new UserSnDelayChange()
  76. {
  77. CreateDate = DateTime.Now,
  78. QueryCount = Id,
  79. SnNo = posCoupon.ExchangeCode,
  80. });
  81. couponIds.Add(items.Id);
  82. codes.Add(items.ExchangeCode);
  83. }
  84. count += couponIds.Count / 2;
  85. BuyCount += num / 2;
  86. }
  87. db.SaveChanges();
  88. }
  89. db.SaveChanges();
  90. dosomething(Id, couponIds, codes, count);
  91. //推动数据到MQ
  92. Utils.Instance.SendMqOrder(new Orders()
  93. {
  94. Id = queryList.Id,
  95. Status = 2,
  96. CreateDate = queryList.CreateDate,
  97. Remark = queryList.Remark,
  98. BuyCount = BuyCount,
  99. PayStatus = 1,
  100. ProductId = 10000,
  101. DeliveryType = 0,
  102. PayMode = 4,
  103. PayDate = queryList.CreateDate,
  104. TotalPrice = queryList.ChangeAmt,
  105. Mobile = user.Mobile,
  106. RealName = user.RealName,
  107. OrderNo = queryList.SeoKeyword,
  108. UserId = queryList.UserId,
  109. });
  110. }
  111. catch (Exception ex)
  112. {
  113. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "盟主兑换机具券异常");
  114. }
  115. }
  116. else
  117. {
  118. Thread.Sleep(5000);
  119. }
  120. }
  121. }
  122. public void dosomething(int Id, List<int> couponIds, List<string> codes, int count = 0)
  123. {
  124. WebCMSEntities db = new WebCMSEntities();
  125. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  126. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  127. if(query != null)
  128. {
  129. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  130. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  131. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  132. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  133. Array.Reverse(ParentNavList);
  134. int index = 0;
  135. bool BigLeaderFlag = false; //大盟主标记
  136. bool BuyPrizeFlag = false; //购机奖励发放标识
  137. decimal BuyPrize = 0; //购机奖励
  138. foreach(string ParentId in ParentNavList)
  139. {
  140. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  141. index += 1;
  142. //给券打大盟主标记
  143. if(parentUser.LeaderLevel == 1 && PosCouponPrizeService.Instance.CheckLeaderReserve(db, count, parentUser.Id) && !BigLeaderFlag)
  144. {
  145. if(PosCouponPrizeService.Instance.CheckPosCouponLeaderFlag(db, parentUser.Id, count, codes) > 0)
  146. {
  147. BigLeaderFlag = true;
  148. }
  149. }
  150. else if(parentUser.LeaderLevel == 2 && !BigLeaderFlag)
  151. {
  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. int Step = 0;
  157. if(Kind == 1)
  158. {
  159. Step = 3;
  160. }
  161. else
  162. {
  163. Step = 2;
  164. }
  165. int result = PosCouponPrizeService.Instance.SetPosCouponLeaderFlag(db, parentUser.Id, num / Step, couponIds);
  166. if(result > 0) BigLeaderFlag = true;
  167. }
  168. }
  169. if(parentUser.OperateLevel > 0)
  170. {
  171. int groupCount = 0;
  172. for (int i = 0; i < ApplyList.Count; i++)
  173. {
  174. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  175. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  176. if(Kind == 1)
  177. {
  178. groupCount += num / 3;
  179. }
  180. else
  181. {
  182. groupCount += num / 2;
  183. }
  184. }
  185. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  186. {
  187. //购机奖
  188. decimal CurBuyPrize = GetBuyPrize(parentUser, query);
  189. decimal GetPrize = CurBuyPrize - BuyPrize;
  190. int Kind = 0;
  191. if(query.Remark == "兑换机具券")
  192. {
  193. Kind = 101;
  194. }
  195. if(GetPrize > 0 && !BuyPrizeFlag)
  196. {
  197. PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  198. BuyPrizeFlag = true;
  199. }
  200. if(CurBuyPrize == -1) BuyPrizeFlag = true;
  201. if(parentUser.OperateLevel == 2 && CurBuyPrize == 100) BuyPrizeFlag = true;
  202. BuyPrize = CurBuyPrize;
  203. }
  204. }
  205. }
  206. }
  207. db.Dispose();
  208. opdb.Dispose();
  209. }
  210. public void addprize(int Id)
  211. {
  212. WebCMSEntities db = new WebCMSEntities();
  213. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  214. var query = db.LeaderReserveRecord.FirstOrDefault(m => m.Id == Id && m.Remark == "兑换机具券");
  215. if(query != null)
  216. {
  217. JsonData ApplyList = JsonMapper.ToObject(query.SeoTitle);
  218. UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(query.UserId);
  219. string ParentNav = orderUser.ParentNav + "," + orderUser.Id + ",";
  220. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  221. Array.Reverse(ParentNavList);
  222. int index = 0;
  223. bool BigLeaderFlag = false; //大盟主标记
  224. decimal BuyPrize = 0; //购机奖励
  225. foreach(string ParentId in ParentNavList)
  226. {
  227. UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(int.Parse(ParentId));
  228. index += 1;
  229. if(parentUser.OperateLevel > 0)
  230. {
  231. int groupCount = 0;
  232. for (int i = 0; i < ApplyList.Count; i++)
  233. {
  234. int num = Convert.ToInt32(ApplyList[i]["Num"].ToString());
  235. int Kind = Convert.ToInt32(ApplyList[i]["Type"].ToString());
  236. if(Kind == 1)
  237. {
  238. groupCount += num / 3;
  239. }
  240. else
  241. {
  242. groupCount += num / 2;
  243. }
  244. }
  245. if(PosCouponPrizeService.Instance.CheckOpReserve(opdb, groupCount, parentUser.Id))
  246. {
  247. //购机奖
  248. decimal CurBuyPrize = GetBuyPrize(parentUser, query, groupCount);
  249. decimal GetPrize = CurBuyPrize - BuyPrize;
  250. int Kind = 0;
  251. if(query.Remark == "兑换机具券")
  252. {
  253. Kind = 101;
  254. }
  255. if(GetPrize > 0 && !db.UserAccountRecord.Any(m => m.QueryCount == Id && m.ChangeType == 128))
  256. {
  257. PosCouponPrizeService.Instance.OpAccount(db, Id, parentUser.Id, GetPrize, groupCount, 128, Kind);
  258. }
  259. BuyPrize = CurBuyPrize;
  260. }
  261. }
  262. }
  263. }
  264. db.Dispose();
  265. opdb.Dispose();
  266. }
  267. /// <summary>
  268. /// 获取购机奖金额
  269. /// </summary>
  270. /// <param name="user"></param>
  271. /// <returns></returns>
  272. public decimal GetBuyPrize(UserRankItem user, LeaderReserveRecord record, int count = 0)
  273. {
  274. if(user.OperateLevel == 2)
  275. {
  276. if(PosCouponPrizeService.Instance.TradeTeamCheck2(user.Id))
  277. {
  278. return 60;
  279. }
  280. if(!PosCouponPrizeService.Instance.TradeTeamCheck(record.UserId))
  281. {
  282. decimal Amount = count * 60;
  283. RedisDbconn.Instance.AddList("TradeFilterQueue", "{\"OrderId\":\"" + record.Id + "\",\"PrizeUserId\":\"" + user.Id + "\",\"Amount\":\"" + Amount + "\",\"Kind\":\"2\"}");
  284. return -1;
  285. }
  286. if(PosCouponPrizeService.Instance.CheckSpecialUserNav(record.UserId))
  287. {
  288. return -1;
  289. }
  290. return 60;
  291. }
  292. if(user.OperateLevel == 3) return 80;
  293. if(user.OperateLevel == 4) return 100;
  294. return 0;
  295. }
  296. }
  297. }