SycnProfitServiceV3.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. 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. WebCMSEntities db = new WebCMSEntities();
  62. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  63. if(string.IsNullOrEmpty(StartId))
  64. {
  65. StartId = "0";
  66. }
  67. 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);
  68. CustomerSqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr);
  69. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(ProfitAmount) from ProfitRecord where UserId>0 and SeoTitle='" + date + "' group by UserId");
  70. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  71. int index = 0;
  72. foreach (DataRow dr in dt.Rows)
  73. {
  74. index += 1;
  75. int UserId = int.Parse(dr["UserId"].ToString());
  76. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  77. try
  78. {
  79. string content = UserId + "|0|" + ProfitMoney + "|" + index;
  80. RedisDbconn.Instance.AddList("DoTradeProfitQueue", content);
  81. }
  82. catch (Exception ex)
  83. {
  84. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  85. }
  86. function.WriteLog(index.ToString(), "同步分润数据");
  87. }
  88. db.Dispose();
  89. }
  90. private void DoTradeProfit2(int BrandId, string date, string SysUserName)
  91. {
  92. // 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);
  93. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  94. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  95. string sql = "";
  96. 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);
  97. int num = 0;
  98. foreach(DataRow dr in dt.Rows)
  99. {
  100. string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  101. string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  102. string UserId = dr["UserId"].ToString();
  103. string ChangeType = dr["ChangeType"].ToString();
  104. string ChangeAmount = dr["ProfitAmount"].ToString();
  105. string Remark = dr["Remark"].ToString();
  106. num += 1;
  107. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  108. sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');";
  109. if(num >= 20)
  110. {
  111. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  112. num = 0;
  113. sql = "";
  114. }
  115. }
  116. if(!string.IsNullOrEmpty(sql))
  117. {
  118. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  119. }
  120. CustomerSqlConn.op("update ProfitRecord set Version=1 where SeoTitle='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr);
  121. }
  122. private void DoTradeProfitUnion(int BrandId, string date, string SysUserName)
  123. {
  124. WebCMSEntities db = new WebCMSEntities();
  125. string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt");
  126. if(string.IsNullOrEmpty(StartId))
  127. {
  128. StartId = "0";
  129. }
  130. 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.connstr2);
  131. CustomerSqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr2);
  132. DataTable dt = OtherMySqlConn.dtable("select UserId,sum(ProfitAmount) from ProfitRecord where UserId>0 and SeoTitle='" + date + "' group by UserId");
  133. function.WriteLog("分润:" + BrandId + ":" + dt.Rows.Count, "同步分润数据");
  134. int index = 0;
  135. foreach (DataRow dr in dt.Rows)
  136. {
  137. index += 1;
  138. int UserId = int.Parse(dr["UserId"].ToString());
  139. decimal ProfitMoney = decimal.Parse(dr[1].ToString());
  140. try
  141. {
  142. string content = UserId + "|2|" + ProfitMoney + "|" + index;
  143. RedisDbconn.Instance.AddList("DoTradeProfitQueue", content);
  144. }
  145. catch (Exception ex)
  146. {
  147. function.WriteLog(DateTime.Now.ToString() + "\n" + UserId + "," + ProfitMoney + "\n" + ex.ToString(), "同步到余额异常");
  148. }
  149. function.WriteLog(index.ToString(), "同步分润数据");
  150. }
  151. db.Dispose();
  152. }
  153. private void DoTradeProfit2Union(int BrandId, string date, string SysUserName)
  154. {
  155. // 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.connstr2);
  156. string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00";
  157. string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
  158. string sql = "";
  159. 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.connstr2);
  160. int num = 0;
  161. foreach(DataRow dr in dt.Rows)
  162. {
  163. string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  164. string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  165. string UserId = dr["UserId"].ToString();
  166. string ChangeType = dr["ChangeType"].ToString();
  167. string ChangeAmount = dr["ProfitAmount"].ToString();
  168. string Remark = dr["Remark"].ToString();
  169. num += 1;
  170. sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";";
  171. sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');";
  172. if(num >= 20)
  173. {
  174. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  175. num = 0;
  176. sql = "";
  177. }
  178. }
  179. if(!string.IsNullOrEmpty(sql))
  180. {
  181. CustomerSqlConn.op(sql, MysqlConn.kxsconnstr);
  182. }
  183. CustomerSqlConn.op("update ProfitRecord set Version=1 where SeoTitle='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr2);
  184. }
  185. #region
  186. private bool CheckUser(WebCMSEntities db, int Id)
  187. {
  188. DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  189. //超过90天创客判断
  190. return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate);
  191. }
  192. #endregion
  193. }
  194. }