FluxService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 FluxService
  10. {
  11. public readonly static FluxService Instance = new FluxService();
  12. private FluxService()
  13. { }
  14. public void Start(JobMqMsg jobInfo)
  15. {
  16. string content = "";
  17. try
  18. {
  19. string Msg = CheckFlux();
  20. // jobInfo.Status = Msg == "success" ? 1: 0;
  21. // jobInfo.Msg = Msg == "success" ? "执行完成": Msg;
  22. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "FluxBack");
  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), "fluxback_err");
  32. }
  33. else
  34. {
  35. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "fluxback_service");
  36. }
  37. }
  38. }
  39. //检查机具扣费情况
  40. private string CheckFlux()
  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<FluxProfitSummary> list = db.FluxProfitSummary.Where(m => m.TradeMonth == TradeMonth && m.CheckStatus == 0).ToList();
  52. foreach (FluxProfitSummary sub in list)
  53. {
  54. int UserId = sub.UserId; //创客
  55. decimal FluxProfitAmt = sub.FluxProfitAmt; //交易分润
  56. decimal ActualProfit = 0;
  57. //核对
  58. bool check = db.FluxProfitDetail.Any(m => m.UserId == sub.UserId && m.TradeDate == sub.TradeMonth);
  59. if (check)
  60. {
  61. ActualProfit = db.FluxProfitDetail.Where(m => m.UserId == sub.UserId && m.TradeDate == sub.TradeMonth).Sum(m => m.FluxProfitAmt);
  62. }
  63. if (ActualProfit == FluxProfitAmt)
  64. {
  65. //核对成功
  66. FluxProfitSummary edit = db.FluxProfitSummary.FirstOrDefault(m => m.Id == sub.Id);
  67. if (edit != null)
  68. {
  69. edit.CheckStatus = 1;
  70. db.SaveChanges();
  71. }
  72. UserAccount account = db.UserAccount.FirstOrDefault(m => m.UserId == sub.UserId);
  73. if (account == null)
  74. {
  75. account = db.UserAccount.Add(new UserAccount()
  76. {
  77. Id = sub.UserId,
  78. CreateDate = DateTime.Now,
  79. UserId = sub.UserId,
  80. UserProperty = 0,
  81. UserType = 2,
  82. }).Entity;
  83. db.SaveChanges();
  84. }
  85. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  86. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  87. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  88. account.BalanceAmount += FluxProfitAmt;
  89. account.TotalAmount += FluxProfitAmt;
  90. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  91. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  92. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  93. db.SaveChanges();
  94. UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  95. {
  96. CreateDate = DateTime.Now,
  97. UpdateDate = DateTime.Now,
  98. UserId = sub.UserId, //创客
  99. ChangeType = 60, //变动类型
  100. ProductType = sub.BrandId, //产品类型
  101. ChangeAmount = FluxProfitAmt, //变更金额
  102. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  103. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  104. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  105. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  106. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  107. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  108. }).Entity;
  109. db.SaveChanges();
  110. RedisDbconn.Instance.HAddNumber("TmpFluxProfit", sub.UserId + "_" + sub.BrandId + "_" + StatDate.ToString("yyyy年MM月") + "_" + StatDate.ToString("yyyyMM"), FluxProfitAmt);
  111. }
  112. else
  113. {
  114. ErrorMsg msg = new ErrorMsg()
  115. {
  116. Time = DateTime.Now,
  117. ErrorContent = "同步流量卡分佣失败,数据不一致",
  118. Obj = Newtonsoft.Json.JsonConvert.SerializeObject(sub),
  119. };
  120. RedisDbconn.Instance.AddList("FluxServiceWarning", msg);
  121. }
  122. }
  123. //首页-快联盟产品-收益查看-流量卡分佣
  124. Dictionary<string, decimal> datalist = RedisDbconn.Instance.HGetAll<decimal>("TmpFluxProfit");
  125. foreach (string key in datalist.Keys)
  126. {
  127. string[] fields = key.Split('_');
  128. string UserId = fields[0];
  129. string BrandId = fields[1];
  130. string TradeM = fields[2];
  131. string TradeMValue = fields[3];
  132. RedisDbconn.Instance.AddList("FluxProfitSummary:" + UserId + ":" + BrandId, new TradeProfitItem()
  133. {
  134. TradeProfit = datalist[key],
  135. TradeMonth = TradeM,
  136. TradeMonthValue = TradeMValue,
  137. CreateDate = DateTime.Now,
  138. });
  139. }
  140. RedisDbconn.Instance.Clear("TmpFluxProfit");
  141. tran.Commit();
  142. }
  143. catch (Exception ex)
  144. {
  145. tran.Rollback();
  146. ErrorMsg msg = new ErrorMsg()
  147. {
  148. Time = DateTime.Now,
  149. ErrorContent = ex.ToString(),
  150. };
  151. LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "ProfitServiceError");
  152. }
  153. }
  154. db.Dispose();
  155. return "success";
  156. }
  157. }
  158. }