using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Snapshot;
using Microsoft.AspNetCore.Mvc;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Tools;
using Hotline.Settings.Hotspots;
using Hotline.Share.Requests;
using SqlSugar;
using XF.Domain.Authentications;
using Hotline.Settings;
using Hotline.Caching.Interfaces;
using Hotline.Share.Enums.Order;
using Hotline.Share.Enums.Snapshot;
using XF.Utility.EnumExtensions;
using Amazon.Runtime.Internal.Util;
using System.Collections;
using Mapster;
using Hotline.Share.Dtos.Settings;
using XF.Domain.Repository;
using Hotline.Configurations;
using Microsoft.Extensions.Options;
using Hotline.Snapshot.IRepository;
using Hotline.Application.Snapshot.Contracts;

namespace Hotline.Api.Controllers.Snapshot;

/// <summary>
/// 随手拍统计
/// </summary>
public class BiSnapshotController : BaseController
{
    private readonly IBiSnapshotApplication _biSnapshotApplication;
    private readonly IIndustryRepository _industryRepository;
    private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
    private readonly IRepository<SystemArea> _areaRepository;
    private readonly IOptionsSnapshot<AppConfiguration> _appOptions;

    public BiSnapshotController(IBiSnapshotApplication biSnapshotApplication, IIndustryRepository industryRepository, ISystemDicDataCacheManager systemDicDataCacheManager, IRepository<SystemArea> areaRepository, IOptionsSnapshot<AppConfiguration> appOptions)
    {
        _biSnapshotApplication = biSnapshotApplication;
        _industryRepository = industryRepository;
        _systemDicDataCacheManager = systemDicDataCacheManager;
        _areaRepository = areaRepository;
        _appOptions = appOptions;
    }

    /// <summary>
    /// 随手拍统计基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("statistics/basedata")]
    public async Task<Dictionary<string, object>> GetSnapshotStatisticsBaseDataAsync()
    {
        return new Dictionary<string, object>()
        {
            { "industry", await _industryRepository.GetDataBaseAsync() }
        };
    }

    /// <summary>
    /// 随手拍统计
    /// </summary>
    /// <returns></returns>
    [HttpGet("statistics")]
    public async Task<SnapshotStatisticsOutDto> GetSnapshotStatisticsAsync([FromQuery] SnapshotStatisticsInDto dto)
        => await _biSnapshotApplication.GetSnapshotStatisticsAsync(dto, HttpContext.RequestAborted);

    /// <summary>
    /// 随手拍统计详情集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("statistics/details")]
    public async Task<PagedDto<SnapshotStatisticsDetailOutDto>> GetSnapshotStatisticsDetailAsync([FromQuery] SnapshotStatisticsDetailInDto dto)
        => (await _biSnapshotApplication.GetSnapshotStatisticsDetail(dto).ToPagedListAsync(dto)).ToPaged();


    /// <summary>
    /// 市民红包审核统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("redpack/audit")]
    public IList<RedPackStatisticsOutDto> GetRedPackAuditStatisticsAsync([FromQuery] RedPackStatisticsInDto dto)
        => _biSnapshotApplication.GetRedPackAuditStatistics(dto);

    /// <summary>
    /// 市民红包审核统计详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("redpack/audit/details")]
    public async Task<PagedDto<RedPackStatisticsDetailsOutDto>> GetRedPackAuditStatisticsDetailsAsync([FromQuery] RedPackStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetRedPackAuditStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 热点类型小类统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("hotspot-statistics")]
    public async Task<IList<HotspotStatisticsOutDto>> GetHotspotStatisticsAsync([FromQuery] HotspotStatisticsInDto dto)
        => await _biSnapshotApplication.GetHotspotStatistics(dto).ToListAsync();

    /// <summary>
    /// 热点类型小类统计明细
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("hotspot-statistics-detail")]
    public async Task<PagedDto<HotspotStatisticsDetailsOutDto>> GetHotspotStatisticsDetailAsync([FromQuery] HotspotStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.HotspotStatisticsDetail(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 热点类型-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("hotspot-data-statistics")]
    public async Task<IList<HotspotDataStatisticsOutDto>> GetHotspotDataStatisticsAsync([FromQuery] HotspotDataStatisticsInDto dto)
    {
        var items = await _biSnapshotApplication.GetHotspotDataStatisticsAsync(dto).ToListAsync();
        if (items.Count != 0)
        {
            items.Add(new HotspotDataStatisticsOutDto
            {
                Name = "合计",
                OrderCount = items.Sum(x => x.OrderCount),
            });
        }
        return items;
    }

    /// <summary>
    /// 办件统计-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("processing-statistics")]
    public async Task<IList<SnapshotProcessingStatisticsOutDto>> GetSnapshotProcessingStatistics([FromQuery] SnapshotProcessingStatisticsInDto dto)
        => await _biSnapshotApplication.GetSnapshotProcessingStatistics(dto).ToListAsync();

    /// <summary>
    /// 办件统计明细-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("processing-statistics-detail")]
    public async Task<PagedDto<SnapshotProcessingStatisticsDetailsOutDto>> GetSnapshotProcessingStatisticsDetails([FromQuery] SnapshotProcessingStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetSnapshotProcessingStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 网格员办理情况统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("guider-work-statistics")]
    public async Task<IList<GuiderWorkStatisticsOutDto>> GetGuiderWorkStatisticsAsync([FromQuery] GuiderWorkStatisticsInDto dto)
        => await _biSnapshotApplication.GetGuiderWorkStatisticsAsync(dto).ToListAsync();

    /// <summary>
    /// 网格员办理情况统计详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("guider-work-statistics-detail")]
    public async Task<PagedDto<GuiderWorkStatisticsDetailsOutDto>> GetGuiderWorkStatisticsDetailsAsync([FromQuery] GuiderWorkStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetGuiderWorkStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 网格员系统工单状态日志基础数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("guider-work-log/basedata")]
    public async Task<Dictionary<string, object>> GetGuiderWorkLogsAsyncBasedataAsync()
    {
        return new Dictionary<string, object>
        {
            { "orderStatus", EnumExts.GetDescriptions<EGuiderSystemReplyType>()},
        };
    }

    /// <summary>
    /// 网格员系统工单状态日志
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("guider-work-log")]
    public async Task<PagedDto<GuiderWorkLogsOutDto>> GetGuiderWorkLogsAsync([FromQuery] GuiderWorkLogsInDto dto)
        => (await _biSnapshotApplication.GetGuiderWorkLogs(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 重复件-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("duplicate")]
    public async Task<PagedDto<DuplicateItemsOutDto>> GetDuplicateItemsAsync([FromQuery] DuplicateItemsInDto dto)
    => (await _biSnapshotApplication.GetDuplicateItems(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 社区统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("community-statistics")]
    public async Task<IList<CommunityStatisticsOutDto>> GetCommunityStatisticsAsync([FromQuery] CommunityStatisticsInDto dto)
        => await _biSnapshotApplication.GetCommunityStatistics(dto).ToListAsync();

    /// <summary>
    /// 社区统计-详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("community-statistics-detail")]
    public async Task<PagedDto<CommunityStatisticsDetailsOutDto>> GetCommunityStatisticsDetailsAsync([FromQuery] CommunityStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetCommunityStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 随手拍区域统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("county-redpack-statistics")]
    public async Task<IList<CountyRedPackStatisticsOutDto>> GetCountyRedPackStatisticsAsync([FromQuery] CountyRedPackStatisticsInDto dto)
        => await _biSnapshotApplication.GetCountyRedPackStatistics(dto).ToListAsync();

    /// <summary>
    /// 部门考核统计-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("department-statistics")]
    public async Task<IList<SnapshotDepartmentStatisticsOutDto>> GetSnapshotDepartmentStatisticsAsync([FromQuery] SnapshotDepartmentStatisticsInDto dto)
        => await _biSnapshotApplication.GetSnapshotDepartmentStatistics(dto).ToListAsync();

    /// <summary>
    /// 部门平均办理时间-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("department-avetime-statistics")]
    public async Task<IList<SnapshotDepartmentAveTimeStatisticsOutDto>> GetSnapshotDepartmentAveTimeStatisticsAsync([FromQuery] SnapshotDepartmentAveTimeStatisticsInDto dto)
        => await _biSnapshotApplication.GetSnapshotDepartmentAveTimeStatistics(dto).ToListAsync();

    /// <summary>
    /// 部门平均办理时间-随手拍-详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("department-avetime-statistics-detail")]
    public async Task<PagedDto<SnapshotDepartmentAveTimeStatisticsDetailsOutDto>> GetSnapshotDepartmentAveTimeStatisticsDtailsAsync([FromQuery] SnapshotDepartmentAveTimeStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetSnapshotDepartmentAveTimeStatisticsDtails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 检查合规统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("compliant-statistics")]
    public async Task<IList<CompliantStatisticsOutDto>> CompliantStatisticsAsync([FromQuery] CompliantStatisticsInDto dto)
    {
        var items = await _biSnapshotApplication.GetCompliantStatistics(dto).ToListAsync();
        items.AddSumLine("OrgName");
        return items;
    }

    /// <summary>
    /// 行业统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("industry-statistics")]
    public async Task<IndustryStatisticsOutDto> GetIndustryStatisticsAsync([FromQuery] IndustryStatisticsInDto dto)
    {
        var areaCode = _appOptions.Value.AreaCode;
        return new IndustryStatisticsOutDto
        {
            Headers = await _areaRepository.Queryable().Where(m => m.ParentId == areaCode).Select(m => new SystemDicDataOutDto { 
                Id = m.Id,
                DicDataName = m.AreaName,
                DicDataValue = m.AreaName,
            }).ToListAsync(),
            Data = _biSnapshotApplication.GetIndustryStatistics(dto)
        };
    }

    /// <summary>
    /// 行业统计详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("industry-statistics-detail")]
    public async Task<PagedDto<IndustryStatisticsDetailsOutDto>> GetIndustryStatisticsDetailsAsync([FromQuery] IndustryStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetIndustryStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 检查合规统计-详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("compliant-statistics-detail")]
    public async Task<PagedDto<CompliantStatisticsDetailsOutDto>> CompliantStatisticsDetailsAsync([FromQuery] CompliantStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetCompliantStatisticsDetails(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 重办统计-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("re_transact-statistics")]
    public async Task<ReTransactStatisticsOutDto> GetReTransactStatisticsAsync([FromQuery] ReTransactStatisticsInDto dto)
    {
        var headers = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.InstaShotSpecialReason).Adapt<IList<SystemDicDataOutDto>>();
        return new ReTransactStatisticsOutDto
        {
            Headers = headers,
            Data = _biSnapshotApplication.GetReTransactStatistics(dto)
        };
    }

    /// <summary>
    /// 重办统计详情-随手拍
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("re_transact-statistics-detail")]
    public async Task<PagedDto<ReTransactStatisticsDetailsOutDto>> GetReTransactStatisticsDetailAsync([FromQuery] ReTransactStatisticsDetailsInDto dto)
        => (await _biSnapshotApplication.GetReTransactStatisticsDetail(dto).ToPagedListAsync(dto)).ToPaged();

    /// <summary>
    /// 随手拍区域积分统计
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("county_points")]
    public async Task<IList<SnapshotCountyPointsStatisticsOutDto>> GetAreaPointsStatistics([FromQuery] SnapshotCountyPointsStatisticsInDto dto)
        => await _biSnapshotApplication.GetAreaPointsStatistics(dto).ToListAsync(HttpContext.RequestAborted);
}
