ResetSmallStoreHelper.cs 7.1 KB

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