SycnProfitServiceV3.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  61. if(string.IsNullOrEmpty(StartId))
  62. {
  63. StartId = "0";
  64. }
  65. CustomerSqlConn.op("insert into ProfitRecord (CreateDate,CreateMan,SeoTitle,UserId,DirectFlag,ProfitAmount) select now(),'root','" + date + "',UserId,ProfitType,sum(CreditTradeProfit+TradeProfit) from ProfitRewardRecord p where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "' group by UserId,ProfitType order by UserId", MysqlConn.connstr);
  66. CustomerSqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr);
  67. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(ProfitAmount) from ProfitRecord where UserId>0 and SeoTitle='" + 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 + "|0|" + 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. db.Dispose();
  87. }
  88. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  89. {
  90. // CustomerSqlConn.op("insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ProductType,ChangeType,ChangeAmount,Remark) select now() CreateDate,now() UpdateDate,UserId," + BrandId + " ProductType,301 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);
  91. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  92. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  93. string sql = "";
  94. DataTable dt = CustomerSqlConn.dtable("select UserId,301 ChangeType,ProfitAmount,(case when DirectFlag=1 then '直拓商户分润' else '品牌推广服务费' end) Remark from ProfitRecord where SeoTitle='" + date + "' and Version=0 and UserId>0 order by Id", MysqlConn.connstr);
  95. int num = 0;
  96. foreach(DataRow dr in dt.Rows)
  97. {
  98. string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  99. string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  100. string UserId = dr["UserId"].ToString();
  101. string ChangeType = dr["ChangeType"].ToString();
  102. string ChangeAmount = dr["ProfitAmount"].ToString();
  103. string Remark = dr["Remark"].ToString();
  104. num += 1;
  105. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  106. sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');";
  107. if(num >= 20)
  108. {
  109. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  110. num = 0;
  111. sql = "";
  112. }
  113. }
  114. if(!string.IsNullOrEmpty(sql))
  115. {
  116. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  117. }
  118. CustomerSqlConn.op("update ProfitRecord set Version=1 where SeoTitle='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr);
  119. }
  120. #region
  121. private bool CheckUser(WebCMSEntities db, int Id)
  122. {
  123. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  124. //超过90天创客判断
  125. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  126. }
  127. #endregion
  128. }
  129. }