SycnProfitServiceV3.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 SycnProfitServiceV3
  11. {
  12. public readonly static SycnProfitServiceV3 Instance = new SycnProfitServiceV3();
  13. private SycnProfitServiceV3()
  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>("SycnProfitQueue2");
  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. int BrandId = int.Parse(data[0]);
  33. string date = data[1];
  34. int OpType = int.Parse(data[2]);
  35. string SysUserName = data[3];
  36. if (OpType == 0)
  37. {
  38. DoTradeProfit(BrandId, date, SysUserName);
  39. }
  40. else if (OpType == 1)
  41. {
  42. DoTradeProfit2(BrandId, date, SysUserName);
  43. }
  44. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步分润数据");
  45. }
  46. catch (Exception ex)
  47. {
  48. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步分润数据异常");
  49. }
  50. }
  51. else
  52. {
  53. Thread.Sleep(60000);
  54. }
  55. }
  56. }
  57. private void DoTradeProfit(int BrandId, string date, string SysUserName)
  58. {
  59. WebCMSEntities db = new WebCMSEntities();
  60. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  61. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  62. if(string.IsNullOrEmpty(StartId))
  63. {
  64. StartId = "0";
  65. }
  66. OtherMySqlConn.op("insert into ProfitRecord (CreateDate,CreateMan,SeoTitle,BrandId,UserId,DirectFlag,ProfitAmount) select now(),'root','" + date + "'," + BrandId + ",UserId,ProfitType,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord p where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "' group by UserId,ProfitType order by UserId");
  67. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(CreditTradeProfit+DebitNonTradeCapProfit+DebitTradeCapProfit) from ProfitRewardRecord where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "' group by UserId");
  68. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  69. int index = 0;
  70. foreach (DataRow dr in dt.Rows)
  71. {
  72. index += 1;
  73. int UserId = int.Parse(dr["UserId"].ToString());
  74. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  75. try
  76. {
  77. string content = UserId + "|" + BrandId + "|" + ProfitMoney + "|" + index;
  78. RedisDbconn.Instance.AddList("DoTradeProfitQueue", content);
  79. }
  80. catch (Exception ex)
  81. {
  82. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  83. }
  84. function.WriteLog(index.ToString(), "同步分润数据");
  85. }
  86. OtherMySqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'");
  87. db.Dispose();
  88. dt.Dispose();
  89. RedisDbconn.Instance.AddList("DoTradeProfitQueue", "end");
  90. SycnProfitServiceV2.Instance.StartProfit();
  91. }
  92. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  93. {
  94. CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ProductType,ChangeType,ChangeAmount,Remark) select now() CreateDate,now() UpdateDate,UserId," + BrandId + " ProductType,1 ChangeType,ProfitAmount,(case when DirectFlag=1 then '直拓商户分润' else '品牌推广服务费' end) Remark from ProfitRecord where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id", MysqlConn.connstr);
  95. CustomerSqlConn.op("update ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr);
  96. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  97. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  98. string sql = "";
  99. DataTable dt = CustomerSqlConn.dtable("select UserId,sum(ChangeAmount) from UserAccountRecord where CreateDate>='" + start + "' and CreateDate<'" + end + "' group by UserId", MysqlConn.connstr);
  100. int num = 0;
  101. foreach(DataRow dr in dt.Rows)
  102. {
  103. num += 1;
  104. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[1].ToString() + ",TotalAmount=TotalAmount+" + dr[1].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  105. if(num >= 20)
  106. {
  107. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  108. num = 0;
  109. sql = "";
  110. }
  111. }
  112. if(!string.IsNullOrEmpty(sql))
  113. {
  114. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  115. }
  116. }
  117. #region
  118. private bool CheckUser(WebCMSEntities db, int Id)
  119. {
  120. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  121. //超过90天创客判断
  122. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  123. }
  124. #endregion
  125. }
  126. }