using AutoMapper;

using CloudGaming.Code.DataAccess;
using CloudGaming.DtoModel.Game;
using CloudGaming.DtoModel.Mall;

using HuanMeng.DotNetCore.CacheHelper;

using StackExchange.Redis;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Cache.Special;

/// <summary>
/// 商城产品缓存 
/// </summary>
/// <param name="dao"></param>
/// <param name="database"></param>
/// <param name="mapper"></param>
/// <param name="appConfig"></param>
public class ProductCacheEntityCache(DAO dao, IDatabase database, IMapper mapper) : RedisDataEntityCache<ProductCache>(database, 60 * 60 * 12,"产品")
{
    public override string key => "cache:Product:list";

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override List<ProductCache> GetDataList()
    {
        var list = dao.DaoPhone.Context.T_Products.AsNoTracking().Where(it => it.IsEnable).ToList();
        if (list == null)
        {
            list = new List<T_Products>();
        }

        var products = mapper.Map<List<ProductCache>>(list);
        var rewards = dao.DaoPhone.Context.T_Products_Reward.AsNoTracking().ToList();
        products.ForEach(product =>
        {
            //获取所有奖励
            var _rewards = rewards.Where(it => it.T_ProductId == product.Id).ToList();
            product.Rewards = _rewards;
            if (product.Rewards == null)
            {
                product.Rewards = new List<T_Products_Reward>();
            }
        });
        return products;
    }
}
