ResetSmallStoreHelper.cs 3.2 KB

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