ResetSmallStoreHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Models;
  8. using Library;
  9. using LitJson;
  10. public class ResetSmallStoreHelper
  11. {
  12. public readonly static ResetSmallStoreHelper Instance = new ResetSmallStoreHelper();
  13. private ResetSmallStoreHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. // 每月1号小分仓额度,额度为上个月实际发放的总分润
  23. private void DoWorks()
  24. {
  25. while (true)
  26. {
  27. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  28. WebCMSEntities db = new WebCMSEntities();
  29. string connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  30. try
  31. {
  32. string content = RedisDbconn.Instance.RPop<string>("ResetSmallStoreQueue");
  33. if(!string.IsNullOrEmpty(content))
  34. {
  35. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  36. string PreMonth = DateTime.Now.AddMonths(-2).ToString("yyyyMM");
  37. string check = function.ReadInstance("/ResetSmallStore/" + Month + ".txt");
  38. if(string.IsNullOrEmpty(check))
  39. {
  40. function.WritePage("/ResetSmallStore/", Month + ".txt", DateTime.Now.ToString());
  41. CustomerSqlConn.op("update UserAccount set ThisMonthPreAmount=0,ValidPreAmount=0 where ThisMonthPreAmount>0", connstr);
  42. string minId = "0";
  43. DataTable minIdDt = CustomerSqlConn.dtable("select min(Id) from ProfitRewardRecord where TradeMonth='" + PreMonth + "'", connstr);
  44. if(minIdDt.Rows.Count > 0)
  45. {
  46. minId = minIdDt.Rows[0][0].ToString();
  47. }
  48. DoMethod(db, PreMonth, minId);
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算小分仓额度异常");
  55. }
  56. db.Dispose();
  57. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  58. Thread.Sleep(60000);
  59. }
  60. }
  61. public void StartReset()
  62. {
  63. Thread th = new Thread(DoWorksReset);
  64. th.IsBackground = true;
  65. th.Start();
  66. }
  67. private void DoWorksReset()
  68. {
  69. while (true)
  70. {
  71. WebCMSEntities db = new WebCMSEntities();
  72. try
  73. {
  74. string content = RedisDbconn.Instance.RPop<string>("ResetSmallStoreByUserIdQueue");
  75. if(!string.IsNullOrEmpty(content))
  76. {
  77. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  78. string PreMonth = DateTime.Now.AddMonths(-2).ToString("yyyyMM");
  79. string minId = "0";
  80. DoMethod(db, PreMonth, minId, content);
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "重置小分仓额度异常");
  86. }
  87. db.Dispose();
  88. Thread.Sleep(60000);
  89. }
  90. }
  91. public void DoMethod(WebCMSEntities db, string PreMonth, string minId = "0", string UId = "")
  92. {
  93. string sql = "";
  94. if(string.IsNullOrEmpty(UId))
  95. {
  96. sql = "select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where Id>=" + minId + " and UserId>0 and TradeMonth='" + PreMonth + "' group by UserId";
  97. }
  98. else
  99. {
  100. sql = "select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where UserId=" + UId + " and TradeMonth='" + PreMonth + "' group by UserId";
  101. }
  102. string connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  103. DataTable dt = CustomerSqlConn.dtable(sql, connstr);
  104. LogHelper.Instance.WriteLog("总数" + dt.Rows.Count, "计算小分仓额度日志");
  105. int index = 0;
  106. foreach (DataRow dr in dt.Rows)
  107. {
  108. index += 1;
  109. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  110. decimal ProfitAmount = decimal.Parse(function.CheckNum(dr[1].ToString()));
  111. decimal PreProfitAmount = ProfitAmount;
  112. var prelist = db.PreSendStockDetail.Select(m => new { m.Id, m.BrandId, m.ToUserId, m.Status, m.ApplyFlag, m.AuthFlag }).Where(m => m.ToUserId == UserId && m.Status == 1 && m.ApplyFlag == 0 && m.AuthFlag == 1).ToList();
  113. foreach (var prepos in prelist)
  114. {
  115. if (prepos.BrandId == 1 || prepos.BrandId == 2 || prepos.BrandId == 4 || prepos.BrandId == 6 || prepos.BrandId == 7 || prepos.BrandId == 8 || prepos.BrandId == 10)
  116. {
  117. if (ProfitAmount > 200)
  118. {
  119. ProfitAmount -= 200;
  120. PreSendStockDetail edit = db.PreSendStockDetail.FirstOrDefault(m => m.Id == prepos.Id);
  121. if (edit != null)
  122. {
  123. edit.AuthFlag = 1;
  124. }
  125. }
  126. }
  127. else if (prepos.BrandId == 3 || prepos.BrandId == 5 || prepos.BrandId == 9 || prepos.BrandId == 11)
  128. {
  129. if (ProfitAmount > 300)
  130. {
  131. ProfitAmount -= 300;
  132. PreSendStockDetail edit = db.PreSendStockDetail.FirstOrDefault(m => m.Id == prepos.Id);
  133. if (edit != null)
  134. {
  135. edit.AuthFlag = 1;
  136. }
  137. }
  138. }
  139. }
  140. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  141. if (account == null)
  142. {
  143. account = db.UserAccount.Add(new UserAccount()
  144. {
  145. Id = UserId,
  146. UserId = UserId,
  147. }).Entity;
  148. db.SaveChanges();
  149. }
  150. account.ThisMonthPreAmount = PreProfitAmount;
  151. account.ValidPreAmount = ProfitAmount;
  152. if (index % 200 == 0)
  153. {
  154. db.SaveChanges();
  155. }
  156. LogHelper.Instance.WriteLog(index.ToString(), "计算小分仓额度日志");
  157. }
  158. db.SaveChanges();
  159. }
  160. }