ProfitService.cs 9.2 KB

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