using Hotline.Api.Controllers;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Tools;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Users;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Cache;
using XF.Domain.Repository;

namespace Hotline.Tests.Application;
public class BiSnapshotApplicationTest : TestBase
{
    private readonly IBiSnapshotApplication _biSnapshotApplication;
    public BiSnapshotApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, ITypedCache<SystemSetting> cacheSettingData, IBiSnapshotApplication biSnapshotApplication, ThirdAccounSupplierFactory thirdAccountDomainFactory, IServiceProvider serviceProvider) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService, thirdAccountRepository, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _biSnapshotApplication = biSnapshotApplication;
    }

    [Fact]
    public async Task GetDuplicateItems_Test()
    {
        var inDto = new DuplicateItemsInDto();
        var items = await _biSnapshotApplication.GetDuplicateItems(inDto).ToListAsync();
        var count = items.Count;
    }

    [Fact]
    public async Task GetCountyStatistics_Test()
    {
        var inDto = new CommunityStatisticsInDto
        {
            StartTime = DateTime.Now.AddDays(-30),
            EndTime = DateTime.Now
        };

        var items = await _biSnapshotApplication.GetCommunityStatistics(inDto).ToListAsync();
        var first = items.FirstOrDefault();
        first.ShouldNotBeNull();
        first.CommunityName.ShouldBe("自贡市");

        var end = true;
        while (end)
        {
            var hasChild = items.Where(m => m.SumCount != 0).FirstOrDefault();
            if (hasChild == null)
            {
                end = false;
                return;
            }
            inDto.CommunityCode = hasChild.CommunityCode;
            items = await _biSnapshotApplication.GetCommunityStatistics(inDto).ToListAsync();
            if (hasChild.SumCount != 0)
            {
                var inDto2 = inDto.Adapt<CommunityStatisticsDetailsInDto>();
                inDto2.CommunityFullName = hasChild.CommunityFullName;
                var details = await _biSnapshotApplication.GetCommunityStatisticsDetails(inDto2).ToListAsync();
                details.Count.ShouldBe(hasChild.SumCount);
            }
        }
    }

    [Fact]
    public async Task GetCountyRedPackStatistics_Test()
    {
        var inDto = new CountyRedPackStatisticsInDto
        {
            StartTime = DateTime.Now.AddDays(-30),
            EndTime = DateTime.Now,
        };
        var items = await _biSnapshotApplication.GetCountyRedPackStatistics(inDto).ToListAsync();

        var transposedItems = items.SelectMany(item => item.GetType().GetProperties()
            .Select(prop => new { Property = prop.Name, Value = prop.GetValue(item) }))
            .GroupBy(x => x.Property)
            .ToDictionary(g => g.Key, g => g.Select(x => x.Value).ToList());

        transposedItems.ShouldNotBeNull();
        transposedItems.Count.ShouldNotBe(0);
    }

    [Fact]
    public async Task GetSnapshotDepartmentStatistics_Test()
    {
        var inDto = new SnapshotDepartmentStatisticsInDto
        {
            StartTime = DateTime.Now.AddDays(-30),
            EndTime = DateTime.Now,
        };
        var items = await _biSnapshotApplication.GetSnapshotDepartmentStatistics(inDto).ToListAsync();
        items.ShouldNotBeNull();

        var a = await _biSnapshotApplication.GetSnapshotDepartmentAveTimeStatistics(inDto.Adapt<SnapshotDepartmentAveTimeStatisticsInDto>()).ToListAsync();
        a.ShouldNotBeNull();

        var b = _biSnapshotApplication.GetReTransactStatistics(inDto.Adapt<ReTransactStatisticsInDto>());
        b.ShouldNotBeNull();
        var c = b.ToJson();

        var bInDto = inDto.Adapt<ReTransactStatisticsDetailsInDto>();
        bInDto.OrgCode = "001";
        var d = await _biSnapshotApplication.GetReTransactStatisticsDetail(bInDto).ToListAsync();
        d.ShouldNotBeNull();

        var e = _biSnapshotApplication.GetIndustryStatistics(inDto.Adapt<IndustryStatisticsInDto>());
        e.ShouldNotBeNull();

        var areaPointsInDto = new SnapshotCountyPointsStatisticsInDto
        {
            StartTime = DateTime.Now.AddDays(-30),
            EndTime = DateTime.Now,
        };
        var areaPointsOutDto = _biSnapshotApplication.GetAreaPointsStatistics(areaPointsInDto).ToList();
        areaPointsOutDto.ShouldNotBeNull();
    }

    [Fact]
    public async Task GetCompliantStatisticsDetails_Test()
    {
        var inDto = new CompliantStatisticsDetailsInDto()
        {
            PageIndex = 1,
            PageSize = 20,
            StartTime = DateTime.Now.AddDays(-30),
            EndTime = DateTime.Now,
            FieldName = "orderCountNum",
        };
        var items = await _biSnapshotApplication.GetCompliantStatisticsDetails(inDto).ToPagedListAsync(inDto);
        items.Total.ShouldNotBe(0);
    }

    /// <summary>
    /// 检查合规统计
    /// </summary>
    /// <returns></returns>
    [Fact]
    public async Task GetCompliantStatistics_Test()
    {
        var inDto = new CompliantStatisticsInDto
        {
            StartTime = DateTime.Now.AddDays(-7),
            EndTime = DateTime.Now
        };
        var items = await _biSnapshotApplication.GetCompliantStatistics(inDto).ToListAsync();
        items.AddSumLine("OrgName");
        items.ShouldNotBeNull();
    }

    /// <summary>
    /// 市民红包审批统计
    /// </summary>
    /// <returns></returns>
    [Fact]
    public async Task GetRedPackAuditStatistics_Test()
    {
        var items = _biSnapshotApplication.GetRedPackAuditStatistics(new RedPackStatisticsInDto()
        {
            StartTime = DateTime.Now.AddDays(-7),
            EndTime = DateTime.Now
        });
        items.ShouldNotBeNull();
    }
}
