Exemplo n.º 1
0
        protected override GetSuperRetailTraderProfitConfigRD ProcessRequest(DTO.Base.APIRequest <GetSuperRetailTraderProfitConfigRP> pRequest)
        {
            var parameter          = pRequest.Parameters;
            var loggingSessionInfo = new SessionManager().CurrentUserLoginInfo; //登录状态信息
            var rd = new GetSuperRetailTraderProfitConfigRD();
            T_SuperRetailTraderProfitConfigBLL           bll = new T_SuperRetailTraderProfitConfigBLL(loggingSessionInfo);
            T_SuperRetailTraderConfigBLL                 SuperRetailTraderConfigService = new T_SuperRetailTraderConfigBLL(loggingSessionInfo);
            List <T_SuperRetailTraderProfitConfigEntity> _model = bll.QueryByEntity(new T_SuperRetailTraderProfitConfigEntity()
            {
                IsDelete = 0, CustomerId = loggingSessionInfo.ClientID, Status = "10"
            }, null).ToList();                                                                                                                                                                                            //获取最新

            if (_model.Count == 2)
            {
                foreach (var item in _model)
                {
                    if (item.Level == 1)
                    {
                        continue;
                    }
                    item.Level = 4;
                }
                _model.Add(new T_SuperRetailTraderProfitConfigEntity()
                {
                    Level = 2, Status = "90", SuperRetailTraderProfitConfigId = null, Profit = 0
                });
                _model.Add(new T_SuperRetailTraderProfitConfigEntity()
                {
                    Level = 3, Status = "90", SuperRetailTraderProfitConfigId = null, Profit = 0
                });
            }
            else if (_model.Count == 3)
            {
                foreach (var item in _model)
                {
                    if (item.Level == 1)
                    {
                        continue;
                    }
                    item.Level = item.Level + 1;;
                }
                _model.Add(new T_SuperRetailTraderProfitConfigEntity()
                {
                    Level = 2, Status = "90", SuperRetailTraderProfitConfigId = null, Profit = 0
                });
            }

            //获取level==1 的数据

            var first = SuperRetailTraderConfigService.QueryByEntity(new T_SuperRetailTraderConfigEntity()
            {
                CustomerId = loggingSessionInfo.ClientID, IsDelete = 0
            }, null).FirstOrDefault();

            if (first == null)
            {
                throw new APIException("系统繁忙,请稍候重试。")
                      {
                          ErrorCode = 135
                      };
            }
            if (_model.Where(m => m.Level == 1).Count() == 0)
            {
                _model.Add(new T_SuperRetailTraderProfitConfigEntity()
                {
                    Level = 1, Status = "10", SuperRetailTraderProfitConfigId = null, Profit = first.SkuCommission
                });
            }
            else
            {
                _model.Where(m => m.Level == 1).FirstOrDefault().Profit = first.SkuCommission;
            }
            rd.ProfitConfigList = _model.Select(m => new GetSuperRetailTraderProfitConfigInfoRD()
            {
                Level = m.Level, Profit = m.Profit, Status = m.Status, SuperRetailTraderProfitConfigId = m.SuperRetailTraderProfitConfigId
            }).ToList();
            return(rd);
        }
        /// <summary>
        /// 计算超级分销商佣金,分润用到的批处理
        /// </summary>
        public void CalculateSuperRetailTraderOrderJob()
        {
            var numCount = 50;

            var customerIDs = CustomerBLL.Instance.GetCustomerList();

            foreach (var customer in customerIDs)
            {
                string connString = customer.Value;
                var    count      = RedisOpenAPI.Instance.CCSuperRetailTraderOrder().GetSuperRetailTraderOrderLength(new CC_Order
                {
                    CustomerID = customer.Key
                });
                if (count.Code != ResponseCode.Success)
                {
                    BaseService.WriteLog("从redis获取待绑定优惠券数量失败");
                    continue;
                }
                if (count.Result <= 0)
                {
                    continue;
                }
                if (count.Result < numCount)
                {
                    numCount = Convert.ToInt32(count.Result);
                }

                DataTable dtProfitDetail = CreateTableProfitDetail();
                DataTable dtAmountDetail = CreateTableAmountDetail();
                DataTable dtAmount       = CreateTableAmount();

                for (var i = 0; i < numCount; i++)
                {
                    var response = RedisOpenAPI.Instance.CCSuperRetailTraderOrder().GetSuperRetailTraderOrder(new CC_Order
                    {
                        CustomerID = customer.Key
                    });
                    if (response.Code == ResponseCode.Success)
                    {
                        var orderInfo = response.Result.OrderInfo.JsonDeserialize <T_InoutEntity>();
                        //var loggingSessionInfo = response.Result.LogSession.JsonDeserialize<LoggingSessionInfo>();
                        var        loggingSessionInfo = CustomerBLL.Instance.GetBSLoggingSession(customer.Key, "1");
                        T_InoutBLL inoutBLL           = new T_InoutBLL(loggingSessionInfo);
                        //inoutBLL.CalculateSuperRetailTraderOrder(loggingSessionInfo, orderInfo);
                        if (orderInfo != null)
                        {
                            if (orderInfo.data_from_id == "35" || orderInfo.data_from_id == "36")
                            {
                                T_SuperRetailTraderBLL bllSuperRetailTrader = new T_SuperRetailTraderBLL(loggingSessionInfo);
                                DataSet dsAllFather = bllSuperRetailTrader.GetAllFather(orderInfo.sales_user);
                                if (dsAllFather != null && dsAllFather.Tables.Count > 0 && dsAllFather.Tables[0].Rows.Count > 0)
                                {
                                    T_SuperRetailTraderProfitConfigBLL bllSuperRetailTraderProfitConfig = new T_SuperRetailTraderProfitConfigBLL(loggingSessionInfo);
                                    T_SuperRetailTraderConfigBLL       bllSuperRetailTraderConfig       = new T_SuperRetailTraderConfigBLL(loggingSessionInfo);
                                    T_SuperRetailTraderProfitDetailBLL bllSuperRetailTraderProfitDetail = new T_SuperRetailTraderProfitDetailBLL(loggingSessionInfo);

                                    VipAmountBLL       bllVipAmount       = new VipAmountBLL(loggingSessionInfo);
                                    VipAmountDetailBLL bllVipAmountDetail = new VipAmountDetailBLL(loggingSessionInfo);

                                    var entitySuperRetailTraderProfitConfig = bllSuperRetailTraderProfitConfig.QueryByEntity(new T_SuperRetailTraderProfitConfigEntity()
                                    {
                                        CustomerId = loggingSessionInfo.ClientID, IsDelete = 0, Status = "10"
                                    }, null);
                                    var entityConfig = bllSuperRetailTraderConfig.QueryByEntity(new T_SuperRetailTraderConfigEntity()
                                    {
                                        CustomerId = loggingSessionInfo.ClientID, IsDelete = 0
                                    }, null).SingleOrDefault();
                                    if (entityConfig != null && entitySuperRetailTraderProfitConfig != null)
                                    {
                                        //佣金比列
                                        decimal SkuCommission = Convert.ToDecimal(entityConfig.SkuCommission) * Convert.ToDecimal(0.01);
                                        //商品分润比列
                                        decimal DistributionProfit = Convert.ToDecimal(entityConfig.DistributionProfit) * Convert.ToDecimal(0.01);


                                        foreach (DataRow dr in dsAllFather.Tables[0].Rows)
                                        {
                                            decimal amount            = 0;
                                            string  strAmountSourceId = string.Empty;
                                            T_SuperRetailTraderProfitConfigEntity singlProfitConfig = new T_SuperRetailTraderProfitConfigEntity();
                                            if (dr["level"].ToString() == "1")      //佣金
                                            {
                                                strAmountSourceId = "34";
                                                singlProfitConfig = entitySuperRetailTraderProfitConfig.Where(a => a.Level == Convert.ToInt16(dr["level"].ToString())).SingleOrDefault();
                                                if (singlProfitConfig != null)
                                                {
                                                    if (singlProfitConfig.ProfitType == "Percent")
                                                    {
                                                        amount = Convert.ToDecimal(orderInfo.actual_amount) * SkuCommission;
                                                    }
                                                }
                                            }
                                            else      //分润
                                            {
                                                strAmountSourceId = "33";
                                                singlProfitConfig = entitySuperRetailTraderProfitConfig.Where(a => a.Level == Convert.ToInt16(dr["level"].ToString())).SingleOrDefault();
                                                if (singlProfitConfig != null)
                                                {
                                                    if (singlProfitConfig.ProfitType == "Percent")
                                                    {
                                                        //amount = Convert.ToDecimal(orderInfo.actual_amount) * DistributionProfit * Convert.ToDecimal(singlProfitConfig.Profit) * Convert.ToDecimal(0.01);
                                                        amount = Convert.ToDecimal(orderInfo.actual_amount) * Convert.ToDecimal(singlProfitConfig.Profit) * Convert.ToDecimal(0.01);
                                                    }
                                                }
                                            }
                                            var vipAmountDetail = bllVipAmountDetail.QueryByEntity(new VipAmountDetailEntity()
                                            {
                                                ObjectId = orderInfo.order_id, AmountSourceId = strAmountSourceId
                                            }, null);
                                            if (vipAmountDetail != null && vipAmountDetail.Length > 0)
                                            {
                                                continue;
                                            }
                                            if (amount > 0)
                                            {
                                                IDbTransaction tran = new JIT.CPOS.BS.DataAccess.Base.TransactionHelper(loggingSessionInfo).CreateTransaction();
                                                try
                                                {
                                                    T_SuperRetailTraderProfitDetailEntity entitySuperRetailTraderProfitDetail = new T_SuperRetailTraderProfitDetailEntity()
                                                    {
                                                        SuperRetailTraderProfitConfigId = singlProfitConfig.SuperRetailTraderProfitConfigId,
                                                        SuperRetailTraderID             = new Guid(dr["SuperRetailTraderID"].ToString()),
                                                        Level             = Convert.ToInt16(dr["level"].ToString()),
                                                        ProfitType        = "Cash",
                                                        Profit            = amount,
                                                        OrderType         = "Order",
                                                        OrderId           = orderInfo.order_id,
                                                        OrderDate         = Convert.ToDateTime(orderInfo.order_date),
                                                        VipId             = orderInfo.vip_no,
                                                        OrderActualAmount = orderInfo.actual_amount,
                                                        SalesId           = new Guid(orderInfo.sales_user),
                                                        OrderNo           = orderInfo.order_no,
                                                        CustomerId        = loggingSessionInfo.ClientID
                                                    };
                                                    bllSuperRetailTraderProfitDetail.Create(entitySuperRetailTraderProfitDetail, (SqlTransaction)tran);

                                                    VipAmountEntity       entityVipAmount       = new VipAmountEntity();
                                                    VipAmountDetailEntity entityVipAmountDetail = new VipAmountDetailEntity();
                                                    entityVipAmountDetail = new VipAmountDetailEntity {
                                                        VipAmountDetailId = Guid.NewGuid(),
                                                        VipId             = dr["SuperRetailTraderID"].ToString(),
                                                        Amount            = amount,
                                                        UsedReturnAmount  = 0,
                                                        EffectiveDate     = DateTime.Now,
                                                        DeadlineDate      = Convert.ToDateTime("9999-12-31 23:59:59"),
                                                        AmountSourceId    = strAmountSourceId,
                                                        ObjectId          = orderInfo.order_id,
                                                        CustomerID        = loggingSessionInfo.ClientID,
                                                        Reason            = "超级分销商",
                                                        IsWithdrawCash    = 0
                                                    };
                                                    bllVipAmountDetail.Create(entityVipAmountDetail, (SqlTransaction)tran);
                                                    //DataRow dr_ProfitDetail = dtProfitDetail.NewRow();
                                                    //dr_ProfitDetail["Id"] = Guid.NewGuid();
                                                    //dr_ProfitDetail["SuperRetailTraderProfitConfigId"] = new Guid(singlProfitConfig.SuperRetailTraderProfitConfigId.ToString());
                                                    //dr_ProfitDetail["SuperRetailTraderID"] = new Guid(dr["SuperRetailTraderID"].ToString());
                                                    //dr_ProfitDetail["Level"] = Convert.ToInt16(dr["level"].ToString());
                                                    //dr_ProfitDetail["ProfitType"] = "Cash";
                                                    //dr_ProfitDetail["Profit"] = amount;
                                                    //dr_ProfitDetail["OrderType"] = "Order";
                                                    //dr_ProfitDetail["OrderId"] = orderInfo.order_id;
                                                    //dr_ProfitDetail["OrderNo"] = orderInfo.order_no;
                                                    //dr_ProfitDetail["OrderDate"] = Convert.ToDateTime(orderInfo.order_date);
                                                    //dr_ProfitDetail["OrderActualAmount"] = orderInfo.actual_amount;
                                                    //dr_ProfitDetail["SalesId"] = new Guid(orderInfo.sales_user);
                                                    //dr_ProfitDetail["VipId"] = orderInfo.vip_no;
                                                    //dr_ProfitDetail["CreateBy"] = loggingSessionInfo.ClientID;
                                                    //dr_ProfitDetail["CreateTime"] = DateTime.Now;
                                                    //dr_ProfitDetail["LastUpdateBy"] = loggingSessionInfo.ClientID;
                                                    //dr_ProfitDetail["LastUpdateTime"] = DateTime.Now;
                                                    //dr_ProfitDetail["CustomerId"] = loggingSessionInfo.ClientID;
                                                    //dr_ProfitDetail["IsDelete"] = 0;

                                                    //dtProfitDetail.Rows.Add(dr_ProfitDetail);

                                                    //DataRow dr_AmountDetail = dtAmountDetail.NewRow();
                                                    //dr_AmountDetail["VipAmountDetailId"] = Guid.NewGuid();
                                                    //dr_AmountDetail["VipId"] = dr["SuperRetailTraderID"].ToString();
                                                    //dr_AmountDetail["VipCardCode"] = "";
                                                    //dr_AmountDetail["UnitID"] = "";
                                                    //dr_AmountDetail["UnitName"] = "";
                                                    //dr_AmountDetail["SalesAmount"] = 0;
                                                    //dr_AmountDetail["Amount"] = amount;
                                                    //dr_AmountDetail["UsedReturnAmount"] = 0;
                                                    //dr_AmountDetail["Reason"] = "超级分销商";
                                                    //dr_AmountDetail["EffectiveDate"] = DateTime.Now;
                                                    //dr_AmountDetail["DeadlineDate"] = Convert.ToDateTime("9999-12-31 23:59:59");
                                                    //dr_AmountDetail["AmountSourceId"] = strAmountSourceId;
                                                    //dr_AmountDetail["ObjectId"] = orderInfo.order_id;
                                                    //dr_AmountDetail["Remark"] = "超级分销商";
                                                    //dr_AmountDetail["IsValid"] = 0;
                                                    //dr_AmountDetail["IsWithdrawCash"] = 0;
                                                    //dr_AmountDetail["CustomerID"] = loggingSessionInfo.ClientID;
                                                    //dr_AmountDetail["CreateTime"] = DateTime.Now;
                                                    //dr_AmountDetail["CreateBy"] = loggingSessionInfo.ClientID;
                                                    //dr_AmountDetail["LastUpdateBy"] = loggingSessionInfo.ClientID;
                                                    //dr_AmountDetail["LastUpdateTime"] = DateTime.Now;
                                                    //dr_AmountDetail["IsDelete"] = 0;
                                                    ////dr_AmountDetail["IsCalculated"] = 1;

                                                    //dtAmountDetail.Rows.Add(dr_AmountDetail);

                                                    entityVipAmount = bllVipAmount.QueryByEntity(new VipAmountEntity()
                                                    {
                                                        VipId = dr["SuperRetailTraderID"].ToString(), IsDelete = 0
                                                    }, null).SingleOrDefault();
                                                    if (entityVipAmount == null)
                                                    {
                                                        entityVipAmount = new VipAmountEntity {
                                                            VipId                  = dr["SuperRetailTraderID"].ToString(),
                                                            BeginAmount            = 0,
                                                            InAmount               = amount,
                                                            OutAmount              = 0,
                                                            EndAmount              = amount,
                                                            TotalAmount            = amount,
                                                            BeginReturnAmount      = 0,
                                                            InReturnAmount         = 0,
                                                            OutReturnAmount        = 0,
                                                            ReturnAmount           = 0,
                                                            ImminentInvalidRAmount = 0,
                                                            InvalidReturnAmount    = 0,
                                                            ValidReturnAmount      = 0,
                                                            TotalReturnAmount      = 0,
                                                            IsLocking              = 0,
                                                            CustomerID             = loggingSessionInfo.ClientID,
                                                            VipCardCode            = ""
                                                        };
                                                        bllVipAmount.Create(entityVipAmount, tran);
                                                        //DataRow dr_Amount = dtAmount.NewRow();
                                                        //dr_Amount["VipId"] = dr["SuperRetailTraderID"].ToString();
                                                        //dr_Amount["VipCardCode"] = "";
                                                        //dr_Amount["BeginAmount"] = 0;
                                                        //dr_Amount["InAmount"] = amount;
                                                        //dr_Amount["OutAmount"] = 0;
                                                        //dr_Amount["EndAmount"] = amount;
                                                        //dr_Amount["TotalAmount"] = amount;
                                                        //dr_Amount["BeginReturnAmount"] = 0;
                                                        //dr_Amount["InReturnAmount"] = 0;
                                                        //dr_Amount["OutReturnAmount"] = 0;
                                                        //dr_Amount["ReturnAmount"] = 0;
                                                        //dr_Amount["ImminentInvalidRAmount"] = 0;
                                                        //dr_Amount["InvalidReturnAmount"] = 0;
                                                        //dr_Amount["ValidReturnAmount"] = 0;
                                                        //dr_Amount["TotalReturnAmount"] = 0;
                                                        //dr_Amount["PayPassword"] = "";
                                                        //dr_Amount["IsLocking"] = 0;
                                                        //dr_Amount["CustomerID"] = loggingSessionInfo.ClientID;
                                                        //dr_Amount["CreateTime"] = DateTime.Now;
                                                        //dr_Amount["CreateBy"] = loggingSessionInfo.ClientID;
                                                        //dr_Amount["LastUpdateBy"] =loggingSessionInfo.ClientID;
                                                        //dr_Amount["LastUpdateTime"] = DateTime.Now;
                                                        //dr_Amount["IsDelete"] = 0;
                                                        //dtAmount.Rows.Add(dr_Amount);
                                                    }
                                                    else
                                                    {
                                                        entityVipAmount.InReturnAmount    = (entityVipAmount.InReturnAmount == null ? 0 : entityVipAmount.InReturnAmount.Value) + amount;
                                                        entityVipAmount.TotalReturnAmount = (entityVipAmount.TotalReturnAmount == null ? 0 : entityVipAmount.TotalReturnAmount.Value) + amount;

                                                        entityVipAmount.ValidReturnAmount = (entityVipAmount.ValidReturnAmount == null ? 0 : entityVipAmount.ValidReturnAmount.Value) + amount;
                                                        entityVipAmount.ReturnAmount      = (entityVipAmount.ReturnAmount == null ? 0 : entityVipAmount.ReturnAmount.Value) + amount;

                                                        bllVipAmount.Update(entityVipAmount);
                                                    }
                                                    tran.Commit();
                                                }
                                                catch (Exception)
                                                {
                                                    tran.Rollback();
                                                    throw;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //if (dtAmount.Rows.Count > 0)
                //{
                //    Utils.SqlBulkCopy(connString, dtAmount, "VipAmount");
                //}
                //if (dtAmountDetail.Rows.Count>0 && dtProfitDetail.Rows.Count>0)
                //{
                //    Utils.SqlBulkCopy(connString, dtProfitDetail, "T_SuperRetailTraderProfitDetail");
                //    Utils.SqlBulkCopy(connString, dtAmountDetail, "VipAmountDetail");

                //}
            }
        }
Exemplo n.º 3
0
        protected override SetTSuperRetailTraderConfigRD ProcessRequest(DTO.Base.APIRequest <SetTSuperRetailTraderConfigRP> pRequest)
        {
            var parameter          = pRequest.Parameters;
            var loggingSessionInfo = new SessionManager().CurrentUserLoginInfo; //登录状态信息
            var rd = new SetTSuperRetailTraderConfigRD();
            T_SuperRetailTraderConfigBLL bll = new T_SuperRetailTraderConfigBLL(loggingSessionInfo);

            #region 修改协议
            if (parameter.IsFlag == true)
            {
                var oldmodel = bll.GetByID(parameter.Id);
                oldmodel.Agreement     = parameter.Agreement;
                oldmodel.AgreementName = parameter.AgreementName;
                bll.Update(oldmodel);
                rd = GetRd(oldmodel);
                return(rd);
            }
            #endregion

            #region 修改可分配利润
            if (String.IsNullOrEmpty(parameter.Id)) //头一次添加规则信息
            {
                T_SuperRetailTraderConfigEntity _model = GetModel(loggingSessionInfo.ClientID, parameter);
                _model.Id = Guid.NewGuid();
                //改动了数据 要重新插入信息
                bll.UpdateByCondition(loggingSessionInfo.ClientID); //修改以前信息为失效状态
                bll.Create(_model);
                rd = GetRd(_model);

                //修改 佣金分润信息
                T_SuperRetailTraderProfitConfigBLL SuperRetailTraderProfitConfigService = new T_SuperRetailTraderProfitConfigBLL(loggingSessionInfo);
                SuperRetailTraderProfitConfigService.UpdateByCustomerId(loggingSessionInfo.ClientID);
            }
            else   //有可能是修改有可能是添加
            {
                //判断总和是否超过了100%
                var currentmodel = bll.GetByID(parameter.Id);
                if (currentmodel == null)
                {
                    throw new APIException("系统繁忙,请稍后重试!");
                }

                if (!(currentmodel.Cost == parameter.Cost && currentmodel.CustomerProfit == parameter.CustomerProfit && currentmodel.DistributionProfit == parameter.DistributionProfit && currentmodel.SkuCommission == parameter.SkuCommission))
                {
                    //改动了数据 要重新插入信息
                    bll.UpdateByCondition(loggingSessionInfo.ClientID); //修改以前信息为失效状态
                    T_SuperRetailTraderConfigEntity _model = GetModel(loggingSessionInfo.ClientID, parameter);
                    _model.Id            = Guid.NewGuid();
                    _model.AgreementName = currentmodel.AgreementName;
                    _model.Agreement     = currentmodel.Agreement;
                    _model.RefId         = currentmodel.Id; //相关编号
                    bll.Create(_model);
                    rd = GetRd(_model);
                }
                else
                {
                    rd = GetRd(currentmodel);
                }
            }
            #endregion

            #region 默认设置分润体系 1:2:1 的体系
            //将该商户下面所有的分润设置设为失效状态
            T_SuperRetailTraderProfitConfigBLL superretailtraderprofitconfigService = new T_SuperRetailTraderProfitConfigBLL(loggingSessionInfo);
            superretailtraderprofitconfigService.UpdateByCustomerId(loggingSessionInfo.ClientID);
            int[]   precent            = new int[] { 1, 2, 1 };
            int     level              = 2;
            double  DistributionProfit = (Convert.ToDouble(parameter.DistributionProfit) / 4.0);
            decimal specialProfit      = 0.0m;
            foreach (var item in precent)
            {
                decimal Profit = Math.Round(Convert.ToDecimal(DistributionProfit * item), 1);

                if (level == 4)
                {
                    Profit = Convert.ToDecimal(parameter.DistributionProfit - specialProfit);
                }
                else
                {
                    specialProfit += Profit;
                }
                superretailtraderprofitconfigService.Create(new T_SuperRetailTraderProfitConfigEntity()
                {
                    CustomerId = loggingSessionInfo.ClientID,
                    Level      = level,
                    Profit     = Profit, //小数点有问题
                    IsDelete   = 0,
                    Status     = "10",
                    ProfitType = "Percent",
                    RefSuperRetailTraderProfitConfigId = null,
                    SuperRetailTraderProfitConfigId    = Guid.NewGuid()
                });

                if (level == 2)
                {
                    superretailtraderprofitconfigService.Create(new T_SuperRetailTraderProfitConfigEntity()
                    {
                        CustomerId = loggingSessionInfo.ClientID,
                        Level      = 1,
                        Profit     = parameter.SkuCommission,
                        IsDelete   = 0,
                        Status     = "10",
                        ProfitType = "Percent",
                        RefSuperRetailTraderProfitConfigId = null,
                        SuperRetailTraderProfitConfigId    = Guid.NewGuid()
                    });
                }
                level = level + 1;
            }
            #endregion
            return(rd);
        }
Exemplo n.º 4
0
        protected override SetuperRetailTraderProfitConfigRD ProcessRequest(DTO.Base.APIRequest <SetuperRetailTraderProfitConfigRP> pRequest)
        {
            var parameter          = pRequest.Parameters;
            var loggingSessionInfo = new SessionManager().CurrentUserLoginInfo; //登录状态信息

            if (loggingSessionInfo == null)
            {
                throw new APIException("登录超时,请重试")
                      {
                          ErrorCode = 135
                      };
            }
            var rd = new SetuperRetailTraderProfitConfigRD();
            T_SuperRetailTraderProfitConfigBLL bll = new T_SuperRetailTraderProfitConfigBLL(loggingSessionInfo);
            int level   = 0;
            var _models = parameter.ProfitConfigList.OrderBy(m => m.Level);

            #region  务端验证 将未分配完的利润合并到商家利润里面去

            T_SuperRetailTraderConfigBLL SuperRetailTraderConfigService = new T_SuperRetailTraderConfigBLL(loggingSessionInfo);
            List <EqualsCondition>       lstWhereCondition = new List <EqualsCondition>()
            {
                new EqualsCondition()
                {
                    FieldName = "CustomerId", Value = loggingSessionInfo.ClientID
                },
                new EqualsCondition()
                {
                    FieldName = "IsDelete", Value = 0
                },
            };

            List <OrderBy> orderCondition = new List <OrderBy>()
            {
                new OrderBy()
                {
                    FieldName = "CreateTime", Direction = OrderByDirections.Desc
                }
            };
            var model = SuperRetailTraderConfigService.Query(lstWhereCondition.ToArray(), orderCondition.ToArray());

            if (model == null || model.Length == 0)
            {
                throw new APIException("请先设置商家佣金信息")
                      {
                          ErrorCode = 135
                      };
            }

            decimal?ProfitMoney = parameter.ProfitConfigList.Where(m => m.Level != 1 && m.Status == "10").Sum(m => m.Profit); //分销体系百分比

            if (ProfitMoney > model[0].DistributionProfit)                                                                    //设置分润比例大于商家比例
            {
                throw new APIException("分润体系不能大于佣金体系,请重新设置")
                      {
                          ErrorCode = 135
                      };
            }

            if (ProfitMoney != model[0].DistributionProfit)  //新入一条规则信息 如果设置比例不等于商家比例
            {
                SuperRetailTraderConfigService.UpdateByCondition(loggingSessionInfo.ClientID);
                decimal?undistributedprofit            = model[0].DistributionProfit - ProfitMoney;
                T_SuperRetailTraderConfigEntity entity = new T_SuperRetailTraderConfigEntity()
                {
                    AgreementName      = model[0].AgreementName,
                    Agreement          = model[0].Agreement,
                    Cost               = model[0].Cost,
                    DistributionProfit = model[0].DistributionProfit - undistributedprofit,
                    CustomerProfit     = model[0].CustomerProfit + undistributedprofit,
                    IsDelete           = 0,
                    CustomerId         = loggingSessionInfo.ClientID,
                    Id            = Guid.NewGuid(),
                    RefId         = model[0].Id,
                    SkuCommission = model[0].SkuCommission
                };
                SuperRetailTraderConfigService.Create(entity);
            }
            #endregion
            bll.UpdateByCustomerId(loggingSessionInfo.ClientID);

            foreach (var item in _models)
            {
                Guid id = Guid.NewGuid();

                if (item.Status.Trim() == "10")
                {
                    level = level + 1;    //如果是启用状态那么 level就自增
                }
                var oldmodel = bll.GetByID(item.SuperRetailTraderProfitConfigId);

                string refid = null;
                if (oldmodel != null)
                {
                    refid           = oldmodel.SuperRetailTraderProfitConfigId.ToString();
                    oldmodel.Status = "90";  //将本条信息置为失效状态。
                    bll.Update(oldmodel);
                }


                T_SuperRetailTraderProfitConfigEntity _model = new T_SuperRetailTraderProfitConfigEntity()
                {
                    SuperRetailTraderProfitConfigId = id,
                    Status     = item.Status,
                    Profit     = item.Profit,
                    Level      = level,
                    CustomerId = loggingSessionInfo.ClientID,
                    IsDelete   = 0,
                    ProfitType = "Percent",
                    RefSuperRetailTraderProfitConfigId = refid,
                };
                bll.Create(_model); //添加一条新的信息。

                SetSuperRetailTraderProfitConfigInfo info = new SetSuperRetailTraderProfitConfigInfo()
                {
                    Level  = item.Level == null ? 1 : int.Parse(item.Level + ""),
                    Profit = item.Profit,
                    Status = item.Status,
                    SuperRetailTraderProfitConfigId = id
                };
                rd.lst.Add(info); //返回值
            }
            return(rd);
        }