using AutoMapper;

using CloudGaming.Code.DataAccess;
using CloudGaming.DtoModel.Mall;
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 SevenDayEntityCache(DAO dao, IDatabase database, IMapper mapper) : RedisDataEntityCache<SevenDayCache>(database, 60 * 60 * 24 * 7, "七天签到")
{
    public override List<SevenDayCache> GetDataList()
    {
        var list = dao.DaoPhone.Context.T_SevenDaySignIn.AsNoTracking().OrderBy(it => it.OrderId).ToList();
        if (list == null)
        {
            list = new List<T_SevenDaySignIn>();
        }

        var sevenDayCache = mapper.Map<List<SevenDayCache>>(list);
        var rewards = dao.DaoPhone.Context.T_SevenDaySignIn_Reward.AsNoTracking().ToList();
        sevenDayCache.ForEach(item =>
        {
            //获取所有奖励
            var _rewards = rewards.Where(it => it.SigninId == item.Id).ToList();
            item.Rewards = _rewards;
            if (item.Rewards == null)
            {
                item.Rewards = new List<T_SevenDaySignIn_Reward>();
            }
        });
        return sevenDayCache;
    }

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