Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// 计算销售会员卡分润
        /// </summary>
        /// <param name="loggingSessionInfo"></param>
        /// <param name="orderInfo"></param>
        public void CalculateSalesVipCardOrder(LoggingSessionInfo loggingSessionInfo, T_InoutEntity orderInfo)
        {
            if (orderInfo != null)
            {
                if (orderInfo.Field17 != null && orderInfo.Field18 != null && orderInfo.Field17.Length > 0 && orderInfo.Field18.Length > 0)
                {
                    VipBLL bllVip = new VipBLL(loggingSessionInfo);

                    T_Inout_DetailBLL           bllInoutDetail            = new T_Inout_DetailBLL(loggingSessionInfo);
                    T_VirtualItemTypeSettingBLL bllVirtualItemTypeSetting = new T_VirtualItemTypeSettingBLL(loggingSessionInfo);

                    var entityInoutDetail = bllInoutDetail.QueryByEntity(new T_Inout_DetailEntity()
                    {
                        order_id = orderInfo.order_id
                    }, null).FirstOrDefault();
                    var vipCardType = bllVirtualItemTypeSetting.QueryByEntity(new T_VirtualItemTypeSettingEntity()
                    {
                        SkuId = entityInoutDetail.sku_id, IsDelete = 0
                    }, null).FirstOrDefault();
                    if (vipCardType != null)
                    {
                        VipCardUpgradeRuleBLL bllVipCardUpgradeRule = new VipCardUpgradeRuleBLL(loggingSessionInfo);

                        int intVipCardTypeID         = Convert.ToInt32(vipCardType.ObjecetTypeId);
                        var entityVipCardUpgradeRule = bllVipCardUpgradeRule.QueryByEntity(new VipCardUpgradeRuleEntity()
                        {
                            VipCardTypeID = intVipCardTypeID, IsPurchaseUpgrade = 1, IsDelete = 0
                        }, null).SingleOrDefault();
                        #region
                        //if (entityVipCardUpgradeRule != null)
                        //{
                        //    VipCardGradeChangeLogEntity entityVipCardGradeChangeLog = new VipCardGradeChangeLogEntity();
                        //    VipCardStatusChangeLogEntity entityVipCardStatusChangeLog = new VipCardStatusChangeLogEntity();

                        //    VipCardGradeChangeLogBLL bllVipCardGradeChangeLog = new VipCardGradeChangeLogBLL(loggingSessionInfo);
                        //    VipCardStatusChangeLogBLL bllVipCardStatusChangeLog = new VipCardStatusChangeLogBLL(loggingSessionInfo);
                        //    VipCardVipMappingBLL vipCardVipMappingBLL = new VipCardVipMappingBLL(loggingSessionInfo);

                        //    //会员等级改变以及如日志
                        //    DataSet dsVipInfo = bllVip.GetVipCardLevel(orderInfo.vip_no, loggingSessionInfo.ClientID);
                        //    if(dsVipInfo.Tables.Count>0 && dsVipInfo.Tables[0].Rows.Count>0)
                        //    {
                        //        //会员升级
                        //        vipCardVipMappingBLL.BindVirtualItem(orderInfo.vip_no, orderInfo.VipCardCode, "", intVipCardTypeID);
                        //        //日志
                        //        entityVipCardGradeChangeLog = new VipCardGradeChangeLogEntity()
                        //        {
                        //            ChangeLogID=Guid.NewGuid().ToString(),
                        //            VipCardUpgradeRuleId=entityVipCardUpgradeRule.VipCardUpgradeRuleId,
                        //            OrderType = "SalesCard",
                        //            OrderId=orderInfo.order_id,
                        //            VipCardID = dsVipInfo.Tables[0].Rows[0]["VipCardID"].ToString(),
                        //            ChangeBeforeVipCardID = dsVipInfo.Tables[0].Rows[0]["VipCardID"].ToString(),
                        //            ChangeBeforeGradeID = Convert.ToInt32(dsVipInfo.Tables[0].Rows[0]["VipCardTypeID"].ToString()),
                        //            NowGradeID = intVipCardTypeID,
                        //            ChangeReason="Upgrade",
                        //            OperationType = 2,
                        //            ChangeTime=DateTime.Now,
                        //            UnitID=orderInfo.sales_unit_id,
                        //            OperationUserID=orderInfo.sales_user
                        //        };
                        //        bllVipCardGradeChangeLog.Create(entityVipCardGradeChangeLog);
                        //    }
                        //}
                        #endregion
                        //计算分润
                        VipCardProfitRuleBLL bllVipCardProfitRule = new VipCardProfitRuleBLL(loggingSessionInfo);
                        var entityVipCardProfitRule = bllVipCardProfitRule.QueryByEntity(new VipCardProfitRuleEntity()
                        {
                            VipCardTypeID = intVipCardTypeID, IsDelete = 0
                        }, null);
                        if (entityVipCardProfitRule != null)
                        {
                            VipAmountBLL       bllVipAmount       = new VipAmountBLL(loggingSessionInfo);
                            VipAmountDetailBLL bllVipAmountDetail = new VipAmountDetailBLL(loggingSessionInfo);
                            VipAmountEntity    entityVipAmount    = new VipAmountEntity();

                            foreach (var ProfitRule in entityVipCardProfitRule)
                            {
                                decimal amount            = 0;
                                string  strAmountSourceId = string.Empty;
                                string  strVipId          = string.Empty;

                                if (ProfitRule.ProfitOwner == "Employee")
                                {
                                    strAmountSourceId = "37";
                                    strVipId          = orderInfo.sales_user;
                                }
                                if (ProfitRule.ProfitOwner == "Unit")
                                {
                                    strAmountSourceId = "40";
                                    strVipId          = orderInfo.sales_unit_id;
                                }
                                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)
                                    {
                                        amount = (decimal)ProfitRule.FirstCardSalesProfitPct * (decimal)orderInfo.actual_amount * (decimal)0.01;
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }

                                amount = (decimal)ProfitRule.FirstCardSalesProfitPct * (decimal)orderInfo.actual_amount * (decimal)0.01;
                                if (amount > 0)
                                {
                                    IDbTransaction tran = new JIT.CPOS.BS.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          = orderInfo.order_id,
                                        CustomerID        = loggingSessionInfo.ClientID,
                                        Reason            = "超级分销商"
                                    };
                                    bllVipAmountDetail.Create(entityVipAmountDetail, (SqlTransaction)tran);

                                    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);
                                }
                            }
                        }
                    }
                }
            }
        }