StandardService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 StandardService
  10. {
  11. public readonly static StandardService Instance = new StandardService();
  12. private StandardService()
  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), "StandardBack");
  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), "standardback_err");
  32. }
  33. else
  34. {
  35. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "standardback_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<MerchantRebateDetail> list = db.MerchantRebateDetail.Where(m => m.TradeMonth == TradeMonth && m.SyncStatus == 0).ToList();
  52. foreach (MerchantRebateDetail sub in list)
  53. {
  54. MachineForMerNo machinefor = RedisDbconn.Instance.Get<MachineForMerNo>("MachineForMerNo:" + sub.SnNo) ?? new MachineForMerNo();
  55. PosMachines machine = RedisDbconn.Instance.Get<PosMachines>("PosMachines:" + machinefor.SnId) ?? new PosMachines();
  56. Users user = db.Users.FirstOrDefault(m => m.Id == machine.UserId) ?? new Users();
  57. int TopUserId = 0;
  58. if (!string.IsNullOrEmpty(user.ParentNav))
  59. {
  60. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  61. }
  62. MerchantRebateDetail edit = db.MerchantRebateDetail.FirstOrDefault(m => m.Id == sub.Id);
  63. if (edit != null)
  64. {
  65. edit.SyncStatus = 1;
  66. db.SaveChanges();
  67. }
  68. UserAccount account = db.UserAccount.FirstOrDefault(m => m.UserId == user.Id);
  69. if (account == null)
  70. {
  71. account = db.UserAccount.Add(new UserAccount()
  72. {
  73. Id = user.Id,
  74. CreateDate = DateTime.Now,
  75. UserId = user.Id,
  76. UserProperty = 0,
  77. UserType = 2,
  78. }).Entity;
  79. db.SaveChanges();
  80. }
  81. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  82. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  83. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  84. account.BalanceAmount += sub.CreditRewardAmount;
  85. account.TotalAmount += sub.CreditRewardAmount;
  86. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  87. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  88. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  89. db.SaveChanges();
  90. UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  91. {
  92. CreateDate = DateTime.Now,
  93. UpdateDate = DateTime.Now,
  94. UserId = user.Id, //创客
  95. ChangeType = 40, //变动类型
  96. ProductType = sub.BrandId, //产品类型
  97. ChangeAmount = sub.CreditRewardAmount, //变更金额
  98. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  99. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  100. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  101. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  102. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  103. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  104. }).Entity;
  105. db.SaveChanges();
  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. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "StandardServiceError");
  118. }
  119. }
  120. db.Dispose();
  121. return "success";
  122. }
  123. }
  124. }