/// <summary>
        /// 添加商品到购物车
        /// </summary>
        public async Task <AddShopCarResponse> AddCarAsync([FromBody] AddShopCarRequest request)
        {
            var response = new AddShopCarResponse();

            try
            {
                var colorId = _context.ColorVersionEntitys.FirstOrDefault(a => a.VersionID == request.VersionID)?.PKID;
                if (!colorId.HasValue)
                {
                    response.Successful = false;
                    response.Message    = "该版本不存在,请重新选择!";
                }
                var unitPrice = _context.PriceEntitys.FirstOrDefault(p => p.ProductID == request.ProductID && p.VersionID == request.VersionID)?.Price;
                unitPrice = unitPrice ?? 0;

                var userInfo = accountService.GetUserInfoByUserNameAsync(new Account.Model.Request.GetShopCarByUserNameRequest {
                    UserName = "******"
                });
                var shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == userInfo.userInfo.PKID);
                if (shopCar == null)
                {
                    _context.ShopCarEntitys.Add(new ShopCarEntity {
                        UserId = userInfo.userInfo.PKID
                    });
                    _context.SaveChanges();
                    shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == userInfo.userInfo.PKID);
                }
                var carListItem = _context.CarListEntitys.SingleOrDefault(a => a.CarID == shopCar.PKID && a.ProductID == request.ProductID && a.VersionID == request.VersionID);
                if (carListItem != null)
                {
                    carListItem.Count += 1;
                    _context.Update(carListItem);
                }
                else
                {
                    _context.CarListEntitys.Add(new CarListEntity
                    {
                        CarID     = shopCar.PKID,
                        ProductID = request.ProductID,
                        VersionID = request.VersionID,
                        Count     = 1,
                        UnitPrice = unitPrice.Value,
                        ColorID   = colorId.Value,
                        IsCheck   = true
                    });
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                response.Successful = false;
                response.Message    = ex.Message;
                logger.LogError(ex, $"AddCarAsync方法报错 Message:{JsonConvert.SerializeObject(request)}");
            }

            return(response);
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> AddCar(int ProductID, int VersionID)
        {
            var getColorResponse = await shopCarService.GetColorAsync(new GetColorRequest { VersionId = VersionID });

            //发送新增购物车MQ
            var addShopCarRequest = new AddShopCarRequest {
                ProductID = ProductID, VersionID = VersionID
            };

            try
            {
                eventBus.Publish("AddShopCar", addShopCarRequest);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加购物车方法出错!");
                return(Json("error"));
            }

            return(Json("ok"));
        }
Ejemplo n.º 3
0
        public async Task <AddShopCarResponse> AddShopCarAsync(AddShopCarRequest request)
        {
            string url = $"{configuration["ServiceAddress:Service.ShopCar"]}{configuration["MehtodName:ShopCar.ShopCarOperation.AddCarAsync"]}";

            return(await apiHelperService.PostAsync <AddShopCarResponse>(url, request));
        }