ActiveService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models;
  5. using Library;
  6. using LitJson;
  7. namespace MySystem
  8. {
  9. public class ActiveService
  10. {
  11. public readonly static ActiveService Instance = new ActiveService();
  12. private ActiveService()
  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), "ActiveBack");
  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. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "activeback_err");
  32. }
  33. else
  34. {
  35. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "activeback_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<ActiveReward> list = db.ActiveReward.Where(m => m.CreateDate >= StartDate && m.CreateDate < EndDate && m.Status == -1).ToList();
  52. foreach (ActiveReward sub in list)
  53. {
  54. ActiveReward edit = db.ActiveReward.FirstOrDefault(m => m.Id == sub.Id);
  55. if (edit != null)
  56. {
  57. edit.Status = 0;
  58. db.SaveChanges();
  59. }
  60. UserAccount account = db.UserAccount.FirstOrDefault(m => m.UserId == sub.UserId);
  61. if (account == null)
  62. {
  63. account = db.UserAccount.Add(new UserAccount()
  64. {
  65. Id = sub.UserId,
  66. CreateDate = DateTime.Now,
  67. UserId = sub.UserId,
  68. UserProperty = 0,
  69. UserType = 2,
  70. }).Entity;
  71. db.SaveChanges();
  72. }
  73. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  74. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  75. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  76. account.BalanceAmount += sub.RewardAmount;
  77. account.TotalAmount += sub.RewardAmount;
  78. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  79. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  80. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  81. db.SaveChanges();
  82. UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  83. {
  84. CreateDate = DateTime.Now,
  85. UpdateDate = DateTime.Now,
  86. UserId = sub.UserId, //创客
  87. ChangeType = 0, //变动类型
  88. ProductType = sub.BrandId, //产品类型
  89. ChangeAmount = sub.RewardAmount, //变更金额
  90. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  91. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  92. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  93. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  94. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  95. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  96. }).Entity;
  97. db.SaveChanges();
  98. }
  99. tran.Commit();
  100. }
  101. catch (Exception ex)
  102. {
  103. tran.Rollback();
  104. ErrorMsg msg = new ErrorMsg()
  105. {
  106. Time = DateTime.Now,
  107. ErrorContent = ex.ToString(),
  108. };
  109. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "ActiveServiceError");
  110. }
  111. }
  112. db.Dispose();
  113. return "success";
  114. }
  115. }
  116. }