using Kivii;
using Kivii.Linq;
using Kivii.Web;
using Njust.Pdf.Analysis.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Njust.Pdf.Analysis.Tranforms
{
    [Api(Description = "作者分布查询")]
    public class DisAuthorDistributed : RestfulExecution<DisAuthor>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisAuthor>();
            rtns.Results = new List<DisAuthor>();

            var conn = KiviiContext.GetOpenedDbConnection<DisAuthor>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisAuthor>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disAuthors = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disAuthors.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;

            //for (var i = 0; i < 20; i++)
            //{
            //    var disAuthor = new DisAuthor();
            //    disAuthor.Language = "中文";
            //    disAuthor.DomainName = "航空发动机";
            //    if (i % 2 == 0) disAuthor.DomainName = "隐身技术";
            //    disAuthor.Year = $"{2020 + i}";
            //    disAuthor.Author = $"测试作者{i}";
            //    disAuthor.Count = i * 3;
            //    rtns.Results.Add(disAuthor);
            //}
            //for (var i = 0; i < 20; i++)
            //{
            //    var disAuthor = new DisAuthor();
            //    disAuthor.Language = "英文";
            //    disAuthor.DomainName = "航空发动机";
            //    if (i % 2 == 0) disAuthor.DomainName = "隐身技术";
            //    disAuthor.Year = $"{2020 + i}";
            //    disAuthor.Author = $"testauthor{i}";
            //    disAuthor.Count = i * 3;
            //    rtns.Results.Add(disAuthor);
            //}
        }
    }

    [Api(Description = "文献年份分布查询")]
    public class DisTrendDistributed : RestfulExecution<DisTrend>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisTrend>();
            rtns.Results = new List<DisTrend>();

            var conn = KiviiContext.GetOpenedDbConnection<DisTrend>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisTrend>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disTrends = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disTrends.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;

            //var rtns = new RestfulQueryResponse<DisTrend>();
            //rtns.Results = new List<DisTrend>();

            //for (var i = 0; i < 20; i++)
            //{
            //    var disTrend = new DisTrend();
            //    disTrend.Language = "中文";
            //    disTrend.DomainName = "航空发动机";
            //    if (i % 2 == 0) disTrend.DomainName = "隐身技术";
            //    disTrend.Year = $"{2020 + i}";

            //    disTrend.Count = i * 3;
            //    rtns.Results.Add(disTrend);
            //}
            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    [Api(Description = "作者合作分布查询")]
    public class DisCoauthorDistributed : RestfulExecution<DisCoauthor>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisCoauthor>();
            rtns.Results = new List<DisCoauthor>();

            var conn = KiviiContext.GetOpenedDbConnection<DisCoauthor>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisCoauthor>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disCoauthors = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disCoauthors.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;

            //for (var i = 0; i < 20; i++)
            //{
            //    var disCoauthor = new DisCoauthor();
            //    disCoauthor.Language = "中文";
            //    disCoauthor.DomainName = "航空发动机";
            //    if (i % 2 == 0) disCoauthor.DomainName = "隐身技术";
            //    disCoauthor.Author1 = $"测试作者{i}";
            //    disCoauthor.Author2 = $"测试作者{i + 1}";
            //    disCoauthor.Count = i * 2;
            //    rtns.Results.Add(disCoauthor);
            //}
        }
    }

    [Api(Description = "机构分布查询")]
    public class DisOrganDistributed : RestfulExecution<DisOrgan>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {

            var rtns = new RestfulQueryResponse<DisOrgan>();
            rtns.Results = new List<DisOrgan>();

            var conn = KiviiContext.GetOpenedDbConnection<DisOrgan>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisOrgan>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disOrgans = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disOrgans.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //    var rtns = new RestfulQueryResponse<DisOrgan>();
            //    rtns.Results = new List<DisOrgan>();
            //    for (var i = 0; i < 20; i++)
            //    {
            //        var disOrgran = new DisOrgan();
            //        disOrgran.Language = "中文";
            //        disOrgran.DomainName = "航空发动机";
            //        if (i % 2 == 0) disOrgran.DomainName = "隐身技术";
            //        disOrgran.Year = $"{2020 + i}";
            //        disOrgran.Organ = $"机构{i}";
            //        disOrgran.Count = i * 3;
            //        rtns.Results.Add(disOrgran);
            //    }
            //    rtns.Total = rtns.Results.Count();
            //    return rtns;
            //}
        }
    }

    [Api(Description = "机构合作分布查询")]
    public class DisCoOrganDistributed : RestfulExecution<DisCoOrgan>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisCoOrgan>();
            rtns.Results = new List<DisCoOrgan>();

            var conn = KiviiContext.GetOpenedDbConnection<DisCoOrgan>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisCoOrgan>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disCoOrgrans = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disCoOrgrans.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisCoOrgran>();
            //rtns.Results = new List<DisCoOrgran>();
            //for (var i = 0; i < 20; i++)
            //{
            //    var disCoOrgran = new DisCoOrgran();
            //    disCoOrgran.Language = "中文";
            //    disCoOrgran.DomainName = "航空发动机";
            //    if (i % 2 == 0) disCoOrgran.DomainName = "隐身技术";
            //    disCoOrgran.Organ1 = $"机构{i}";
            //    disCoOrgran.Organ2 = $"机构{i + 1}";

            //    disCoOrgran.Count = i * 2;
            //    rtns.Results.Add(disCoOrgran);
            //}
            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    [Api(Description = "国家分布查询")]
    public class DisCountryDistributed : RestfulExecution<DisCountry>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisCountry>();
            rtns.Results = new List<DisCountry>();

            var conn = KiviiContext.GetOpenedDbConnection<DisCountry>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisCountry>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disCountrys = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disCountrys.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisCountry>();
            //rtns.Results = new List<DisCountry>();
            //for (var i = 0; i < 20; i++)
            //{
            //    var disCountry = new DisCountry();
            //    disCountry.Language = "中文";
            //    disCountry.DomainName = "航空发动机";
            //    if (i % 2 == 0) disCountry.DomainName = "隐身技术";
            //    disCountry.Year = $"{2020 + i}";
            //    disCountry.Country = $"国家{i}";
            //    disCountry.Count = i * 3;
            //    rtns.Results.Add(disCountry);
            //}
            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    //[Api(Description = "国家合作分布查询")]
    //public class DisCoCountryDistributed : RestfulExecution<DisCoCountry>
    //{
    //    public override object OnExecution(IRequest req, IResponse res)
    //    {
    //        var rtns = new RestfulQueryResponse<DisCoCountry>();
    //        rtns.Results = new List<DisCoCountry>();

    //        for (var i = 0; i < 20; i++)
    //        {
    //            var disCoCountry = new DisCoCountry();
    //            disCoCountry.Language = "中文";
    //            disCoCountry.DomainName = "航空发动机";
    //            if (i % 2 == 0) disCoCountry.DomainName = "隐身技术";
    //            disCoCountry.Country1 = $"国家{i}";
    //            disCoCountry.Country2 = $"国家{i + 1}";
    //            disCoCountry.Count = i * 3;
    //            rtns.Results.Add(disCoCountry);
    //        }
    //        rtns.Total = rtns.Results.Count();
    //        return rtns;
    //    }
    //}

    //[Api(Description = "基金分布查询")]
    //public class DisFundDistributed : RestfulExecution<DisFund>
    //{
    //    public override object OnExecution(IRequest req, IResponse res)
    //    {
    //            var rtns = new RestfulQueryResponse<DisFund>();
    //            rtns.Results = new List<DisFund>();

    //            for (var i = 0; i < 20; i++)
    //            {
    //                var disFund = new DisFund();
    //                disFund.Language = "中文";
    //                disFund.DomainName = "航空发动机";
    //                if (i % 2 == 0) disFund.DomainName = "隐身技术";
    //                disFund.Year = $"{2020 + i}";
    //                disFund.Fund = $"基金{i}";
    //                disFund.Count = i * 3;
    //                rtns.Results.Add(disFund);
    //            }
    //            rtns.Total = rtns.Results.Count();
    //            return rtns;
    //        }
    //}

    [Api(Description = "来源分布查询")]
    public class DisSourceDistributed : RestfulExecution<DisSource>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisSource>();
            rtns.Results = new List<DisSource>();

            var conn = KiviiContext.GetOpenedDbConnection<DisSource>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisSource>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disSources = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disSources.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisSource>();
            //rtns.Results = new List<DisSource>();
            //for (var i = 0; i < 20; i++)
            //{
            //    var disSource = new DisSource();
            //    disSource.Language = "中文";
            //    disSource.DomainName = "航空发动机";
            //    if (i % 2 == 0) disSource.DomainName = "隐身技术";
            //    disSource.Year = $"{2020 + i}";
            //    disSource.Source = $"机构{i}";
            //    disSource.Count = i * 3;
            //    rtns.Results.Add(disSource);
            //}
            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    [Api(Description = "关键词共现查询")]
    public class DisCoKeywordDistributed : RestfulExecution<DisCoKeyword>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisCoKeyword>();
            rtns.Results = new List<DisCoKeyword>();

            var conn = KiviiContext.GetOpenedDbConnection<DisCoKeyword>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisCoKeyword>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disCoKeywords = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disCoKeywords.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisCoKeyword>();
            //rtns.Results = new List<DisCoKeyword>();
            //for (var i = 0; i < 20; i++)
            //{
            //    var disCoKeyword = new DisCoKeyword();
            //    disCoKeyword.Language = "中文";
            //    disCoKeyword.DomainName = "航空发动机";
            //    if (i % 2 == 0) disCoKeyword.DomainName = "隐身技术";
            //    disCoKeyword.KeyWord1 = $"关键词{i}";
            //    disCoKeyword.KeyWord2 = $"关键词{i + 1}";
            //    disCoKeyword.Count = i * 3;
            //    rtns.Results.Add(disCoKeyword);
            //}
            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    [Api(Description = "时序主题分布查询")]
    public class DisTimeTopicDistributed : RestfulExecution<DisTimeTopic>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisTimeTopic>();
            rtns.Results = new List<DisTimeTopic>();

            var conn = KiviiContext.GetOpenedDbConnection<DisTimeTopic>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisTimeTopic>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disTimeTopics = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disTimeTopics.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisTimeTopic>();
            //rtns.Results = new List<DisTimeTopic>();
            //for (var i = 0; i < 10; i++)
            //{
            //    for (var j = 0; j < 6; j++)
            //    {
            //        var disTimeTopic = new DisTimeTopic();
            //        disTimeTopic.Language = "中文";
            //        disTimeTopic.DomainName = "航空发动机";
            //        if (j % 2 == 0) disTimeTopic.DomainName = "隐身技术";
            //        disTimeTopic.Year = $"{2020 + i}";
            //        disTimeTopic.Topic = $"Topic{j}";
            //        disTimeTopic.TopicWord = "";
            //        disTimeTopic.TopicWordProbability = "";
            //        for (var x = 0; x < 10; x++)
            //        {
            //            disTimeTopic.TopicWord = disTimeTopic.TopicWord + $"主题词{x},";
            //            disTimeTopic.TopicWordProbability = disTimeTopic.TopicWordProbability + $"{1 - x * 0.05},";
            //        }

            //        rtns.Results.Add(disTimeTopic);
            //    }
            //}

            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }

    [Api(Description = "主题演化分布查询")]
    public class DisTopicEvolutionDistributed : RestfulExecution<DisTopicEvolution>
    {
        #region QueryArgs
        public virtual int? Skip { get; set; }

        public virtual int? Take { get; set; }

        public virtual string OrderBy { get; set; }

        public string OrderByDesc { get; set; }

        public virtual string Include { get; set; }

        public virtual string Fields { get; set; }

        public string QueryKeys { get; set; }

        public string QueryValues { get; set; }

        #endregion

        public override object OnExecution(IRequest req, IResponse res)
        {
            var rtns = new RestfulQueryResponse<DisTopicEvolution>();
            rtns.Results = new List<DisTopicEvolution>();

            var conn = KiviiContext.GetOpenedDbConnection<DisTopicEvolution>();
            var dynamicParams = Request.GetRequestParams();
            var autoQuery = Request.TryResolve<IAutoQueryDb>();
            autoQuery.IncludeTotal = true;
            var request = new RestfulQuery<DisTopicEvolution>();
            request = request.PopulateWith(this);
            var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
            var disTopicEvolutions = autoQuery.Execute(Request, conn, request, sqlExpress);

            rtns.Results.AddRange(disTopicEvolutions.Results);
            rtns.Total = rtns.Results.Count();
            return rtns;
            //var rtns = new RestfulQueryResponse<DisTopicEvolution>();
            //rtns.Results = new List<DisTopicEvolution>();

            //for (var i = 0; i < 10; i++)
            //{
            //    for (var j = 0; j < 6; j++)
            //    {
            //        var disTopicEvolution = new DisTopicEvolution();
            //        disTopicEvolution.Language = "中文";
            //        disTopicEvolution.DomainName = "航空发动机";
            //        if (j % 2 == 0) disTopicEvolution.DomainName = "隐身技术";
            //        disTopicEvolution.SourceYear = $"{2020 + i}";
            //        disTopicEvolution.TargetYear = $"{2021 + i}";
            //        disTopicEvolution.SourceTopic = $"Topic{j}";
            //        disTopicEvolution.TargeTopic = $"Topic{j + 1}";
            //        disTopicEvolution.SourceTopicWord = "";
            //        disTopicEvolution.TargetTopicWord = "";
            //        for (var x = 0; x < 3; x++)
            //        {
            //            disTopicEvolution.SourceTopicWord = disTopicEvolution.SourceTopicWord + $"主题词{x},";
            //            disTopicEvolution.TargetTopicWord = disTopicEvolution.TargetTopicWord + $"主题词{x},";
            //        }

            //        rtns.Results.Add(disTopicEvolution);
            //    }
            //}

            //rtns.Total = rtns.Results.Count();
            //return rtns;
        }
    }
}
