Exemplo n.º 1
0
        public void Post()
        {
            string Data = DataObj.GetData();

            if (Data.IsNullOrEmpty())
            {
                DataObj.OutError("1000");
                return;
            }
            JObject json = new JObject();

            try
            {
                json = (JObject)JsonConvert.DeserializeObject(Data);
            }
            catch (Exception Ex)
            {
                Log.Write("[FastOrderController]:", "【Data】" + Data, Ex);
                json = null;
            }
            if (json == null)
            {
                DataObj.OutError("1000");
                return;
            }

            FastOrder InFastOrder = new FastOrder();

            InFastOrder = JsonToObject.ConvertJsonToModel(InFastOrder, json);

            UserTrack UserTrack = new UserTrack();

            UserTrack = JsonToObject.ConvertJsonToModel(UserTrack, json);

            if (InFastOrder.Id == 99999)
            {
                DataObj.Msg = "当前通道维护中,建议您开通收款到银行卡功能,享受更多收款通道";
                DataObj.OutError("1000");
                return;
            }

            if (InFastOrder.CashType.IsNullOrEmpty())
            {
                InFastOrder.CashType = "D0";
                //DataObj.OutError("1000");
                //return;
            }
            decimal Amount = InFastOrder.Amoney;
            byte    payway = InFastOrder.OType;

            if (Amount <= 0)
            {
                DataObj.OutError("1000");
                return;
            }
            if (payway != 1 && payway != 2 && payway != 3)
            {
                DataObj.Msg = "你当前版本不支持该交易,请等待新版本发布及升级!";
                DataObj.OutError("1000");
                return;
            }

            Users Users = Entity.Users.FirstOrDefault(n => n.Token == InFastOrder.Token);

            #region 用户
            if (Users == null)//用户令牌不存在
            {
                DataObj.OutError("2004");
                return;
            }
            if (Users.State != 1)//用户被锁定
            {
                DataObj.OutError("2003");
                return;
            }
            if (Users.CardStae != 2)//未实名认证
            {
                DataObj.OutError("2006");
                return;
            }
            if (Amount.IsNullOrEmpty() || payway.IsNullOrEmpty())
            {
                DataObj.OutError("1000");
                return;
            }
            #endregion

            FastUser FastUser = Entity.FastUser.FirstOrDefault(o => o.UId == Users.Id);
            if (FastUser == null)
            {
                DataObj.OutError("2070");
                return;
            }
            #region 取通道
            IList <FastPayWay> FastPayWayList = null;
            if (InFastOrder.Id.IsNullOrEmpty())
            {
                //这是旧逻辑,有一些旧版还在用
                if (payway == 1)
                {
                    FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.SNum2 < Amount && n.ENum2 >= Amount && n.HasAliPay == 1).OrderBy(n => n.Sort).ToList();
                }
                else if (payway == 2)
                {
                    FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.SNum < Amount && n.ENum >= Amount && n.HasWeiXin == 1).OrderBy(n => n.Sort).ToList();
                }
                else if (payway == 3)
                {
                    FastPayWayList = Entity.FastPayWay.Where(n => n.State == 1 && n.BankSNum < Amount && n.BankENum >= Amount && n.HasBank == 1).OrderBy(n => n.Sort).ToList();
                }
                else
                {
                    DataObj.OutError("1000");
                    return;
                }
            }
            else
            {
                var query = Entity.FastPayWay.Where(n => n.Id == InFastOrder.Id && n.State == 1);
                if (payway == 1)
                {
                    query = query.Where(o => o.HasAliPay == 1 && o.SNum2 <= Amount && o.ENum2 >= Amount);
                }
                else if (payway == 2)
                {
                    query = query.Where(o => o.HasWeiXin == 1 && o.SNum <= Amount && o.ENum >= Amount);
                }
                else if (payway == 3)
                {
                    query = query.Where(o => o.HasBank == 1 && o.BankSNum <= Amount && o.BankENum >= Amount);
                }
                FastPayWayList = query.ToList();
            }
            #endregion
            if (FastPayWayList.Count < 1)
            {
                DataObj.OutError("2079");
                return;
            }
            #region 通道验证及商户进件信息验证
            FastPayWay         FastPayWay  = null;
            FastUserPay        FastUserPay = null;
            IList <FastPayWay> PayWayList  = new List <FastPayWay>();
            foreach (var p in FastPayWayList)
            {
                if (p.TimeType == 1)//限制时间,模式1
                {
                    DateTime STime    = p.STime;
                    DateTime ETime    = p.ETime;
                    DateTime NowSTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + STime.ToString("HH:mm:ss"));
                    DateTime NowETime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " " + ETime.ToString("HH:mm:ss"));
                    if (NowSTime <= DateTime.Now && DateTime.Now <= NowETime)
                    {
                        //当前时间允许交易
                        PayWayList.Add(p);
                    }
                }
                else
                {
                    PayWayList.Add(p);
                }
            }
            if (PayWayList.Count < 1)
            {
                DataObj.OutError("2071");
                return;
            }
            foreach (var p in PayWayList)
            {
                FastUserPay temp = Entity.FastUserPay.FirstOrDefault(n => n.UId == Users.Id && n.PayWay == p.Id && n.MerState == 1 && n.CardState == 1 && n.BusiState == 1);
                if (temp != null)
                {
                    FastPayWay  = p;
                    FastUserPay = temp;
                    break;
                }
            }
            if (FastUserPay == null)
            {
                DataObj.OutError("2072");
                return;
            }
            if (FastPayWay == null)
            {
                DataObj.OutError("2073");
                return;
            }
            #endregion
            string[] PayConfigArr = FastPayWay.QueryArray.Split(',');
            #region 验证配置
            if (FastPayWay.DllName == "HFPay")
            {
                if (PayConfigArr.Length != 3)
                {
                    DataObj.OutError("2074");
                    return;
                }
            }
            if (FastPayWay.DllName == "HFJSPay")
            {
                if (PayConfigArr.Length != 3)
                {
                    DataObj.OutError("2074");
                    return;
                }
            }
            #endregion

            decimal UserCost  = 0;//用户
            decimal BankCost  = 0;
            decimal BankMin   = 0;
            decimal BankMax   = 0;
            decimal AgentCost = 0; //代理
            if (payway == 1)
            {                      //支付宝
                UserCost  = FastUserPay.UserCost2;
                BankCost  = FastPayWay.BankCost2;
                BankMin   = FastPayWay.MinCost2;
                BankMax   = FastPayWay.MaxCost2;
                AgentCost = FastPayWay.Cost2;
            }
            if (payway == 2)//微信
            {
                UserCost  = FastUserPay.UserCost;
                BankCost  = FastPayWay.BankCost;
                BankMin   = FastPayWay.MinCost;
                BankMax   = FastPayWay.MaxCost;
                AgentCost = FastPayWay.Cost;
            }
            if (payway == 3)//银联
            {
                UserCost  = FastUserPay.UserCost3;
                BankCost  = FastPayWay.BankCost3;
                BankMin   = FastPayWay.MinCost3;
                BankMax   = FastPayWay.MaxCost3;
                AgentCost = FastPayWay.Cost3;
            }
            #region 创建交易
            //=======================生成订单===========================
            SysAgent         SysAgent     = Entity.SysAgent.FirstOrNew(n => n.Id == Users.Agent);
            IList <SysAgent> SysAgentList = SysAgent.GetAgentsById(Entity);
            SysAgent         TopAgent     = SysAgentList.FirstOrNew();

            FastOrder FastOrder = new FastOrder();
            FastOrder.ComeWay = 1;
            FastOrder.UId     = Users.Id;
            FastOrder.Agent   = SysAgent.Id;

            FastOrder.PayId = string.Empty;

            FastOrder.OType    = payway;
            FastOrder.PayWay   = FastPayWay.Id;
            FastOrder.CashType = InFastOrder.CashType;
            FastOrder.Amoney   = Amount;

            //用户手续费
            decimal Poundage = Amount * UserCost + FastUserPay.UserCash;
            Poundage           = Poundage.Ceiling();
            FastOrder.Poundage = Poundage;
            //用户最终金额
            FastOrder.PayMoney = FastOrder.Amoney - FastOrder.Poundage;

            if (FastOrder.PayMoney < 0)
            {
                DataObj.OutError("2076");
                return;
            }
            FastOrder.UserRate  = UserCost;
            FastOrder.AgentRate = AgentCost;
            FastOrder.SysRate   = BankCost;
            FastOrder.UserCash  = FastUserPay.UserCash;
            FastOrder.SysCash   = FastPayWay.Cash;
            FastOrder.SameGet   = 0;
            //计算手续费差
            SysMoneySet SysMoneySet = Entity.SysMoneySet.FirstOrNew();
            decimal     PaySplit    = 0;//代理商费率
            PaySplit = SysAgent.GetSplit(TopAgent.Tier, Entity);
            decimal PayGet = Amount * PaySplit;
            PayGet = PayGet.Floor();
            //一级代理利润
            decimal AgentPayGet = PayGet;
            AgentPayGet           = AgentPayGet.Floor();
            FastOrder.AgentPayGet = 0;
            string  AgentPath = "|";
            string  Split     = "|";
            decimal MyGet     = PayGet;
            foreach (var p in SysAgentList)
            {
                PaySplit   = SysAgent.GetSplit(p.Tier, Entity);
                AgentPath += p.Id + "|";
                MyGet      = Amount * PaySplit;//各级代理分润
                MyGet      = MyGet.Floor();
                Split     += MyGet.ToString("F2") + "|";
            }
            FastOrder.AgentPath = AgentPath;
            FastOrder.Split     = Split;

            decimal BankMoney = Amount * BankCost;
            if (BankMoney < BankMin)
            {
                BankMoney = BankMin;
            }
            if (BankMoney > BankMax)
            {
                BankMoney = BankMax;
            }
            BankMoney = BankMoney.Floor();
            //用户手续费(含代付手续费)-代付分润-银行手续费-银行代付成本
            decimal HFGet = Poundage - AgentPayGet - BankMoney - FastPayWay.Cash;
            FastOrder.HFGet = HFGet;

            FastOrder.State   = 1;
            FastOrder.AddTime = DateTime.Now;

            FastOrder.PayState   = 0;
            FastOrder.AgentState = 0;
            FastOrder.UserState  = 0;

            FastOrder.CardName = FastUserPay.CardName;
            FastOrder.Bank     = FastUserPay.Bank;
            FastOrder.Card     = FastUserPay.Card;
            FastOrder.Bin      = FastUserPay.Bin;

            Entity.FastOrder.AddObject(FastOrder);
            Entity.SaveChanges();
            Entity.Refresh(RefreshMode.StoreWins, FastOrder);
            #endregion

            //=======================================
            UserTrack.ENo      = DataObj.ENo;
            UserTrack.OPType   = "到银行卡交易";
            UserTrack.UserName = FastOrder.TNum;
            UserTrack.UId      = FastOrder.UId;
            UserTrack.SeavGPSLog(Entity);
            //=======================================

            //=======================分润记录===========================
            MyGet = PayGet;
            int tier = 1;
            foreach (var p in SysAgentList)
            {
                PaySplit = SysAgent.GetSplit(p.Tier, Entity);
                MyGet    = Amount * PaySplit;//各级代理分润
                MyGet    = MyGet.Floor();
                FastSplit FastSplit = new FastSplit();
                FastSplit.Tnum    = FastOrder.TNum;
                FastSplit.Profit  = MyGet;
                FastSplit.AgentId = p.Id;
                FastSplit.Tier    = p.Tier;
                FastSplit.AddTime = DateTime.Now;
                Entity.FastSplit.AddObject(FastSplit);
                tier++;
            }
            Entity.SaveChanges();
            //=======================请求接口开始===========================
            if (FastOrder.OType == 1 || FastOrder.OType == 2)
            {
                if (FastPayWay.DllName == "HFPay")
                {
                    string NoticeUrl = NoticePath + "/PayCenter/HFPay/FastNotice.html";//后台通过地址
                    #region 微信&支付宝
                    string Action = "";
                    if (FastOrder.OType == 1)
                    {
                        Action = "AliSao";
                    }
                    else if (FastOrder.OType == 2)
                    {
                        Action = "WxSao";
                    }
                    //提交结算中心
                    string merId  = PayConfigArr[0]; //商户号
                    string merKey = PayConfigArr[1]; //商户密钥
                    string PayWay = PayConfigArr[2]; //绑定通道

                    decimal money      = FastOrder.Amoney * 100;
                    string  OrderMoney = money.ToString("F0");//金额,以分为单

                    string PostJson = "{\"action\":\"" + Action + "\",\"txnamt\":\"" + OrderMoney + "\",\"merid\":\"" + merId + "\",\"payway\":\"" + PayWay + "\",\"orderid\":\"" + FastOrder.TNum + "\",\"backurl\":\"" + NoticeUrl + "\"}";

                    string DataBase64 = LokFuEncode.Base64Encode(PostJson, "utf-8");
                    string Sign       = (DataBase64 + merKey).GetMD5();

                    DataBase64 = HttpUtility.UrlEncode(DataBase64);
                    string PostData = string.Format("req={0}&sign={1}", DataBase64, Sign);

                    string HF_Url = "https://api.zhifujiekou.com/api/mpgateway";

                    string Ret = Utils.PostRequest(HF_Url, PostData, "utf-8");

                    JObject JS = new JObject();
                    try
                    {
                        JS = (JObject)JsonConvert.DeserializeObject(Ret);
                    }
                    catch (Exception)
                    {
                        FastOrder.State  = 0;
                        FastOrder.Remark = "数据请求出错";
                        Entity.SaveChanges();
                        JS = null;
                    }
                    if (JS != null)
                    {
                        string resp = JS["resp"].ToString();
                        Ret = LokFuEncode.Base64Decode(resp, "utf-8");
                        try
                        {
                            JS = (JObject)JsonConvert.DeserializeObject(Ret);
                        }
                        catch (Exception Ex)
                        {
                            FastOrder.State  = 0;
                            FastOrder.Remark = "JSON加载出错";
                            Entity.SaveChanges();
                            JS = null;
                        }
                        if (JS != null)
                        {
                            string respcode = JS["respcode"].ToString();
                            if (respcode != "00")
                            {
                                string respmsg = JS["respmsg"].ToString();
                                FastOrder.State  = 0;
                                FastOrder.Remark = respmsg;
                                Entity.SaveChanges();
                            }
                            else
                            {
                                if (JS["formaction"] == null)
                                {
                                    FastOrder.State  = 0;
                                    FastOrder.Remark = "接口没有返回二维码";
                                    Entity.SaveChanges();
                                }
                                else
                                {
                                    string BankNum = JS["queryid"].ToString();
                                    string qr_code = JS["formaction"].ToString();
                                    FastOrder.PayId = qr_code;
                                    FastOrder.Trade = BankNum;
                                    Entity.SaveChanges();
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
            else if (FastOrder.OType == 3)
            {
                if (FastPayWay.DllName == "HFPay" || FastPayWay.DllName == "HFJSPay")
                {
                    #region
                    //银联不用请求第三方,直接生成链接
                    FastOrder.PayId = PayPath + "/paycenter/pay-" + FastOrder.Id + ".html?sign=" + ((FastOrder.Id * 100 + 99) + "Pay").GetMD5().Substring(8, 8);
                    Entity.SaveChanges();
                    #endregion
                }
            }
            if (FastOrder.State == 1)
            {
                if (FastOrder.PayState == 1)
                {
                    if (FastOrder.UserState == 1)
                    {
                        FastOrder.State = 3;
                    }
                    else
                    {
                        FastOrder.State = 2;
                    }
                }
                else
                {
                    FastOrder.State = 1;
                }
            }
            else
            {
                FastOrder.State = 0;
                DataObj.OutError("1005");
                return;
            }
            FastOrder.Cols = "TNum,PayId,Amoney,Poundage,State";

            DataObj.Data = FastOrder.OutJson();
            DataObj.Code = "0000";
            DataObj.OutString();
        }
        /// <summary>
        /// 订单分润/分润退款
        /// </summary>
        /// <param name="O"></param>
        /// <param name="Entity"></param>
        /// <param name="Type">1分润入帐 2分润退款</param>
        /// <returns></returns>
        public static FastOrder PayAgent(this FastOrder O, LokFuEntity Entity, int Type)
        {
            FastConfig FastConfig = Entity.FastConfig.FirstOrNew();

            if (FastConfig.AgentWay != 1)
            {
                return(O);
            }
            if (O.Agent.IsNullOrEmpty())
            {//代理商没有情况下
                return(O);
            }
            //if (O.AgentPayGet.IsNullOrEmpty())
            //{
            //    //没有佣金
            //    //直接标识为已结算
            //    O.AgentState = 1;
            //    O.AgentTime = DateTime.Now;
            //    Entity.SaveChanges();
            //    return O;
            //}
            if (Type != 1)//类型不对
            {
                return(O);
            }
            Users OrderUser = Entity.Users.FirstOrDefault(n => n.Id == O.UId);//读取订单用户

            if (Type == 1)
            {
                string TypeString = "收付直通车";
                #region 结算
                if (O.AgentState != 0)
                {//已结算,不能重复结算
                    return(O);
                }
                O.AgentState = 1;
                O.AgentTime  = DateTime.Now;
                Entity.SaveChanges();
                //获取各级代理商
                SysAgent SysAgent = new SysAgent();
                SysAgent.Id = O.Agent;
                IList <SysAgent> SysAgentList = SysAgent.GetAgentsById(Entity);
                decimal          AIdPayGet    = (decimal)O.AgentPayGet; //
                decimal          sumpayget    = 0;
                int tier = 1;
                foreach (var p in SysAgentList)
                {
                    if (p.State == 1)
                    {
                        decimal PayGet = SysAgent.GetSplit(p.Tier, Entity);
                        AIdPayGet = O.Amoney * PayGet; //当前级总佣金,需获取是否有下级拆分他的佣金
                        AIdPayGet = AIdPayGet.Floor();
                        decimal AIdPayGetNext = 0;     //定义下一级佣金
                        if (tier < SysAgentList.Count)
                        {
                            //不是最后一级,需计算下级拆分金额
                            SysAgent SysAgentNext = SysAgentList.Skip(tier).Take(1).FirstOrDefault();
                            if (SysAgentNext != null)
                            {
                                if (SysAgentNext.State == 1)
                                {//下级状态有效时才计算,如关闭了则不正计算下级
                                    decimal PayGetNext = SysAgent.GetSplit(SysAgentNext.Tier, Entity);
                                    AIdPayGetNext = O.Amoney * PayGetNext;
                                    AIdPayGetNext = AIdPayGetNext.Floor();
                                }
                            }
                        }
                        decimal AIdPayGetMy = AIdPayGet - AIdPayGetNext; //当前级所能得到真实佣金

                        decimal UsersGetAll = 0;                         //定义所有用户佣金
                        //最后一级代理商 处理用户分润
                        #region 最后一级代理商 处理用户分润
                        if (tier == SysAgentList.Count)
                        {
                            //获取各级分润配置
                            SysSet SysSet   = Entity.SysSet.FirstOrNew();
                            int    MaxLevel = SysSet.GlobaPromoteMaxLevel;
                            //有用户分润,开始执行用户分润
                            Users Users = new Users();
                            Users.Id = O.UId;
                            //获取用户各级关系,最大级不超过用户配置级数。返回数据包含当前用户,当前用户级数标识Tier为0
                            IList <Users> UsersList = Users.GetUsersById(Entity, MaxLevel);
                            int           UsersTier = 1;
                            foreach (var U in UsersList.Where(n => n.Tier > 0 && n.State == 1))
                            {
                                //UsersGetAll
                                //UserPromoteGet UserPromoteGet = UserPromoteGetList.FirstOrDefault(n => n.PromoteLevel == U.Tier);
                                //if (UserPromoteGet != null)
                                //{
                                decimal PromoteGet = Users.GetUsersSplit(Entity, U.Tier);
                                decimal UsersGet   = O.Amoney * PromoteGet;
                                UsersGet = UsersGet.Floor();
                                if (UsersTier == 1)
                                {
                                    UsersGetAll = UsersGet;
                                }
                                //UsersGetSum += UsersGet;

                                if (UsersTier < UsersList.Count)
                                {
                                    //不是最后一级,需计算下级拆分金额
                                    byte  nexttier  = (byte)(U.Tier + 1);
                                    Users UsersNext = UsersList.FirstOrDefault(o => o.Tier == nexttier);
                                    if (UsersNext != null)
                                    {
                                        if (UsersNext.State == 1)
                                        {    //下级状态有效时才计算,如关闭了则不正计算下级
                                            decimal PayGetNext     = Users.GetUsersSplit(Entity, UsersNext.Tier);
                                            decimal UserPayGetNext = O.Amoney * PayGetNext;
                                            UserPayGetNext = UserPayGetNext.Floor();
                                            //减掉下级用户的金额
                                            UsersGet = UsersGet - UserPayGetNext;
                                        }
                                    }
                                }
                                if (UsersGet > 0)
                                {
                                    //帐户变动记录
                                    string Remark = string.Format("{0}[{1}]", TypeString, O.TNum);
                                    string SP_Ret = Entity.SP_UsersMoney(U.Id, O.TNum, UsersGet, 8, Remark, 0);
                                    if (SP_Ret != "3")
                                    {
                                        Utils.WriteLog(string.Format("U{0},O{1},T{2}:{3}【{4}】", U.Id, O.TNum, 8, UsersGet, SP_Ret), "SP_UsersMoney");
                                    }
                                    //记录清分记录
                                    OrderProfitLog OPL = new OrderProfitLog();
                                    OPL.AddTime   = DateTime.Now;
                                    OPL.UId       = U.Id;
                                    OPL.Agent     = 0;
                                    OPL.TNum      = O.TNum;
                                    OPL.LogType   = 1;
                                    OPL.Tier      = U.Tier;
                                    OPL.Profit    = UsersGet;
                                    OPL.Amoney    = O.Amoney;
                                    OPL.OrderType = 21;
                                    OPL.UserName  = OrderUser.UserName;
                                    Entity.OrderProfitLog.AddObject(OPL);
                                    //=====增加统计记录=====
                                    ShareTotal ShareTotal = Entity.ShareTotal.FirstOrDefault(n => n.UId == U.Id && n.Tier == U.Tier);
                                    if (ShareTotal == null)
                                    {
                                        ShareTotal          = new ShareTotal();
                                        ShareTotal.UId      = U.Id;
                                        ShareTotal.AddTime  = DateTime.Now;
                                        ShareTotal.ShareNum = 0;
                                        ShareTotal.Amount   = O.Amoney;
                                        ShareTotal.Profit   = UsersGet;
                                        ShareTotal.Tier     = U.Tier;
                                        Entity.ShareTotal.AddObject(ShareTotal);
                                    }
                                    else
                                    {
                                        ShareTotal.Amount += O.Amoney;
                                        ShareTotal.Profit += UsersGet;
                                    }
                                    sumpayget = sumpayget + UsersGet;
                                }
                                UsersTier++;
                                // }
                            }
                        }
                        #endregion
                        //20160704 Lin 增加统计用户拆分多少,最后一级代理金额减掉这部分金额即可。
                        //解决用户分剩下钱规系统逻辑问题
                        AIdPayGetMy = AIdPayGetMy - UsersGetAll;

                        #region 最后一级代理商 处理同级分润
                        if (tier == SysAgentList.Count)
                        {
                            if (!p.SameAgent.IsNullOrEmpty() && AIdPayGet > 0)
                            {
                                SysMoneySet SysMoneySet = Entity.SysMoneySet.FirstOrNew();
                                decimal     SameMoney   = AIdPayGet * SysMoneySet.SameAgent;
                                SameMoney = SameMoney.Floor();
                                SysAgent SameSysAgent = Entity.SysAgent.FirstOrNew(o => o.Id == p.SameAgent);
                                if (SameSysAgent.State == 1 && !SameSysAgent.MyUId.IsNullOrEmpty())
                                {
                                    //帐户变动记录
                                    string Remark = string.Format("{0}[{1}]", "直通车同级分润", O.TNum);
                                    string SP_Ret = Entity.SP_UsersMoney(SameSysAgent.MyUId, O.TNum, SameMoney, 8, Remark, 0);
                                    if (SP_Ret != "3")
                                    {
                                        Utils.WriteLog(string.Format("U{0},O{1},T{2}:{3}【{4}】", SameSysAgent.MyUId, O.TNum, 8, SameMoney, SP_Ret), "SP_UsersMoney");
                                    }
                                    //记录清分记录
                                    OrderProfitLog OPL = new OrderProfitLog();
                                    OPL.AddTime   = DateTime.Now;
                                    OPL.UId       = SameSysAgent.MyUId;
                                    OPL.Agent     = SameSysAgent.Id;
                                    OPL.TNum      = O.TNum;
                                    OPL.LogType   = 3;
                                    OPL.Tier      = p.Tier;
                                    OPL.Profit    = SameMoney;
                                    OPL.Amoney    = O.Amoney;
                                    OPL.OrderType = 21;
                                    OPL.UserName  = OrderUser.UserName;
                                    Entity.OrderProfitLog.AddObject(OPL);
                                    O.SameGet = SameMoney;
                                }
                            }
                        }
                        #endregion

                        if (!p.MyUId.IsNullOrEmpty() && AIdPayGetMy > 0)//某一级未绑定钱包,钱留给系统^-^
                        {
                            //获取钱包信息
                            //为了减少系统开销,这里不读取用户信息——By Lin
                            //Users Users = Entity.Users.FirstOrDefault(n => n.Id == p.MyUId);
                            if (p.MyUId > 0)
                            {
                                //帐户变动记录
                                string Remark = string.Format("{0}[{1}]", TypeString, O.TNum);
                                string SP_Ret = Entity.SP_UsersMoney(p.MyUId, O.TNum, AIdPayGetMy, 8, Remark, 0);
                                if (SP_Ret != "3")
                                {
                                    Utils.WriteLog(string.Format("U{0},O{1},T{2}:{3}【{4}】", p.MyUId, O.TNum, 8, AIdPayGetMy, SP_Ret), "SP_UsersMoney");
                                }
                                //记录清分记录
                                OrderProfitLog OPL = new OrderProfitLog();
                                OPL.AddTime   = DateTime.Now;
                                OPL.UId       = p.MyUId;
                                OPL.Agent     = p.Id;
                                OPL.TNum      = O.TNum;
                                OPL.LogType   = 2;
                                OPL.Tier      = p.Tier;
                                OPL.Profit    = AIdPayGetMy;
                                OPL.Amoney    = O.Amoney;
                                OPL.OrderType = 21;
                                OPL.UserName  = OrderUser.UserName;
                                Entity.OrderProfitLog.AddObject(OPL);
                                sumpayget = sumpayget + AIdPayGetMy;
                            }
                        }
                    }
                    tier++;
                }

                Entity.SaveChanges();
                //统计所有分润
                //decimal Profit = 0;
                //if (Entity.OrderProfitLog.Count(n => n.TNum == O.TNum) > 0)
                //{
                //    Profit = Entity.OrderProfitLog.Where(n => n.TNum == O.TNum).Sum(n => n.Profit);//是否会为空的情况
                //}
                O.AgentPayGet = sumpayget;//记录总佣金支出,以便总系统计算利润
                O.AgentState  = 1;
                Entity.SaveChanges();
                #endregion
            }
            if (Type == 2)
            {
                //没有退款,分润也不需要写
            }
            return(O);
        }