AddPosCouponPrizeHelper.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class AddPosCouponPrizeHelper
  11. {
  12. public readonly static AddPosCouponPrizeHelper Instance = new AddPosCouponPrizeHelper();
  13. private AddPosCouponPrizeHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void DoWorks()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("AddPosCouponPrizeQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. int oid = int.Parse(content);
  32. WebCMSEntities db = new WebCMSEntities();
  33. DoSomething(db, oid);
  34. db.Dispose();
  35. }
  36. else
  37. {
  38. Thread.Sleep(60000);
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "补下单机具券奖励异常");
  44. }
  45. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "补下单机具券奖励日志");
  46. }
  47. }
  48. public void DoSomething(WebCMSEntities db, int OrderId)
  49. {
  50. List<int> ids = new List<int>();
  51. ids.Add(10);
  52. ids.Add(11);
  53. ids.Add(77);
  54. ids.Add(78);
  55. ids.Add(79);
  56. Orders order = db.Orders.FirstOrDefault(m => m.Status > 0 && m.Id == OrderId && ids.Contains(m.ProductId));
  57. if(order != null)
  58. {
  59. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == order.Id) ?? new OrderProduct();
  60. // Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  61. UserRankItem user = PosCouponPrizeService.Instance.GetUserLevel(db, order.UserId);
  62. List<int> prizeChk = new List<int>();
  63. if(!db.UserAccountRecord.Any(m => m.ChangeType == 112 && m.QueryCount == order.Id))
  64. {
  65. prizeChk.Add(1);
  66. }
  67. if(!db.UserAccountRecord.Any(m => m.ChangeType == 118 && m.QueryCount == order.Id))
  68. {
  69. prizeChk.Add(2);
  70. }
  71. if(!db.LeaderAccountRecord.Any(m => m.QueryCount == order.Id))
  72. {
  73. prizeChk.Add(3);
  74. }
  75. int ParentUserId = user.ParentUserId;
  76. bool buyPrize = false;
  77. bool OncePrizeFlag4 = true;
  78. int leaderFlag = 0;
  79. bool DirectPrizeFlag = true;
  80. bool PosCouponFlag = true;
  81. bool OperateFlag = true;
  82. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  83. {
  84. //推荐奖励
  85. if(prizeChk.Contains(1))
  86. {
  87. AlipayPayBack2Service.Instance.DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  88. }
  89. DirectPrizeFlag = false;
  90. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  91. if (acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  92. {
  93. //购机奖励
  94. if(prizeChk.Contains(2))
  95. {
  96. AlipayPayBack2Service.Instance.OpAccount(db, order, order.UserId, 100, 1);
  97. }
  98. buyPrize = true;
  99. }
  100. if(user.LeaderLevel == 2 && PosCouponFlag)
  101. {
  102. if(PosCouponLeader(db, user.Id, pro.ProductCount, order.SnNos.Split(',')))
  103. {
  104. PosCouponFlag = false;
  105. }
  106. }
  107. if(user.UserType == 1 && OperateFlag)
  108. {
  109. PosCouponOp(db, user.Id, order.SnNos.Split(','));
  110. OperateFlag = false;
  111. }
  112. }
  113. while (ParentUserId > 0)
  114. {
  115. // Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  116. UserRankItem puser = PosCouponPrizeService.Instance.GetUserLevel(db, ParentUserId);
  117. if(DirectPrizeFlag)
  118. {
  119. if(prizeChk.Contains(1))
  120. {
  121. AlipayPayBack2Service.Instance.DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  122. }
  123. DirectPrizeFlag = false;
  124. }
  125. if (puser.LeaderLevel > 0)
  126. {
  127. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  128. if (acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  129. {
  130. //购机奖励
  131. if(prizeChk.Contains(2))
  132. {
  133. AlipayPayBack2Service.Instance.OpAccount(db, order, puser.Id, 100, 1);
  134. }
  135. buyPrize = true;
  136. }
  137. if (acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
  138. {
  139. if(prizeChk.Contains(3))
  140. {
  141. //扣减备用金
  142. AlipayPayBack2Service.Instance.OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
  143. //返回到余额
  144. AlipayPayBack2Service.Instance.OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  145. }
  146. leaderFlag = puser.LeaderLevel;
  147. OncePrizeFlag4 = false;
  148. }
  149. }
  150. if(puser.LeaderLevel == 2 && PosCouponFlag)
  151. {
  152. if(PosCouponLeader(db, puser.Id, pro.ProductCount, order.SnNos.Split(',')))
  153. {
  154. PosCouponFlag = false;
  155. }
  156. }
  157. if(puser.UserType == 1 && OperateFlag)
  158. {
  159. PosCouponOp(db, puser.Id, order.SnNos.Split(','));
  160. OperateFlag = false;
  161. }
  162. ParentUserId = puser.ParentUserId;
  163. }
  164. }
  165. db.Dispose();
  166. }
  167. public bool PosCouponLeader(WebCMSEntities db, int Id, int ProductCount, string[] Codes)
  168. {
  169. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == Id) ?? new UserAccount();
  170. foreach(string Code in Codes)
  171. {
  172. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == Code);
  173. if(coupon != null && pacccount.LeaderReserve >= 400 * ProductCount)
  174. {
  175. coupon.LeaderUserId = Id;
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. }
  182. db.SaveChanges();
  183. return true;
  184. }
  185. public void PosCouponOp(WebCMSEntities db, int Id, string[] Codes)
  186. {
  187. foreach(string Code in Codes)
  188. {
  189. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == Code);
  190. if(coupon != null)
  191. {
  192. coupon.OpId = Id;
  193. }
  194. }
  195. db.SaveChanges();
  196. }
  197. }