ResetSmallStoreHelper.cs 5.9 KB

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