SycnAgentProfitService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 SycnAgentProfitService
  11. {
  12. public readonly static SycnAgentProfitService Instance = new SycnAgentProfitService();
  13. private SycnAgentProfitService()
  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>("SycnAgentProfitQueue");
  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. DoTradeProfitUnion(BrandId, date, SysUserName);
  40. }
  41. else if (OpType == 1)
  42. {
  43. DoTradeProfit2(BrandId, date, SysUserName);
  44. DoTradeProfit2Union(BrandId, date, SysUserName);
  45. }
  46. function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步区域代理分润数据");
  47. }
  48. catch (Exception ex)
  49. {
  50. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步区域代理分润数据异常");
  51. }
  52. }
  53. else
  54. {
  55. Thread.Sleep(60000);
  56. }
  57. }
  58. }
  59. private void DoTradeProfit(int BrandId, string date, string SysUserName)
  60. {
  61. string StartId = function.ReadInstance("/PublicParams/AgentProfitRecordId" + date + ".txt");
  62. if(string.IsNullOrEmpty(StartId))
  63. {
  64. StartId = "0";
  65. }
  66. CustomerSqlConn.op("insert into ProfitRecordForAgent (CreateDate,CreateMan,TradeMonth,UserId,TradeAmount,ProfitAmount,Remark) select now(),'root','" + date + "',UserId,sum(CreditTradeAmt),sum(TradeProfit),'来客吧区域代理奖励(直联)' from AgentProfitRecord p where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "' group by UserId order by UserId", MysqlConn.connstr);
  67. CustomerSqlConn.op("update AgentProfitRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr);
  68. }
  69. private void DoTradeProfitUnion(int BrandId, string date, string SysUserName)
  70. {
  71. string StartId = function.ReadInstance("/PublicParams/AgentProfitRecordId" + date + ".txt");
  72. if(string.IsNullOrEmpty(StartId))
  73. {
  74. StartId = "0";
  75. }
  76. CustomerSqlConn.op("insert into ProfitRecordForAgent (CreateDate,CreateMan,TradeMonth,UserId,TradeAmount,ProfitAmount,Remark) select now(),'root','" + date + "',UserId,sum(CreditTradeAmt),sum(TradeProfit),'来客吧区域代理奖励(银联)' from AgentProfitRecord p where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "' group by UserId order by UserId", MysqlConn.connstr2);
  77. CustomerSqlConn.op("update AgentProfitRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr2);
  78. }
  79. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  80. {
  81. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  82. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  83. string sql = "";
  84. DataTable dt = CustomerSqlConn.dtable("select UserId,302 ChangeType,ProfitAmount,'来客吧区域代理奖励(直联)' Remark from ProfitRecordForAgent where TradeMonth='" + date + "' and Version=0 and UserId>0 order by Id", MysqlConn.connstr);
  85. int num = 0;
  86. foreach(DataRow dr in dt.Rows)
  87. {
  88. string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  89. string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  90. string UserId = dr["UserId"].ToString();
  91. string ChangeType = dr["ChangeType"].ToString();
  92. string ChangeAmount = dr["ProfitAmount"].ToString();
  93. string Remark = dr["Remark"].ToString();
  94. num += 1;
  95. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  96. sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');";
  97. if(num >= 20)
  98. {
  99. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  100. num = 0;
  101. sql = "";
  102. }
  103. }
  104. if(!string.IsNullOrEmpty(sql))
  105. {
  106. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  107. }
  108. CustomerSqlConn.op("update ProfitRecordForAgent set Version=1 where TradeMonth='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr);
  109. }
  110. private void DoTradeProfit2Union(int BrandId, string date, string SysUserName)
  111. {
  112. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  113. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  114. string sql = "";
  115. DataTable dt = CustomerSqlConn.dtable("select UserId,302 ChangeType,ProfitAmount,'来客吧区域代理奖励(银联)' Remark from ProfitRecordForAgent where TradeMonth='" + date + "' and Version=0 and UserId>0 order by Id", MysqlConn.connstr2);
  116. int num = 0;
  117. foreach(DataRow dr in dt.Rows)
  118. {
  119. string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  120. string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  121. string UserId = dr["UserId"].ToString();
  122. string ChangeType = dr["ChangeType"].ToString();
  123. string ChangeAmount = dr["ProfitAmount"].ToString();
  124. string Remark = dr["Remark"].ToString();
  125. num += 1;
  126. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  127. sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');";
  128. if(num >= 20)
  129. {
  130. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  131. num = 0;
  132. sql = "";
  133. }
  134. }
  135. if(!string.IsNullOrEmpty(sql))
  136. {
  137. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  138. }
  139. CustomerSqlConn.op("update ProfitRecordForAgent set Version=1 where TradeMonth='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr2);
  140. }
  141. }
  142. }