SycnHelpProfitService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. public class SycnHelpProfitService
  11. {
  12. public readonly static SycnHelpProfitService Instance = new SycnHelpProfitService();
  13. private SycnHelpProfitService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(doSomething);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void doSomething()
  22. {
  23. while (true)
  24. {
  25. string content = RedisDbconn.Instance.RPop<string>("SycnHelpProfitQueue2");
  26. if (!string.IsNullOrEmpty(content))
  27. {
  28. try
  29. {
  30. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步助利宝分润数据");
  31. string[] data = content.Split(new string[] { "#cut#" }, StringSplitOptions.None);
  32. string date = data[0];
  33. int OpType = int.Parse(data[1]);
  34. string SysUserName = data[2];
  35. if (OpType == 0)
  36. {
  37. DoHelpProfit(date, SysUserName);
  38. }
  39. else if (OpType == 1)
  40. {
  41. DoHelpProfit2(date, SysUserName);
  42. }
  43. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步助利宝分润数据");
  44. }
  45. catch (Exception ex)
  46. {
  47. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步助利宝分润数据异常");
  48. }
  49. }
  50. else
  51. {
  52. Thread.Sleep(60000);
  53. }
  54. }
  55. }
  56. #region 助力宝分润展示
  57. public void DoHelpProfit(string month, string sysUserName)
  58. {
  59. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  60. OtherMySqlConn.dtable("update HelpProfitReward set Status=1 where Status=0 and TradeMonth='" + month + "'");
  61. }
  62. #endregion
  63. #region 助力宝分润到账
  64. public void DoHelpProfit2(string month, string sysUserName)
  65. {
  66. WebCMSEntities db = new WebCMSEntities();
  67. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  68. DataTable dt = OtherMySqlConn.dtable("select UserId,RewardType,sum(CreditRewardAmount),sum(CreditTradeAmt) from HelpProfitReward where Status=1 and TradeMonth='" + month + "' GROUP BY UserId,RewardType");
  69. function.WriteLog("数量:" + dt.Rows.Count, "同步助利宝分润");
  70. int index = 0;
  71. foreach (DataRow dr in dt.Rows)
  72. {
  73. index += 1;
  74. int UserId = int.Parse(dr["UserId"].ToString());
  75. int RewardType = int.Parse(dr["RewardType"].ToString());
  76. decimal ProfitMoney = decimal.Parse(dr[2].ToString());
  77. decimal TradeAmt = decimal.Parse(dr[3].ToString());
  78. var tran = db.Database.BeginTransaction();
  79. bool op = false;
  80. try
  81. {
  82. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  83. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  84. if (account == null)
  85. {
  86. account = db.UserAccount.Add(new UserAccount()
  87. {
  88. Id = UserId,
  89. UserId = UserId,
  90. }).Entity;
  91. db.SaveChanges();
  92. }
  93. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  94. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  95. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  96. account.BalanceAmount += ProfitMoney;
  97. account.TotalAmount += ProfitMoney;
  98. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  99. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  100. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  101. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  102. {
  103. CreateDate = DateTime.Now,
  104. UpdateDate = DateTime.Now,
  105. UserId = UserId, //创客
  106. ProductType = 101,
  107. ChangeType = RewardType, //变动类型
  108. ChangeAmount = ProfitMoney, //变更金额
  109. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  110. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  111. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  112. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  113. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  114. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  115. Remark = RewardType == 1 ? "直拓商户分润" : "推荐交易分润",
  116. }).Entity;
  117. db.SaveChanges();
  118. tran.Commit();
  119. op = true;
  120. }
  121. catch (Exception ex)
  122. {
  123. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步助利宝分润异常");
  124. tran.Rollback();
  125. }
  126. function.WriteLog(index.ToString(), "同步助利宝分润");
  127. }
  128. OtherMySqlConn.dtable("update HelpProfitReward set Status=2 where Status=1 and TradeMonth='" + month + "'");
  129. db.Dispose();
  130. }
  131. #endregion
  132. }
  133. }