using Hotline.KnowledgeBase;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Dtos;
using Hotline.Share.Enums.KnowledgeBase;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Hotline.Settings;
using Hotline.Repository.SqlSugar.Extensions;
using XF.Domain.Exceptions;
using Hotline.Orders;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Requests;
using Hotline.Tools;
using MapsterMapper;
using Org.BouncyCastle.Utilities;
using Hotline.Api.Filter;

namespace Hotline.Api.Controllers.Bi
{
	public class BiKnowledgeController : BaseController
	{
		private readonly IKnowledgeRepository _knowledgeRepository;
		private readonly IMapper _mapper;
		public BiKnowledgeController(IKnowledgeRepository knowledgeRepository, IMapper mapper) 
		{
			_knowledgeRepository = knowledgeRepository;
			_mapper = mapper;
		}

		/// <summary>
		///知识库数据统计
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpGet("data_list")]
		public async Task<PagedDto<KnowledgeBiDataListVo>> DataList([FromQuery] KnowledgeBiDataListDto dto)
		{
            if (!dto.CreationTimeStart.HasValue || !dto.CreationTimeEnd.HasValue) 
				throw UserFriendlyException.SameMessage("请选择时间!");

			var query = _knowledgeRepository.DataList(dto, HttpContext.RequestAborted);
			switch (dto.SortField)
			{
				case "addNum":
					query = dto.SortRule == 0 ? query.OrderBy(x => x.AddNum) : query.OrderByDescending(x => x.AddNum);
					break;
				case "deleteNum":
					query = dto.SortRule == 0 ? query.OrderBy(x => x.DeleteNum) : query.OrderByDescending(x => x.DeleteNum);
					break;
				case "sendBackNum":
					query = dto.SortRule == 0 ? query.OrderBy(x => x.SendBackNum) : query.OrderByDescending(x => x.SendBackNum);
					break;
				case "passNum":
					query = dto.SortRule == 0 ? query.OrderBy(x => x.PassNum) : query.OrderByDescending(x => x.PassNum);
					break;
				case "publicNum":
					query = dto.SortRule == 0 ? query.OrderBy(x => x.PublicNum) : query.OrderByDescending(x => x.PublicNum);
					break;
			}
            var (total, items) = await query.ToPagedListAsync(dto, HttpContext.RequestAborted);

			var Total = new KnowledgeBiDataListVo
			{
				OrgName = "合计",
				OrgId = "0",
				OrgType = null,
				AddNum = items.Select(x => x.AddNum).Sum(),
				DeleteNum = items.Select(x => x.DeleteNum).Sum(),
				SendBackNum = items.Select(x => x.SendBackNum).Sum(),
				PassNum = items.Select(x => x.PassNum).Sum(),
				PublicNum = items.Select(x => x.PublicNum).Sum(),
			};
			items.Add(Total);

			return new PagedDto<KnowledgeBiDataListVo>(total, items);
		}

		/// <summary>
		/// 知识库数据统计导出
		/// </summary>
		/// <returns></returns>
		[HttpPost("data_list/_export")]
		[LogFilterAlpha("导出日志")]
		public async Task<FileStreamResult> DataList([FromBody] ExportExcelDto<KnowledgeBiDataListDto> dto)
		{
			if (!dto.QueryDto.CreationTimeStart.HasValue || !dto.QueryDto.CreationTimeEnd.HasValue) 
				throw UserFriendlyException.SameMessage("请选择时间!");
			var query = _knowledgeRepository.DataList(dto.QueryDto, HttpContext.RequestAborted);
			List<KnowledgeBiDataListVo> data;
			if (dto.IsExportAll)
			{
				data = await query.ToListAsync(HttpContext.RequestAborted);
			}
			else
			{
				var (_, items) = await query.ToPagedListAsync(dto.QueryDto, HttpContext.RequestAborted);
				data = items;
			}
			data.Add(new KnowledgeBiDataListVo
			{
				OrgName = "合计",
				OrgId = "0",
				OrgType = null,
				AddNum = data.Select(x => x.AddNum).Sum(),
				DeleteNum = data.Select(x => x.DeleteNum).Sum(),
				SendBackNum = data.Select(x => x.SendBackNum).Sum(),
				PassNum = data.Select(x => x.PassNum).Sum(),
				PublicNum = data.Select(x => x.PublicNum).Sum(),
			});
			dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass<KnowledgeBiDataListVo>(dto.ColumnInfos);

			var dtos = data
				.Select(stu => _mapper.Map(stu, typeof(KnowledgeBiDataListVo), dynamicClass))
				.Cast<object>()
				.ToList();
			var stream = ExcelHelper.CreateStream(dtos);
			return ExcelStreamResult(stream, "知识库数据统计");
		}
	}
}
