AddPosCouponPrizeHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. List<int> prizeChk = new List<int>();
  62. if(!db.UserAccountRecord.Any(m => m.ChangeType == 112 && m.QueryCount == order.Id))
  63. {
  64. prizeChk.Add(1);
  65. }
  66. if(!db.UserAccountRecord.Any(m => m.ChangeType == 118 && m.QueryCount == order.Id))
  67. {
  68. prizeChk.Add(2);
  69. }
  70. if(!db.LeaderAccountRecord.Any(m => m.QueryCount == order.Id))
  71. {
  72. prizeChk.Add(3);
  73. }
  74. int ParentUserId = user.ParentUserId;
  75. bool buyPrize = false;
  76. bool OncePrizeFlag4 = true;
  77. int leaderFlag = 0;
  78. bool DirectPrizeFlag = true;
  79. bool PosCouponFlag = true;
  80. bool OperateFlag = true;
  81. if(user.LeaderLevel > 0 || user.UserType == 1) //盟主或运营中心
  82. {
  83. //推荐奖励
  84. if(prizeChk.Contains(1))
  85. {
  86. AlipayPayBack2Service.Instance.DirectPrize(db, order.Id, order.UserId, pro.ProductCount);
  87. }
  88. DirectPrizeFlag = false;
  89. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId) ?? new UserAccount();
  90. if (acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  91. {
  92. //购机奖励
  93. if(prizeChk.Contains(2))
  94. {
  95. AlipayPayBack2Service.Instance.OpAccount(db, order, order.UserId, 100, 1);
  96. }
  97. buyPrize = true;
  98. }
  99. if(user.LeaderLevel == 2 && PosCouponFlag)
  100. {
  101. if(PosCouponLeader(db, user.Id, pro.ProductCount, order.SnNos.Split(',')))
  102. {
  103. PosCouponFlag = false;
  104. }
  105. }
  106. if(user.UserType == 1 && OperateFlag)
  107. {
  108. PosCouponOp(db, user.Id, order.SnNos.Split(','));
  109. OperateFlag = false;
  110. }
  111. }
  112. while (ParentUserId > 0)
  113. {
  114. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  115. if(DirectPrizeFlag)
  116. {
  117. if(prizeChk.Contains(1))
  118. {
  119. AlipayPayBack2Service.Instance.DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  120. }
  121. DirectPrizeFlag = false;
  122. }
  123. if (puser.LeaderLevel > 0)
  124. {
  125. UserAccount acccount = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id) ?? new UserAccount();
  126. if (acccount.LeaderReserve >= 400 * pro.ProductCount && !buyPrize)
  127. {
  128. //购机奖励
  129. if(prizeChk.Contains(2))
  130. {
  131. AlipayPayBack2Service.Instance.OpAccount(db, order, puser.Id, 100, 1);
  132. }
  133. buyPrize = true;
  134. }
  135. if (acccount.LeaderReserve >= 400 * pro.ProductCount && puser.LeaderLevel > leaderFlag && leaderFlag < 2 && OncePrizeFlag4)
  136. {
  137. if(prizeChk.Contains(3))
  138. {
  139. //扣减备用金
  140. AlipayPayBack2Service.Instance.OpReserve(db, order, puser.Id, 400 * pro.ProductCount, 2, order.UserId, "购机奖励");
  141. //返回到余额
  142. AlipayPayBack2Service.Instance.OpLeaderAccount(db, order, puser.Id, 400, pro.ProductCount);
  143. }
  144. leaderFlag = puser.LeaderLevel;
  145. OncePrizeFlag4 = false;
  146. }
  147. }
  148. if(puser.LeaderLevel == 2 && PosCouponFlag)
  149. {
  150. if(PosCouponLeader(db, puser.Id, pro.ProductCount, order.SnNos.Split(',')))
  151. {
  152. PosCouponFlag = false;
  153. }
  154. }
  155. if(puser.UserType == 1 && OperateFlag)
  156. {
  157. PosCouponOp(db, puser.Id, order.SnNos.Split(','));
  158. OperateFlag = false;
  159. }
  160. ParentUserId = puser.ParentUserId;
  161. }
  162. }
  163. db.Dispose();
  164. }
  165. public bool PosCouponLeader(WebCMSEntities db, int Id, int ProductCount, string[] Codes)
  166. {
  167. UserAccount pacccount = db.UserAccount.FirstOrDefault(m => m.Id == Id) ?? new UserAccount();
  168. foreach(string Code in Codes)
  169. {
  170. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == Code);
  171. if(coupon != null && pacccount.LeaderReserve >= 400 * ProductCount)
  172. {
  173. coupon.LeaderUserId = Id;
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. }
  180. db.SaveChanges();
  181. return true;
  182. }
  183. public void PosCouponOp(WebCMSEntities db, int Id, string[] Codes)
  184. {
  185. foreach(string Code in Codes)
  186. {
  187. PosCoupons coupon = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == Code);
  188. if(coupon != null)
  189. {
  190. coupon.OpId = Id;
  191. }
  192. }
  193. db.SaveChanges();
  194. }
  195. }