Beispiel #1
0
        /// <summary>
        /// 支付回调/收款处理虚拟商品订单
        /// </summary>
        public void SetVirtualItem(LoggingSessionInfo loggingSessionInfo, string orderId)
        {
            var inoutBLL  = new T_InoutBLL(loggingSessionInfo);
            var inoutInfo = this._currentDAO.GetByID(orderId);

            if (inoutInfo != null)
            {
                //如果是经销商订单,付款完成后,订单状态修改成完成状态
                if (inoutInfo.data_from_id == "21")
                {
                    inoutInfo.Field7 = "700";
                    inoutInfo.status = "700";
                    inoutBLL.Update(inoutInfo);
                    InoutService inoutService = new InoutService(loggingSessionInfo);
                    T_VirtualItemTypeSettingBLL virtualItemTypeSettingBLL = new T_VirtualItemTypeSettingBLL(loggingSessionInfo);
                    VipCardVipMappingBLL        vipCardVipMappingBLL      = new VipCardVipMappingBLL(loggingSessionInfo);
                    T_Inout_DetailBLL           inoutDetailBLL            = new T_Inout_DetailBLL(loggingSessionInfo);
                    var    VipBLL      = new VipBLL(loggingSessionInfo);
                    var    inoutDetail = inoutService.GetInoutDetailInfoByOrderId(orderId).FirstOrDefault();
                    string itemId      = inoutDetail.item_id;

                    var virtualItemTypeSettingInfo = virtualItemTypeSettingBLL.QueryByEntity(new T_VirtualItemTypeSettingEntity()
                    {
                        ItemId = itemId
                    }, null).FirstOrDefault();
                    if (virtualItemTypeSettingInfo != null)
                    {
                        int    objectTypeId = int.Parse(virtualItemTypeSettingInfo.ObjecetTypeId);
                        string objectNo     = vipCardVipMappingBLL.BindVirtualItem(inoutInfo.vip_no, inoutInfo.VipCardCode, "", objectTypeId);
                        //将卡/券的编号保存到订单明细
                        T_Inout_DetailEntity inoutDetailEntity = inoutDetailBLL.GetByID(inoutDetail.order_detail_id);
                        if (inoutDetailEntity != null)
                        {
                            inoutDetailEntity.Field10 = objectNo;
                            inoutDetailBLL.Update(inoutDetailEntity);
                        }
                    }
                    //将Col48至为1
                    var VipData = VipBLL.GetByID(inoutInfo.vip_no);
                    if (VipData != null)
                    {
                        VipData.Col48 = "1";
                        VipBLL.Update(VipData);
                    }
                    // 判断客户是否是符合潜在经销商条件
                    var isCan = VipBLL.IsSetVipDealer(inoutInfo.vip_no);

                    if (isCan)
                    {
                        new RetailTraderBLL(loggingSessionInfo).CreatePrepRetailTrader(loggingSessionInfo, inoutInfo.vip_no); // 创建潜在经销商
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 获取会员折扣
        /// </summary>
        /// <returns></returns>
        public decimal GetVipDiscount()
        {
            decimal vipDiscount = 10;    //会员折扣
            //判断是否启用会员折扣
            var basicSettingBLL = new CustomerBasicSettingBLL(CurrentUserInfo);
            var vipBLL          = new VipBLL(CurrentUserInfo);
            //var sysVipCardGradeBLL = new SysVipCardGradeBLL(CurrentUserInfo);
            var vipCardVipMappingBLL = new VipCardVipMappingBLL(CurrentUserInfo);
            var vipCardBLL           = new VipCardBLL(CurrentUserInfo);
            var vipCardRuleBLL       = new VipCardRuleBLL(CurrentUserInfo);
            //string vipDiscountSetting = basicSettingBLL.GetSettingValueByCode("VipCardGradeSalesPreferentia");
            //if (!string.IsNullOrEmpty(vipDiscountSetting))
            //{
            //    if (int.Parse(vipDiscountSetting) > 0)//启用
            //    {
            //        var vipInfo = vipBLL.GetByID(CurrentUserInfo.UserID);
            //        if (vipInfo != null)
            //        {
            //            if (vipInfo.VipLevel>0)
            //            {
            //                //获取会员等级折扣
            //                var sysVipCardGradeInfo = sysVipCardGradeBLL.GetByID(vipInfo.VipLevel);
            //                if (sysVipCardGradeInfo != null)
            //                    vipDiscount = sysVipCardGradeInfo.SalesPreferentiaAmount.Value;
            //            }
            //        }
            //    }
            //}
            var vipCardMappingInfo = vipCardVipMappingBLL.QueryByEntity(new VipCardVipMappingEntity()
            {
                VIPID = CurrentUserInfo.UserID
            }, null).FirstOrDefault();

            if (vipCardMappingInfo != null)
            {
                var vipCardInfo = vipCardBLL.QueryByEntity(new VipCardEntity()
                {
                    VipCardID = vipCardMappingInfo.VipCardID
                }, null).FirstOrDefault();
                if (vipCardInfo != null)
                {
                    var vipCardRuleInfo = vipCardRuleBLL.QueryByEntity(new VipCardRuleEntity()
                    {
                        VipCardTypeID = vipCardInfo.VipCardTypeID
                    }, null).FirstOrDefault();
                    if (vipCardRuleInfo != null && vipCardRuleInfo.CardDiscount > 0)
                    {
                        vipDiscount = vipCardRuleInfo.CardDiscount == null ? 10 : (vipCardRuleInfo.CardDiscount.Value / 10);
                    }
                }
            }
            return(vipDiscount);
        }
Beispiel #3
0
        /// <summary>
        /// 保存会员卡信息
        /// </summary>
        /// <param name="vipCardID">会员卡ID</param>
        /// <param name="vipCardEntity">会员卡实体</param>
        /// <returns></returns>
        public bool SaveVipCardInfo(string vipCardID, VipCardEntity vipCardEntity)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    if (vipCardID.Trim().Length == 0)
                    {
                        var basicServer = new VipCardBasicBLL(this.CurrentUserInfo);
                        //新增
                        vipCardEntity.VipCardID           = Utils.NewGuid();
                        vipCardEntity.VipCardCode         = basicServer.GetVipCardCode(vipCardEntity.UnitID);
                        vipCardEntity.MembershipTime      = Convert.ToDateTime(vipCardEntity.BeginDate);
                        vipCardEntity.TotalAmount         = 0;
                        vipCardEntity.PurchaseTotalAmount = 0;
                        vipCardEntity.PurchaseTotalCount  = 0;
                        vipCardEntity.CustomerID          = CurrentUserInfo.CurrentUser.customer_id;
                        this._currentDAO.Create(vipCardEntity);

                        //添加会员卡与VIP关系
                        var vipMapping       = new VipCardVipMappingBLL(this.CurrentUserInfo);
                        var vipMappingEntity = new VipCardVipMappingEntity()
                        {
                            MappingID = Utils.NewGuid(),
                            VipCardID = vipCardEntity.VipCardID,
                            VIPID     = vipCardEntity.VipId
                        };
                        vipMapping.Create(vipMappingEntity);
                    }
                    else
                    {
                        //修改
                        vipCardEntity.VipCardID = vipCardID;
                        this._currentDAO.Update(vipCardEntity);
                    }

                    var mappingServer = new VipCardUnitMappingBLL(this.CurrentUserInfo);
                    var mappingEntity = new VipCardUnitMappingEntity()
                    {
                        VipCardID = vipCardEntity.VipCardID,
                        UnitID    = vipCardEntity.UnitID
                    };
                    var mappingEntitys = mappingServer.QueryByEntity(mappingEntity, null);

                    //根据会员卡ID删除会员卡与门店关系表
                    this._currentDAO.DeteleVipCardUnitMapping(vipCardEntity.VipCardID);

                    if (mappingEntitys != null && mappingEntitys.Length > 0)
                    {
                        //更新会员卡与门店关系表
                        this._currentDAO.UpdateVipCardUnitMapping(vipCardEntity.VipCardID, vipCardEntity.UnitID);
                    }
                    else
                    {
                        //新增会员卡与门店关系表
                        mappingEntity.MappingID = Utils.NewGuid();
                        mappingServer.Create(mappingEntity);
                    }

                    scope.Complete();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public void updateVipCardByType(string vipId, int vipCardTypeId, string changeReason, string remark, string vipCode, IDbTransaction pTran)
        {
            var vipCardVipMappingBLL      = new VipCardVipMappingBLL(CurrentUserInfo);
            var sysVipCardTypeBLL         = new SysVipCardTypeBLL(CurrentUserInfo);
            var vipCardBLL                = new VipCardBLL(CurrentUserInfo);
            var vipCardStatusChangeLogBLL = new VipCardStatusChangeLogBLL(CurrentUserInfo);

            UnitService unitServer = new UnitService(CurrentUserInfo);
            string      unitId     = unitServer.GetUnitByUnitTypeForWX("总部", null).Id; //获取总部门店标识

            VipCardEntity vipCardInfo = vipCardBLL.GetVipCardByVipMapping(vipId);

            //更新卡信息
            vipCardInfo.VipCardTypeID = vipCardTypeId;
            vipCardBLL.Update(vipCardInfo, pTran);

            //var vipCardInfo = vipCardBLL.QueryByEntity(new VipCardEntity() { VipCardTypeID = vipCardTypeId, VipCardStatusId = 0, MembershipUnit = "" }, null).FirstOrDefault();

            //不存在,制卡
            //if (vipCardInfo == null)
            //{
            //    vipCardInfo = new VipCardEntity();
            //    vipCardInfo.VipCardID = Guid.NewGuid().ToString();
            //    vipCardInfo.VipCardTypeID = vipCardTypeId;
            //    vipCardInfo.VipCardTypeName = vipCardTypeName;
            //    vipCardInfo.VipCardCode = vipCode;
            //    vipCardInfo.VipCardStatusId = 1;//正常
            //    vipCardInfo.MembershipUnit = unitId;
            //    vipCardInfo.MembershipTime = DateTime.Now;
            //    vipCardInfo.CustomerID = CurrentUserInfo.ClientID;
            //    vipCardBLL.Create(vipCardInfo, pTran);
            //}

            //新增会员卡操作状态信息
            var vipCardStatusChangeLogEntity = new VipCardStatusChangeLogEntity()
            {
                LogID           = Guid.NewGuid().ToString().Replace("-", ""),
                VipCardStatusID = vipCardInfo.VipCardStatusId,
                VipCardID       = vipCardInfo.VipCardID,
                Action          = "升级",
                Reason          = changeReason,
                Remark          = remark,
                UnitID          = unitId,
                CustomerID      = CurrentUserInfo.ClientID
            };

            vipCardStatusChangeLogBLL.Create(vipCardStatusChangeLogEntity, pTran);


            var vipCardVipMappingEntity = vipCardVipMappingBLL.QueryByEntity(new VipCardVipMappingEntity()
            {
                VipCardID = vipCardInfo.VipCardID
            }, null).FirstOrDefault();

            vipCardVipMappingEntity.LastUpdateTime = DateTime.Now;

            vipCardVipMappingBLL.Update(vipCardVipMappingEntity, pTran);
            //绑定会员卡和会员
            //var vipCardVipMappingEntity = new VipCardVipMappingEntity()
            //{
            //    MappingID = Guid.NewGuid().ToString().Replace("-", ""),
            //    VIPID = vipId,
            //    VipCardID = vipCardInfo.VipCardID,
            //    CustomerID = CurrentUserInfo.ClientID
            //};
            //vipCardVipMappingBLL.Create(vipCardVipMappingEntity, pTran);

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

            eventService.PublishMsg(vipMsg);
        }
        /// <summary>
        /// 会员微信注册绑卡
        /// </summary>
        /// <param name="vipId"></param>
        /// <param name="vipCode"></param>
        /// <param name="unitId">会籍店</param>
        public void BindVipCard(string vipId, string vipCode, string unitId)
        {
            var vipCardVipMappingBLL      = new VipCardVipMappingBLL(CurrentUserInfo);
            var sysVipCardTypeBLL         = new SysVipCardTypeBLL(CurrentUserInfo);
            var vipCardBLL                = new VipCardBLL(CurrentUserInfo);
            var vipCardStatusChangeLogBLL = new VipCardStatusChangeLogBLL(CurrentUserInfo);

            UnitService unitServer = new UnitService(CurrentUserInfo);

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

            if (vipCardMappingInfo == null)
            {
                //判断商户是否有付费的会员卡,有付费会员卡时,不自动绑卡
                List <IWhereCondition> freeCardCon = new List <IWhereCondition> {
                };
                freeCardCon.Add(new EqualsCondition()
                {
                    FieldName = "CustomerID", Value = CurrentUserInfo.ClientID
                });
                freeCardCon.Add(new DirectCondition("VipCardLevel=1"));
                var orderBys = new OrderBy[1];
                orderBys[0] = new OrderBy()
                {
                    FieldName = "VipCardLevel", Direction = OrderByDirections.Asc
                };
                var freeCardTypeInfo = sysVipCardTypeBLL.Query(freeCardCon.ToArray(), orderBys).FirstOrDefault();
                if (freeCardTypeInfo != 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)
                    {
                        //查询此类型会员卡是否存在
                        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);
                        }
                        else//存在更新成现在的
                        {
                            vipCardInfo.VipCardStatusId = 1;//正常
                            vipCardInfo.MembershipUnit  = unitId;
                            vipCardInfo.MembershipTime  = DateTime.Now;
                            vipCardInfo.VipCardTypeID   = vipCardTypeInfo.VipCardTypeID;
                            vipCardBLL.Update(vipCardInfo);
                            //存在导入的卡时,更新当前会员的VipCode
                            var vipbll = new VipBLL(CurrentUserInfo);
                            List <IWhereCondition> wheres = new List <IWhereCondition>();
                            wheres.Add(new MoreThanCondition()
                            {
                                FieldName = "status", Value = 0
                            });
                            wheres.Add(new EqualsCondition()
                            {
                                FieldName = "vipid", Value = vipId
                            });
                            wheres.Add(new EqualsCondition()
                            {
                                FieldName = "clientid", Value = CurrentUserInfo.ClientID
                            });
                            var VipInfo = vipbll.Query(wheres.ToArray(), null).FirstOrDefault();
                            if (VipInfo != null && !string.IsNullOrEmpty(VipInfo.VIPID))
                            {
                                VipInfo.VipCode = vipCardInfo.VipCardCode;
                                //更新VipCode信息
                                vipbll.Update(VipInfo);
                            }
                        }
                        //新增会员卡操作状态信息
                        var vipCardStatusChangeLogEntity = new VipCardStatusChangeLogEntity()
                        {
                            LogID           = Guid.NewGuid().ToString().Replace("-", ""),
                            VipCardStatusID = vipCardInfo.VipCardStatusId,
                            VipCardID       = vipCardInfo.VipCardID,
                            Action          = "注册",
                            UnitID          = unitId,
                            CustomerID      = CurrentUserInfo.ClientID
                        };
                        vipCardStatusChangeLogBLL.Create(vipCardStatusChangeLogEntity);

                        //绑定会员卡和会员
                        var vipCardVipMappingEntity = new VipCardVipMappingEntity()
                        {
                            MappingID  = Guid.NewGuid().ToString().Replace("-", ""),
                            VIPID      = vipId,
                            VipCardID  = vipCardInfo.VipCardID,
                            CustomerID = CurrentUserInfo.ClientID
                        };
                        vipCardVipMappingBLL.Create(vipCardVipMappingEntity);
                        //开卡礼
                        var vipCardUpgradeRewardBll        = new VipCardUpgradeRewardBLL(CurrentUserInfo);
                        var vipCardUpgradeRewardEntityList = vipCardUpgradeRewardBll.QueryByEntity(new VipCardUpgradeRewardEntity()
                        {
                            VipCardTypeID = vipCardTypeInfo.VipCardTypeID
                        }, 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");
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new APIException("系统未创建会员卡类型")
                              {
                                  ErrorCode = ERROR_CODES.INVALID_BUSINESS
                              }
                    };
                }
            }
            else //更新旧卡 需要确认旧卡是否为卡体系内的卡
            {
                var oldVipCardInfo = vipCardBLL.QueryByEntity(new VipCardEntity()
                {
                    VipCardID = vipCardMappingInfo.VipCardID, CustomerID = CurrentUserInfo.ClientID
                }, null).FirstOrDefault();
                int oldVipCardTypeID = oldVipCardInfo.VipCardTypeID ?? 0;
                //查询最低等级的会员卡类型
                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
                          };
                }
                if (oldVipCardInfo != null)
                {
                    //旧卡不为空时,确认当前VipCardTypeID是否属于卡体系
                    var curvipCardTypeInfo = sysVipCardTypeBLL.QueryByEntity(new SysVipCardTypeEntity()
                    {
                        CustomerID = CurrentUserInfo.ClientID, Category = 0, VipCardTypeID = oldVipCardInfo.VipCardTypeID
                    }, new OrderBy[] { new OrderBy()
                                       {
                                           FieldName = "VipCardTypeID", Direction = OrderByDirections.Asc
                                       } }).FirstOrDefault();
                    if (curvipCardTypeInfo == null)         //若为空  则更新为当前信息
                    {
                        oldVipCardInfo.VipCardStatusId = 1; //正常
                        oldVipCardInfo.MembershipUnit  = unitId;
                        oldVipCardInfo.MembershipTime  = DateTime.Now;
                        oldVipCardInfo.VipCardTypeID   = vipCardTypeInfo.VipCardTypeID;
                        vipCardBLL.Update(oldVipCardInfo);
                        //存在导入的卡时,更新当前会员的VipCode
                        var vipbll = new VipBLL(CurrentUserInfo);
                        List <IWhereCondition> wheres = new List <IWhereCondition>();
                        wheres.Add(new MoreThanCondition()
                        {
                            FieldName = "status", Value = 0
                        });
                        wheres.Add(new EqualsCondition()
                        {
                            FieldName = "vipid", Value = vipId
                        });
                        wheres.Add(new EqualsCondition()
                        {
                            FieldName = "clientid", Value = CurrentUserInfo.ClientID
                        });
                        var VipInfo = vipbll.Query(wheres.ToArray(), null).FirstOrDefault();
                        if (VipInfo != null && !string.IsNullOrEmpty(VipInfo.VIPID))
                        {
                            VipInfo.VipCode = oldVipCardInfo.VipCardCode;
                            //更新VipCode信息
                            vipbll.Update(VipInfo);
                        }
                        //开卡礼
                        var vipCardUpgradeRewardBll        = new VipCardUpgradeRewardBLL(CurrentUserInfo);
                        var vipCardUpgradeRewardEntityList = vipCardUpgradeRewardBll.QueryByEntity(new VipCardUpgradeRewardEntity()
                        {
                            VipCardTypeID = vipCardTypeInfo.VipCardTypeID
                        }, 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");
                                }
                            }
                        }
                    }
                }
                //老卡操作状态信息
                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 eventService = new EventService();
            var vipMsg       = new EventContract
            {
                Operation  = OptEnum.Update,
                EntityType = EntityTypeEnum.Vip,
                Id         = vipId,
            };

            eventService.PublishMsg(vipMsg);
        }
        /// <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);
        }
        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="rechargeOrderInfo"></param>
        /// <param name="vipInfo"></param>
        /// <param name="unitInfo"></param>
        /// <param name="paymentTypeId"></param>
        public void Recharge(RechargeOrderEntity rechargeOrderInfo, VipEntity vipInfo, t_unitEntity unitInfo, string paymentTypeId)
        {
            var rechargeOrderBll = new RechargeOrderBLL(CurrentUserInfo);
            //会员
            var vipCardVipMappingBll = new VipCardVipMappingBLL(CurrentUserInfo);
            var vipAmountBll         = new VipAmountBLL(CurrentUserInfo);
            //门店
            var unitBLL = new t_unitBLL(CurrentUserInfo);

            rechargeOrderInfo.Status      = 1;//已支付
            rechargeOrderInfo.PayID       = paymentTypeId;
            rechargeOrderInfo.PayDateTime = DateTime.Now;
            rechargeOrderBll.Update(rechargeOrderInfo);

            //获取会员卡绑卡信息
            var vipCardVipMappingList = vipCardVipMappingBll.QueryByEntity(new VipCardVipMappingEntity()
            {
                VIPID = vipInfo.VIPID
            }, null);

            //会员无卡并且是续费充值的
            if (vipCardVipMappingList.Count() == 0 && rechargeOrderInfo.OrderDesc == "ReRecharge")
            {
                vipCardVipMappingBll.BindVipCard(vipInfo.VIPID, vipInfo.VipCardCode, rechargeOrderInfo.UnitId);
            }

            var vipAmountEntity = vipAmountBll.QueryByEntity(new VipAmountEntity()
            {
                VipId = vipInfo.VIPID, VipCardCode = vipInfo.VipCode
            }, null).FirstOrDefault();
            //充值
            var amountDetailInfo = new VipAmountDetailEntity()
            {
                Amount         = rechargeOrderInfo.TotalAmount.Value,
                AmountSourceId = "4",
                ObjectId       = rechargeOrderInfo.OrderID.ToString()
            };

            var vipAmountDetailId = vipAmountBll.AddVipAmount(vipInfo, unitInfo, ref vipAmountEntity, amountDetailInfo, null, CurrentUserInfo);

            if (!string.IsNullOrWhiteSpace(vipAmountDetailId) && rechargeOrderInfo.TotalAmount.Value != 0)
            {
                //发送账户余额变动微信模板消息
                var CommonBLL = new CommonBLL();
                CommonBLL.BalanceChangedMessage(rechargeOrderInfo.OrderNo, vipAmountEntity, amountDetailInfo, vipInfo.WeiXinUserId, vipInfo.VIPID, CurrentUserInfo);
            }

            //赠送
            if (rechargeOrderInfo.ReturnAmount.Value != 0)
            {
                var returnAmountInfo = new VipAmountDetailEntity()
                {
                    Amount         = rechargeOrderInfo.ReturnAmount.Value,
                    AmountSourceId = "6",
                    ObjectId       = rechargeOrderInfo.OrderID.ToString()
                };
                var vipReturnDetailId = vipAmountBll.AddVipAmount(vipInfo, unitInfo, ref vipAmountEntity, returnAmountInfo, null, CurrentUserInfo);

                if (!string.IsNullOrWhiteSpace(vipReturnDetailId) && rechargeOrderInfo.ReturnAmount.Value != 0)
                {
                    //发送账户余额变动微信模板消息
                    var CommonBLL = new CommonBLL();
                    CommonBLL.BalanceChangedMessage(rechargeOrderInfo.OrderNo, vipAmountEntity, returnAmountInfo, vipInfo.WeiXinUserId, vipInfo.VIPID, CurrentUserInfo);
                }
            }
            if (rechargeOrderInfo.OrderDesc == "Upgrade")
            {
                //会员卡升级
                vipCardVipMappingBll.BindVirtualItem(vipInfo.VIPID, vipInfo.VipCode, rechargeOrderInfo.UnitId, rechargeOrderInfo.VipCardTypeId ?? 0, "Recharge", orderId: rechargeOrderInfo.OrderID.ToString());
            }

            //充值分润
            RedisRechargeOrderBLL redisRechargeOrderBll = new RedisRechargeOrderBLL();

            redisRechargeOrderBll.SetRedisToRechargeOrder(CurrentUserInfo, rechargeOrderInfo);
        }