StatTimerService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using MySystem.Models;
  8. namespace MySystem
  9. {
  10. public class StatTimerService
  11. {
  12. public readonly static StatTimerService Instance = new StatTimerService();
  13. private StatTimerService()
  14. { }
  15. public void Start(JobMqMsg jobInfo)
  16. {
  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. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  28. }
  29. }
  30. public void dosomething()
  31. {
  32. WebCMSEntities db = new WebCMSEntities();
  33. string yesterday = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd HH:mm:ss");
  34. string today = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  35. // 统计商户激活数据
  36. DataTable dt = dbconn.dtable("select UserId,BrandId,MerStandardDate from PosMerchantInfo where MerStandardDate>='" + yesterday + "' and MerStandardDate<'" + today + "' and QueryCount=0");
  37. dbconn.op("update PosMerchantInfo set QueryCount=1 where MerStandardDate>='" + yesterday + "' and MerStandardDate<'" + today + "' and QueryCount=0");
  38. foreach (DataRow dr in dt.Rows)
  39. {
  40. DateTime date = DateTime.Parse(dr["MerStandardDate"].ToString());
  41. string TradeMonth = date.ToString("yyyyMM");
  42. string TradeDate = date.ToString("yyyyMMdd");
  43. int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
  44. int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
  45. int MerchantCount = 1;
  46. int Level = 0;
  47. while (UserId > 0)
  48. {
  49. Level += 1;
  50. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  51. if (user != null)
  52. {
  53. if (user.AuthFlag == 1)
  54. {
  55. if (Level == 1)
  56. {
  57. RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
  58. RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
  59. RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
  60. RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
  61. }
  62. RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
  63. RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
  64. RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
  65. RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
  66. }
  67. UserId = user.ParentUserId;
  68. }
  69. else
  70. {
  71. UserId = 0;
  72. }
  73. }
  74. }
  75. db.Dispose();
  76. }
  77. public void ResetUserData()
  78. {
  79. RedisDbconn.Instance.Clear("TotalUser:*");
  80. RedisDbconn.Instance.Clear("TeamTotalUser:*");
  81. RedisDbconn.Instance.Clear("AddUser:*");
  82. RedisDbconn.Instance.Clear("TeamAddUser:*");
  83. WebCMSEntities db = new WebCMSEntities();
  84. // 统计创客数据
  85. DataTable dt = dbconn.dtable("select Id,AuthFlag from Users where AuthFlag=1 order by Id");
  86. foreach (DataRow dr in dt.Rows)
  87. {
  88. int UserId = int.Parse(dr["Id"].ToString()); //创客ID
  89. DateTime date = DateTime.Parse(dr["AuthFlag"].ToString());
  90. string TradeMonth = date.ToString("yyyyMM");
  91. string TradeDate = date.ToString("yyyyMMdd");
  92. int Level = 0;
  93. while (UserId > 0)
  94. {
  95. Level += 1;
  96. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  97. if (user != null)
  98. {
  99. if (user.AuthFlag == 1)
  100. {
  101. if (Level == 1)
  102. {
  103. RedisDbconn.Instance.AddInt("TotalUser:" + UserId); //累计总创客
  104. RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeDate); //新增创客
  105. RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeMonth); //新增创客
  106. }
  107. RedisDbconn.Instance.AddInt("TeamTotalUser:" + UserId); //累计总创客
  108. RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeDate); //新增创客
  109. RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeMonth); //新增创客
  110. }
  111. UserId = user.ParentUserId;
  112. }
  113. }
  114. }
  115. db.Dispose();
  116. }
  117. public void SetUserData(int uid)
  118. {
  119. WebCMSEntities db = new WebCMSEntities();
  120. // 统计创客数据
  121. DataTable dt = dbconn.dtable("select Id,AuthFlag from Users where Id=" + uid + " and AuthFlag=1 order by Id");
  122. if (dt.Rows.Count > 0)
  123. {
  124. DataRow dr = dt.Rows[0];
  125. int UserId = int.Parse(dr["Id"].ToString()); //创客ID
  126. DateTime date = DateTime.Parse(dr["AuthFlag"].ToString());
  127. string TradeMonth = date.ToString("yyyyMM");
  128. string TradeDate = date.ToString("yyyyMMdd");
  129. int Level = 0;
  130. while (UserId > 0)
  131. {
  132. Level += 1;
  133. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  134. if (user != null)
  135. {
  136. if (user.AuthFlag == 1)
  137. {
  138. if (Level == 1)
  139. {
  140. RedisDbconn.Instance.AddInt("TotalUser:" + UserId); //累计总创客
  141. RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeDate); //新增创客
  142. RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeMonth); //新增创客
  143. }
  144. RedisDbconn.Instance.AddInt("TeamTotalUser:" + UserId); //累计总创客
  145. RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeDate); //新增创客
  146. RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeMonth); //新增创客
  147. }
  148. UserId = user.ParentUserId;
  149. }
  150. }
  151. }
  152. db.Dispose();
  153. }
  154. }
  155. }