using Hotline.Application.StatisticalReport;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.StatisticalReport;
using Hotline.Tools;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using XF.Domain.Authentications;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Api.Filter;

namespace Hotline.Api.Controllers
{
    public class StatisticalReportController : BaseController
    {
        private readonly IMapper _mapper;
        private readonly ISessionContext _sessionContext;
        private readonly IOrderReportApplication _orderReportApplication;

        public StatisticalReportController(
            IMapper mapper,
            ISessionContext sessionContext,
            IOrderReportApplication orderReportApplication)
        {
            _mapper = mapper;
            _sessionContext = sessionContext;
            _orderReportApplication = orderReportApplication;
        }
        /// <summary>
        /// 坐席退件明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("getseatreturnorderlist")]
        [AllowAnonymous]
        public async Task<PagedDto<SeatReturnOrderListDto>> GetSeatReturnOrderList([FromQuery] SeatReturnOrderListRequestDto dto)
        {
            RefAsync<int> total = 0;
            var dataList = await _orderReportApplication.GetSeatReturnOrderList(dto)
            //转分页数据
            .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            return new PagedDto<SeatReturnOrderListDto>(total.Value, _mapper.Map<IReadOnlyList<SeatReturnOrderListDto>>(dataList));
        }

        /// <summary>
        /// 坐席退件明细--导出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("getseatreturnorderlist_export")]
		[LogFilterAlpha("导出日志")]
		public async Task<FileStreamResult> GetSeatReturnOrderListExport([FromBody] ExportExcelDto<SeatReturnOrderListRequestDto> dto)
        {
            var query = _orderReportApplication.GetSeatReturnOrderList(dto.QueryDto);

            List<SeatReturnOrderListDto> data;
            if (dto.IsExportAll)
            {
                data = await query.ToListAsync(HttpContext.RequestAborted);
            }
            else
            {
                RefAsync<int> total = 0;
                data = await query.ToPageListAsync(dto.QueryDto.PageIndex, dto.QueryDto.PageSize, total);
            }

            dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<SeatReturnOrderListDto>(dto.ColumnInfos);

            var dtos = data
                .Select(stu => _mapper.Map(stu, typeof(SeatReturnOrderListDto), dynamicClass))
                .Cast<object>()
                .ToList();

            var stream = ExcelHelper.CreateStream(dtos);
            return ExcelStreamResult(stream, "坐席退件明细数据");
        }

        /// <summary>
        /// 中心退件明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("getcenterreturnorderlist")]
        [AllowAnonymous]
        public async Task<PagedDto<SeatReturnOrderListDto>> GetCenterReturnOrderList([FromQuery] SeatReturnOrderListRequestDto dto)
        {
            RefAsync<int> total = 0;
            var dataList = await _orderReportApplication.GetCenterReturnOrderList(dto)
            //转分页数据
            .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            return new PagedDto<SeatReturnOrderListDto>(total.Value, _mapper.Map<IReadOnlyList<SeatReturnOrderListDto>>(dataList));
        }

        /// <summary>
        /// 中心退件明细--导出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("getcenterreturnorderlist_export")]
		[LogFilterAlpha("导出日志")]
		public async Task<FileStreamResult> GetCenterReturnOrderListExport([FromBody] ExportExcelDto<SeatReturnOrderListRequestDto> dto)
        {
            var query = _orderReportApplication.GetCenterReturnOrderList(dto.QueryDto);

            List<SeatReturnOrderListDto> data;
            if (dto.IsExportAll)
            {
                data = await query.ToListAsync(HttpContext.RequestAborted);
            }
            else
            {
                RefAsync<int> total = 0;
                data = await query.ToPageListAsync(dto.QueryDto.PageIndex, dto.QueryDto.PageSize, total);
            }

            dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<SeatReturnOrderListDto>(dto.ColumnInfos);

            var dtos = data
                .Select(stu => _mapper.Map(stu, typeof(SeatReturnOrderListDto), dynamicClass))
                .Cast<object>()
                .ToList();

            var stream = ExcelHelper.CreateStream(dtos);
            return ExcelStreamResult(stream, "中心退件明细数据");
        }

        /// <summary>
        /// 延期审批统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("order_delay_statisical_list")]
        public async Task<PagedDto<OrderDelayStatisicalReturnDto>> OrderDelayStatisicalList([FromQuery] OrderDelayStatisicalRequestDto dto)
        {
            var (total, items) = await _orderReportApplication.OrderDelayStatisicalList(dto).ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
            return new PagedDto<OrderDelayStatisicalReturnDto>(total, _mapper.Map<IReadOnlyList<OrderDelayStatisicalReturnDto>>(items));
        }

        /// <summary>
        /// 延期审批统计--导出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("order_delay_statisical_list_export")]
		[LogFilterAlpha("导出日志")]
		public async Task<FileStreamResult> OrderDelayStatisicalListExport([FromBody] ExportExcelDto<OrderDelayStatisicalRequestDto> dto)
        {
            var query = _orderReportApplication.OrderDelayStatisicalList(dto.QueryDto);

            List<OrderDelayStatisicalReturnDto> data;
            if (dto.IsExportAll)
            {
                data = await query.ToListAsync(HttpContext.RequestAborted);
            }
            else
            {
                RefAsync<int> total = 0;
                data = await query.ToPageListAsync(dto.QueryDto.PageIndex, dto.QueryDto.PageSize, total);
            }

            dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<OrderDelayStatisicalReturnDto>(dto.ColumnInfos);

            var dtos = data
                .Select(stu => _mapper.Map(stu, typeof(OrderDelayStatisicalReturnDto), dynamicClass))
                .Cast<object>()
                .ToList();

            var stream = ExcelHelper.CreateStream(dtos);
            return ExcelStreamResult(stream, "延期审批统计数据");
        }
    }
}
