Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id)
        {
            var good = await _goodsRepository.GetAsync(id);

            var categories = await _categoryRepository.GetListAsync();

            var categoriesItem = new List <SelectListItem>();

            categories.ForEach(p => categoriesItem.Add(
                                   new SelectListItem
            {
                Text  = p.Position,
                Value = p.Id.ToString()
            }
                                   ));

            var promoCategories = await _promoCategoryRepository.GetListAsync();

            var promoCategoriesItem = new List <SelectListItem>();

            promoCategories.ForEach(p => promoCategoriesItem.Add(
                                        new SelectListItem
            {
                Text  = p.Name,
                Value = p.Id.ToString()
            }
                                        ));

            promoCategoriesItem.Insert(0, new SelectListItem
            {
                Text  = "",
                Value = "0",
            });

            var model = new CreateGoodModel {
                Categories      = categoriesItem,
                CategoryId      = good.CategoryId,
                PromoCategories = promoCategoriesItem,
                PromoCategoryId = good.PromoCategoryId.HasValue ? good.PromoCategoryId.Value : 0,
                Cost            = good.Cost,
                RealCost        = good.RealCost,
                Description     = good.Description,
                Title           = good.Title,
                DBPathFile      = good.ImagePath,
                Id            = good.Id,
                IsAutoGetting = good.IsAutoIssuance,
            };

            return(View(model));
        }
        public async Task <ApiResult> UpdateGoods(GoodsUpdateDto input)
        {
            var goods = await repository.GetAsync(input.Id);

            if (goods == null)
            {
                throw new ApplicationServiceException("没有查询到该商品!");
            }
            goods.CreateOrUpdateGoods(input.GoodsName, input.GoodsImage, input.Price, input.CategoryId);
            repository.Update(goods);
            if (await new GoodsValidityCheckSpecification(repository, goodsCategoryRepository).IsSatisfiedBy(goods))
            {
                if (await unitofWork.CommitAsync())
                {
                    await localEventBus.SendEvent(EventTopicDictionary.Goods.Loc_WriteToElasticsearch, goods);

                    return(ApiResult.Ok("商品更新成功"));
                }
            }
            return(ApiResult.Ok("商品更新失败"));
        }
Ejemplo n.º 3
0
 public override async Task <BaseApiResult <GoodsQueryDto> > Query(GoodsDetailQueryReq input)
 {
     return(await HandleAsync(input, async() =>
     {
         //通过仓储获取商品聚合
         var goods = await goodsRepository.GetAsync(x => x.Id == input.GoodsId);
         if (goods == null)
         {
             throw new ApplicationException("没有找到该商品!");
         }
         return goods.SetDto <GoodsEntity, GoodsQueryDto>();
     }));
 }
Ejemplo n.º 4
0
 async Task SetActorDataIfNotExists(Guid id)
 {
     if (ActorData == null)
     {
         Goods goods = default;
         try
         {
             goods = await repository.GetAsync(id);
         }
         catch (Exception e)
         {
             Console.WriteLine($"商品对象获取失败,失败原因:{e.GetBaseException()?.Message ?? e.Message}");
         }
         finally
         {
             if (goods == null)
             {
                 throw new ApplicationServiceException("没有找到该商品!");
             }
         }
         ActorData = goods.CopyTo <Goods, GoodsActor>();
     }
 }
Ejemplo n.º 5
0
        public async Task <bool> IsSatisfiedBy(LimitedTimeActivitie entity)
        {
            //检查活动商品有效性
            var goods = await goodsRepository.GetAsync(entity.GoodsId);

            if (goods == null)
            {
                throw new DomainException("所选商品无效!");
            }
            if (goods.Price <= entity.ActivitiePrice)
            {
                throw new DomainException("活动促销价应低于商品原价!");
            }
            return(true);
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <BuyResponse> > BuyItem([FromBody] BuyGoodRequest goodRequest)
        {
            var userId = GetUserId();
            var user   = await _userRepository.GetByEmailAsync(userId);

            var good = await _goodsRepository.GetAsync(goodRequest.Id);

            if (good == null)
            {
                return(new ActionResult <BuyResponse>(new BuyResponse {
                    Msg = "Товар не найден!", IsError = true
                }));
            }

            Serilog.Log.Logger.Information($"UserId={userId} Good={good.Id} Price={good.Cost}");

            if (user.Balance >= good.Cost)
            {
                PromoItem promoItem = null;
                if (good.IsAutoIssuance && good.PromoCategoryId.HasValue)
                {
                    promoItem = await _goodsRepository.PickUpPromoItem(good);
                }

                //user.Balance = user.Balance - good.Cost;
                await _userRepository.Update(user);

                var order = new Order
                {
                    GoodId    = goodRequest.Id,
                    UserId    = user.Id,
                    OrderedAt = DateTime.UtcNow,
                    PromoItem = promoItem,
                };

                if (promoItem != null)
                {
                    order.Completed   = true;
                    order.CompletedAt = DateTime.UtcNow;
                }

                await _orderRepository.AddAsync(order);

                //add user transaction
                await _userScoreRepository.AddAsync(new UserTransaction
                {
                    Score     = -good.Cost,
                    UserId    = user.Id,
                    CreatedAt = DateTime.UtcNow,
                    Type      = TransactionType.GoodBuy
                });

                //var transactions = await _userScoreRepository.GetAllAsync(user.Id);
                //var sum = transactions.Sum(p => p.Score);
                user.Balance -= good.Cost;

                await _userRepository.Update(user);

                if (promoItem != null)
                {
                    return(new ActionResult <BuyResponse>(new BuyResponse {
                        Msg = $"Товар приобретен: {promoItem.Code}", IsAuto = true, PromoCode = promoItem.Code
                    }));
                }
            }
            else
            {
                return(new ActionResult <BuyResponse>(new BuyResponse {
                    Msg = "Не хватает баллов", IsError = true
                }));
            }

            return(new ActionResult <BuyResponse>(new BuyResponse {
                Msg = "Товар приобретен, ждет подтверждения"
            }));
        }