public ActionResult Amount(ExchangeStoreCouponRuleRequest 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 = "兑换积分需大于最小积分限制!"
                    }
                });
            }

            return(new RestfulResult {
                Data = new ExecuteResult <ExchangeStoreCouponRuleResponse>(new ExchangeStoreCouponRuleResponse()
                {
                    Amount = StorePromotionRule.AmountFromPoints(request.StorePromotionId, request.Points)
                })
            });
        }
    }
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;
                }))
            });
        }