using IRaCIS.Core.Application.BusinessFilter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Service.MinimalApiService
{
    /// <summary>
    /// minimal api  测试 
    /// 学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html
    /// 组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0
    /// 升级只用改一个地方 搜索关键词: 统一使用 Directory.Build.props 管理项目
    //https://www.toutiao.com/article/7407615998343348787/?app=news_article&timestamp=1724760241&use_new_style=1&req_id=20240827200400D600951D2F54551B5ACF&group_id=7407615998343348787&share_token=03C009BA-571C-47AB-86E5-87DA93F08975&tt_from=weixin&utm_source=weixin&utm_medium=toutiao_ios&utm_campaign=client_share&wxshare_count=1&source=m_redirect&wid=1729739417949

    /// </summary>
    [ApiExplorerSettings(GroupName = "Institution")]

    public class TestMinimalApiService(IUserInfo _userInfo, IRepository<TestLength> _testLengthRepository) : ServiceBase
    {

        /// <summary>
        /// 测试efcore json 列支持情况
        /// https://devblogs.microsoft.com/dotnet/array-mapping-in-ef-core-8/
        /// </summary>
        /// <returns></returns>
        public async Task<IResponseOutput> TestEfcoreJson()
        {

            var dateTime = DateTime.Parse("2024-11-08");

            //await _testLengthRepository.AddAsync(new TestLength()
            //{
            //    Name = "Testddd",
            //    TestJsonObjectLsit=new List<TestJsonObject>() { new TestJsonObject() { Name="name1",Description="des1"} , new TestJsonObject() { Name = "name1", Description = "des1" } }
            //});

            //await _testLengthRepository.SaveChangesAsync();

            //对于json 对象  不能使用count  any 操作,但是简单的类型可以的
            var jobjectList = _testLengthRepository.AsQueryable().ToList();

            var jobjectList1 = _testLengthRepository.AsQueryable().Where(t=>t.TestJsonObjectLsit.Any(c=>c.Name=="name1")).ToList();

            var jobjectList2 = _testLengthRepository.AsQueryable().Where(t => t.TestJsonObjectLsit.Any(c => c.Name == "name1")).SelectMany(t=>t.TestJsonObjectLsit).ToList();


            var d1 = _testLengthRepository.Where(t => t.StringList.Any(t => t == "string1")).ToList();
            var d2 = _testLengthRepository.Where(t => t.StringList.Contains("string1")).ToList();


            //selectMany 报错 不支持
            //var d20 = _testLengthRepository.Where(t => t.StringList.Contains("string1")).SelectMany(t => t.StringList).ToList();

            var d3 = _testLengthRepository.Where(t => t.TestEnumList.Contains(TestEnum.Default)).ToList();
            var d4 = _testLengthRepository.Where(t => t.TestEnumList.Any(t => t == TestEnum.Default)).ToList();

            var d5 = _testLengthRepository.Where(t => t.DateTimeList.Contains(dateTime)).ToList();
            var d6 = _testLengthRepository.Where(t => t.DateTimeList.Any(t => t == dateTime)).ToList();

            var d7 = _testLengthRepository.Where(t=>t.DateTimeList.Any(t=>t.Year==2024)).ToList();

            var d8 = _testLengthRepository.Where(t => t.DateTimeList.Any(t => t.Month == 11)).ToList();

            var d90 = _testLengthRepository.Where(t => t.DateTimeList.Any(t => t.Year == 2024)).Select(t => t.DateTimeList).ToList();

            //selectMany 报错 不支持
            //var d9 = _testLengthRepository.Where(t => t.DateTimeList.Any(t => t.Year == 2024)).SelectMany(t=>t.DateTimeList).ToList();
            //var d10 = _testLengthRepository.Where(t => t.DateTimeList.Any(t => t.Year == 2024)).SelectMany(t => t.DateTimeList).Where(t=>t>dateTime.AddSeconds(1)).ToList();

            return ResponseOutput.Ok();
        }



        [TrialGlobalLimit("AddOrUpdateTrial", "BeforeOngoingCantOpt", "AfterStopCannNotOpt")]
        public Task<List<string>> GetProjectList1Async()
        {
            var list = new List<string>()
            {
                "Auth",
                "DCC",
                "PM"
            };
            return Task.FromResult(list);
        }

        [AllowAnonymous]
        [TrialGlobalLimit("BeforeOngoingCantOpt")]
        public IResponseOutput GetTest()
        {

            //throw new BusinessValidationFailedException("手动抛出的异常");

            return ResponseOutput.Ok(_userInfo.IP);
        }

        public IResponseOutput GetTestI18n()
        {
            var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;

            //CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US);
            //CultureInfo.CurrentUICulture = new CultureInfo(StaticData.CultureInfo.en_US);

            return ResponseOutput.Ok(I18n.T("TaskAllocation_DoctorConfigExists"));
        }

    }

}