public async Task <GetShopListResponse> GetShopListAsync([FromBody] GetShopListRequest request)
        {
            GetShopListResponse response = new GetShopListResponse();

            try
            {
                var customer = _context.CustomerEntitys.FirstOrDefault(a => a.CustomerPhone == request.UserName);
                if (customer != null)
                {
                    var shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == customer.CustomerID);
                    if (shopCar == null)
                    {
                        shopCar = new ShopCarEntity
                        {
                            UserId = customer.CustomerID
                        };
                        _context.Add(shopCar);
                        _context.SaveChanges();
                    }
                    shopCar = _context.ShopCarEntitys.SingleOrDefault(a => a.UserId == customer.CustomerID);
                    ResetCheck(shopCar.PKID);
                    var query = (from cl in _context.CarListEntitys
                                 join v in _context.VersionInfoEntitys
                                 on cl.VersionID equals v.ProductID
                                 join p in _context.ProductEntities
                                 on cl.ProductID equals p.ProductID
                                 where cl.CarID == shopCar.PKID
                                 select new ShopList
                    {
                        CarID = cl.CarID,
                        ProductID = p.ProductID,
                        ProductName = p.ProductName,
                        VersionID = cl.VersionID,
                        versionInfo = v.VersionInfo,
                        Price = cl.UnitPrice,
                        Count = cl.Count,
                        ProductImg = p.ProductImg,
                        Subtotal = cl.UnitPrice * cl.Count
                    }).ToList();

                    response.TotalPrice = query.Sum(a => a.Subtotal);
                    response.ShopCount  = query.Sum(a => a.Count);

                    response.ShopLists.AddRange(query);
                }
            }
            catch (Exception ex)
            {
                response.Successful = false;
                response.Message    = ex.Message;
                logger.LogError(ex, $"GetShopListAsync 方法报错 Message:{JsonConvert.SerializeObject(request)}");
            }
            return(response);
        }
Beispiel #2
0
        public async Task <IActionResult> ShopCar()
        {
            GetShopListRequest request = new GetShopListRequest {
                UserName = "******"
            };
            var getShopListResponse = await shopCarService.GetShopList(request);

            ViewBag.Total = getShopListResponse.TotalPrice;
            ViewBag.Count = getShopListResponse.ShopCount;

            if (!getShopListResponse.ShopLists.Any())
            {
                return(Content("购物车为空"));
            }
            else
            {
                return(View(getShopListResponse.ShopLists));
            }
        }
        public async Task <GetShopListResponse> GetShopList(GetShopListRequest request)
        {
            string url = $"{configuration["ServiceAddress:Service.ShopCar"]}{configuration["MehtodName:ShopCar.ShopCarOperation.GetShopListAsync"]}";

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