ConsumerOrdersStatService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using MySystem.PxcModels;
  7. namespace MySystem
  8. {
  9. public class ConsumerOrdersStatService
  10. {
  11. public readonly static ConsumerOrdersStatService Instance = new ConsumerOrdersStatService();
  12. private ConsumerOrdersStatService()
  13. { }
  14. public void Start(JobMqMsg jobInfo)
  15. {
  16. string content = "";
  17. try
  18. {
  19. dosomething();
  20. string Msg = "success";
  21. jobInfo.Status = Msg == "success" ? 1 : 0;
  22. jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  23. RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  24. }
  25. catch (Exception ex)
  26. {
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. Dictionary<string, string> data = new Dictionary<string, string>();
  30. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  31. data.Add("ErrMsg", ex.ToString());
  32. TendisDbconn.Instance.AddList("public_err", data);
  33. }
  34. else
  35. {
  36. TendisDbconn.Instance.AddList("public_service", DateTime.Now.ToString() + ":" + ex.ToString());
  37. }
  38. }
  39. }
  40. public void dosomething()
  41. {
  42. bool op = true;
  43. while (op)
  44. {
  45. int OrderId = TendisDbconn.Instance.RPop<int>("ConsumerOrdersStat");
  46. if (OrderId > 0)
  47. {
  48. WebCMSEntities db = new WebCMSEntities();
  49. using (var tran = db.Database.BeginTransaction())
  50. {
  51. try
  52. {
  53. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == OrderId);
  54. if (order != null)
  55. {
  56. order.PayMoney = order.PayMoney * 10000M;
  57. string TradeMonth = order.UpdateDate.Value.ToString("yyyyMM");
  58. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId);
  59. if (merchant != null)
  60. {
  61. merchant.LastConsumeDate = order.UpdateDate;
  62. merchant.TotalConsumeCount += 1;
  63. merchant.TotalOrder += 1;
  64. db.SaveChanges();
  65. BothdisDbconn.Instance.AddNumber("TotalAmount:" + merchant.UserId, order.PayMoney);
  66. decimal TotalAmount = BothdisDbconn.Instance.Get<decimal>("TotalAmount:" + merchant.UserId);
  67. BothdisDbconn.Instance.AddNumber("TotalAmount:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  68. // 微信/支付宝
  69. if (order.PayMode == 1)
  70. {
  71. BothdisDbconn.Instance.AddInt("TotalOrder:Alipay:" + merchant.UserId + ":" + TradeMonth);
  72. }
  73. else
  74. {
  75. BothdisDbconn.Instance.AddInt("TotalOrder:WeChat:" + merchant.UserId + ":" + TradeMonth);
  76. }
  77. // 活动、非活动
  78. if (order.IsAct == 1)
  79. {
  80. BothdisDbconn.Instance.AddNumber("TotalAmount:Active:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  81. }
  82. else
  83. {
  84. BothdisDbconn.Instance.AddNumber("TotalAmount:UnActive:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  85. }
  86. // 遍历上级,累加团队数据
  87. int UserId = merchant.UserId;
  88. int Level = 0;
  89. while (UserId > 0)
  90. {
  91. Level += 1;
  92. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  93. if (user != null)
  94. {
  95. if (Level >= 1)
  96. {
  97. BothdisDbconn.Instance.AddNumber("TeamTotalAmount:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  98. // 微信/支付宝
  99. if (order.PayMode == 1)
  100. {
  101. BothdisDbconn.Instance.AddInt("TeamTotalOrder:Alipay:" + merchant.UserId + ":" + TradeMonth);
  102. }
  103. else
  104. {
  105. BothdisDbconn.Instance.AddInt("TeamTotalOrder:WeChat:" + merchant.UserId + ":" + TradeMonth);
  106. }
  107. // 活动、非活动
  108. if (order.IsAct == 1)
  109. {
  110. BothdisDbconn.Instance.AddNumber("TeamTotalAmount:Active:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  111. }
  112. else
  113. {
  114. BothdisDbconn.Instance.AddNumber("TeamTotalAmount:UnActive:" + merchant.UserId + ":" + TradeMonth, order.PayMoney);
  115. }
  116. }
  117. UserId = user.ParentUserId;
  118. }
  119. else
  120. {
  121. UserId = 0;
  122. }
  123. }
  124. // 判断激活商户,从绑定音箱码开始,30天内,活动交易额满1000为激活
  125. if (merchant.BindStatus == 1 && merchant.BindDate > DateTime.Now.AddDays(-30) && TotalAmount >= 1000)
  126. {
  127. merchant.ActivationStatus = 1;
  128. merchant.ActivationDate = DateTime.Now;
  129. db.SaveChanges();
  130. BothdisDbconn.Instance.AddInt("TotalActMerchant:" + merchant.UserId + ":" + DateTime.Now.ToString("yyyyMM"));
  131. }
  132. }
  133. tran.Commit();
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. tran.Rollback();
  139. TendisDbconn.Instance.AddList("public_service", DateTime.Now.ToString() + ":" + ex.ToString());
  140. }
  141. }
  142. db.Dispose();
  143. }
  144. else
  145. {
  146. op = false;
  147. }
  148. }
  149. }
  150. }
  151. }