LeaderApplyCouponsHelper.cs 14 KB

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