Browse Source

增加分润程序

lcl 1 year ago
parent
commit
71c5ed1685
6 changed files with 351 additions and 31 deletions
  1. 34 0
      Entity/ProfitResult.cs
  2. 14 0
      Entity/ProfitUsers.cs
  3. 13 0
      Entity/SubUser.cs
  4. 32 31
      Startup.cs
  5. 238 0
      Util/HaoDa/ProfitHelper.cs
  6. 20 0
      Util/PublicFunction.cs

+ 34 - 0
Entity/ProfitResult.cs

@@ -0,0 +1,34 @@
+using System;
+namespace MySystem
+{
+    public class ProfitResult
+    {
+        public int UserId { get; set; }
+        public decimal Money { get; set; }
+        public int MachineCount { get; set; }
+        public string UserNav { get; set; }
+        public string Message { get; set; }
+        public int BankCardType { get; set; }
+        public int QrPayFlag { get; set; }
+        public int DirectFlag { get; set; }
+        public int BrandId { get; set; }
+        public int IsSubsidy { get; set; } //是否补贴
+        public int HelpFlag { get; set; }
+        public int MerchantId { get; set; }
+        public decimal TradeAmount { get; set; }
+        public string PosSn { get; set; }
+        public decimal ProfitRate { get; set; } //创客预设分润比例
+        public decimal ProfitPercent { get; set; } //创客预设分润百分比
+        public decimal AddOrSubRate { get; set; } //浮动空间
+        public decimal ProfitRateBase { get; set; } //分润比例基数
+    }
+
+    public class ActiveCheckData
+    {
+        public string TableName { get; set; }
+        public string StatField { get; set; }
+        public string CheckField { get; set; }
+        public string OtherCondition { get; set; }
+        public int StartId { get; set; }
+    }
+}

+ 14 - 0
Entity/ProfitUsers.cs

@@ -0,0 +1,14 @@
+using System;
+namespace MySystem
+{
+    public class ProfitUsers
+    {
+        public int UserId { get; set; }
+        public string MakerCode { get; set; }
+        public string RealName { get; set; }
+        public int ParentUserId { get; set; }
+        public int UserLevel { get; set; }
+        public string UserNav { get; set; }
+        public DateTime CreateDate { get; set; }
+    }
+}

+ 13 - 0
Entity/SubUser.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+namespace MySystem
+{
+    public class SubUser
+    {
+        public int Id { get; set; }
+        public int ParentUserId { get; set; }
+        public string ParentNav { get; set; }
+        public int UserLevel { get; set; }
+        public int PreUserLevel { get; set; }
+    }
+}

+ 32 - 31
Startup.cs

@@ -135,39 +135,40 @@ namespace MySystem
             });
 
             initMainServer();
-            if(Library.ConfigurationManager.EnvironmentFlag == 1)
-            {
-                TestHaoDaService.Instance.Start(); //好哒测试
-                // MerchantStandardService.Instance.Start();
-            }
-            if(Library.ConfigurationManager.EnvironmentFlag == 2)
-            {
-                MerchantConfirmService.Instance.Start(); //提交商户进件
-                CheckWeChatSignService.Instance.Start(); //查询商户审核状态
-                CheckWeChatBindService.Instance.Start(); //执行好哒微信绑定appid
-                ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现
-                HaoDaExtHelper.Instance.StartWeChat();
-                HaoDaExtHelper.Instance.StartAlipay();
-                HaoDaExtQueryHelper.Instance.StartWeChat();
-                HaoDaExtQueryHelper.Instance.StartAlipay();
-                HaoDaExtQueryAuthHelper.Instance.StartWeChat();
-                HaoDaExtQueryAuthHelper.Instance.StartAlipay();
-                HaoDaAuthQueryHelper.Instance.Start(); //查询实名认证状态
-                WeChatPayBackService.Instance.Start(); //支付回调
-                WeChatPayBackService.Instance.StartProfitShare(); //分账队列
-                ProfitHelper.Instance.StartListenTrade(); //返现队列-支付宝
-                ProfitHelper.Instance.StartListenWxTrade(); //返现队列-微信
+            // if(Library.ConfigurationManager.EnvironmentFlag == 1)
+            // {
+            //     TestHaoDaService.Instance.Start(); //好哒测试
+            //     // MerchantStandardService.Instance.Start();
+            // }
+            // if(Library.ConfigurationManager.EnvironmentFlag == 2)
+            // {
+            //     MerchantConfirmService.Instance.Start(); //提交商户进件
+            //     CheckWeChatSignService.Instance.Start(); //查询商户审核状态
+            //     CheckWeChatBindService.Instance.Start(); //执行好哒微信绑定appid
+            //     ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现
+            //     HaoDaExtHelper.Instance.StartWeChat();
+            //     HaoDaExtHelper.Instance.StartAlipay();
+            //     HaoDaExtQueryHelper.Instance.StartWeChat();
+            //     HaoDaExtQueryHelper.Instance.StartAlipay();
+            //     HaoDaExtQueryAuthHelper.Instance.StartWeChat();
+            //     HaoDaExtQueryAuthHelper.Instance.StartAlipay();
+            //     HaoDaAuthQueryHelper.Instance.Start(); //查询实名认证状态
+            //     WeChatPayBackService.Instance.Start(); //支付回调
+            //     WeChatPayBackService.Instance.StartProfitShare(); //分账队列
+            //     ProfitHelper.Instance.StartListenTrade(); //返现队列-支付宝
+            //     ProfitHelper.Instance.StartListenWxTrade(); //返现队列-微信
 
-                AlipayPayBackFeeService.Instance.Start();
-                ActiveRewardService.Instance.StartAct();
-                ActiveRewardService.Instance.StartOpenReward();
-                ActiveRewardService.Instance.StartLeaderReward();
-                ActiveRewardService.Instance.StartOperateReward();
+            //     AlipayPayBackFeeService.Instance.Start();
+            //     ActiveRewardService.Instance.StartAct();
+            //     ActiveRewardService.Instance.StartOpenReward();
+            //     ActiveRewardService.Instance.StartLeaderReward();
+            //     ActiveRewardService.Instance.StartOperateReward();
                 
-                MerchantStandardService.Instance.Start(); //商户缴纳服务费次月活动交易额大于等于1W,奖励进件创客50元
-                MerchantStandardService.Instance.StartThree(); //商户缴纳服务费次月起连续不间断三个月,每月活动交易额大于1W,奖励进件创客100元
-                MerchantStandardService.Instance.StartActive();
-            }
+            //     MerchantStandardService.Instance.Start(); //商户缴纳服务费次月活动交易额大于等于1W,奖励进件创客50元
+            //     MerchantStandardService.Instance.StartThree(); //商户缴纳服务费次月起连续不间断三个月,每月活动交易额大于1W,奖励进件创客100元
+            //     MerchantStandardService.Instance.StartActive();
+            // }
+            ProfitHelper.Instance.DoProfit(); //每月分润
         }
 
         //初始化数据结构

+ 238 - 0
Util/HaoDa/ProfitHelper.cs

@@ -221,5 +221,243 @@ namespace MySystem
             function.WriteLog("结束返现:" + orderidstring + "\n\n\n", "返现逻辑日志");
         }
         #endregion
+
+
+
+
+
+        #region 分润
+
+        public void StartListenProfit()
+        {
+            Thread th = new Thread(StartListenProfitDo);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void StartListenProfitDo()
+        {
+            while(true)
+            {
+                if(DateTime.Now.Day < 10 && DateTime.Now.Hour > 2 && DateTime.Now.Hour < 5)
+                {
+                    DoProfit();
+                    Thread.Sleep(600000);
+                }
+                else
+                {
+                    Thread.Sleep(3600000);
+                }
+            }
+        }
+
+        //分润算法
+        public void DoProfit()
+        {
+            string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
+            string check = function.ReadInstance("/ProfitFlag/" + TradeMonth + ".txt");
+            if(!string.IsNullOrEmpty(check))
+            {
+                return;
+            }
+            function.WritePage("/ProfitFlag/", TradeMonth + ".txt", DateTime.Now.ToString());
+            Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.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", AppConfig.Base.SqlConnStr);
+            foreach(DataRow dr in dt.Rows)
+            {
+                int UserId = int.Parse(dr["UserId"].ToString());
+                bool IsActive = dr["IsAct"].ToString() == "1";
+                decimal TotalAmount = decimal.Parse(dr[2].ToString());
+                List<ProfitUsers> users = new List<ProfitUsers>();
+                int TopUserId = 0; //顶级创客Id
+                Models.KxsMain.Users us = kxsdb.Users.FirstOrDefault(a => a.Id == UserId);
+                if (us != null)
+                {
+                    string uidstring = us.Id.ToString();
+                    if (!string.IsNullOrEmpty(us.ParentNav))
+                    {
+                        uidstring = us.ParentNav.Trim(',').Replace(",,", ",") + "," + uidstring;
+                        TopUserId = int.Parse(function.CheckInt(us.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]));
+                    }
+                    string[] uidlist = uidstring.Split(',');
+                    Array.Reverse(uidlist);
+                    foreach (string uidstr in uidlist)
+                    {
+                        int puid = int.Parse(function.CheckInt(uidstr));
+                        Models.KxsMain.Users pus = kxsdb.Users.FirstOrDefault(a => a.Id == puid);
+                        if (pus != null)
+                        {
+                            users.Add(new ProfitUsers()
+                            {
+                                UserId = pus.Id,
+                                UserNav = pus.ParentNav,
+                                UserLevel = pus.UserLevel,
+                                CreateDate = pus.CreateDate.Value,
+                            });
+                        }
+                    }
+                }
+                List<ProfitResult> list = new List<ProfitResult>();
+                list = StartProft(TotalAmount, 1, users, IsActive);
+                try
+                {
+                    foreach (ProfitResult sub in list)
+                    {
+                        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,
+                                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();
+                        }
+                        if(IsActive)
+                        {
+                            editprofitrecord.CreditTradeAmt += TotalAmount;
+                            editprofitrecord.CreditTradeProfit += sub.Money;
+                        }
+                        else
+                        {
+                            editprofitrecord.TradeAmt += TotalAmount;
+                            editprofitrecord.TradeProfit += sub.Money;
+                        }
+                        dbnew.SaveChanges();
+                    }
+                    dbnew.SaveChanges();
+                }
+                catch (Exception ex)
+                { 
+                    LogHelper.Instance.WriteLog(ex.ToString(), "分润异常");
+                }
+            }
+            dbnew.Dispose();
+            kxsdb.Dispose();
+        }
+
+        #endregion
+
+        #region 分润算法
+        public List<ProfitResult> StartProft(decimal TotalAmount, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
+        {
+            LogHelper.Instance.WriteLog("\n\nTotalAmount:" + TotalAmount + ";", "来客吧分润日志");
+            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 = db.ProfitObjects.FirstOrDefault();
+            if (obj.Status == 1) //判断分润是否开启
+            {
+                int maxLevel = obj.MaxLevel; //最大等级
+                int maxFloor = obj.MaxFloor; //最大层级
+                decimal diffLevelProfit = 0;  //等级级差
+                ProfitObjectLevels maxlevel = db.ProfitObjectLevels.FirstOrDefault(m => m.Id == 9) ?? new ProfitObjectLevels(); //获取最高等级参数
+                decimal maxPercent = IsActive ? maxlevel.DebitPercents : maxlevel.Percents;
+                LogHelper.Instance.WriteLog("maxPercent:" + maxPercent + ";", "来客吧分润日志");
+                decimal maxLevelProfit = TotalAmount * maxPercent;
+                LogHelper.Instance.WriteLog("maxLevelProfit:" + maxLevelProfit + ";", "来客吧分润日志");
+                maxLevelProfit = PublicFunction.NumberFormat(maxLevelProfit);
+                LogHelper.Instance.WriteLog("maxLevelProfit:" + maxLevelProfit + ";", "来客吧分润日志");
+                // decimal diffDiviProfit = 0; //分红级差
+                int curLevel = 0; //当前层级的会员等级
+                for (int curFloor = 1; curFloor <= users.Count; curFloor++)
+                {
+                    ProfitUsers user = new ProfitUsers();
+                    if (curFloor <= users.Count)
+                    {
+                        user = users[curFloor - 1];
+                    }
+                    SubUser selfUser = GetUser(user.UserId, Month) ?? new SubUser();
+                    int UserLevel = selfUser.PreUserLevel > selfUser.UserLevel ? selfUser.PreUserLevel : selfUser.UserLevel; //当前会员等级
+                    //判断当前创客是否有直推的激活机具,并且在活动时间内
+                    if (curLevel == maxLevel)
+                    {
+                        break;
+                    }
+                    if (UserLevel <= maxLevel && UserLevel > curLevel)
+                    {
+                        ProfitObjectLevels objlevel = db.ProfitObjectLevels.FirstOrDefault(m => m.Id == UserLevel); //获取当前等级参数
+                        if (objlevel != null)
+                        {
+                            decimal getLevelProfit = 0;  //等级分润
+                            UserProfitSet profitSet = new UserProfitSet();
+                            decimal profitPercent = IsActive ? objlevel.DebitPercents : objlevel.Percents;
+                            LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + profitPercent + ";", "来客吧分润日志");
+                            if (profitPercent > 0)
+                            {
+                                decimal profitTmp = TotalAmount * profitPercent;
+                                getLevelProfit += profitTmp;
+                                LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + profitTmp + ";", "来客吧分润日志");
+                            }
+                            decimal money = getLevelProfit;
+                            getLevelProfit -= diffLevelProfit;
+                            if (objlevel.LevelDiff == 1)  //判断是否有级差
+                            {
+                                diffLevelProfit = money;
+                            }
+                            if (getLevelProfit >= obj.MinProfitVal)
+                            {
+                                result.Add(new ProfitResult()
+                                {
+                                    UserId = user.UserId,
+                                    UserNav = user.UserNav,
+                                    Money = PublicFunction.NumberFormat(getLevelProfit),
+                                    ProfitRate = profitSet.ProfitRate,
+                                    ProfitPercent = profitSet.ProfitPercent,
+                                    AddOrSubRate = profitSet.AddOrSubRate,
+                                    ProfitRateBase = profitSet.ProfitRateBase,
+                                });
+                                LogHelper.Instance.WriteLog("money:" + UserLevel + ":" + PublicFunction.NumberFormat(getLevelProfit) + ";", "来客吧分润日志");
+                            }
+                        }
+                    }
+                    if(curLevel < UserLevel)
+                    {
+                        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
     }
 }

+ 20 - 0
Util/PublicFunction.cs

@@ -213,5 +213,25 @@ namespace MySystem
             return new JwtSecurityTokenHandler().WriteToken(securityToken);
         }
         #endregion
+    
+    
+        public static decimal NumberFormat(decimal number, int floatCount = 2)
+        {
+            string str = number.ToString();
+            if (str.Contains("."))
+            {
+                string[] list = str.Split('.');
+                if (list[1].Length > floatCount)
+                {
+                    str = list[0] + "." + list[1].Substring(0, floatCount);
+                }
+            }
+            else
+            {
+                str += ".00";
+            }
+            return decimal.Parse(str);
+        }
+    
     }
 }