using AutoMapper;

using CloudGaming.Code.DataAccess;
using CloudGaming.DtoModel.RedemptionCode;
using CloudGaming.DtoModel.SevenSign;

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>
public class RedemptionCodeEntityCache(DAO dao, IDatabase database, IMapper mapper) : RedisDataEntityCache<RedemptionCodeCache>(database,60 * 60 * 1, "兑换码")
{
    public override List<RedemptionCodeCache> GetDataList()
    {
        var list = dao.DaoPhone.Context.T_RedemptionCode.AsNoTracking().Where(it => it.IsActive).ToList();
        if (list == null)
        {
            return new List<RedemptionCodeCache>();
        }

        var sevenDayCache = mapper.Map<List<RedemptionCodeCache>>(list);
        var rewards = dao.DaoPhone.Context.T_RewardConfig.Where(it => it.ParentNodeType == (int)RewardConfigType.兑换码).AsNoTracking().ToList();
        sevenDayCache.ForEach(item =>
        {
            //获取所有奖励
            var _rewards = rewards.Where(it => it.ParentNodeId == item.Id).ToList();
            item.RewardConfigs = _rewards;
            if (item.RewardConfigs == null)
            {
                item.RewardConfigs = new List<T_RewardConfig>();
            }
        });
        return sevenDayCache;
    }

    public override string key => "cache:RedemptionCode:list";
}
