ResetSmallStoreHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  28. WebCMSEntities db = new WebCMSEntities();
  29. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].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. string minId = "0";
  42. DataTable minIdDt = OtherMySqlConn.dtable("select min(Id) from ProfitRewardRecord where TradeMonth='" + PreMonth + "'");
  43. if(minIdDt.Rows.Count > 0)
  44. {
  45. minId = minIdDt.Rows[0][0].ToString();
  46. }
  47. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where Id>=" + minId + " and UserId>0 and TradeMonth='" + PreMonth + "' group by UserId");
  48. function.WriteLog("总数" + dt.Rows.Count, "计算小分仓额度日志");
  49. int index = 0;
  50. foreach(DataRow dr in dt.Rows)
  51. {
  52. index += 1;
  53. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  54. decimal ProfitAmount = decimal.Parse(function.CheckNum(dr[1].ToString()));
  55. var prelist = db.PreSendStockDetail.Select(m => new { m.Id, m.BrandId, m.ToUserId, m.Status, m.ApplyFlag }).Where(m => m.ToUserId == UserId && m.Status == 1 && m.ApplyFlag == 0);
  56. foreach(var prepos in prelist)
  57. {
  58. if(prepos.BrandId == 1 || prepos.BrandId == 2 || prepos.BrandId == 4 || prepos.BrandId == 6 || prepos.BrandId == 7 || prepos.BrandId == 8)
  59. {
  60. if(ProfitAmount > 200)
  61. {
  62. ProfitAmount -= 200;
  63. PreSendStockDetail edit = db.PreSendStockDetail.FirstOrDefault(m => m.Id == prepos.Id);
  64. if(edit != null)
  65. {
  66. edit.AuthFlag = 1;
  67. }
  68. }
  69. }
  70. else if(prepos.BrandId == 3 || prepos.BrandId == 5 || prepos.BrandId == 9)
  71. {
  72. if(ProfitAmount > 300)
  73. {
  74. ProfitAmount -= 300;
  75. PreSendStockDetail edit = db.PreSendStockDetail.FirstOrDefault(m => m.Id == prepos.Id);
  76. if(edit != null)
  77. {
  78. edit.AuthFlag = 1;
  79. }
  80. }
  81. }
  82. }
  83. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  84. if (account == null)
  85. {
  86. account = db.UserAccount.Add(new UserAccount()
  87. {
  88. Id = UserId,
  89. UserId = UserId,
  90. }).Entity;
  91. db.SaveChanges();
  92. }
  93. account.ThisMonthPreAmount = ProfitAmount;
  94. account.ValidPreAmount = ProfitAmount;
  95. if(index % 200 == 0)
  96. {
  97. db.SaveChanges();
  98. }
  99. function.WriteLog(index.ToString(), "计算小分仓额度日志");
  100. }
  101. db.SaveChanges();
  102. }
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算小分仓额度异常");
  108. }
  109. db.Dispose();
  110. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  111. Thread.Sleep(60000);
  112. }
  113. }
  114. }