using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Caching.Memory;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;

namespace SlnMesnac.Repository.service
{
    public class BaseCodeServiceImpl:BaseServiceImpl<BaseCode>,IBaseCodeService
    {
        private readonly IMemoryCache _memoryCache;
        
        public BaseCodeServiceImpl(Repository<BaseCode> rep, IMemoryCache memoryCache) : base(rep)
        {
            _memoryCache = memoryCache;
        }

        public void CreateBaseCode()
        {
            _rep.Context.CodeFirst.InitTables(typeof(BaseCode));
        }


        public List<BaseCode> QuListCache()
        {
            string key = "QuListCache";
            if (_memoryCache.TryGetValue<List<BaseCode>>(key, out var list))
            {
                return list;
            }
            var entity = Query();
            _memoryCache.Set(key, entity, TimeSpan.FromMinutes(5));

            return entity;
        }

        /// <summary>
        /// 根据Code去查询单个数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public BaseCode QueryCode(string code)
        {
          return  Query(x => x.Code == code).First();
        }
    }
}