using Hotline.Caches;
using Hotline.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Cache;
using XF.Domain.Dependency;

namespace Hotline.CacheManager
{
    public class SysDicDataCacheManager : ISysDicDataCacheManager, IScopeDependency
    {
        private readonly ISysDicDataRepository _sysDicDataRepository;
        private readonly ITypedCache<IReadOnlyList<SysDicData>> _cacheSysDicData;

        public SysDicDataCacheManager(ISysDicDataRepository sysDicDataRepository,ITypedCache<IReadOnlyList<SysDicData>> cacheSysDicData)
        {
            _sysDicDataRepository = sysDicDataRepository;
            _cacheSysDicData = cacheSysDicData;
        }

        public IReadOnlyList<SysDicData> GetSysDicDataCache(string code)
        {
            var sysDicDataList = _cacheSysDicData.GetOrAdd(code, k =>
            {
                return _sysDicDataRepository.Queryable().Where(x => x.DicTypeCode == code).ToTreeAsync(x => x.Children, it => it.ParentId, "").GetAwaiter().GetResult();
            });
            return sysDicDataList;
        }
    }
}
