Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="okMessage"></param>
        /// <returns></returns>
        private ExecuteResult <CardInfoResponse> GetInfo(GetCardInfoRequest request, string okMessage)
        {
            if (request == null || request.AuthUser == null)
            {
                return(new ExecuteResult <CardInfoResponse>(null)
                {
                    StatusCode = StatusCode.ClientError, Message = "参数错误"
                });
            }

            var entities = _cardRepository.GetListForUserId(request.AuthUser.Id, CardType.YintaiMemberCard, DataStatus.Normal).ToList();

            if (entities.Count == 0)
            {
                return(new ExecuteResult <CardInfoResponse>(null)
                {
                    StatusCode = StatusCode.ClientError, Message = "你还没有绑定会员卡,请您先绑定"
                });
            }

            if (entities.Count > 1)
            {
                return(new ExecuteResult <CardInfoResponse>(null)
                {
                    StatusCode = StatusCode.InternalServerError, Message = "服务器开小差了,请您等等再试"
                });
            }

            var entity = entities[0];

            var pointResult = _groupCardService.GetPoint(new GroupCardPointRequest
            {
                CardNo = entity.CardNo
            });

            var cardProfile = JsonExtension.FromJson_ <CardProfile>(entity.CardProfile);

            if (pointResult.Success)
            {
                var response = new CardInfoResponse
                {
                    Point    = pointResult.Point,
                    CardLvl  = cardProfile.Lvl,
                    CardNo   = entity.CardNo,
                    CardType = cardProfile.Type,
                    User_Id  = entity.User_Id,
                    LastDate = DateTime.Now,
                    Id       = entity.Id
                };

                var result = new ExecuteResult <CardInfoResponse>(response);

                if (!String.IsNullOrEmpty(okMessage))
                {
                    result.Message = okMessage;
                }

                return(result);
            }

            return(new ExecuteResult <CardInfoResponse>(null)
            {
                StatusCode = StatusCode.InternalServerError, Message = String.Concat("查询积分失败,", pointResult.Desc)
            });
        }
Ejemplo n.º 2
0
        public ActionResult Exchange(ExchangeStoreCouponRequest request, int?authuid, UserModel authUser)
        {
            request.AuthUser = authUser;
            var storepromotion = _storeproRepo.Get(sp => sp.Id == request.StorePromotionId && sp.ActiveStartDate <= DateTime.Now && sp.ActiveEndDate >= DateTime.Now).FirstOrDefault();

            if (storepromotion == null)
            {
                return new RestfulResult
                       {
                           Data = new ExecuteResult {
                               StatusCode = StatusCode.InternalServerError, Message = "促销活动无效!"
                           }
                       }
            }
            ;
            if (storepromotion.MinPoints > request.Points)
            {
                return(new RestfulResult
                {
                    Data = new ExecuteResult {
                        StatusCode = StatusCode.InternalServerError, Message = "兑换积点需大于最小积点限制!"
                    }
                });
            }
            if (storepromotion.MinPoints.HasValue && storepromotion.UnitPerPoints.HasValue && storepromotion.UnitPerPoints.Value > 0 &&
                (request.Points % storepromotion.UnitPerPoints) != 0)
            {
                return(new RestfulResult
                {
                    Data = new ExecuteResult {
                        StatusCode = StatusCode.InternalServerError, Message = string.Format("积点需为{0}的整数倍!", storepromotion.UnitPerPoints)
                    }
                });
            }
            var storescope = _storeproscopeRepo.Get(s => s.StorePromotionId == request.StorePromotionId && s.StoreId == request.StoreId && s.Status != (int)DataStatus.Deleted).FirstOrDefault();

            if (storescope == null)
            {
                return new RestfulResult
                       {
                           Data = new ExecuteResult {
                               StatusCode = StatusCode.InternalServerError, Message = "门店无效!"
                           }
                       }
            }
            ;
            var cardInfo = _cardRepo.Get(c => c.User_Id == authUser.Id && c.Status != (int)DataStatus.Deleted).FirstOrDefault();

            if (cardInfo == null)
            {
                return new RestfulResult {
                           Data = new ExecuteResult {
                               StatusCode = StatusCode.InternalServerError, Message = "没有绑定卡!"
                           }
                }
            }
            ;
            var pointResult = _groupData.GetPoint(new GroupCardPointRequest()
            {
                CardNo = cardInfo.CardNo
            });

            if (pointResult == null)
            {
                return new RestfulResult
                       {
                           Data = new ExecuteResult {
                               StatusCode = StatusCode.InternalServerError, Message = "会员卡信息错误!"
                           }
                       }
            }
            ;
            if (pointResult.Point < request.Points)
            {
                return new RestfulResult
                       {
                           Data = new ExecuteResult {
                               StatusCode = StatusCode.InternalServerError, Message = "积分不足!"
                           }
                       }
            }
            ;
            StoreCouponEntity newCoupon = null;

            using (var ts = new TransactionScope())
            {
                // step1: create coupon code
                newCoupon = _storecouponRepo.Insert(new StoreCouponEntity()
                {
                    Amount           = StorePromotionRule.AmountFromPoints(request.StorePromotionId, request.Points),
                    CreateDate       = DateTime.Now,
                    CreateUser       = authUser.Id,
                    Points           = request.Points,
                    Status           = (int)CouponStatus.Normal,
                    StorePromotionId = request.StorePromotionId,
                    UpdateDate       = DateTime.Now,
                    UpdateUser       = authUser.Id,
                    UserId           = authUser.Id,
                    ValidStartDate   = storepromotion.CouponStartDate,
                    ValidEndDate     = storepromotion.CouponEndDate,
                    VipCard          = cardInfo.CardNo,
                    StoreId          = request.StoreId,
                    Code             = StorePromotionRule.CreateCode(request.StorePromotionId)
                });

                // step2: deduce points
                var exchangeResult = _groupData.Exchange(new GroupExchangeRequest()
                {
                    CardNo     = cardInfo.CardNo,
                    IdentityNo = request.IdentityNo
                });

                string groupErr;
                bool   isGroupExchangeSuccess = GroupServiceHelper.SendHttpMessage(ConfigManager.GroupHttpUrlExchange,
                                                                                   ConfigManager.GroupHttpPublicKey,
                                                                                   ConfigManager.GroupHttpPrivateKey,
                                                                                   new {
                    cardno     = cardInfo.CardNo,
                    amount     = request.Points,
                    identityno = request.IdentityNo.Trim(),
                    storeno    = ConfigManager.AppStoreNoInGroup
                },
                                                                                   out groupErr);

                // step3: commit
                if (isGroupExchangeSuccess)
                {
                    // if (exchangeResult.Success)
                    ts.Complete();
                }
                else
                {
                    Logger.Info(groupErr);
                    return(new RestfulResult
                    {
                        Data = new ExecuteResult {
                            StatusCode = StatusCode.InternalServerError,
                            Message = string.IsNullOrEmpty(groupErr)?"积分扣减异常!":groupErr
                        }
                    });
                }
            }
            return(new RestfulResult {
                Data = new ExecuteResult <ExchangeStoreCouponResponse>(
                    new ExchangeStoreCouponResponse().FromEntity <ExchangeStoreCouponResponse>(newCoupon,
                                                                                               s => {
                    s.StoreName = storescope.StoreName;
                    s.Exclude = storepromotion.Notice;
                }))
            });
        }