ResetSmallStoreHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  33. // {
  34. string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  35. string check = function.ReadInstance("/ResetSmallStore/" + Month + ".txt");
  36. if(string.IsNullOrEmpty(check))
  37. {
  38. function.WritePage("/ResetSmallStore/", Month + ".txt", DateTime.Now.ToString());
  39. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where UserId>0 and TradeMonth='" + Month + "' group by UserId order by UserId");
  40. function.WriteLog("总数" + dt.Rows.Count, "计算小分仓额度日志");
  41. int index = 0;
  42. foreach(DataRow dr in dt.Rows)
  43. {
  44. index += 1;
  45. int UserId = int.Parse(function.CheckInt(dr["UserId"].ToString()));
  46. decimal ProfitAmount = decimal.Parse(function.CheckNum(dr[1].ToString()));
  47. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  48. if (account == null)
  49. {
  50. account = db.UserAccount.Add(new UserAccount()
  51. {
  52. Id = UserId,
  53. UserId = UserId,
  54. }).Entity;
  55. db.SaveChanges();
  56. }
  57. account.ThisMonthPreAmount = ProfitAmount;
  58. account.ValidPreAmount = ProfitAmount;
  59. if(index % 200 == 0)
  60. {
  61. db.SaveChanges();
  62. }
  63. function.WriteLog(index.ToString(), "计算小分仓额度日志");
  64. }
  65. db.SaveChanges();
  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. }