ResetSmallStoreHelper.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 check = function.ReadInstance("/ResetSmallStore/" + Month + ".txt");
  37. if(string.IsNullOrEmpty(check))
  38. {
  39. function.WritePage("/ResetSmallStore/", Month + ".txt", DateTime.Now.ToString());
  40. string minId = "0";
  41. DataTable minIdDt = OtherMySqlConn.dtable("select min(Id) from ProfitRewardRecord where TradeMonth='" + Month + "'");
  42. if(minIdDt.Rows.Count > 0)
  43. {
  44. minId = minIdDt.Rows[0][0].ToString();
  45. }
  46. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where Id>=" + minId + " and UserId>0 and TradeMonth='" + Month + "' group by UserId");
  47. function.WriteLog("总数" + dt.Rows.Count, "计算小分仓额度日志");
  48. int index = 0;
  49. foreach(DataRow dr in dt.Rows)
  50. {
  51. index += 1;
  52. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  53. decimal ProfitAmount = decimal.Parse(function.CheckNum(dr[1].ToString()));
  54. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  55. if (account == null)
  56. {
  57. account = db.UserAccount.Add(new UserAccount()
  58. {
  59. Id = UserId,
  60. UserId = UserId,
  61. }).Entity;
  62. db.SaveChanges();
  63. }
  64. account.ThisMonthPreAmount = ProfitAmount;
  65. account.ValidPreAmount = ProfitAmount;
  66. if(index % 200 == 0)
  67. {
  68. db.SaveChanges();
  69. }
  70. function.WriteLog(index.ToString(), "计算小分仓额度日志");
  71. }
  72. db.SaveChanges();
  73. }
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "计算小分仓额度异常");
  79. }
  80. db.Dispose();
  81. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "计算小分仓额度日志");
  82. Thread.Sleep(60000);
  83. }
  84. }
  85. }