LeaderApplyCouponsHelper.cs 14 KB

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