Jelajahi Sumber

测试分润

lcl 1 tahun lalu
induk
melakukan
20795e83a9

+ 75 - 75
AppStart/Helper/OperateService.cs

@@ -1,82 +1,82 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.Linq;
-using System.Threading;
-using MySystem.Models;
-using Library;
+// using System;
+// using System.Collections.Generic;
+// using System.Data;
+// using System.Linq;
+// using System.Threading;
+// using MySystem.Models;
+// using Library;
 
-namespace MySystem
-{
-    /// <summary>
-    /// 运营中心定时程序
-    /// </summary>
-    public class OperateService
-    {
-        public readonly static OperateService Instance = new OperateService();
-        private OperateService()
-        { }
+// namespace MySystem
+// {
+//     /// <summary>
+//     /// 运营中心定时程序
+//     /// </summary>
+//     public class OperateService
+//     {
+//         public readonly static OperateService Instance = new OperateService();
+//         private OperateService()
+//         { }
 
 
-        #region 每天统计一次数据
-        public void Start()
-        {
-            Thread th = new Thread(doSomething);
-            th.IsBackground = true;
-            th.Start();
-        }
+//         #region 每天统计一次数据
+//         public void Start()
+//         {
+//             Thread th = new Thread(doSomething);
+//             th.IsBackground = true;
+//             th.Start();
+//         }
 
-        public void doSomething()
-        {
-            while (true)
-            {
-                if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 4)
-                {
-                    try
-                    {
-                        string Month = DateTime.Now.ToString("yyyyMM");
-                        string Date = DateTime.Now.ToString("yyyyMMdd");
-                        string check = function.ReadInstance("/Operate/" + Date + ".txt");
-                        if (string.IsNullOrEmpty(check))
-                        {
-                            function.WritePage("/Operate/", "" + Date + ".txt", DateTime.Now.ToString("HH:mm:ss"));
-                            WebCMSEntities db = new WebCMSEntities();
+//         public void doSomething()
+//         {
+//             while (true)
+//             {
+//                 if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 4)
+//                 {
+//                     try
+//                     {
+//                         string Month = DateTime.Now.ToString("yyyyMM");
+//                         string Date = DateTime.Now.ToString("yyyyMMdd");
+//                         string check = function.ReadInstance("/Operate/" + Date + ".txt");
+//                         if (string.IsNullOrEmpty(check))
+//                         {
+//                             function.WritePage("/Operate/", "" + Date + ".txt", DateTime.Now.ToString("HH:mm:ss"));
+//                             WebCMSEntities db = new WebCMSEntities();
 
-                            //统计发货量
-                            DataTable dt = CustomerSqlConn.dtable("select CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\' and TransType in (10,11) and OpId=',Id,') where Id=',Id,';') from Users where UserType=1", MysqlConn.SqlConnStr);
-                            foreach(DataRow dr in dt.Rows)
-                            {
-                                CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
-                            }
-                            dt = CustomerSqlConn.dtable("select distinct CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\' and TransType in (10,11) and StoreId in (select Id from StoreHouse where UserId=',QueryCount,') and OpId=',OpId,') where Id=', QueryCount,';') from StoreForOperate where Sort=0 and Status>-1", MysqlConn.OpSqlConnStr);
-                            foreach(DataRow dr in dt.Rows)
-                            {
-                                CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
-                            }
+//                             //统计发货量
+//                             DataTable dt = CustomerSqlConn.dtable("select CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\' and TransType in (10,11) and OpId=',Id,') where Id=',Id,';') from Users where UserType=1", MysqlConn.SqlConnStr);
+//                             foreach(DataRow dr in dt.Rows)
+//                             {
+//                                 CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
+//                             }
+//                             dt = CustomerSqlConn.dtable("select distinct CONCAT('update Users set ThisMonthSend=(select count(Id) from StoreStockChange where CreateDate>=\'" + DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00\' and TransType in (10,11) and StoreId in (select Id from StoreHouse where UserId=',QueryCount,') and OpId=',OpId,') where Id=', QueryCount,';') from StoreForOperate where Sort=0 and Status>-1", MysqlConn.OpSqlConnStr);
+//                             foreach(DataRow dr in dt.Rows)
+//                             {
+//                                 CustomerSqlConn.op(dr[0].ToString(), MysqlConn.SqlConnStr);
+//                             }
 
-                            //统计库存
-                            Dictionary<int, int> StoreList = db.StoreHouse.Select(m => new { m.UserId, m.LaveNum }).Where(m => m.LaveNum > 0).GroupBy(m => m.UserId).Select(m => new { UserId = m.Key, Count = m.Count() }).ToDictionary(m => m.UserId, m => m.Count);
-                            foreach(int UserId in StoreList.Keys)
-                            {
-                                Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
-                                if(user != null)
-                                {
-                                    user.StoreStock += StoreList[UserId];
-                                }
-                            }
-                            db.SaveChanges();
+//                             //统计库存
+//                             Dictionary<int, int> StoreList = db.StoreHouse.Select(m => new { m.UserId, m.LaveNum }).Where(m => m.LaveNum > 0).GroupBy(m => m.UserId).Select(m => new { UserId = m.Key, Count = m.Count() }).ToDictionary(m => m.UserId, m => m.Count);
+//                             foreach(int UserId in StoreList.Keys)
+//                             {
+//                                 Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
+//                                 if(user != null)
+//                                 {
+//                                     user.StoreStock += StoreList[UserId];
+//                                 }
+//                             }
+//                             db.SaveChanges();
 
-                            db.Dispose();
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "运营中心重置本月发货量异常");
-                    }
-                }
-                Thread.Sleep(800000);
-            }
-        }
-        #endregion
-    }
-}
+//                             db.Dispose();
+//                         }
+//                     }
+//                     catch (Exception ex)
+//                     {
+//                         function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "运营中心重置本月发货量异常");
+//                     }
+//                 }
+//                 Thread.Sleep(800000);
+//             }
+//         }
+//         #endregion
+//     }
+// }

+ 88 - 74
AppStart/Helper/Profit/ProfitHelper.cs

@@ -332,24 +332,25 @@ namespace MySystem
 
         public void StartListenProfitDo()
         {
-            while(true)
-            {
-                if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0)
-                {
+            // while(true)
+            // {
+            //     if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0)
+            //     {
                     DoProfit();
-                    Thread.Sleep(600000);
-                }
-                else
-                {
-                    Thread.Sleep(3600000);
-                }
-            }
+            //         Thread.Sleep(600000);
+            //     }
+            //     else
+            //     {
+            //         Thread.Sleep(3600000);
+            //     }
+            // }
         }
 
         //分润算法
         public void DoProfit()
         {
             string TradeMonth = DateTime.Now.ToString("yyyyMM");
+            KxsMainModels.WebCMSEntities kxsdb = new KxsMainModels.WebCMSEntities();
             WebCMSEntities dbnew = new WebCMSEntities();
             DataTable dt = CustomerSqlConn.dtable("select IsAct,UserId,sum(TotalAmount) from UserAmountSummary where TradeMonth='" + TradeMonth + "' and SeoTitle='self' group by IsAct,UserId", MysqlConn.SqlConnStr);
             foreach(DataRow dr in dt.Rows)
@@ -359,7 +360,7 @@ namespace MySystem
                 decimal TotalAmount = decimal.Parse(dr[2].ToString());
                 List<ProfitUsers> users = new List<ProfitUsers>();
                 int TopUserId = 0; //顶级创客Id
-                Users us = dbnew.Users.FirstOrDefault(a => a.Id == UserId);
+                KxsMainModels.Users us = kxsdb.Users.FirstOrDefault(a => a.Id == UserId);
                 if (us != null)
                 {
                     string uidstring = us.Id.ToString();
@@ -373,7 +374,7 @@ namespace MySystem
                     foreach (string uidstr in uidlist)
                     {
                         int puid = int.Parse(function.CheckInt(uidstr));
-                        Users pus = dbnew.Users.FirstOrDefault(a => a.Id == puid);
+                        KxsMainModels.Users pus = kxsdb.Users.FirstOrDefault(a => a.Id == puid);
                         if (pus != null)
                         {
                             users.Add(new ProfitUsers()
@@ -387,71 +388,67 @@ namespace MySystem
                     }
                 }
                 List<ProfitResult> list = new List<ProfitResult>();
-                list = StartProft(dbnew, TotalAmount, 1, users, IsActive);
-                using (var tran = dbnew.Database.BeginTransaction())
+                list = StartProft(TotalAmount, 1, users, IsActive);
+                try
                 {
-                    try
+                    foreach (ProfitResult sub in list)
                     {
-                        foreach (ProfitResult sub in list)
+                        if (sub.Message == "分红奖励")
+                        { 
+                            UserAccountRecord accountRecord = dbnew.UserAccountRecord.Add(new UserAccountRecord()
+                            {
+                                CreateDate = DateTime.Now,
+                                UpdateDate = DateTime.Now,
+                                UserId = sub.UserId, //创客
+                                ChangeType = 11, //变动类型
+                                ProductType = 1, //产品类型
+                                ChangeAmount = sub.Money, //变更金额
+                                Status = -1,
+                            }).Entity;
+                            dbnew.SaveChanges();
+                        }
+                        else
                         {
-                            if (sub.Message == "分红奖励")
-                            { 
-                                UserAccountRecord accountRecord = dbnew.UserAccountRecord.Add(new UserAccountRecord()
+                            int ProfitType = sub.UserId == UserId ? 0 : 1;
+                            ProfitRewardRecord editprofitrecord = dbnew.ProfitRewardRecord.FirstOrDefault(m => m.UserId == sub.UserId && m.BrandId == 1 && m.ProfitType == ProfitType && m.TradeMonth == TradeMonth);
+                            if (editprofitrecord == null)
+                            {
+                                editprofitrecord = dbnew.ProfitRewardRecord.Add(new ProfitRewardRecord()
                                 {
                                     CreateDate = DateTime.Now,
-                                    UpdateDate = DateTime.Now,
                                     UserId = sub.UserId, //创客
-                                    ChangeType = 11, //变动类型
-                                    ProductType = 1, //产品类型
-                                    ChangeAmount = sub.Money, //变更金额
-                                    Status = -1,
+                                    BrandId = 1, //品牌
+                                    ProfitType = ProfitType, //创客分润类型
+                                    TradeMonth = TradeMonth, //交易月
+                                    TopUserId = TopUserId, //顶级创客
                                 }).Entity;
                                 dbnew.SaveChanges();
-                            }
-                            else
-                            {
-                                int ProfitType = sub.UserId == UserId ? 0 : 1;
-                                ProfitRewardRecord editprofitrecord = dbnew.ProfitRewardRecord.FirstOrDefault(m => m.UserId == sub.UserId && m.BrandId == 1 && m.ProfitType == ProfitType && m.TradeMonth == TradeMonth);
-                                if (editprofitrecord == null)
+                                string RecordNo = "KPM";
+                                int RecordId = editprofitrecord.Id;
+                                string RecordIdString = RecordId.ToString();
+                                for (int i = 0; i < 14 - RecordId.ToString().Length; i++)
                                 {
-                                    editprofitrecord = dbnew.ProfitRewardRecord.Add(new ProfitRewardRecord()
-                                    {
-                                        CreateDate = DateTime.Now,
-                                        UserId = sub.UserId, //创客
-                                        BrandId = 1, //品牌
-                                        ProfitType = ProfitType, //创客分润类型
-                                        TradeMonth = TradeMonth, //交易月
-                                        TopUserId = TopUserId, //顶级创客
-                                    }).Entity;
-                                    dbnew.SaveChanges();
-                                    string RecordNo = "KPM";
-                                    int RecordId = editprofitrecord.Id;
-                                    string RecordIdString = RecordId.ToString();
-                                    for (int i = 0; i < 14 - RecordId.ToString().Length; i++)
-                                    {
-                                        RecordIdString = "0" + RecordIdString;
-                                    }
-                                    RecordNo += RecordIdString;
-                                    editprofitrecord.RecordNo = RecordNo;
-                                    dbnew.SaveChanges();
+                                    RecordIdString = "0" + RecordIdString;
                                 }
-                                editprofitrecord.TradeAmt += TotalAmount; //商户交易总额                            
-                                editprofitrecord.DebitCapTradeNum += 1; //商户借记卡封顶交易总笔数
-                                decimal ProfitMoney = sub.Money;
+                                RecordNo += RecordIdString;
+                                editprofitrecord.RecordNo = RecordNo;
                                 dbnew.SaveChanges();
                             }
+                            editprofitrecord.TradeAmt += TotalAmount; //商户交易总额
+                            editprofitrecord.DebitCapTradeNum += 1; //商户借记卡封顶交易总笔数
+                            decimal ProfitMoney = sub.Money;
+                            dbnew.SaveChanges();
                         }
-                        dbnew.SaveChanges();
-                        tran.Commit();
-                    }
-                    catch (Exception ex)
-                    { 
-                        tran.Rollback();
-                        function.WriteLog(ex.ToString(), "分润异常");
                     }
+                    dbnew.SaveChanges();
+                }
+                catch (Exception ex)
+                { 
+                    function.WriteLog(ex.ToString(), "分润异常");
                 }
             }
             dbnew.Dispose();
+            kxsdb.Dispose();
         }
 
         public void DoActives(WebCMSEntities dbnew, TradeRecord trade_record)
@@ -719,17 +716,17 @@ namespace MySystem
 
 
         #region 分润算法
-        public List<ProfitResult> StartProft(WebCMSEntities dbnew, decimal TotalAmount, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
+        public List<ProfitResult> StartProft(decimal TotalAmount, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
         {
-            int BrandId = 1;
-            List<UserLevelSet> levels = RedisDbconn.Instance.GetList<UserLevelSet>("pobjlevel");
+            string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
+            WebCMSEntities db = new WebCMSEntities();
+            List<UserLevelSet> levels = db.UserLevelSet.ToList();
             List<ProfitResult> result = new List<ProfitResult>();
-            ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj" + BrandId);
+            ProfitObjects obj = db.ProfitObjects.FirstOrDefault();
             if (obj.Status == 1) //判断分润是否开启
             {
                 int maxLevel = obj.MaxLevel; //最大等级
                 int maxFloor = obj.MaxFloor; //最大层级
-                int maxContinueFloor = RedisDbconn.Instance.Get<int>("pobjconc" + BrandId); //最大继续分润层级
                 decimal diffLevelProfit = 0;  //等级级差
                 // decimal diffDiviProfit = 0; //分红级差
                 int curLevel = 0; //当前层级的会员等级
@@ -740,13 +737,8 @@ namespace MySystem
                     {
                         user = users[curFloor - 1];
                     }
-                    int UserLevel = 1; //user.UserLevel; //当前会员等级
-                    decimal utrade = RedisDbconn.Instance.Get<decimal>("TeamTradeStat:" + user.UserId + ":" + BrandId + ":" + DateTime.Now.ToString("yyMM"));
-                    UserLevelSet checklevel = levels.Where(m => m.UpLevelIntegral < utrade).OrderBy(m => m.Id).FirstOrDefault();
-                    if (checklevel != null)
-                    {
-                        UserLevel = checklevel.Id + 1;
-                    }
+                    SubUser selfUser = GetUser(user.UserId, Month) ?? new SubUser();
+                    int UserLevel = selfUser.PreUserLevel > selfUser.UserLevel ? selfUser.PreUserLevel : selfUser.UserLevel; //当前会员等级
                     //判断当前创客是否有直推的激活机具,并且在活动时间内
                     if (UserLevel == maxLevel && UserLevel == curLevel)
                     {
@@ -754,7 +746,7 @@ namespace MySystem
                     }
                     if (UserLevel <= maxLevel && UserLevel > curLevel)
                     {
-                        ProfitObjectLevels objlevel = RedisDbconn.Instance.Get<ProfitObjectLevels>("pobjlv" + LevelKindId + ":" + UserLevel); //获取当前等级参数
+                        ProfitObjectLevels objlevel = db.ProfitObjectLevels.FirstOrDefault(m => m.Id == UserLevel); //获取当前等级参数
                         if (objlevel != null)
                         {
                             decimal getLevelProfit = 0;  //等级分润
@@ -788,8 +780,30 @@ namespace MySystem
                     curLevel = UserLevel;
                 }
             }
+            db.Dispose();
             return result;
         }
+
+        public SubUser GetUser(int UserId, string Month)
+        {
+            SubUser user = new SubUser();
+            DataTable dt = GetDataTable("select * from Users" + Month + " where Id=" + UserId);
+            if(dt.Rows.Count > 0)
+            {
+                user.Id = int.Parse(dt.Rows[0]["Id"].ToString());
+                user.ParentUserId = int.Parse(dt.Rows[0]["ParentUserId"].ToString());
+                user.ParentNav = dt.Rows[0]["ParentNav"].ToString();
+                user.UserLevel = int.Parse(dt.Rows[0]["UserLevel"].ToString());
+                user.PreUserLevel = int.Parse(dt.Rows[0]["PreUserLevel"].ToString());
+            }
+            return user;
+        }
+
+        public DataTable GetDataTable(string sqlstr)
+        {
+            DataTable dt = CustomerSqlConn.dtable(sqlstr, Library.ConfigurationManager.AppSettings["KxsStatSqlConnStr"].ToString());
+            return dt;
+        }
         #endregion
 
         #region 活动奖励算法

+ 1 - 1
Config/MysqlConn.cs

@@ -5,6 +5,6 @@ namespace MySystem
     public class MysqlConn
     {
         public static string SqlConnStr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
-        public static string OpSqlConnStr = ConfigurationManager.AppSettings["OpSqlConnStr"].ToString();
+        // public static string OpSqlConnStr = ConfigurationManager.AppSettings["OpSqlConnStr"].ToString();
     }
 }

+ 17 - 13
Startup.cs

@@ -125,7 +125,7 @@ namespace MySystem
                 app.UseHsts();
                 // Library.ConfigurationManager.EnvironmentFlag = 2;
             }
-            Library.ConfigurationManager.EnvironmentFlag = 2;
+            Library.ConfigurationManager.EnvironmentFlag = 1;
             Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
             // app.UseStatusCodePagesWithReExecute("/public/errpage/pc/{0}.html");
 
@@ -171,19 +171,23 @@ namespace MySystem
             });
 
             //必须打开的
-            MerchantConfirmService.Instance.Start(); //特约商户进件队列
-            CheckAlipaySignService.Instance.Start(); //特约商户签约队列(支付宝)
-            CheckWeChatSignService.Instance.Start(); //特约商户签约队列(微信)
-            AlipayPayBackService.Instance.Start(); //支付宝支付回调队列
-            AlipayPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
-            WeChatPayBackService.Instance.Start(); //微信支付回调队列
-            WeChatPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
-            ProfitHelper.Instance.StartActive(); //发放达标奖励队列
-            ProfitHelper.Instance.StartListenTrade(); //支付宝返现队列
-            ProfitHelper.Instance.StartListenWxTrade(); //微信返现队列
-            ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(微信)
-            AlipayShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(支付宝)
+            // MerchantConfirmService.Instance.Start(); //特约商户进件队列
+            // CheckAlipaySignService.Instance.Start(); //特约商户签约队列(支付宝)
+            // CheckWeChatSignService.Instance.Start(); //特约商户签约队列(微信)
+            // AlipayPayBackService.Instance.Start(); //支付宝支付回调队列
+            // AlipayPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
+            // WeChatPayBackService.Instance.Start(); //微信支付回调队列
+            // WeChatPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
+            // ProfitHelper.Instance.StartActive(); //发放达标奖励队列
+            // ProfitHelper.Instance.StartListenTrade(); //支付宝返现队列
+            // ProfitHelper.Instance.StartListenWxTrade(); //微信返现队列
+            // ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(微信)
+            // AlipayShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(支付宝)
             //必须打开的
+
+
+
+            ProfitHelper.Instance.StartListenProfit();
         }
     }
 }

+ 2 - 0
appsettings.Development.json

@@ -19,6 +19,8 @@
     "Database": "QrCodePlateMainServer",
     "SqlConnStr": "server=47.109.31.237;port=3306;user=QrCodePlateMainServer;password=ll4DFaALMu9YIooM;database=QrCodePlateMainServer;charset=utf8;",
     "SpSqlConnStr": "server=47.109.31.237;port=3306;user=QrCodePlateSpServer;password=T2ZzxUq2fix0cTqa;database=QrCodePlateSpServer;charset=utf8;",
+    "KxsSqlConnStr": "server=47.109.31.237;port=3306;user=KxsMainServer;password=FrW8ZfxlcaVdm1r0;database=KxsMainServer;charset=utf8;",
+    "KxsStatSqlConnStr": "server=47.109.31.237;port=3306;user=KxsMainServer;password=FrW8ZfxlcaVdm1r0;database=KxsStatServer;charset=utf8;",
     "RedisConnStr": "47.109.31.237:6379,password=klm@redis,DefaultDatabase=4,poolsize=500,preheat=50,asyncPipeline=true",
     "IOSAppVersion": "1.0.0",
     "AndroidAppVersion": "1.0.0",