using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading; using MySystem.Models; using Library; namespace MySystem { public class SycnAgentProfitService { public readonly static SycnAgentProfitService Instance = new SycnAgentProfitService(); private SycnAgentProfitService() { } public void Start() { Thread th = new Thread(doSomething); th.IsBackground = true; th.Start(); } public void doSomething() { while (true) { string content = RedisDbconn.Instance.RPop("SycnAgentProfitQueue"); if (!string.IsNullOrEmpty(content)) { try { function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步区域代理分润数据"); string[] data = content.Split(new string[] { "#cut#" }, StringSplitOptions.None); int BrandId = int.Parse(data[0]); string date = data[1]; int OpType = int.Parse(data[2]); string SysUserName = data[3]; if (OpType == 0) { DoTradeProfit(BrandId, date, SysUserName); DoTradeProfitUnion(BrandId, date, SysUserName); } else if (OpType == 1) { DoTradeProfit2(BrandId, date, SysUserName); DoTradeProfit2Union(BrandId, date, SysUserName); } function.WriteLog(DateTime.Now.ToString() + "\r\n\r\n", "同步区域代理分润数据"); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "同步区域代理分润数据异常"); } } else { Thread.Sleep(60000); } } } private void DoTradeProfit(int BrandId, string date, string SysUserName) { string StartId = function.ReadInstance("/PublicParams/AgentProfitRecordId" + date + ".txt"); if(string.IsNullOrEmpty(StartId)) { StartId = "0"; } 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); CustomerSqlConn.op("update AgentProfitRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr); } private void DoTradeProfitUnion(int BrandId, string date, string SysUserName) { string StartId = function.ReadInstance("/PublicParams/AgentProfitRecordId" + date + ".txt"); if(string.IsNullOrEmpty(StartId)) { StartId = "0"; } 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); CustomerSqlConn.op("update AgentProfitRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr2); } private void DoTradeProfit2(int BrandId, string date, string SysUserName) { string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00"; string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss"); string sql = ""; 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); int num = 0; foreach(DataRow dr in dt.Rows) { string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string UserId = dr["UserId"].ToString(); string ChangeType = dr["ChangeType"].ToString(); string ChangeAmount = dr["ProfitAmount"].ToString(); string Remark = dr["Remark"].ToString(); num += 1; sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";"; sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');"; if(num >= 20) { CustomerSqlConn.op(sql, MysqlConn.kxsconnstr); num = 0; sql = ""; } } if(!string.IsNullOrEmpty(sql)) { CustomerSqlConn.op(sql, MysqlConn.kxsconnstr); } CustomerSqlConn.op("update ProfitRecordForAgent set Version=1 where TradeMonth='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr); } private void DoTradeProfit2Union(int BrandId, string date, string SysUserName) { string start = date.Substring(0, 4) + "-" + date.Substring(4) + "-01 00:00:00"; string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss"); string sql = ""; 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); int num = 0; foreach(DataRow dr in dt.Rows) { string CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string UserId = dr["UserId"].ToString(); string ChangeType = dr["ChangeType"].ToString(); string ChangeAmount = dr["ProfitAmount"].ToString(); string Remark = dr["Remark"].ToString(); num += 1; sql += "update UserAccount set BalanceAmount=BalanceAmount+" + dr[2].ToString() + ",TotalAmount=TotalAmount+" + dr[2].ToString() + " where Id=" + dr["UserId"].ToString() + ";"; sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ChangeType + "," + ChangeAmount + ",'" + Remark + "');"; if(num >= 20) { CustomerSqlConn.op(sql, MysqlConn.kxsconnstr); num = 0; sql = ""; } } if(!string.IsNullOrEmpty(sql)) { CustomerSqlConn.op(sql, MysqlConn.kxsconnstr); } CustomerSqlConn.op("update ProfitRecordForAgent set Version=1 where TradeMonth='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr2); } } }