/// <summary>
        /// 支付完成时注册绑卡   (目前可用于绑卡列表处的绑卡升级处理)
        /// </summary>
        /// <param name="vipId"></param>
        /// <param name="vipCode"></param>
        /// <param name="unitId"></param>
        /// <param name="ObjecetTypeId">卡类型Id</param>
        /// <param name="OrderType">订单类型 SalesCard=销售 Recharge=充值</param>
        /// <param name="OperationType">操作类型 1-手动 2-自动</param>
        /// <returns></returns>
        public string BindVirtualItem(string vipId, string vipCode, string unitId, int ObjecetTypeId, string OrderType = "SalesCard", int OperationType = 1, string orderId = "")
        {
            var vipCardVipMappingBLL      = new VipCardVipMappingBLL(CurrentUserInfo);
            var sysVipCardTypeBLL         = new SysVipCardTypeBLL(CurrentUserInfo);
            var vipCardBLL                = new VipCardBLL(CurrentUserInfo);
            var vipCardStatusChangeLogBLL = new VipCardStatusChangeLogBLL(CurrentUserInfo);

            string ObjectNo = string.Empty;//卡号或券号

            try
            {
                UnitService unitServer = new UnitService(CurrentUserInfo);
                if (string.IsNullOrEmpty(unitId))
                {
                    unitId = unitServer.GetUnitByUnitTypeForWX("总部", null).Id; //获取总部门店标识
                }
                //根据vipid查询VipCardVipMapping,判断是否有绑卡
                var vipCardMappingInfo = vipCardVipMappingBLL.QueryByEntity(new VipCardVipMappingEntity()
                {
                    VIPID = vipId
                }, null).FirstOrDefault();

                //判断商户是否有付费的会员卡,有付费会员卡时,不自动绑卡
                //List<IWhereCondition> freeCardCon = new List<IWhereCondition> { };
                //freeCardCon.Add(new EqualsCondition() { FieldName = "CustomerID", Value = CurrentUserInfo.ClientID });
                //freeCardCon.Add(new DirectCondition("Prices>0"));
                //var freeCardTypeInfo = sysVipCardTypeBLL.Query(freeCardCon.ToArray(), null).FirstOrDefault();
                //if (freeCardTypeInfo == null)
                //{
                ////查询最低等级的会员卡类型
                //var vipCardTypeInfo = sysVipCardTypeBLL.QueryByEntity(new SysVipCardTypeEntity() { CustomerID = CurrentUserInfo.ClientID, Category = 0 }, new OrderBy[] { new OrderBy() { FieldName = "vipcardlevel", Direction = OrderByDirections.Asc } }).FirstOrDefault();
                //if (vipCardTypeInfo != null)
                //{


                if (vipCardMappingInfo == null)//绑卡
                {
                    //最低等级卡类型
                    var vipCardTypeInfo = sysVipCardTypeBLL.QueryByEntity(new SysVipCardTypeEntity()
                    {
                        CustomerID = CurrentUserInfo.ClientID, Category = 0, VipCardLevel = 1
                    }, new OrderBy[] { new OrderBy()
                                       {
                                           FieldName = "vipcardlevel", Direction = OrderByDirections.Asc
                                       } }).FirstOrDefault();
                    if (vipCardTypeInfo == null)
                    {
                        throw new APIException("系统未创建会员卡类型")
                              {
                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                              };
                    }
                    //查询此类型会员卡是否存在
                    var vipCardInfo = vipCardBLL.QueryByEntity(new VipCardEntity()
                    {
                        VipCardTypeID = vipCardTypeInfo.VipCardTypeID, VipCardStatusId = 0, MembershipUnit = ""
                    }, null).FirstOrDefault();
                    //不存在,制卡
                    if (vipCardInfo == null)
                    {
                        vipCardInfo               = new VipCardEntity();
                        vipCardInfo.VipCardID     = Guid.NewGuid().ToString();
                        vipCardInfo.VipCardTypeID = vipCardTypeInfo.VipCardTypeID;
                        //vipCardInfo.VipCardTypeName = vipCardTypeInfo.VipCardTypeName;
                        vipCardInfo.VipCardCode     = vipCode;
                        vipCardInfo.VipCardStatusId = 1;//正常
                        vipCardInfo.MembershipUnit  = unitId;
                        vipCardInfo.MembershipTime  = DateTime.Now;
                        vipCardInfo.CustomerID      = CurrentUserInfo.ClientID;
                        vipCardBLL.Create(vipCardInfo);
                    }
                    ObjectNo = vipCardInfo.VipCardCode;
                    //绑定会员卡和会员
                    var vipCardVipMappingEntity = new VipCardVipMappingEntity()
                    {
                        MappingID  = Guid.NewGuid().ToString().Replace("-", ""),
                        VIPID      = vipId,
                        VipCardID  = vipCardInfo.VipCardID,
                        CustomerID = CurrentUserInfo.ClientID
                    };
                    vipCardVipMappingBLL.Create(vipCardVipMappingEntity);
                    vipCardMappingInfo = vipCardVipMappingEntity;
                    //新增会员卡操作状态信息
                    var vipCardStatusChangeLogEntity = new VipCardStatusChangeLogEntity()
                    {
                        LogID           = Guid.NewGuid().ToString().Replace("-", ""),
                        VipCardStatusID = vipCardInfo.VipCardStatusId,
                        VipCardID       = vipCardInfo.VipCardID,
                        Action          = "注册",
                        UnitID          = unitId,
                        CustomerID      = CurrentUserInfo.ClientID
                    };
                    vipCardStatusChangeLogBLL.Create(vipCardStatusChangeLogEntity);
                }

                #region 老卡业务处理

                ObjectNo = vipCode;
                //老卡信息获取
                var oldVipCardInfo = vipCardBLL.QueryByEntity(new VipCardEntity()
                {
                    VipCardID = vipCardMappingInfo.VipCardID
                }, null).FirstOrDefault();

                //老卡卡类型ID
                int oldVipCardTypeID = 0;
                if (oldVipCardInfo != null)
                {
                    //获取老卡的类型
                    var oldVipCardType = sysVipCardTypeBLL.GetByID(oldVipCardInfo.VipCardTypeID);
                    //获取即将升级卡的类型
                    var newVipCardType = sysVipCardTypeBLL.GetByID(ObjecetTypeId);
                    if (newVipCardType.VipCardLevel > oldVipCardType.VipCardLevel)
                    {
                        //老卡卡类型ID
                        oldVipCardTypeID = oldVipCardInfo.VipCardTypeID ?? 0;
                        //更新卡信息
                        oldVipCardInfo.VipCardTypeID = ObjecetTypeId;
                        vipCardBLL.Update(oldVipCardInfo);
                        //老卡操作状态信息
                        var oldVipCardStatusChangeLogEntity = new VipCardStatusChangeLogEntity()
                        {
                            LogID           = Guid.NewGuid().ToString().Replace("-", ""),
                            VipCardStatusID = oldVipCardInfo.VipCardStatusId,
                            VipCardID       = oldVipCardInfo.VipCardID,
                            Action          = "升级处理",
                            UnitID          = unitId,
                            CustomerID      = CurrentUserInfo.ClientID
                        };
                        vipCardStatusChangeLogBLL.Create(oldVipCardStatusChangeLogEntity);

                        //入升级日志
                        var vipCardGradeChangeLogBll = new VipCardGradeChangeLogBLL(CurrentUserInfo);
                        var vipCardUpgradeRuleBll    = new VipCardUpgradeRuleBLL(CurrentUserInfo);
                        var vipCardUpgradeRuleEntity = vipCardUpgradeRuleBll.QueryByEntity(new VipCardUpgradeRuleEntity()
                        {
                            CustomerID = CurrentUserInfo.ClientID, VipCardTypeID = ObjecetTypeId
                        }, null).FirstOrDefault();
                        var VipCardGradeChangeLogEntity = new VipCardGradeChangeLogEntity()
                        {
                            ChangeLogID           = Guid.NewGuid().ToString().Replace("-", ""),
                            VipCardUpgradeRuleId  = vipCardUpgradeRuleEntity.VipCardUpgradeRuleId,
                            OrderType             = OrderType,
                            VipCardID             = oldVipCardInfo.VipCardID,
                            ChangeBeforeVipCardID = oldVipCardInfo.VipCardID,
                            ChangeBeforeGradeID   = oldVipCardTypeID,
                            NowGradeID            = oldVipCardInfo.VipCardTypeID,
                            ChangeReason          = "upgrade",
                            OperationType         = OperationType,
                            ChangeTime            = DateTime.Now,
                            UnitID     = unitId,
                            OrderId    = orderId,
                            CustomerID = CurrentUserInfo.ClientID,
                        };
                        vipCardGradeChangeLogBll.Create(VipCardGradeChangeLogEntity);

                        //开卡礼
                        var vipCardUpgradeRewardBll        = new VipCardUpgradeRewardBLL(CurrentUserInfo);
                        var vipCardUpgradeRewardEntityList = vipCardUpgradeRewardBll.QueryByEntity(new VipCardUpgradeRewardEntity()
                        {
                            VipCardTypeID = ObjecetTypeId
                        }, null);
                        var redisVipMappingCouponBLL = new JIT.CPOS.BS.BLL.RedisOperationBLL.Coupon.RedisVipMappingCouponBLL();
                        if (vipCardUpgradeRewardEntityList.Length > 0)
                        {
                            foreach (var vipCardUpgradeRewardEntity in vipCardUpgradeRewardEntityList)
                            {
                                for (int i = 0; i < vipCardUpgradeRewardEntity.CouponNum; i++)
                                {
                                    redisVipMappingCouponBLL.SetVipMappingCoupon(new CC_Coupon()
                                    {
                                        CustomerId   = CurrentUserInfo.ClientID,
                                        CouponTypeId = vipCardUpgradeRewardEntity.CouponTypeId.ToString()
                                    }, vipCardUpgradeRewardEntity.CardUpgradeRewardId.ToString(), vipId, "OpenVipCard");
                                }
                            }
                        }
                        //消费升级
                        if (OperationType == 2)
                        {
                            //群发消息表
                            var InnerGroupNewsBll    = new InnerGroupNewsBLL(CurrentUserInfo);
                            var innerGroupNewsEntity = new InnerGroupNewsEntity()
                            {
                                GroupNewsId        = Guid.NewGuid().ToString().Replace("-", ""),
                                SentType           = 2,
                                NoticePlatformType = 1,
                                BusType            = 3,
                                Title      = "升级提醒",
                                Text       = "升级成功",
                                CustomerID = CurrentUserInfo.ClientID
                            };
                            InnerGroupNewsBll.Create(innerGroupNewsEntity);
                            //消息和用户关系表
                            var newsUserMappingBll    = new NewsUserMappingBLL(CurrentUserInfo);
                            var newsUserMappingEntity = new NewsUserMappingEntity()
                            {
                                MappingID   = Guid.NewGuid().ToString().Replace("-", ""),
                                UserID      = vipId,
                                GroupNewsID = innerGroupNewsEntity.GroupNewsId,
                                CustomerId  = CurrentUserInfo.ClientID,
                                HasRead     = 0
                            };
                            newsUserMappingBll.Create(newsUserMappingEntity);
                        }
                    }
                }
                #endregion
            }
            catch { throw new APIException("升级失败!")
                          {
                              ErrorCode = ERROR_CODES.INVALID_BUSINESS
                          }; };

            //记录会员信息改变,并且把旧的会员的标识也放在契约里
            var eventService = new EventService();
            var vipMsg       = new EventContract
            {
                Operation  = OptEnum.Update,
                EntityType = EntityTypeEnum.Vip,
                Id         = vipId,
            };
            eventService.PublishMsg(vipMsg);

            return(ObjectNo);
        }
Esempio n. 2
0
        /// <summary>
        /// 计算充值分润
        /// </summary>
        /// <param name="loggingSessionInfo"></param>
        /// <param name="rechargeOrderInfo"></param>
        public void CalculateRechargeOrder(LoggingSessionInfo loggingSessionInfo, RechargeOrderEntity rechargeOrderInfo)
        {
            VipCardProfitRuleBLL bllVipCardProfitRule = new VipCardProfitRuleBLL(loggingSessionInfo);

            VipCardProfitRuleEntity[] entityVipCardProfitRule = null;

            entityVipCardProfitRule = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
            {
                VipCardTypeID = rechargeOrderInfo.VipCardTypeId, IsDelete = 0
            }, null);
            if (entityVipCardProfitRule != null)
            {
                var                bllVipCardGradeChangeLog = new VipCardGradeChangeLogBLL(loggingSessionInfo);
                VipAmountBLL       bllVipAmount             = new VipAmountBLL(loggingSessionInfo);
                VipAmountDetailBLL bllVipAmountDetail       = new VipAmountDetailBLL(loggingSessionInfo);
                VipAmountEntity    entityVipAmount          = new VipAmountEntity();

                string connString = string.Empty;
                foreach (var ProfitRule in entityVipCardProfitRule)
                {
                    decimal amount            = 0;
                    string  strAmountSourceId = string.Empty;
                    string  strVipId          = string.Empty;
                    string  strUserType       = string.Empty;
                    if (ProfitRule.IsApplyAllUnits == 0)
                    {
                        VipCardProfitRuleUnitMappingBLL bllVipCardProfitRuleUnitMapping = new VipCardProfitRuleUnitMappingBLL(loggingSessionInfo);
                        var vipCardProfitRuleUnitMapping = bllVipCardProfitRuleUnitMapping.QueryByEntity(new VipCardProfitRuleUnitMappingEntity()
                        {
                            CardBuyToProfitRuleId = ProfitRule.CardBuyToProfitRuleId, UnitID = rechargeOrderInfo.UnitId, IsDelete = 0, CustomerID = loggingSessionInfo.ClientID
                        }, null).SingleOrDefault();
                        if (vipCardProfitRuleUnitMapping == null)
                        {
                            continue;
                        }
                    }
                    if (ProfitRule.ProfitOwner == "Employee")
                    {
                        strAmountSourceId = "38";
                        strVipId          = rechargeOrderInfo.UserId;
                        strUserType       = "User";
                    }
                    if (ProfitRule.ProfitOwner == "Unit")
                    {
                        strAmountSourceId = "42";
                        strVipId          = rechargeOrderInfo.UnitId;
                        strUserType       = "Unit";
                    }
                    var entityVipCardGradeChangeLog = bllVipCardGradeChangeLog.QueryByEntity(new VipCardGradeChangeLogEntity()
                    {
                        OrderId = rechargeOrderInfo.OrderID.ToString()
                    }, null).SingleOrDefault();
                    if (entityVipCardGradeChangeLog != null)//首充
                    {
                        amount = (decimal)ProfitRule.FirstRechargeProfitPct * (decimal)rechargeOrderInfo.ActuallyPaid * (decimal)0.01;
                    }
                    else//续充
                    {
                        VipCardReRechargeProfitRuleBLL bllVipCardReRechargeProfitRule = new VipCardReRechargeProfitRuleBLL(loggingSessionInfo);
                        var entityVipCardReRechargeProfitRule = bllVipCardReRechargeProfitRule.QueryByEntity(new VipCardReRechargeProfitRuleEntity()
                        {
                            VipCardTypeID = rechargeOrderInfo.VipCardTypeId, IsDelete = 0
                        }, null);
                        if (entityVipCardReRechargeProfitRule != null)
                        {
                            decimal discount = 0;
                            foreach (var ReRechargeProfitRule in entityVipCardReRechargeProfitRule)
                            {
                                if (ReRechargeProfitRule.ProfitType == "Superposition")
                                {
                                    discount = ((decimal)rechargeOrderInfo.ActuallyPaid / (decimal)ReRechargeProfitRule.LimitAmount) * (decimal)ReRechargeProfitRule.ProfitPct * (decimal)0.01;
                                }
                                if (ReRechargeProfitRule.ProfitType == "Step")
                                {
                                    if (rechargeOrderInfo.ActuallyPaid >= ReRechargeProfitRule.LimitAmount)
                                    {
                                        discount = (decimal)ReRechargeProfitRule.ProfitPct;
                                    }
                                }
                            }
                            amount = (decimal)rechargeOrderInfo.ActuallyPaid * discount;
                        }
                    }
                    //入库
                    if (amount > 0)
                    {
                        IDbTransaction tran = new DataAccess.Base.TransactionHelper(loggingSessionInfo).CreateTransaction();

                        VipAmountDetailEntity 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          = rechargeOrderInfo.OrderID.ToString(),
                            CustomerID        = loggingSessionInfo.ClientID,
                            Reason            = "超级分销商"
                        };
                        bllVipAmountDetail.Create(entityVipAmountDetail, (SqlTransaction)tran);

                        T_SplitProfitRecordBLL    bllSplitProfitRecord    = new T_SplitProfitRecordBLL(loggingSessionInfo);
                        T_SplitProfitRecordEntity entitySplitProfitRecord = new T_SplitProfitRecordEntity()
                        {
                            ID          = Guid.NewGuid().ToString(),
                            SourceType  = "Amount",
                            SourceId    = strAmountSourceId,
                            ObjectId    = rechargeOrderInfo.OrderID.ToString(),
                            UserType    = strUserType,
                            UserId      = rechargeOrderInfo.UserId,
                            SplitAmount = amount,
                            SplitSattus = "10",
                            CustomerID  = loggingSessionInfo.ClientID,
                            IsDelete    = 0
                        };
                        bllSplitProfitRecord.Create(entitySplitProfitRecord);

                        entityVipAmount = bllVipAmount.QueryByEntity(new VipAmountEntity()
                        {
                            VipId = strVipId, IsDelete = 0, CustomerID = loggingSessionInfo.ClientID
                        }, null).SingleOrDefault();
                        if (entityVipAmount == null)
                        {
                            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);
                        }
                    }
                }
            }
        }