//------------------------------------------------------------------------------------------- public IList <Product> RecommenceByHobbyGetListProduct(Recommence recommence) { int cate1 = 0; int cate2 = 0; int cate3 = 0; int cate4 = 0; IEnumerable <Product> resultProducts = new List <Product>(); foreach (var item in recommence.ListDataProductCodes) { var list = ListDistinct(recommence.ProductCodeOfUserLogging.ToList(), item); if (list != null) { resultProducts = resultProducts.Concat(ListDistinct(recommence.ProductCodeOfUserLogging.ToList(), item)); } } var listCategory = recommence.ProductCodeOfUserLogging.Select(x => x.CategoryId).ToList(); //get 2 selected nearest category. if (listCategory.Count >= 4) { cate1 = listCategory[listCategory.Count - 1]; cate2 = listCategory[listCategory.Count - 2]; cate3 = listCategory[listCategory.Count - 3]; cate4 = listCategory[listCategory.Count - 4]; } //get products in 2 category from listResult var result = resultProducts.Where(x => x.CategoryId == cate1 || x.CategoryId == cate2 || x.CategoryId == cate4 || x.CategoryId == cate3); //create new product if not existed //_productService.CreateListProducts(result.ToList()); return(result.Distinct().ToList()); }
//---------------------------------------------------------------------------------------- //find output from data from client => user and list products to recommend public IList <Product> RecommendByPriceAvarageGetListProducts(Recommence recommence) { if (recommence != null) { int cate1 = 0; int cate2 = 0; var userLoginAvarePrice = GetAvaragePrice(recommence.ProductCodeOfUserLogging.ToList()); //get all products of users have same avarage price from list data var resultProducts = Distance(recommence).Distinct(); IList <Recommence> result = new List <Recommence>(); var listCategory = recommence.ProductCodeOfUserLogging.Select(x => x.CategoryId).ToList(); //find 2 selected nearest catagory if (listCategory.Count >= 2) { cate1 = listCategory[listCategory.Count - 1]; cate2 = listCategory[listCategory.Count - 2]; } //get final list product after query with conditions //var resultAfter = resultProducts.Where(x => x.CategoryId == cate1 || x.CategoryId == cate2); return(resultProducts.Distinct().ToList()); } return(null); }
public async Task <IHttpActionResult> GetData(int userId) { var productCodesOfUserLogging = _kService.GetAllProductCodeByUserIdToRecommend(userId); IList <List <ProductRecommence> > listDataOfProductCode = _kService.GetAllUsersAndAllProductCodesToRecommend(userId); var result = new Recommence(userId, productCodesOfUserLogging, listDataOfProductCode); await CreateQueue(System.Text.Json.JsonSerializer.Serialize(result)); return(Ok()); }
//------------------------------------------------------------------------------------------ //find distance of avarage price to another users public IList <Product> Distance(Recommence recommence) { IList <double> list = new List <double>(); //find avarage by user Logging var avaragePrice = GetAvaragePrice(recommence.ProductCodeOfUserLogging.ToList()); Dictionary <List <Product>, double> dic = new Dictionary <List <Product>, double>(); //Distance logging user and other users foreach (var item in recommence.ListDataProductCodes) { var avaragePriceData = GetAvaragePrice(item); var distance = Math.Sqrt(avaragePrice * avaragePrice - avaragePriceData * avaragePriceData); list.Add(distance); } IEnumerable <Product> listProducts = new List <Product>(); // take users have nearest average price and return list users for (int i = 0; i < list.Count; i++) { dic.Add(recommence.ListDataProductCodes[i], list[i]); } var products = dic.OrderBy(x => x.Value).Select(x => x.Key); //take list products orderby distance foreach (var item in products) { if (listProducts.Count() <= 10) { var listAfterDistinct = ListDistinct(recommence.ProductCodeOfUserLogging.ToList(), item); if (listAfterDistinct != null) { listProducts = listProducts.Concat(listAfterDistinct); } } else { break; } } return(listProducts.Distinct().ToList()); }
public IList <string> RecommendByBestSeller(Recommence model) { IEnumerable <Product> listProducts = new List <Product>(); foreach (var item in model.ListDataProductCodes) { listProducts = listProducts.Concat(item); } var list = from Product in listProducts group Product by Product into groupProduct select new { product = groupProduct.Key, count = groupProduct.Count() }; var result = list.OrderByDescending(x => x.count).Select(x => x.product.Code); return(result.ToList()); /// }
public ActionResult <IList <string> > LoadAndUpdate(Recommence recommence) { var result = _hobbyService.RecommenceByHobbyGetListProduct(recommence); var userId = recommence.UserId; //create new category if not exist foreach (var item in result) { var categoryId = item.CategoryId; if (_categoryService.CheckCategory(categoryId)) { CreateCategoryViewModel newCate = new CreateCategoryViewModel(); newCate.Code = categoryId; _categoryService.Create(_mapper.Map <Category>(newCate)); } } //create products if not exist var products = _mapper.Map <IEnumerable <CreateProductViewModel> >(result) .Select(x => _productService.Create(_mapper.Map <Product>(x))); //create new user if not exist CreateUserViewModel newUser = new CreateUserViewModel(); newUser.Code = userId; _userService.Create(_mapper.Map <User>(newUser)); //select list products'code IEnumerable <string> listCode = result.Select(x => x.Code); //create new model to store database CreateRecommenceByHobbyViewModel hobbyViewModel = new CreateRecommenceByHobbyViewModel(); var list = _mapper.Map <IEnumerable <ProductViewModel> >(products); hobbyViewModel.ProductRecommenceHobbies = list; hobbyViewModel.UserId = userId; //create RecommenceHobbyModel to save database var a = _mapper.Map <RecommenceHobby>(hobbyViewModel); _hobbyService.LoadAndUpdate(a); //---------------------- //RecommenceByPriceService //get listproducts from model var listProducts = _priceService.RecommendByPriceAvarageGetListProducts(recommence); //check and new category if not exist foreach (var item in listProducts) { if (_categoryService.CheckCategory(item.CategoryId) == false) { CreateCategoryViewModel newModel = new CreateCategoryViewModel(); newModel.Code = item.CategoryId; _categoryService.Create(_mapper.Map <Category>(newModel)); } } //check and create new product if not existed var productPrices = _mapper.Map <IEnumerable <CreateProductViewModel> >(listProducts) .Select(x => _productService.Create(_mapper.Map <Product>(x))); //new model to store database CreateRecommenceByPriceViewModel priceViewModel = new CreateRecommenceByPriceViewModel(); var listp = _mapper.Map <IEnumerable <ProductViewModel> >(productPrices); priceViewModel.ProductRecommencePrices = listp; priceViewModel.UserId = userId; //create new model or update to savechanges var b = _mapper.Map <RecommencePrice>(priceViewModel); _priceService.LoadAndUpdate(b); return(Ok()); }