Beispiel #1
0
        public async Task <ProductResultModel> GetProducts(string tenantId, int sellerId)
        {
            var result = new ProductResultModel {
            };

            result.Products = await this._dbContext.Products
                              .Select(x => new ProductModel
            {
                TenantId                  = x.TenantId,
                ProductId                 = x.ProductId,
                SellerName                = x.Seller.Name,
                BrandName                 = x.Brand.Name,
                Name                      = x.Name,
                Description               = x.Description,
                BasePrice                 = x.BasePrice,
                SpecialPrice              = x.SpecialPrice,
                BrandId                   = x.BrandId,
                SellerId                  = x.SellerId,
                AllowHomeDelivery         = x.AllowHomeDelivery,
                AdditionalNoteRequired    = x.AdditionalNoteRequired,
                AllowPurchaseWithoutStock = x.AllowPurchaseWithoutStock,
                AllowSaveAndSubscription  = x.AllowSaveAndSubscription,
                AllowStorePickup          = x.AllowStorePickup,
                ApplyTaxes                = x.ApplyTaxes,
                CountReviews              = x.CountReviews,
                CountryIsoCode            = x.CountryIsoCode,
                CurrencyIsoCode           = x.CurrencyIsoCode,
                CurrencySymbol            = x.CurrencyIsoCode.Equals("PEN") ? "S/" : "",
                MetaTitle                 = x.MetaTitle,
                MetaDescription           = x.MetaDescription,
                Keywords                  = x.Keywords,
                Rating                    = x.Rating,
                Slug                      = x.Slug,
                TotalLikes                = x.TotalLikes,
                TotalReviews              = x.TotalReviews,
                TotalViews                = x.TotalViews,
                DefaultImage              = x.Images.FirstOrDefault().UrlLinkCatalog,
                HasVariations             = x.HasVariations,
                Images                    = x.Images.Select(x => new ProductImageModel
                {
                    Title           = x.Title,
                    UrlLinkCatalog  = x.UrlLinkCatalog,
                    UrlLinkOriginal = x.UrlLinkOriginal,
                    UrlLinkProduct  = x.UrlLinkProduct,
                    UrlLinkThumb    = x.UrlLinkThumb,
                    UrlLinkZoom     = x.UrlLinkZoom
                }).ToList(),
                ProductStatus = x.ProductStatus,
                ProductType   = x.ProductType
            }).Where(c => c.ProductType == ProductType.Product && c.ProductStatus == ProductStatus.Active && c.TenantId.Equals(tenantId) && c.SellerId.Equals(sellerId)).ToListAsync();

            result.Brands = result.Products.Where(c => c.BrandId.HasValue).Select(c => new BrandFacetModel {
                Id    = c.BrandId.Value,
                Name  = c.Name,
                Count = 0
            }).ToList();

            return(result);
        }
        public async Task <ActionResult <List <ProductResultModel> > > ProductAsync(string DeviceName, float ProductWeight, string PictureResult)
        {
            List <ProductResultModel> productResult = new List <ProductResultModel>();
            //Dictionary<string, int> dict_1 = JsonConvert.DeserializeObject<Dictionary<string, int>>(PictureResult);
            JObject dict_2 = (JObject)JsonConvert.DeserializeObject(PictureResult);

            System.Diagnostics.Debug.WriteLine(dict_2);
            float temp_weight = 0;
            IEnumerable <JProperty> properties = dict_2.Properties();

            foreach (JProperty item in properties)
            {
                CommodityManage product_info = await _context.CommodityManage.FirstOrDefaultAsync(m => m.ProductID == product_dict[item.Name]);

                ProductResultModel temp = new ProductResultModel(product_dict[item.Name], product_info.ProductName, product_info.ProductImage, Convert.ToSingle(product_info.ProductPrice), (int)item.Value);
                productResult.Add(temp);
                temp_weight += Convert.ToSingle(product_info.ProductWeight) * (int)item.Value;
            }
            float abs_weight_gap = Math.Abs(temp_weight - ProductWeight);

            if (temp_weight > ProductWeight)
            {
                if (abs_weight_gap < 0.5 * temp_weight)
                {
                    return(productResult);
                }
                else
                {
                    return(productResult);
                }
            }
            else
            {
                if (abs_weight_gap < 0.5 * temp_weight)
                {
                    return(productResult);
                }
                else
                {
                    int j = 1;
                    for (int i = (int)(-0.05 * (int)ProductWeight); i < 0.05 * (int)ProductWeight; i++)
                    {
                        IList <IList <int> > res            = new List <IList <int> >();
                        Compute_weight       compute_Weight = new Compute_weight();
                        res = compute_Weight.CombinationSum2(gap_weight, (int)ProductWeight + i - (int)temp_weight);
                        foreach (var item in res)
                        {
                            System.Diagnostics.Debug.WriteLine("重量可能的组合结果{0}:", j);
                            foreach (var item_2 in item)
                            {
                                System.Diagnostics.Debug.WriteLine(item_2);
                                CommodityManage product_info_2 = await _context.CommodityManage.FirstOrDefaultAsync(m => m.ProductID == product_dict_weight[item_2]);

                                ProductResultModel temp_2 = new ProductResultModel(product_dict_weight[item_2], product_info_2.ProductName, product_info_2.ProductImage, Convert.ToSingle(product_info_2.ProductPrice), item.Count(t => t == item_2));
                                productResult.Add(temp_2);
                            }
                            j++;
                        }
                    }


                    return(productResult);
                }
            }



            //return $"设备名:{DeviceName},重量:{ProductWeight},图片结果:{dict_2}";
            //return await _context.CommodityManage.ToListAsync();
        }