using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Infrastructure.Api;
using APT.Infrastructure.Api.Redis;
using APT.MS.Domain.ApiModel.PF;

namespace APT.MS.WebApiControllers.Api.PF
{
    [Route("api/PF/Redis")]
    public class RedisController : AuthorizeTreeApiController<T_PF_MENU>
    {

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<List<RedisModel>> Entities([FromBody] KeywordFilter filter)
        {
            return SafeExecute<List<RedisModel>>(() =>
            {
                RedisManager manager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                return manager.GetListKeys(filter.GetOrgId())
                .Select(x => new RedisModel { ID = x.Name, HashEntry = x }).ToList();
            });
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<RedisModel> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData<RedisModel>((result) =>
            {
                RedisManager manager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                var list = manager.GetListKeys(pageFilter.GetOrgId());
                if (!string.IsNullOrEmpty(pageFilter.Parameter1))
                {
                    string name = pageFilter.Parameter1;
                    list = list.Where(i => i.Name.ToString().Contains(name)).ToList();
                }
                result.TotalCount = list.Count;
                result.Data = list.Skip(pageFilter.Start).Take(pageFilter.Limit)
                .Select(x => new RedisModel { ID = x.Name, HashEntry = x }).ToList();
            });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Clear")]
        public JsonActionResult<bool> Clear([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                RedisManager manager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                if (manager.ExistsListKeys(filter.GetOrgId(), filter.Keyword))
                {
                    var redisDataKey = manager.GetListKey(filter.GetOrgId(), filter.Keyword);
                    if (CsRedisManager.KeyExists(redisDataKey))
                    {
                        CsRedisManager.KeyDelete(redisDataKey);
                    }
                    manager.DeleteListKeys(filter.GetOrgId(), filter.Keyword);
                }
                return true;
            });
        }

        [HttpPost, Route("SetStyle")]
        public JsonActionResult<bool> SetStyle([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {

                if (string.IsNullOrEmpty(filter.Keyword))
                    this.ThrowError("020023");
                if (string.IsNullOrEmpty(filter.Parameter1))
                    this.ThrowError("020024");
                var keyPer = "Style_";
                int styleModel = int.Parse(filter.Parameter1);
                //RedisManager manager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                return CsRedisManager.StringSet<int>(keyPer + filter.Keyword, styleModel);
            });
        }

        [HttpPost, Route("GetStyle")]
        public JsonActionResult<int> GetStyle([FromBody] KeywordFilter filter)
        {
            return SafeExecute<int>(() =>
            {
                var style = 0;
                if (string.IsNullOrEmpty(filter.Keyword))
                    this.ThrowError("020023");
                var keyPer = "Style_";
                //RedisManager manager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                if (CsRedisManager.KeyExists(keyPer + filter.Keyword))
                {
                    style = CsRedisManager.StringGet<int>(keyPer + filter.Keyword);
                }
                return style;
            });
        }


        /// <summary>
		/// Keyword：key
		/// Parameter1：value
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("SetValue")]
        public JsonActionResult<bool> SetValue([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                CsRedisManager.StringSet(filter.Keyword + filter.OrgId, filter.Parameter1);
                return true;
            });
        }

        /// <summary>
        /// 返回value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpPost, Route("GetValue")]
        public JsonActionResult<string> GetValue([FromBody] KeywordFilter filter)
        {
            return SafeExecute<string>(() =>
            {
                return CsRedisManager.StringGet(filter.Keyword + filter.OrgId);
            });
        }
    }
}
