using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading; using MySystem.Models; using Library; namespace MySystem { public class SycnProfitServiceV3 { public readonly static SycnProfitServiceV3 Instance = new SycnProfitServiceV3(); private SycnProfitServiceV3() { } public void Start() { Thread th = new Thread(doSomething); th.IsBackground = true; th.Start(); } public void doSomething() { while (true) { string content = RedisDbconn.Instance.RPop("SycnProfitQueue2"); 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); } else if (OpType == 1) { DoTradeProfit2(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) { WebCMSEntities db = new WebCMSEntities(); string StartId = function.ReadInstance("/PublicParams/ProfitRewardRecordId" + date + ".txt"); if(string.IsNullOrEmpty(StartId)) { StartId = "0"; } CustomerSqlConn.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", MysqlConn.connstr); CustomerSqlConn.op("update ProfitRewardRecord set CheckStatus=1 where Id>=" + StartId + " and CheckStatus=0 and BrandId=" + BrandId + " and UserId>0 and TradeMonth='" + date + "'", MysqlConn.connstr); db.Dispose(); } private void DoTradeProfit2(int BrandId, string date, string SysUserName) { // 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); 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," + 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); 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 ProductType = dr["ProductType"].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[1].ToString() + ",TotalAmount=TotalAmount+" + dr[1].ToString() + " where Id=" + dr["UserId"].ToString() + ";"; sql += "insert into UserAccountRecord (CreateDate,UpdateDate,UserId,ProductType,ChangeType,ChangeAmount,Remark) values ('" + CreateDate + "','" + UpdateDate + "'," + UserId + "," + ProductType + "," + 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 ProfitRecord set Version=1 where BrandId=" + BrandId + " and SeoTitle='" + date + "' and Version=0 and UserId>0", MysqlConn.connstr); } #region private bool CheckUser(WebCMSEntities db, int Id) { DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间 //超过90天创客判断 return db.Users.Any(m => m.Id == Id && m.AuthFlag == 1 && m.AuthDate < ExpireDate); } #endregion } }