//using Atomx.Common.Entities;
//using Microsoft.EntityFrameworkCore;

//namespace Atomx.Data.CacheServices
//{

//    public partial interface ICacheService
//    {
//        /// <summary>
//        /// 获取每日热销产品数据
//        /// </summary>
//        /// <param name="reload"></param>
//        /// <returns></returns>
//        Task<List<Product>> GetDailyBestSells(bool? reload = false);

//        /// <summary>
//        /// 获取产品详情
//        /// </summary>
//        /// <param name="id"></param>
//        /// <param name="reload"></param>
//        /// <returns></returns>
//        Task<ProductDetailCachedModel?> GetProductDetails(long id, bool reload = false);

//        /// <summary>
//        /// 获取营销产品
//        /// </summary>
//        /// <param name="id"></param>
//        /// <param name="data"></param>
//        /// <returns></returns>
//        Task<MarketingProductModel> GetMarketingProducts(long id, MarketingProductModel? data = null);

//        /// <summary>
//        /// 删除活动产品
//        /// </summary>
//        /// <param name="productId"></param>
//        /// <returns></returns>
//        Task RemoveMarketingProductByProductId(long marketingId, long productId);

//        /// <summary>
//        /// 删除指定营销活动产品缓存
//        /// </summary>
//        /// <param name="marketingId"></param>
//        /// <returns></returns>
//        Task RemoveMarketingProductByMarketingId(long marketingId);

//        /// <summary>
//        /// 获取营销产品
//        /// </summary>
//        /// <returns></returns>
//        Task<List<MarketingProductModel>> GetMarketingProducts();

//        /// <summary>
//        /// 删除产品缓存
//        /// </summary>
//        /// <param name="id"></param>
//        /// <returns></returns>
//        Task RemoveProduct(long id);

//        /// <summary>
//        /// 获取最新产品
//        /// </summary>
//        /// <returns></returns>
//        Task<List<Product>> GetNewProducts(bool? reload = false);

//        /// <summary>
//        /// 获取分类下的最新产品
//        /// </summary>
//        /// <param name="categoryId"></param>
//        /// <returns></returns>
//        Task<List<Product>> GetNewProductsByCategory(List<long> categoryIds, bool? reload = false);

//        /// <summary>
//        /// 获取热销产品
//        /// </summary>
//        /// <returns></returns>
//        Task<List<Product>> GetHotProducts(bool? reload = false);

//        /// <summary>
//        /// 获取分类下热销产品
//        /// </summary>
//        /// <param name="categoryId"></param>
//        /// <returns></returns>
//        Task<List<Product>> GetHotProductsByCategory(List<long> categoryIds, bool? reload = false);

//        /// <summary>
//        /// 获取首页推荐得精选产品
//        /// </summary>
//        /// <param name="reload"></param>
//        /// <returns></returns>
//        Task<List<Product>> GetIndexFeatured(bool? reload = false);
//    }

//    public partial class CacheService : ICacheService
//    {
//        /// <summary>
//        /// 获取每日热销产品数据
//        /// </summary>
//        /// <param name="reload"></param>
//        /// <returns></returns>
//        public async Task<List<Product>> GetDailyBestSells(bool? reload = false)
//        {
//            var key = $"{CacheKeys.DailyBestSellProductPrefix}{DateTime.UtcNow.Day}";
//            var cacheData = await GetCacheAsync<List<Product>>(key);
//            var needReload = reload.HasValue ? reload.Value : false;
//            if (cacheData == null || needReload)
//            {
//                var query = from p in _dbContext.Products
//                            where p.Status == (int)ProductStatus.Publish && p.StoreId == 0
//                            select p;
//                cacheData = query.OrderByDescending(p => EF.Functions.Random()).Take(100).ToList();
//                await SetCacheAsync(key, cacheData);
//            }
//            return cacheData;
//        }



//        public async Task<ProductDetailCachedModel?> GetProductDetails(long id, bool reload = false)
//        {
//            if (id == 0)
//            {
//                return null;
//            }
//            var productData = await GetCacheAsync<ProductDetailCachedModel>($"{CacheKeys.ProductPrefix}{id}");

//            if (productData == null || reload)
//            {
//                productData = (from p in _dbContext.Products
//                               where p.Id == id && p.Status == (int)ProductStatus.Publish
//                               select new ProductDetailCachedModel
//                               {
//                                   AllowExchange = p.AllowExchange,
//                                   AllowReturn = p.AllowReturn,
//                                   AvailableEndTime = p.AvailableEndTime,
//                                   AvailableStartTime = p.AvailableStartTime,
//                                   Body = p.Body,
//                                   CategoryId = p.CategoryId,
//                                   CategoryPath = p.CategoryPath,
//                                   CreateTime = p.CreateTime,
//                                   Deleted = p.Deleted,
//                                   Description = p.Description,
//                                   DisableCoupons = p.DisableCoupons,
//                                   DisplayOrder = p.DisplayOrder,
//                                   Feature = p.Feature,
//                                   IsFeatured = p.IsFeatured,
//                                   Id = p.Id,
//                                   Image = p.Image,
//                                   InHouse = p.InHouse,
//                                   IsWarehouse = p.IsWarehouse,
//                                   Localized = new(),
//                                   ManufacturerId = p.ManufacturerId,
//                                   MarketPrice = p.MarketPrice,
//                                   MetaDescription = p.MetaDescription,
//                                   MetaTitle = p.MetaTitle,
//                                   Photos = p.Photos,
//                                   PictureCount = p.PictureCount,
//                                   Price = p.Price,
//                                   ProductTypeId = p.ProductTypeId,
//                                   Remarks = p.Remarks,
//                                   ReviewStatus = p.ReviewStatus,
//                                   SaleAttribute = new(),
//                                   SalesQuantity = p.SalesQuantity,
//                                   ShippingId = p.ShippingId,
//                                   SingleShip = p.SingleShip,
//                                   Sku = p.Sku,
//                                   SKUCombinations = new(),
//                                   SkuPrices = p.SkuPrices,
//                                   Slug = p.Slug,
//                                   Specification = p.Specification,
//                                   Status = p.Status,
//                                   StockQuantity = p.StockQuantity,
//                                   StopSales = p.StopSales,
//                                   StoreId = p.StoreId,
//                                   Tags = p.Tags,
//                                   Title = p.Title,
//                                   UpdateTime = p.UpdateTime,
//                                   UserId = p.UserId,
//                                   VendorId = p.VendorId,
//                                   ViewsCount = p.ViewsCount,
//                                   WarehouseId = p.WarehouseId,
//                                   Weight = p.Weight,
//                                   FiveStars = p.FiveStars,
//                                   FourStars = p.FourStars,
//                                   IsBest = p.IsBest,
//                                   IsNew = p.IsNew,
//                                   OneStars = p.OneStars,
//                                   OrderMaximumQuantity = p.OrderMaximumQuantity,
//                                   OrderMinimumQuantity = p.OrderMinimumQuantity,
//                                   Ratings = p.Ratings,
//                                   ReviewsCount = p.ReviewsCount,
//                                   ShippingDays = p.ShippingDays,
//                                   ThreeStars = p.ThreeStars,
//                                   TwoStars = p.TwoStars
//                               }).SingleOrDefault();

//                if (productData == null)
//                {
//                    return null;
//                }
//                var productSaleAttributes = (from p in _dbContext.ProductAttributeRelations
//                                             join sa in _dbContext.ProductAttributes on p.AttributeId equals sa.Id into table
//                                             from a in table.DefaultIfEmpty()
//                                             where p.ProductId == id && a.IsSpecification == false
//                                             orderby p.DisplayOrder descending
//                                             select new ProductSKUAttributeModel
//                                             {
//                                                 AttributeId = p.AttributeId,
//                                                 AttributeName = a.Name,
//                                                 Items = new(),
//                                                 ProductAttributeId = p.Id
//                                             }).ToList();
//                if (productSaleAttributes.Any())
//                {
//                    var productAttributeIds = productSaleAttributes.Select(p => p.ProductAttributeId).ToList();
//                    var attributeValues = (from p in _dbContext.ProductAttributeValues
//                                           where productAttributeIds.Contains(p.ProductAttributeRelationId)
//                                           orderby p.DisplayOrder descending
//                                           select new ProductSKUAttributeValueModel
//                                           {
//                                               Id = p.OptionId,
//                                               Name = p.Name,
//                                               ProductAttributeId = p.ProductAttributeRelationId
//                                           }
//                                           ).ToList();
//                    foreach (var item in productSaleAttributes)
//                    {
//                        var values = attributeValues.Where(p => p.ProductAttributeId == item.ProductAttributeId).ToList();
//                        item.Items = values;
//                    }

//                    productData.SaleAttribute = productSaleAttributes;
//                    var skuCombinations = _dbContext.ProductSKUCombinations.Where(p => p.ProductId == id).ToList();
//                    productData.SKUCombinations = skuCombinations;
//                }
//                await SetCacheAsync($"{CacheKeys.ProductPrefix}{id}", productData);
//            }

//            return productData;
//        }


//        /// <summary>
//        /// 获取营销产品
//        /// </summary>
//        /// <param name="id"></param>
//        /// <param name="data"></param>
//        /// <returns></returns>
//        public async Task<MarketingProductModel> GetMarketingProducts(long id, MarketingProductModel? data = null)
//        {
//            var cacheData = await GetMarketingProducts();
//            if (data != null)
//            {
//                cacheData.RemoveAll(p => p.Id == id);
//                cacheData.Add(data);
//                await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
//            }
//            else
//            {
//                data = cacheData.SingleOrDefault(p => p.Id == id);
//            }
//            return data;
//        }

//        /// <summary>
//        /// 删除活动产品
//        /// </summary>
//        /// <param name="productId"></param>
//        /// <returns></returns>
//        public async Task RemoveMarketingProductByProductId(long marketingId, long productId)
//        {
//            var cacheData = await GetMarketingProducts();
//            cacheData.RemoveAll(p => p.ProductId == productId && p.MarketingId == marketingId);

//            await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
//        }

//        /// <summary>
//        /// 删除指定营销活动产品缓存
//        /// </summary>
//        /// <param name="marketingId"></param>
//        /// <returns></returns>
//        public async Task RemoveMarketingProductByMarketingId(long marketingId)
//        {
//            var cacheData = await GetMarketingProducts();
//            cacheData.RemoveAll(p => p.MarketingId == marketingId);

//            await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
//        }

//        /// <summary>
//        /// 获取营销产品
//        /// </summary>
//        /// <returns></returns>
//        public async Task<List<MarketingProductModel>> GetMarketingProducts()
//        {
//            var cacheData = await GetCacheAsync<List<MarketingProductModel>>(CacheKeys.MarketingProduct);
//            if (cacheData == null)
//            {
//                return new List<MarketingProductModel>();
//            }
//            return cacheData;
//        }

//        public async Task RemoveProduct(long id)
//        {
//            var productData = await GetCacheAsync<ProductDetailCachedModel>($"{CacheKeys.ProductPrefix}{id}");
//            if (productData == null)
//            {
//                await Remove($"{CacheKeys.ProductPrefix}{id}");
//            }
//        }


//        /// <summary>
//        /// 获取最新产品
//        /// </summary>
//        /// <returns></returns>
//        public async Task<List<Product>> GetNewProducts(bool? reload = false)
//        {
//            var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductNewList);
//            if (cacheData == null || reload.HasValue)
//            {
//                var data = _dbContext.Products.OrderByDescending(p => p.CreateTime).Take(100).ToList();
//                cacheData = data;
//                await SetCacheAsync(CacheKeys.MarketingProduct, cacheData, 24);
//            }
//            return cacheData;
//        }

//        /// <summary>
//        /// 获取分类下的最新产品
//        /// </summary>
//        /// <param name="categoryId"></param>
//        /// <returns></returns>
//        public async Task<List<Product>> GetNewProductsByCategory(List<long> categoryIds, bool? reload = false)
//        {
//            var key = string.Join(",", categoryIds.ToArray()).ToMD5();
//            var cacheData = await GetCacheAsync<List<Product>>($"{CacheKeys.ProductNewListPrefix}.{key}");
//            if (cacheData == null || reload.HasValue)
//            {
//                var data = _dbContext.Products.Where(p => categoryIds.Contains(p.CategoryId)).OrderByDescending(p => p.CreateTime).Take(100).ToList();
//                cacheData = data;
//                await SetCacheAsync($"{CacheKeys.ProductNewListPrefix}.{key}", cacheData, 24);
//            }
//            return cacheData;
//        }



//        /// <summary>
//        /// 获取热销产品
//        /// </summary>
//        /// <returns></returns>
//        public async Task<List<Product>> GetHotProducts(bool? reload = false)
//        {
//            var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductHotList);
//            if (cacheData == null || reload.HasValue)
//            {
//                var data = _dbContext.Products.OrderByDescending(p => p.SalesQuantity).Take(100).ToList();
//                cacheData = data;
//                await SetCacheAsync(CacheKeys.ProductHotList, cacheData, 24);
//            }
//            return cacheData;
//        }

//        /// <summary>
//        /// 获取分类下热销产品
//        /// </summary>
//        /// <param name="categoryId"></param>
//        /// <returns></returns>
//        public async Task<List<Product>> GetHotProductsByCategory(List<long> categoryIds, bool? reload = false)
//        {
//            var key = string.Join(",", categoryIds.ToArray()).ToMD5();
//            var cacheData = await GetCacheAsync<List<Product>>($"{CacheKeys.ProductHotListPrefix}.{key}");
//            if (cacheData == null || reload.HasValue)
//            {
//                var data = _dbContext.Products.Where(p => categoryIds.Contains(p.CategoryId)).OrderByDescending(p => p.SalesQuantity).Take(100).ToList();
//                cacheData = data;
//                await SetCacheAsync($"{CacheKeys.ProductNewListPrefix}.{key}", cacheData, 24);
//            }
//            return cacheData;
//        }

//        /// <summary>
//        /// 获取首页推荐得精选产品
//        /// </summary>
//        /// <param name="reload"></param>
//        /// <returns></returns>
//        public async Task<List<Product>> GetIndexFeatured(bool? reload = false)
//        {
//            var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductFeaturedList);
//            if (cacheData == null || reload.HasValue)
//            {
//                var data = _dbContext.Products.Where(p => p.IsFeatured).OrderByDescending(p => p.UpdateTime).Take(20).ToList();
//                cacheData = data;
//                await SetCacheAsync(CacheKeys.ProductFeaturedList, cacheData, 24);
//            }
//            return cacheData;
//        }
//    }
//}
