Esempio n. 1
0
        /// <summary>
        /// 在队列失效的情况下,直接计算
        /// </summary>
        /// <param name="loggingSessionInfo"></param>
        /// <param name="orderInfo"></param>
        public void CalculateVipConsumeForUpgrade(LoggingSessionInfo loggingSessionInfo, T_InoutEntity orderInfo)
        {
            VipCardUpgradeRuleBLL bllVipCardUpgradeRule = new VipCardUpgradeRuleBLL(loggingSessionInfo);
            VipCardVipMappingBLL  vipCardVipMappingBLL  = new VipCardVipMappingBLL(loggingSessionInfo);
            T_InoutBLL            bllInout = new T_InoutBLL(loggingSessionInfo);
            var entityVipCardUpgradeRule   = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
            {
                IsBuyUpgrade = 1, CustomerID = loggingSessionInfo.ClientID
            }, null);

            if (entityVipCardUpgradeRule != null)
            {
                decimal vipSumAmount = bllInout.GetVipSumAmount(orderInfo.vip_no);
                bool    isUpdate     = false;
                foreach (var rule in entityVipCardUpgradeRule.OrderByDescending(a => a.VipCardTypeID))
                {
                    if (rule.BuyAmount > 0)
                    {
                        if (vipSumAmount >= rule.BuyAmount)
                        {
                            if (!isUpdate)
                            {
                                vipCardVipMappingBLL.BindVirtualItem(orderInfo.vip_no, orderInfo.VipCardCode, "", (int)rule.VipCardTypeID, "TotalSales", 2, orderInfo.order_id);
                                isUpdate = true;
                            }
                        }
                    }
                    if (rule.OnceBuyAmount > 0)
                    {
                        if (orderInfo.actual_amount >= rule.OnceBuyAmount)
                        {
                            if (!isUpdate)
                            {
                                vipCardVipMappingBLL.BindVirtualItem(orderInfo.vip_no, orderInfo.VipCardCode, "", (int)rule.VipCardTypeID, "OneSales", 2, orderInfo.order_id);
                                isUpdate = true;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        protected override SetRechargeOrderRD ProcessRequest(APIRequest <SetRechargeOrderRP> pRequest)
        {
            SetRechargeOrderRP rp = pRequest.Parameters;
            SetRechargeOrderRD rd = new SetRechargeOrderRD();

            var            rechargeOrderBll     = new RechargeOrderBLL(CurrentUserInfo);
            TUnitExpandBLL serviceUnitExpandBll = new TUnitExpandBLL(CurrentUserInfo);

            var vipBll = new VipBLL(CurrentUserInfo);
            var vipCardVipMappingBll  = new VipCardVipMappingBLL(CurrentUserInfo);
            var vipCardBll            = new VipCardBLL(CurrentUserInfo);
            var sysVipCardTypeBll     = new SysVipCardTypeBLL(CurrentUserInfo);
            var vipCardUpgradeRuleBll = new VipCardUpgradeRuleBLL(CurrentUserInfo);

            //获取会员信息
            VipEntity vipInfo;
            string    userId = "";

            //微信
            if (pRequest.ChannelId == "2")
            {
                vipInfo = vipBll.GetByID(pRequest.UserID);
                UnitService unitServer = new UnitService(CurrentUserInfo);
                rp.UnitId = unitServer.GetUnitByUnitTypeForWX("OnlineShopping", null).Id; //获取在线商城的门店标识
            }
            //掌柜App
            else
            {
                vipInfo = vipBll.GetByID(rp.VipId);
                userId  = pRequest.UserID;
            }

            string  OrderDesc     = "ReRecharge"; //"升级","充值"
            int?    VipCardTypeId = null;         //会员卡类型
            decimal returnAmount  = 0;            //赠送金额

            //会员卡类型条件
            List <IWhereCondition> complexConditionOne = new List <IWhereCondition> {
            };
            //获取会员与会员卡关系表
            var vipCardVipMappingEntity = vipCardVipMappingBll.QueryByEntity(new VipCardVipMappingEntity()
            {
                CustomerID = CurrentUserInfo.ClientID, VIPID = vipInfo.VIPID
            }, null).FirstOrDefault();

            //该会员有会员卡
            if (vipCardVipMappingEntity != null)
            {
                //获取该会员会员卡信息
                var vipCardEntity = vipCardBll.QueryByEntity(new VipCardEntity()
                {
                    CustomerID = CurrentUserInfo.ClientID, VipCardID = vipCardVipMappingEntity.VipCardID
                }, null).FirstOrDefault();

                //获取该会员卡类型
                var sysVipCardTypeEntity = sysVipCardTypeBll.GetByID(vipCardEntity.VipCardTypeID);
                VipCardTypeId = vipCardEntity.VipCardTypeID;

                //获取会员卡的会员活动
                var rechargeStrategyBLL = new RechargeStrategyBLL(CurrentUserInfo);
                var RechargeActivityDS  = rechargeStrategyBLL.GetRechargeActivityList(CurrentUserInfo.ClientID, VipCardTypeId.ToString(), 3);
                var RechargeStrategy    = new RechargeStrategyInfo();

                if (RechargeActivityDS != null && RechargeActivityDS.Tables[0].Rows.Count > 0)
                {
                    var RechargeStrategyList = DataTableToObject.ConvertToList <RechargeStrategyInfo>(RechargeActivityDS.Tables[0]);
                    //获取与充值金额最接近的活动
                    RechargeStrategy = RechargeStrategyList.Where(n => n.RechargeAmount <= rp.ActuallyPaid).OrderByDescending(n => n.RechargeAmount).FirstOrDefault();
                    if (RechargeStrategy != null)
                    {
                        //梯度
                        if (RechargeStrategy.RuleType == "Step")
                        {
                            //赠送金额 = 会员活动梯度设置赠送金额
                            returnAmount = RechargeStrategy.GiftAmount;
                        }
                        //叠加
                        if (RechargeStrategy.RuleType == "Superposition")
                        {
                            //赠送金额与满赠条件金额比例
                            decimal discount = RechargeStrategy.GiftAmount / RechargeStrategy.RechargeAmount;
                            //赠送金额 = 充值金额 * (设置赠送金额 / 设置满赠条件金额)
                            returnAmount = rp.ActuallyPaid * RechargeStrategy.GiftAmount / RechargeStrategy.RechargeAmount;
                        }
                    }
                }

                //获取等级高的会员卡类型条件
                complexConditionOne.Add(new MoreThanCondition()
                {
                    FieldName = "VipCardLevel", Value = sysVipCardTypeEntity.VipCardLevel, IncludeEquals = false
                });
                complexConditionOne.Add(new EqualsCondition()
                {
                    FieldName = "CustomerID", Value = CurrentUserInfo.ClientID
                });
            }
            //该会员没有会员卡
            else
            {
                //获取所有的会员卡类型条件
                complexConditionOne.Add(new EqualsCondition()
                {
                    FieldName = "CustomerID", Value = CurrentUserInfo.ClientID
                });
            }

            var sysVipCardTypeList = sysVipCardTypeBll.Query(complexConditionOne.ToArray(), null).ToList();

            if (sysVipCardTypeList == null)
            {
                throw new APIException("没有建立会员体系")
                      {
                          ErrorCode = 200
                      };
            }

            //获取会员卡类型升级规则
            List <IWhereCondition> complexConditionTwo = new List <IWhereCondition> {
            };

            complexConditionTwo.Add(new EqualsCondition()
            {
                FieldName = "CustomerID", Value = CurrentUserInfo.ClientID
            });
            complexConditionTwo.Add(new EqualsCondition()
            {
                FieldName = "IsRecharge", Value = 1
            });
            var vipCardUpgradeRuleList = vipCardUpgradeRuleBll.Query(complexConditionTwo.ToArray(), null);

            //将升级卡规则和可升级的会员卡匹配
            var vipCardTypeDetailList = vipCardUpgradeRuleList.Join(sysVipCardTypeList, n => n.VipCardTypeID, m => m.VipCardTypeID, (n, m) => new { n, m }).OrderByDescending(t => t.m.VipCardLevel).ToList();

            //判断是否满充值条件
            bool isUpgrade = false;


            for (int i = 0; i < vipCardTypeDetailList.Count; i++)
            {
                //实付金额 >= 可升级金额
                if (vipCardTypeDetailList[i].n.OnceRechargeAmount <= rp.ActuallyPaid)
                {
                    isUpgrade     = true;
                    VipCardTypeId = vipCardTypeDetailList[i].n.VipCardTypeID ?? 0;
                    break;
                }
            }
            if (isUpgrade)
            {
                OrderDesc = "Upgrade";  //升级
            }


            //充值订单
            var rechargeOrderEntity = new RechargeOrderEntity()
            {
                OrderID       = Guid.NewGuid(),
                OrderNo       = serviceUnitExpandBll.GetUnitOrderNo(),
                OrderDesc     = OrderDesc,
                VipID         = vipInfo.VIPID,
                VipCardNo     = vipInfo.VipCode,
                UnitId        = rp.UnitId,
                UserId        = userId,
                TotalAmount   = rp.ActuallyPaid,
                ActuallyPaid  = rp.ActuallyPaid,
                PosSourceName = rp.PosSourceName,
                ReturnAmount  = returnAmount,
                PayerID       = vipInfo.VIPID,
                Status        = 0,
                CustomerID    = CurrentUserInfo.ClientID,
                VipCardTypeId = VipCardTypeId
            };

            rechargeOrderBll.Create(rechargeOrderEntity);
            rd.orderId = rechargeOrderEntity.OrderID.ToString();

            //支付完成
            if (rp.PayStatus == 1)
            {
                //获取门店信息
                t_unitBLL    unitBLL  = new t_unitBLL(CurrentUserInfo);
                t_unitEntity unitInfo = null;
                if (!string.IsNullOrEmpty(rechargeOrderEntity.UnitId))
                {
                    unitInfo = unitBLL.GetByID(rechargeOrderEntity.UnitId);
                }
                //充值
                rechargeOrderBll.Recharge(rechargeOrderEntity, vipInfo, unitInfo, "");
            }

            return(rd);
        }
        /// <summary>
        /// 计算购卡分润
        /// </summary>
        public void CalculateSalesVipCardOrderJob()
        {
            var numCount = 50;

            var customerIDs = CustomerBLL.Instance.GetCustomerList();

            foreach (var customer in customerIDs)
            {
                string connString = customer.Value;
                var    count      = RedisOpenAPI.Instance.CCSalesCardOrder().GetSalesCardOrderLength(new CC_Order
                {
                    CustomerID = customer.Key
                });
                if (count.Code != ResponseCode.Success)
                {
                    BaseService.WriteLog("从redis获取vipcard订单数据失败");
                    continue;
                }
                if (count.Result <= 0)
                {
                    continue;
                }
                if (count.Result < numCount)
                {
                    numCount = Convert.ToInt32(count.Result);
                }
                //DataTable dtAmountDetail = CreateTableAmountDetail();
                //DataTable dtAmount = CreateTableAmount();
                //DataTable dtSplitProfitRecord = CreateTableSplitProfitRecord();

                for (var i = 0; i < numCount; i++)
                {
                    var response = RedisOpenAPI.Instance.CCSalesCardOrder().GetSalesCardOrder(new CC_Order
                    {
                        CustomerID = customer.Key
                    });
                    if (response.Code == ResponseCode.Success)
                    {
                        var        orderInfo          = response.Result.OrderInfo.JsonDeserialize <T_InoutEntity>();
                        var        loggingSessionInfo = CustomerBLL.Instance.GetBSLoggingSession(customer.Key, "RedisSystem");
                        T_InoutBLL inoutBLL           = new T_InoutBLL(loggingSessionInfo);
                        if (orderInfo != null)
                        {
                            VipBLL                      bllVip                    = new VipBLL(loggingSessionInfo);
                            T_Inout_DetailBLL           bllInoutDetail            = new T_Inout_DetailBLL(loggingSessionInfo);
                            T_VirtualItemTypeSettingBLL bllVirtualItemTypeSetting = new T_VirtualItemTypeSettingBLL(loggingSessionInfo);
                            VipCardUpgradeRuleBLL       bllVipCardUpgradeRule     = new VipCardUpgradeRuleBLL(loggingSessionInfo);
                            VipAmountBLL                bllVipAmount              = new VipAmountBLL(loggingSessionInfo);
                            VipAmountDetailBLL          bllVipAmountDetail        = new VipAmountDetailBLL(loggingSessionInfo);
                            T_SplitProfitRecordBLL      bllSplitProfitRecord      = new T_SplitProfitRecordBLL(loggingSessionInfo);

                            VipAmountEntity           entityVipAmount         = new VipAmountEntity();
                            VipAmountDetailEntity     entityVipAmountDetail   = new VipAmountDetailEntity();
                            T_SplitProfitRecordEntity entitySplitProfitRecord = new T_SplitProfitRecordEntity();

                            DataSet dsVipCardLevel    = bllVip.GetVipCardLevel(orderInfo.vip_no, loggingSessionInfo.ClientID);
                            var     entityInoutDetail = bllInoutDetail.QueryByEntity(new T_Inout_DetailEntity()
                            {
                                order_id = orderInfo.order_id
                            }, null).FirstOrDefault();
                            if (entityInoutDetail == null)
                            {
                                continue;
                            }
                            var vipCardType = bllVirtualItemTypeSetting.QueryByEntity(new T_VirtualItemTypeSettingEntity()
                            {
                                SkuId = entityInoutDetail.sku_id, IsDelete = 0
                            }, null).FirstOrDefault();
                            if (vipCardType != null)
                            {
                                int intVipCardTypeID         = Convert.ToInt32(vipCardType.ObjecetTypeId);
                                var entityVipCardUpgradeRule = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
                                {
                                    VipCardTypeID = intVipCardTypeID, IsPurchaseUpgrade = 1, IsDelete = 0
                                }, null).SingleOrDefault();
                                if (entityVipCardUpgradeRule != null)
                                {
                                    VipCardProfitRuleBLL bllVipCardProfitRule = new VipCardProfitRuleBLL(loggingSessionInfo);
                                    var entityVipCardProfitRule = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                                    {
                                        VipCardTypeID = intVipCardTypeID, IsDelete = 0
                                    }, null);
                                    foreach (var ProfitRule in entityVipCardProfitRule)
                                    {
                                        if (ProfitRule.IsApplyAllUnits == 0)
                                        {
                                            VipCardProfitRuleUnitMappingBLL bllVipCardProfitRuleUnitMapping = new VipCardProfitRuleUnitMappingBLL(loggingSessionInfo);
                                            var vipCardProfitRuleUnitMapping = bllVipCardProfitRuleUnitMapping.QueryByEntity(new VipCardProfitRuleUnitMappingEntity()
                                            {
                                                CardBuyToProfitRuleId = ProfitRule.CardBuyToProfitRuleId, UnitID = orderInfo.sales_unit_id, IsDelete = 0, CustomerID = loggingSessionInfo.ClientID
                                            }, null).SingleOrDefault();
                                            if (vipCardProfitRuleUnitMapping == null)
                                            {
                                                continue;
                                            }
                                        }
                                        decimal amount            = 0;
                                        string  strAmountSourceId = string.Empty;
                                        string  strVipId          = string.Empty;
                                        string  strUserType       = string.Empty;
                                        if (ProfitRule.ProfitOwner == "Employee")
                                        {
                                            strAmountSourceId = "37";
                                            strVipId          = orderInfo.sales_user;
                                            strUserType       = "User";
                                        }
                                        if (ProfitRule.ProfitOwner == "Unit")
                                        {
                                            strAmountSourceId = "40";
                                            strVipId          = orderInfo.sales_unit_id;
                                            strUserType       = "Unit";
                                        }
                                        amount = (decimal)ProfitRule.FirstCardSalesProfitPct * (decimal)orderInfo.actual_amount * (decimal)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 {
                                                //                                 DataRow dr_SplitProfitRecord = dtSplitProfitRecord.NewRow();
                                                //                                 dr_SplitProfitRecord["Id"] = Guid.NewGuid();
                                                //                                 dr_SplitProfitRecord["SourceType"] = "Amount";
                                                //                                 dr_SplitProfitRecord["SourceId"] = strAmountSourceId;
                                                //                                 dr_SplitProfitRecord["ObjectId"] = orderInfo.order_id;
                                                //dr_SplitProfitRecord["UserType"] = strUserType;
                                                //                                 dr_SplitProfitRecord["UserId"] = orderInfo.sales_user;
                                                //                                 dr_SplitProfitRecord["SplitAmount"] = amount;
                                                //                                 dr_SplitProfitRecord["SplitSattus"] = "10";
                                                //                                 dr_SplitProfitRecord["CustomerId"] = loggingSessionInfo.ClientID;
                                                //                                 dr_SplitProfitRecord["CreateTime"] = DateTime.Now;
                                                //                                 dr_SplitProfitRecord["CreateBy"] = loggingSessionInfo.ClientID;
                                                //                                 dr_SplitProfitRecord["LastUpdateTime"] = DateTime.Now;
                                                //                                 dr_SplitProfitRecord["LastUpdateBy"] = loggingSessionInfo.ClientID;
                                                //                                 dr_SplitProfitRecord["IsDelete"] = 0;
                                                //                                 dtSplitProfitRecord.Rows.Add(dr_SplitProfitRecord);

                                                //                                 DataRow dr_AmountDetail = dtAmountDetail.NewRow();
                                                //                                 dr_AmountDetail["VipAmountDetailId"] = Guid.NewGuid();
                                                //dr_AmountDetail["VipId"] = strVipId;
                                                //                                 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);

                                                entitySplitProfitRecord = new T_SplitProfitRecordEntity()
                                                {
                                                    ID          = Guid.NewGuid().ToString(),
                                                    SourceType  = "Amount",
                                                    SourceId    = strAmountSourceId,
                                                    ObjectId    = orderInfo.order_id,
                                                    UserType    = strUserType,
                                                    UserId      = orderInfo.sales_user,
                                                    SplitAmount = amount,
                                                    SplitSattus = "10",
                                                    CustomerID  = loggingSessionInfo.ClientID
                                                };
                                                bllSplitProfitRecord.Create(entitySplitProfitRecord, tran);


                                                entityVipAmountDetail = new VipAmountDetailEntity {
                                                    VipAmountDetailId = Guid.NewGuid(),
                                                    VipId             = strVipId,
                                                    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            = "首次售卡分润",
                                                    Remark            = "首次售卡分润",
                                                    IsWithdrawCash    = 0
                                                };
                                                bllVipAmountDetail.Create(entityVipAmountDetail, (SqlTransaction)tran);

                                                entityVipAmount = bllVipAmount.QueryByEntity(new VipAmountEntity()
                                                {
                                                    VipId = orderInfo.sales_user, IsDelete = 0
                                                }, null).SingleOrDefault();
                                                if (entityVipAmount == null)
                                                {
                                                    //DataRow dr_Amount = dtAmount.NewRow();
                                                    //dr_Amount["VipId"] = orderInfo.sales_user;
                                                    //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);
                                                    entityVipAmount = new VipAmountEntity {
                                                        VipId                  = strVipId,
                                                        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);
                                                }
                                                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 && dtSplitProfitRecord.Rows.Count > 0)
                //{
                //    Utils.SqlBulkCopy(connString, dtSplitProfitRecord, "T_SplitProfitRecord");
                //    Utils.SqlBulkCopy(connString, dtAmountDetail, "VipAmountDetail");

                //}
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 销售升级job
        /// </summary>
        public void CalculateVipConsumeForUpgradeJob()
        {
            var numCount    = 50;
            var customerIDs = CustomerBLL.Instance.GetCustomerList();

            foreach (var customer in customerIDs)
            {
                string connString = customer.Value;
                var    count      = RedisOpenAPI.Instance.CCAllOrder().GetOrderLength(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);
                }
                var loggingSessionInfo = CustomerBLL.Instance.GetBSLoggingSession(customer.Key, "RedisSystem");
                VipCardUpgradeRuleBLL bllVipCardUpgradeRule = new VipCardUpgradeRuleBLL(loggingSessionInfo);
                VipCardVipMappingBLL  vipCardVipMappingBLL  = new VipCardVipMappingBLL(loggingSessionInfo);
                T_InoutBLL            bllInout = new T_InoutBLL(loggingSessionInfo);
                for (var i = 0; i < numCount; i++)
                {
                    var response = RedisOpenAPI.Instance.CCAllOrder().GetOrder(new CC_Order
                    {
                        CustomerID = customer.Key
                    });
                    if (response.Code == ResponseCode.Success)
                    {
                        var orderInfo = response.Result.OrderInfo.JsonDeserialize <T_InoutEntity>();
                        var entityVipCardUpgradeRule = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
                        {
                            IsBuyUpgrade = 1, CustomerID = loggingSessionInfo.ClientID
                        }, null);
                        if (entityVipCardUpgradeRule != null)
                        {
                            decimal vipSumAmount = bllInout.GetVipSumAmount(orderInfo.vip_no);
                            bool    isUpdate     = false;
                            foreach (var rule in entityVipCardUpgradeRule.OrderByDescending(a => a.VipCardTypeID))
                            {
                                if (rule.BuyAmount > 0)
                                {
                                    if (vipSumAmount >= rule.BuyAmount)
                                    {
                                        if (!isUpdate)
                                        {
                                            vipCardVipMappingBLL.BindVirtualItem(orderInfo.vip_no, orderInfo.VipCardCode, "", (int)rule.VipCardTypeID, "TotalSales", 2, orderInfo.order_id);
                                            isUpdate = true;
                                        }
                                    }
                                }
                                if (rule.OnceBuyAmount > 0)
                                {
                                    if (orderInfo.actual_amount >= rule.OnceBuyAmount)
                                    {
                                        if (!isUpdate)
                                        {
                                            vipCardVipMappingBLL.BindVirtualItem(orderInfo.vip_no, orderInfo.VipCardCode, "", (int)rule.VipCardTypeID, "OneSales", 2, orderInfo.order_id);
                                            isUpdate = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        protected override EmptyResponseData ProcessRequest(DTO.Base.APIRequest <UpdateVipCardTypeSystemRP> pRequest)
        {
            var rd   = new EmptyResponseData();
            var para = pRequest.Parameters;
            var loggingSessionInfo   = new SessionManager().CurrentUserLoginInfo;
            var entitySysVipCardType = new SysVipCardTypeEntity();
            var bllSysVipCardType    = new SysVipCardTypeBLL(loggingSessionInfo);
            //卡分润规则
            var bllVipCardProfitRule = new VipCardProfitRuleBLL(loggingSessionInfo);
            //续费充值方式
            var VipCardReRechargeProfitRuleService = new VipCardReRechargeProfitRuleBLL(loggingSessionInfo);

            //编辑会员卡等级
            try
            {
                var SysVipCardTypeInfo = bllSysVipCardType.QueryByEntity(new SysVipCardTypeEntity()
                {
                    CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = para.VipCardTypeID, IsDelete = 0
                }, null).FirstOrDefault();
                switch (para.OperateType)
                {
                //如果为1编辑会员卡等级信息
                case 1:
                    if (SysVipCardTypeInfo != null)
                    {
                        //获取当前会员信息
                        List <IWhereCondition> wheres = new List <IWhereCondition>();
                        wheres.Add(new EqualsCondition()
                        {
                            FieldName = "Category", Value = 0
                        });
                        wheres.Add(new EqualsCondition()
                        {
                            FieldName = "CustomerID", Value = loggingSessionInfo.ClientID
                        });
                        wheres.Add(new EqualsCondition()
                        {
                            FieldName = "VipCardTypeName", Value = para.VipCardTypeName
                        });
                        wheres.Add(new DirectCondition("VipCardLevel!='" + SysVipCardTypeInfo.VipCardLevel + "'"));
                        var ExistVipCardTypeResult = bllSysVipCardType.Query(wheres.ToArray(), null).FirstOrDefault();
                        if (ExistVipCardTypeResult != null)
                        {
                            throw new APIException("会员卡名称不能重复!")
                                  {
                                      ErrorCode = ERROR_CODES.INVALID_BUSINESS
                                  };
                        }
                        SysVipCardTypeInfo.VipCardTypeID   = para.VipCardTypeID;
                        SysVipCardTypeInfo.Category        = 0;
                        SysVipCardTypeInfo.VipCardTypeName = para.VipCardTypeName;
                        SysVipCardTypeInfo.VipCardLevel    = SysVipCardTypeInfo.VipCardLevel;
                        SysVipCardTypeInfo.IsPassword      = SysVipCardTypeInfo.IsPassword;
                        if (SysVipCardTypeInfo.VipCardLevel == 1)    //如果等级为1默认可充值
                        {
                            SysVipCardTypeInfo.Isprepaid = 1;
                        }
                        else
                        {
                            SysVipCardTypeInfo.Isprepaid = para.IsPrepaid;
                        }
                        SysVipCardTypeInfo.IsOnlineSales = para.IsOnlineSales;
                        SysVipCardTypeInfo.PicUrl        = para.PicUrl;
                        SysVipCardTypeInfo.CustomerID    = loggingSessionInfo.ClientID;
                        if (SysVipCardTypeInfo.VipCardLevel == 1)
                        {
                            SysVipCardTypeInfo.Prices           = 0;
                            SysVipCardTypeInfo.ExchangeIntegral = 0;
                            SysVipCardTypeInfo.IsExtraMoney     = 2;
                        }
                    }
                    //如果IsOnlineSales为0则需要逻辑删除分润规则数据
                    if (SysVipCardTypeInfo.IsOnlineSales == 0)
                    {
                        var VipCardProfitRuleInfo = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                        {
                            VipCardTypeID = SysVipCardTypeInfo.VipCardTypeID, CustomerID = loggingSessionInfo.ClientID
                        }, null);
                        if (VipCardProfitRuleInfo.Length > 0)
                        {
                            bllVipCardProfitRule.Delete(VipCardProfitRuleInfo);
                        }
                    }

                    if (SysVipCardTypeInfo.Isprepaid == 0)     //不可充值
                    {
                        var VipCardUpgradeRuleService = new VipCardUpgradeRuleBLL(loggingSessionInfo);
                        //升级规则信息 永远只有一条信息
                        var CardUpgradeRuleEntity = VipCardUpgradeRuleService.QueryByEntity(new VipCardUpgradeRuleEntity()
                        {
                            VipCardTypeID = SysVipCardTypeInfo.VipCardTypeID
                        }, null);
                        if (CardUpgradeRuleEntity != null && CardUpgradeRuleEntity.FirstOrDefault() != null) //如果有设置了升级条件
                        {
                            if (CardUpgradeRuleEntity.FirstOrDefault().IsBuyUpgrade == 1)                    //消费升级+不可充值==>逻辑删除 会员卡销售激励规则
                            {
                                var Ids = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                                {
                                    CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = SysVipCardTypeInfo.VipCardTypeID
                                }, null).Select(m => m.CardBuyToProfitRuleId.Value.ToString()).ToArray();
                                if (Ids.Length > 0)
                                {
                                    bllVipCardProfitRule.Delete(Ids);
                                }
                            }
                        }

                        //将所有梯度信息 逻辑删除
                        string[] ProfitRuleIds = bllVipCardProfitRule.GetRechargeProfitRuleByIsPrepaid(loggingSessionInfo.ClientID, SysVipCardTypeInfo.VipCardTypeID);
                        if (ProfitRuleIds.Length > 0)
                        {
                            VipCardReRechargeProfitRuleService.Delete(ProfitRuleIds);
                        }
                    }
                    bllSysVipCardType.Update(SysVipCardTypeInfo);
                    //修改虚拟商品
#warning 商品代码注释 Bear
                    ItemService _ItemService = new ItemService(loggingSessionInfo);
                    _ItemService.SaveCardToOffenTItem(loggingSessionInfo, SysVipCardTypeInfo);
                    break;

                //如果为2编辑升级规则信息
                case 2:
                    var bllVipCardUpgradeRule = new VipCardUpgradeRuleBLL(loggingSessionInfo);
                    //升级类型不能为空 (1=购卡升级;2=充值升级;3=消费升级;)
                    if (para.UpGradeType != 0)
                    {
                        VipCardUpgradeRuleEntity vipCardUpgradeRuleEntity = new VipCardUpgradeRuleEntity();
                        if (para.OperateObjectID != null)
                        {
                            var VipCardUpgradeRuleInfo = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
                            {
                                CustomerID = loggingSessionInfo.ClientID, VipCardUpgradeRuleId = new Guid(para.OperateObjectID), VipCardTypeID = para.VipCardTypeID
                            }, null).FirstOrDefault();
                            if (VipCardUpgradeRuleInfo != null)
                            {
                                //先置为0 再进行更新
                                SysVipCardTypeInfo.IsExtraMoney           = 2;
                                SysVipCardTypeInfo.Prices                 = 0;
                                SysVipCardTypeInfo.ExchangeIntegral       = 0;
                                VipCardUpgradeRuleInfo.OnceRechargeAmount = 0;
                                VipCardUpgradeRuleInfo.OnceBuyAmount      = 0;
                                VipCardUpgradeRuleInfo.BuyAmount          = 0;
                                switch (para.UpGradeType)
                                {
                                case 1:
                                    VipCardUpgradeRuleInfo.IsPurchaseUpgrade = 1;
                                    //金额和积分 与可补差价在卡等级表里面
                                    SysVipCardTypeInfo.VipCardTypeID    = para.VipCardTypeID;
                                    SysVipCardTypeInfo.IsExtraMoney     = para.IsExtraMoney;
                                    SysVipCardTypeInfo.Prices           = para.Prices;
                                    SysVipCardTypeInfo.ExchangeIntegral = para.ExchangeIntegral;
                                    //充值升级归零
                                    VipCardUpgradeRuleInfo.IsRecharge = 0;
                                    //消费升级置零
                                    VipCardUpgradeRuleInfo.IsBuyUpgrade = 0;
                                    break;

                                case 2:
                                    VipCardUpgradeRuleInfo.IsPurchaseUpgrade  = 0;
                                    VipCardUpgradeRuleInfo.IsRecharge         = 1;
                                    VipCardUpgradeRuleInfo.IsBuyUpgrade       = 0;
                                    VipCardUpgradeRuleInfo.OnceRechargeAmount = para.OnceRechargeAmount;
                                    break;

                                case 3:
                                    VipCardUpgradeRuleInfo.IsPurchaseUpgrade = 0;
                                    VipCardUpgradeRuleInfo.IsRecharge        = 0;
                                    VipCardUpgradeRuleInfo.IsBuyUpgrade      = 1;
                                    VipCardUpgradeRuleInfo.OnceBuyAmount     = para.OnceBuyAmount;
                                    VipCardUpgradeRuleInfo.BuyAmount         = para.BuyAmount;
                                    //如果该卡为不可充值 那么就默认删除
                                    var syscardentity = bllSysVipCardType.GetByID(para.VipCardTypeID);
                                    if (syscardentity != null && syscardentity.Isprepaid == 0)         //消费升级 {不可充值} 默认删除该卡的激励规则
                                    {
                                        var Ids = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                                        {
                                            CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = para.VipCardTypeID
                                        }, null).Select(m => m.CardBuyToProfitRuleId.Value.ToString()).ToArray();
                                        if (Ids.Length > 0)
                                        {
                                            bllVipCardProfitRule.Delete(Ids);
                                        }
                                    }
                                    break;
                                }
                                //更新卡等级部分信息
                                bllSysVipCardType.Update(SysVipCardTypeInfo);
                                //修改虚拟商品
                                ItemService _ItemServices = new ItemService(loggingSessionInfo);
                                _ItemServices.SaveCardToOffenTItem(loggingSessionInfo, SysVipCardTypeInfo);
                                bllVipCardUpgradeRule.Update(VipCardUpgradeRuleInfo);
                            }
                        }
                        else
                        {
                            //先置为0 再进行更新
                            SysVipCardTypeInfo.IsExtraMoney             = 2;
                            SysVipCardTypeInfo.Prices                   = 0;
                            SysVipCardTypeInfo.ExchangeIntegral         = 0;
                            vipCardUpgradeRuleEntity.OnceRechargeAmount = 0;
                            vipCardUpgradeRuleEntity.OnceBuyAmount      = 0;
                            vipCardUpgradeRuleEntity.BuyAmount          = 0;
                            switch (para.UpGradeType)
                            {
                            case 1:
                                vipCardUpgradeRuleEntity.IsPurchaseUpgrade = 1;
                                vipCardUpgradeRuleEntity.VipCardTypeID     = para.VipCardTypeID;
                                //金额和积分 与可补差价在卡等级表里面
                                SysVipCardTypeInfo.VipCardTypeID    = para.VipCardTypeID;
                                SysVipCardTypeInfo.IsExtraMoney     = para.IsExtraMoney;
                                SysVipCardTypeInfo.Prices           = para.Prices;
                                SysVipCardTypeInfo.ExchangeIntegral = para.ExchangeIntegral;
                                //充值升级归零
                                vipCardUpgradeRuleEntity.IsRecharge = 0;
                                //消费升级置零
                                vipCardUpgradeRuleEntity.IsBuyUpgrade = 0;
                                break;

                            case 2:
                                vipCardUpgradeRuleEntity.IsPurchaseUpgrade  = 0;
                                vipCardUpgradeRuleEntity.IsRecharge         = 1;
                                vipCardUpgradeRuleEntity.IsBuyUpgrade       = 0;
                                vipCardUpgradeRuleEntity.OnceRechargeAmount = para.OnceRechargeAmount;
                                break;

                            case 3:          //消费升级
                                vipCardUpgradeRuleEntity.IsPurchaseUpgrade = 0;
                                vipCardUpgradeRuleEntity.IsRecharge        = 0;
                                vipCardUpgradeRuleEntity.IsBuyUpgrade      = 1;
                                vipCardUpgradeRuleEntity.OnceBuyAmount     = para.OnceBuyAmount;
                                vipCardUpgradeRuleEntity.BuyAmount         = para.BuyAmount;
                                break;
                            }
                            //更新卡等级部分信息
                            bllSysVipCardType.Update(SysVipCardTypeInfo);
                            //修改虚拟商品
                            ItemService _ItemServices = new ItemService(loggingSessionInfo);
                            _ItemServices.SaveCardToOffenTItem(loggingSessionInfo, SysVipCardTypeInfo);
                            //添加卡升级规则
                            vipCardUpgradeRuleEntity.VipCardUpgradeRuleId = Guid.NewGuid();
                            vipCardUpgradeRuleEntity.CustomerID           = loggingSessionInfo.ClientID;
                            bllVipCardUpgradeRule.Create(vipCardUpgradeRuleEntity);
                        }
                    }
                    else
                    {
                        throw new APIException("升级类型不能为空!")
                              {
                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                              };
                    }

                    break;

                //如果为3编辑基本权益信息
                case 3:
                    var bllVipCardRule    = new VipCardRuleBLL(loggingSessionInfo);
                    var entityVipCardRule = new VipCardRuleEntity();
                    var VipCardRuleInfo   = bllVipCardRule.QueryByEntity(new VipCardRuleEntity()
                    {
                        CustomerID = loggingSessionInfo.ClientID, RuleID = Convert.ToInt32(para.OperateObjectID), VipCardTypeID = para.VipCardTypeID
                    }, null).FirstOrDefault();
                    if (VipCardRuleInfo != null)
                    {
                        entityVipCardRule.RuleID               = Convert.ToInt32(para.OperateObjectID);
                        entityVipCardRule.VipCardTypeID        = para.VipCardTypeID;
                        entityVipCardRule.CardDiscount         = para.CardDiscount * 10;
                        entityVipCardRule.PaidGivePoints       = para.PaidGivePoints;
                        entityVipCardRule.PaidGivePercetPoints = para.PaidGivePercetPoints;
                        entityVipCardRule.CustomerID           = loggingSessionInfo.ClientID;
                        bllVipCardRule.Update(entityVipCardRule);
                    }
                    break;
                }

                try
                {
                    var msg = new EventContract
                    {
                        Operation  = OptEnum.Update,
                        EntityType = EntityTypeEnum.VipCardType,
                        Id         = SysVipCardTypeInfo.VipCardTypeID.ToString()
                    };
                    var eventService = new EventService();
                    eventService.PublishMsg(msg);
                }
                catch (Exception)
                {
                    throw new Exception("RabbitMQ Error");
                }
            }
            catch (APIException ex)
            {
                throw ex;
            }

            return(rd);
        }
Esempio n. 6
0
        protected override EmptyResponseData ProcessRequest(DTO.Base.APIRequest <SetVipCardTypeSystemRP> pRequest)
        {
            var rd   = new EmptyResponseData();
            var para = pRequest.Parameters;
            var loggingSessionInfo = new SessionManager().CurrentUserLoginInfo;
            //卡相关逻辑
            var bllVipCardType = new SysVipCardTypeBLL(loggingSessionInfo);

            //升级规则逻辑
            var bllVipCardUpgradeRule = new VipCardUpgradeRuleBLL(loggingSessionInfo);

            //基本权益逻辑
            var bllVipCardRule = new VipCardRuleBLL(loggingSessionInfo);

            //开卡礼逻辑
            var bllVipCardUpgradeReward = new VipCardUpgradeRewardBLL(loggingSessionInfo);
            //卡分润规则
            var bllVipCardProfitRule = new VipCardProfitRuleBLL(loggingSessionInfo);
            //续费充值方式
            var VipCardReRechargeProfitRuleService = new VipCardReRechargeProfitRuleBLL(loggingSessionInfo);

            string _CustomerId = loggingSessionInfo.ClientID;

            if (para.VipCardRelateList.Count > 0)
            {
                try
                {
                    foreach (var VipCardSystem in para.VipCardRelateList)
                    {
                        //获取当前会员信息
                        //List<IWhereCondition> wheres = new List<IWhereCondition>();
                        //wheres.Add(new EqualsCondition() { FieldName = "Category", Value = 0 });
                        //wheres.Add(new EqualsCondition() { FieldName = "CustomerID", Value = loggingSessionInfo.ClientID });
                        //wheres.Add(new EqualsCondition() { FieldName = "VipCardTypeName", Value = VipCardSystem.VipCardType.VipCardTypeName });
                        //wheres.Add(new DirectCondition("VipCardLevel!='" + VipCardSystem.VipCardType.VipCardLevel + "'"));
                        //var ExistVipCardTypeResult = bllVipCardType.Query(wheres.ToArray(), null).FirstOrDefault();
                        //if (ExistVipCardTypeResult != null)
                        //{
                        //    throw new APIException("会员卡名称不能重复!") { ErrorCode = ERROR_CODES.INVALID_BUSINESS };
                        //}
                        //获取当前待添加的卡等级数据
                        var VipCardTypeResult = bllVipCardType.QueryByEntity(new SysVipCardTypeEntity()
                        {
                            Category = 0, VipCardLevel = VipCardSystem.VipCardType.VipCardLevel, CustomerID = loggingSessionInfo.ClientID
                        }, null).FirstOrDefault();

                        if (VipCardTypeResult != null)
                        {
                            #region 编辑会员卡等级信息
                            //throw new APIException("卡等级不能重复!") { ErrorCode = ERROR_CODES.INVALID_BUSINESS };
                            //如果存在数据需要进行修改
                            VipCardTypeResult.VipCardTypeName = VipCardSystem.VipCardType.VipCardTypeName;
                            VipCardTypeResult.VipCardLevel    = VipCardSystem.VipCardType.VipCardLevel;
                            if (VipCardTypeResult.VipCardLevel == 1)//如果等级为1默认可充值
                            {
                                VipCardTypeResult.Isprepaid = 1;
                            }
                            else
                            {
                                VipCardTypeResult.Isprepaid = VipCardSystem.VipCardType.IsPrepaid;
                            }
                            VipCardTypeResult.IsOnlineSales = VipCardSystem.VipCardType.IsOnlineSales;
                            //如果IsOnlineSales为0则需要逻辑删除分润规则数据
                            if (VipCardTypeResult.IsOnlineSales == 0)
                            {
                                var VipCardProfitRuleInfo = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                                {
                                    VipCardTypeID = VipCardTypeResult.VipCardTypeID, CustomerID = loggingSessionInfo.ClientID
                                }, null);
                                if (VipCardProfitRuleInfo.Length > 0)
                                {
                                    bllVipCardProfitRule.Delete(VipCardProfitRuleInfo);
                                }
                            }
                            VipCardTypeResult.PicUrl = VipCardSystem.VipCardType.PicUrl;
                            if (VipCardSystem.VipCardUpgradeRule != null)
                            {
                                if (VipCardSystem.VipCardUpgradeRule.IsPurchaseUpgrade == 1)
                                {
                                    VipCardTypeResult.Prices           = VipCardSystem.VipCardUpgradeRule.Prices;
                                    VipCardTypeResult.ExchangeIntegral = VipCardSystem.VipCardUpgradeRule.ExchangeIntegral;
                                    VipCardTypeResult.IsExtraMoney     = VipCardSystem.VipCardUpgradeRule.IsExtraMoney;
                                }
                                else
                                {
                                    VipCardTypeResult.Prices           = 0;
                                    VipCardTypeResult.ExchangeIntegral = 0;
                                    VipCardTypeResult.IsExtraMoney     = 2;
                                }
                            }
                            else
                            {
                                VipCardTypeResult.Prices           = 0;
                                VipCardTypeResult.ExchangeIntegral = 0;
                                VipCardTypeResult.IsExtraMoney     = 2;
                            }
                            //要先生成卡等级数据  不然取不到VipCardTypeID
                            if (VipCardSystem.VipCardType.VipCardLevel != 0)
                            {
                                bllVipCardType.Update(VipCardTypeResult);
                                //修改虚拟商品
#warning 商品代码注释 Bear
                                ItemService _ItemService = new ItemService(loggingSessionInfo);
                                _ItemService.SaveCardToOffenTItem(loggingSessionInfo, VipCardTypeResult);

                                try
                                {
                                    var msg = new EventContract
                                    {
                                        Operation  = OptEnum.Update,
                                        EntityType = EntityTypeEnum.VipCardType,
                                        Id         = VipCardTypeResult.VipCardTypeID.ToString()
                                    };
                                    var eventService = new EventService();
                                    eventService.PublishMsg(msg);
                                }
                                catch (Exception)
                                {
                                    throw new Exception("RabbitMQ Error");
                                }
                            }

                            #endregion
                            #region 编辑卡升级规则信息
                            //编辑卡升级规则
                            if (VipCardSystem.VipCardType.VipCardLevel != 1)
                            {
                                VipCardUpgradeRuleEntity vipCardUpgradeRuleEntity = new VipCardUpgradeRuleEntity();
                                var entityVipCardUpgradeRule = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
                                {
                                    CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = VipCardTypeResult.VipCardTypeID
                                }, null).FirstOrDefault();
                                //entityVipCardUpgradeRule.VipCardUpgradeRuleId = System.Guid.NewGuid();
                                if (entityVipCardUpgradeRule != null)
                                {
                                    if (VipCardTypeResult.Isprepaid == 0) //不可充值
                                    {
                                        //将所有梯度信息 逻辑删除
                                        string[] ProfitRuleIds = bllVipCardProfitRule.GetRechargeProfitRuleByIsPrepaid(loggingSessionInfo.ClientID, VipCardTypeResult.VipCardTypeID);
                                        if (ProfitRuleIds.Length > 0)
                                        {
                                            VipCardReRechargeProfitRuleService.Delete(ProfitRuleIds);
                                        }
                                    }

                                    if (VipCardTypeResult != null && VipCardTypeResult.VipCardTypeID != null)
                                    {
                                        entityVipCardUpgradeRule.VipCardTypeID = VipCardTypeResult.VipCardTypeID;
                                    }
                                    //是否购卡升级
                                    if (VipCardSystem.VipCardUpgradeRule != null)
                                    {
                                        if (VipCardSystem.VipCardUpgradeRule.IsPurchaseUpgrade == 1)
                                        {
                                            entityVipCardUpgradeRule.IsPurchaseUpgrade = 1;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsPurchaseUpgrade = 0;
                                        }
                                        //是否充值升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsRecharge == 1)
                                        {
                                            entityVipCardUpgradeRule.IsRecharge         = 1;
                                            entityVipCardUpgradeRule.OnceRechargeAmount = VipCardSystem.VipCardUpgradeRule.OnceRechargeAmount;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsRecharge         = 0;
                                            entityVipCardUpgradeRule.OnceRechargeAmount = 0;
                                        }
                                        //是否消费升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsBuyUpgrade == 1)
                                        {
                                            entityVipCardUpgradeRule.IsBuyUpgrade  = 1;
                                            entityVipCardUpgradeRule.BuyAmount     = VipCardSystem.VipCardUpgradeRule.BuyAmount;
                                            entityVipCardUpgradeRule.OnceBuyAmount = VipCardSystem.VipCardUpgradeRule.OnceBuyAmount;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsBuyUpgrade  = 0;
                                            entityVipCardUpgradeRule.BuyAmount     = 0;
                                            entityVipCardUpgradeRule.OnceBuyAmount = 0;
                                        }
                                    }
                                    else
                                    {
                                        throw new APIException("升级条件不能为空!")
                                              {
                                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                                              };
                                    }
                                    entityVipCardUpgradeRule.CustomerID = loggingSessionInfo.ClientID;
                                }
                                else
                                {
                                    if (VipCardTypeResult != null && VipCardTypeResult.VipCardTypeID != null)
                                    {
                                        vipCardUpgradeRuleEntity.VipCardTypeID = VipCardTypeResult.VipCardTypeID;
                                    }
                                    //是否购卡升级
                                    if (VipCardSystem.VipCardUpgradeRule != null)
                                    {
                                        if (VipCardSystem.VipCardUpgradeRule.IsPurchaseUpgrade == 1)
                                        {
                                            vipCardUpgradeRuleEntity.IsPurchaseUpgrade = 1;
                                        }
                                        else
                                        {
                                            vipCardUpgradeRuleEntity.IsPurchaseUpgrade = 0;
                                        }
                                        //是否充值升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsRecharge == 1)
                                        {
                                            vipCardUpgradeRuleEntity.IsRecharge         = 1;
                                            vipCardUpgradeRuleEntity.OnceRechargeAmount = VipCardSystem.VipCardUpgradeRule.OnceRechargeAmount;
                                        }
                                        else
                                        {
                                            vipCardUpgradeRuleEntity.IsRecharge         = 0;
                                            vipCardUpgradeRuleEntity.OnceRechargeAmount = 0;
                                        }
                                        //是否消费升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsBuyUpgrade == 1)
                                        {
                                            vipCardUpgradeRuleEntity.IsBuyUpgrade  = 1;
                                            vipCardUpgradeRuleEntity.BuyAmount     = VipCardSystem.VipCardUpgradeRule.BuyAmount;
                                            vipCardUpgradeRuleEntity.OnceBuyAmount = VipCardSystem.VipCardUpgradeRule.OnceBuyAmount;
                                        }
                                        else
                                        {
                                            vipCardUpgradeRuleEntity.IsBuyUpgrade  = 0;
                                            vipCardUpgradeRuleEntity.BuyAmount     = 0;
                                            vipCardUpgradeRuleEntity.OnceBuyAmount = 0;
                                        }
                                    }
                                    else
                                    {
                                        throw new APIException("升级条件不能为空!")
                                              {
                                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                                              };
                                    }
                                    vipCardUpgradeRuleEntity.CustomerID = loggingSessionInfo.ClientID;
                                }

                                //将卡升级规则进行入库
                                if (entityVipCardUpgradeRule != null)
                                {
                                    bllVipCardUpgradeRule.Update(entityVipCardUpgradeRule);
                                }
                                else
                                {
                                    vipCardUpgradeRuleEntity.VipCardUpgradeRuleId = Guid.NewGuid();
                                    bllVipCardUpgradeRule.Create(vipCardUpgradeRuleEntity);
                                }
                            }
                            #endregion
                            #region 编辑基本权益信息
                            //基本权益实体
                            var VipCardRuleInfo = bllVipCardRule.QueryByEntity(new VipCardRuleEntity()
                            {
                                VipCardTypeID = VipCardTypeResult.VipCardTypeID, CustomerID = loggingSessionInfo.ClientID
                            }, null).FirstOrDefault();
                            if (VipCardRuleInfo != null)
                            {
                                if (VipCardSystem.VipCardRule != null)
                                {
                                    VipCardRuleInfo.CardDiscount         = VipCardSystem.VipCardRule.CardDiscount * 10;//因为给的是整数 目前折扣这里乘10
                                    VipCardRuleInfo.PaidGivePoints       = VipCardSystem.VipCardRule.PaidGivePoints;
                                    VipCardRuleInfo.PaidGivePercetPoints = VipCardSystem.VipCardRule.PaidGivePercetPoints;
                                }
                                else
                                {
                                    VipCardRuleInfo.CardDiscount         = 0;//因为给的是整数 目前折扣这里乘10
                                    VipCardRuleInfo.PaidGivePoints       = 0;
                                    VipCardRuleInfo.PaidGivePercetPoints = 0;
                                }
                                //将基本权益数据进行入库
                                bllVipCardRule.Update(VipCardRuleInfo);
                            }
                            else
                            {
                                var vipCardRuleEntity = new VipCardRuleEntity();
                                vipCardRuleEntity.VipCardTypeID        = VipCardTypeResult.VipCardTypeID;
                                vipCardRuleEntity.CardDiscount         = VipCardSystem.VipCardRule.CardDiscount * 10;//因为给的是整数 目前折扣这里乘10
                                vipCardRuleEntity.PaidGivePoints       = VipCardSystem.VipCardRule.PaidGivePoints;
                                vipCardRuleEntity.PaidGivePercetPoints = VipCardSystem.VipCardRule.PaidGivePercetPoints;
                                vipCardRuleEntity.CustomerID           = loggingSessionInfo.ClientID;
                                bllVipCardRule.Create(vipCardRuleEntity);
                            }



                            #endregion
                            #region 编辑开卡礼信息
                            if (VipCardSystem.VipCardUpgradeRewardList != null)
                            {
                                //开卡礼处理
                                foreach (var Rewards in VipCardSystem.VipCardUpgradeRewardList)
                                {
                                    var entityVipCardUpgradeReward = new VipCardUpgradeRewardEntity();
                                    if (VipCardTypeResult != null && VipCardTypeResult.VipCardTypeID != null)
                                    {
                                        entityVipCardUpgradeReward.VipCardTypeID = VipCardTypeResult.VipCardTypeID;
                                    }
                                    entityVipCardUpgradeReward.CouponTypeId = new Guid(Rewards.CouponTypeID);
                                    entityVipCardUpgradeReward.CouponNum    = Rewards.CouponNum;
                                    entityVipCardUpgradeReward.CustomerID   = loggingSessionInfo.ClientID;
                                    if (!string.IsNullOrEmpty(Rewards.CardUpgradeRewardId))
                                    {
                                        entityVipCardUpgradeReward.CardUpgradeRewardId = new Guid(Rewards.CardUpgradeRewardId);
                                    }
                                    switch (Rewards.OperateType) //判断此处为何种操作(0=删除券;1=新增;2=修改;)
                                    {
                                    case 0:                      //删除券
                                        bllVipCardUpgradeReward.Delete(entityVipCardUpgradeReward);
                                        break;

                                    case 1:    //新增券
                                        var existVipCardUpgradeReward = bllVipCardUpgradeReward.QueryByEntity(new VipCardUpgradeRewardEntity()
                                        {
                                            CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = VipCardTypeResult.VipCardTypeID, CouponTypeId = new Guid(Rewards.CouponTypeID)
                                        }, null).FirstOrDefault();
                                        if (existVipCardUpgradeReward != null)
                                        {
                                            existVipCardUpgradeReward.CouponTypeId = new Guid(Rewards.CouponTypeID);
                                            existVipCardUpgradeReward.CouponNum    = Rewards.CouponNum;
                                            existVipCardUpgradeReward.CustomerID   = loggingSessionInfo.ClientID;
                                            bllVipCardUpgradeReward.Update(existVipCardUpgradeReward);
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeReward.CardUpgradeRewardId = Guid.NewGuid();
                                            bllVipCardUpgradeReward.Create(entityVipCardUpgradeReward);
                                        }
                                        break;

                                    case 2:    //修改
                                        bllVipCardUpgradeReward.Update(entityVipCardUpgradeReward);
                                        break;
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region 添加会员卡等级信息
                            //如果卡等级不重复则添加卡等级数据
                            var entityVipCardType = new SysVipCardTypeEntity();
                            entityVipCardType.Category        = 0;
                            entityVipCardType.VipCardTypeName = VipCardSystem.VipCardType.VipCardTypeName;
                            entityVipCardType.VipCardLevel    = VipCardSystem.VipCardType.VipCardLevel;
                            entityVipCardType.IsPassword      = 0;
                            if (entityVipCardType.VipCardLevel == 1)//如果等级为1默认可充值
                            {
                                entityVipCardType.Isprepaid = 1;
                            }
                            else
                            {
                                entityVipCardType.Isprepaid = VipCardSystem.VipCardType.IsPrepaid;
                            }
                            entityVipCardType.IsOnlineSales = VipCardSystem.VipCardType.IsOnlineSales;
                            entityVipCardType.PicUrl        = VipCardSystem.VipCardType.PicUrl;
                            entityVipCardType.CustomerID    = loggingSessionInfo.ClientID;
                            if (VipCardSystem.VipCardUpgradeRule != null)
                            {
                                if (VipCardSystem.VipCardUpgradeRule.IsPurchaseUpgrade == 1)
                                {
                                    entityVipCardType.Prices           = VipCardSystem.VipCardUpgradeRule.Prices;
                                    entityVipCardType.ExchangeIntegral = VipCardSystem.VipCardUpgradeRule.ExchangeIntegral;
                                    entityVipCardType.IsExtraMoney     = VipCardSystem.VipCardUpgradeRule.IsExtraMoney;
                                }
                                else
                                {
                                    entityVipCardType.Prices           = 0;
                                    entityVipCardType.ExchangeIntegral = 0;
                                    entityVipCardType.IsExtraMoney     = 2;
                                }
                            }
                            else
                            {
                                entityVipCardType.Prices           = 0;
                                entityVipCardType.ExchangeIntegral = 0;
                                entityVipCardType.IsExtraMoney     = 2;
                            }
                            var existVipCardType = bllVipCardType.QueryByEntity(new SysVipCardTypeEntity()
                            {
                                Category = 0, VipCardLevel = VipCardSystem.VipCardType.VipCardLevel, CustomerID = loggingSessionInfo.ClientID
                            }, null).FirstOrDefault();
                            if (existVipCardType == null)
                            {
                                //要先生成卡等级数据  不然取不到VipCardTypeID
                                if (VipCardSystem.VipCardType.VipCardLevel != 0 && VipCardSystem.VipCardType.VipCardLevel < 8)
                                {
                                    bllVipCardType.Create(entityVipCardType);
                                    //添加虚拟商品
#warning 商品代码注释 Bear
                                    ItemService _ItemService = new ItemService(loggingSessionInfo);
                                    _ItemService.SaveCardToOffenTItem(loggingSessionInfo, entityVipCardType);

                                    var tmp =
                                        bllVipCardType.QueryByEntity(
                                            new SysVipCardTypeEntity()
                                    {
                                        Category     = 0,
                                        VipCardLevel = entityVipCardType.VipCardLevel,
                                        CustomerID   = loggingSessionInfo.ClientID
                                    }, null).FirstOrDefault();


                                    try
                                    {
                                        var msg = new EventContract
                                        {
                                            Operation  = OptEnum.Create,
                                            EntityType = EntityTypeEnum.VipCardType,
                                            Id         = tmp.VipCardTypeID.ToString()
                                        };
                                        var eventService = new EventService();
                                        eventService.PublishMsg(msg);
                                    }
                                    catch (Exception)
                                    {
                                        throw new Exception("RabbitMQ Error");
                                    }
                                }
                                else
                                {
                                    throw new APIException("等级不能超过七级!")
                                          {
                                              ErrorCode = ERROR_CODES.INVALID_BUSINESS
                                          };
                                }
                            }
                            //else
                            //{
                            //    throw new APIException("会员卡名称不能重复!") { ErrorCode = ERROR_CODES.INVALID_BUSINESS };
                            //}

                            #endregion
                            //因为卡等级不能重复  所以这里根据卡等级获取VipCardTypeID的信息
                            var vipCardTypeInfo = bllVipCardType.QueryByEntity(new SysVipCardTypeEntity()
                            {
                                Category = 0, VipCardLevel = VipCardSystem.VipCardType.VipCardLevel, CustomerID = loggingSessionInfo.ClientID
                            }, null).FirstOrDefault();
                            //因为注册表单暂时没用上 如果卡等级不等于1时 就有升级条件数据存进去
                            if (VipCardSystem.VipCardType.VipCardLevel != 0)
                            {
                                #region 添加卡升级规则信息
                                if (VipCardSystem.VipCardType.VipCardLevel != 1)
                                {
                                    var entityVipCardUpgradeRule = new VipCardUpgradeRuleEntity();
                                    entityVipCardUpgradeRule.VipCardUpgradeRuleId = System.Guid.NewGuid();
                                    if (vipCardTypeInfo != null && vipCardTypeInfo.VipCardTypeID != null)
                                    {
                                        entityVipCardUpgradeRule.VipCardTypeID = vipCardTypeInfo.VipCardTypeID;
                                    }
                                    //是否购卡升级
                                    if (VipCardSystem.VipCardUpgradeRule != null)
                                    {
                                        if (VipCardSystem.VipCardUpgradeRule.IsPurchaseUpgrade == 1)
                                        {
                                            entityVipCardUpgradeRule.IsPurchaseUpgrade = 1;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsPurchaseUpgrade  = 0;
                                            entityVipCardUpgradeRule.OnceRechargeAmount = 0;
                                        }
                                        //是否充值升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsRecharge == 1)
                                        {
                                            entityVipCardUpgradeRule.IsRecharge         = 1;
                                            entityVipCardUpgradeRule.OnceRechargeAmount = VipCardSystem.VipCardUpgradeRule.OnceRechargeAmount;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsRecharge         = 0;
                                            entityVipCardUpgradeRule.OnceRechargeAmount = 0;
                                        }
                                        //是否消费升级
                                        if (VipCardSystem.VipCardUpgradeRule.IsBuyUpgrade == 1)
                                        {
                                            entityVipCardUpgradeRule.IsBuyUpgrade  = 1;
                                            entityVipCardUpgradeRule.BuyAmount     = VipCardSystem.VipCardUpgradeRule.BuyAmount;
                                            entityVipCardUpgradeRule.OnceBuyAmount = VipCardSystem.VipCardUpgradeRule.OnceBuyAmount;
                                        }
                                        else
                                        {
                                            entityVipCardUpgradeRule.IsBuyUpgrade  = 0;
                                            entityVipCardUpgradeRule.BuyAmount     = 0;
                                            entityVipCardUpgradeRule.OnceBuyAmount = 0;
                                        }
                                    }
                                    else
                                    {
                                        throw new APIException("升级条件不能为空!")
                                              {
                                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                                              };
                                    }
                                    entityVipCardUpgradeRule.CustomerID = loggingSessionInfo.ClientID;
                                    //将卡升级规则进行入库
                                    bllVipCardUpgradeRule.Create(entityVipCardUpgradeRule);
                                }
                                #endregion
                                #region 添加基本权益实体
                                //基本权益实体
                                var entityVipCardRule = new VipCardRuleEntity();
                                if (vipCardTypeInfo != null && vipCardTypeInfo.VipCardTypeID != null)
                                {
                                    entityVipCardRule.VipCardTypeID = vipCardTypeInfo.VipCardTypeID;
                                }
                                if (VipCardSystem.VipCardRule != null)
                                {
                                    entityVipCardRule.CardDiscount         = VipCardSystem.VipCardRule.CardDiscount * 10;//因为给的是整数 目前折扣这里乘10
                                    entityVipCardRule.PaidGivePoints       = VipCardSystem.VipCardRule.PaidGivePoints;
                                    entityVipCardRule.PaidGivePercetPoints = VipCardSystem.VipCardRule.PaidGivePercetPoints;
                                }
                                else
                                {
                                    entityVipCardRule.CardDiscount         = 0;//因为给的是整数 目前折扣这里乘10
                                    entityVipCardRule.PaidGivePoints       = 0;
                                    entityVipCardRule.PaidGivePercetPoints = 0;
                                }
                                entityVipCardRule.CustomerID = loggingSessionInfo.ClientID;
                                //将基本权益数据进行入库
                                bllVipCardRule.Create(entityVipCardRule);
                                #endregion
                                #region 添加开卡礼信息
                                if (VipCardSystem.VipCardUpgradeRewardList != null)
                                {
                                    //开卡礼处理
                                    foreach (var Rewards in VipCardSystem.VipCardUpgradeRewardList)
                                    {
                                        var entityVipCardUpgradeReward = new VipCardUpgradeRewardEntity();
                                        entityVipCardUpgradeReward.CardUpgradeRewardId = Guid.NewGuid();
                                        if (vipCardTypeInfo != null && vipCardTypeInfo.VipCardTypeID != null)
                                        {
                                            entityVipCardUpgradeReward.VipCardTypeID = vipCardTypeInfo.VipCardTypeID;
                                        }
                                        entityVipCardUpgradeReward.CouponTypeId = new Guid(Rewards.CouponTypeID);
                                        entityVipCardUpgradeReward.CouponNum    = Rewards.CouponNum;
                                        entityVipCardUpgradeReward.CustomerID   = loggingSessionInfo.ClientID;
                                        var existVipCardUpgradeReward = bllVipCardUpgradeReward.QueryByEntity(new VipCardUpgradeRewardEntity()
                                        {
                                            CustomerID = loggingSessionInfo.ClientID, VipCardTypeID = vipCardTypeInfo.VipCardTypeID, CouponTypeId = new Guid(Rewards.CouponTypeID)
                                        }, null).FirstOrDefault();
                                        if (existVipCardUpgradeReward != null)
                                        {
                                            existVipCardUpgradeReward.CouponTypeId = new Guid(Rewards.CouponTypeID);
                                            existVipCardUpgradeReward.CouponNum    = Rewards.CouponNum;
                                            existVipCardUpgradeReward.CustomerID   = loggingSessionInfo.ClientID;
                                            bllVipCardUpgradeReward.Update(existVipCardUpgradeReward);
                                        }
                                        else
                                        {
                                            bllVipCardUpgradeReward.Create(entityVipCardUpgradeReward);
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                }
                catch (APIException ex)
                {
                    //pTran.Rollback();
                    throw ex;
                }
            }
            return(rd);
        }