DiviService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class DiviService
  10. {
  11. public readonly static DiviService Instance = new DiviService();
  12. private DiviService()
  13. { }
  14. public void Start(JobMqMsg jobInfo)
  15. {
  16. string content = "";
  17. try
  18. {
  19. string Msg = CheckData();
  20. // jobInfo.Status = Msg == "success" ? 1: 0;
  21. // jobInfo.Msg = Msg == "success" ? "执行完成": Msg;
  22. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "DiviBack");
  23. }
  24. catch (Exception ex)
  25. {
  26. if (!string.IsNullOrEmpty(content))
  27. {
  28. Dictionary<string, string> data = new Dictionary<string, string>();
  29. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  30. data.Add("ErrMsg", ex.ToString());
  31. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "diviback_err");
  32. }
  33. else
  34. {
  35. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "diviback_service");
  36. }
  37. }
  38. }
  39. //检查机具扣费情况
  40. private string CheckData()
  41. {
  42. DateTime StatDate = DateTime.Now.AddMonths(-1);
  43. DateTime StartDate = DateTime.Parse(StatDate.ToString("yyyy-MM") + "-01 00:00:00");
  44. DateTime EndDate = StartDate.AddMonths(1);
  45. string TradeMonth = StatDate.ToString("yyyyMM");
  46. WebCMSEntities db = new WebCMSEntities();
  47. using (var tran = db.Database.BeginTransaction())
  48. {
  49. try
  50. {
  51. List<UserAccountRecord> list = db.UserAccountRecord.Where(m => m.CreateDate >= StartDate && m.CreateDate < EndDate && m.Status == -1).ToList();
  52. foreach (UserAccountRecord sub in list)
  53. {
  54. UserAccount account = db.UserAccount.FirstOrDefault(m => m.UserId == sub.UserId);
  55. if (account == null)
  56. {
  57. account = db.UserAccount.Add(new UserAccount()
  58. {
  59. Id = sub.UserId,
  60. CreateDate = DateTime.Now,
  61. UserId = sub.UserId,
  62. UserProperty = 0,
  63. UserType = 2,
  64. }).Entity;
  65. db.SaveChanges();
  66. }
  67. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  68. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  69. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  70. account.BalanceAmount += sub.ChangeAmount;
  71. account.TotalAmount += sub.ChangeAmount;
  72. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  73. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  74. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  75. UserAccountRecord edit = db.UserAccountRecord.FirstOrDefault(m => m.Id == sub.Id);
  76. if (edit != null)
  77. {
  78. edit.Status = 0;
  79. edit.BeforeTotalAmount += BeforeTotalAmount; //变更前总金额
  80. edit.AfterTotalAmount += AfterTotalAmount; //变更后总金额
  81. edit.BeforeFreezeAmount += BeforeFreezeAmount; //变更前冻结金额
  82. edit.AfterFreezeAmount += AfterFreezeAmount; //变更后冻结金额
  83. edit.BeforeBalanceAmount += BeforeBalanceAmount; //变更前余额
  84. edit.AfterBalanceAmount += AfterBalanceAmount; //变更后余额
  85. }
  86. db.SaveChanges();
  87. //首页-快联盟产品-收益查看-分红奖励
  88. List<TradeProfitItem> DiviList = RedisDbconn.Instance.GetList<TradeProfitItem>("TradeProfit:Divi:" + sub.UserId + ":" + sub.ProductType, 1, 1000000);
  89. TradeProfitItem Check = DiviList.FirstOrDefault(m => m.TradeMonthValue == TradeMonth);
  90. if (Check == null)
  91. {
  92. DiviList.Add(new TradeProfitItem()
  93. {
  94. TradeProfit = sub.ChangeAmount,
  95. TradeMonth = sub.CreateDate.Value.ToString("yyyy年MM月"),
  96. TradeMonthValue = sub.CreateDate.Value.ToString("yyyyMM"),
  97. CreateDate = DateTime.Now,
  98. });
  99. }
  100. else
  101. {
  102. Check.TradeProfit += sub.ChangeAmount;
  103. }
  104. RedisDbconn.Instance.Clear("TradeProfit:Divi:" + sub.UserId + ":" + sub.ProductType);
  105. RedisDbconn.Instance.AddList("TradeProfit:Divi:" + sub.UserId + ":" + sub.ProductType, DiviList.ToArray());
  106. }
  107. tran.Commit();
  108. }
  109. catch (Exception ex)
  110. {
  111. tran.Rollback();
  112. ErrorMsg msg = new ErrorMsg()
  113. {
  114. Time = DateTime.Now,
  115. ErrorContent = ex.ToString(),
  116. };
  117. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "DiviServiceError");
  118. }
  119. }
  120. db.Dispose();
  121. return "success";
  122. }
  123. }
  124. }