using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Hotline.Share;
using Hotline.Share.Requests;
using Hotline.Share.Tools;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using XF.Domain.Entities;

namespace Hotline.Repository.SqlSugar.Extensions
{
    public static class SqlSugarRepositoryExtensions
    {
        public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
            where TEntity : class, new()
        {
            RefAsync<int> total = 0;
            var items = await query.ToPageListAsync(pageIndex, pageSize, total, cancellationToken);
            return (total.Value, items);
        }

        public static async Task<(int Total, List<TEntity> Items)> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, PagedRequest dto, CancellationToken cancellationToken = default)
            where TEntity : class, new()
        {
            RefAsync<int> total = 0;
            var items = await query.ToPageListAsync(dto.PageIndex, dto.PageSize, total, cancellationToken);
            return (total.Value, items);
        }

        /// <summary>
        /// 分批次查询固定数量
        /// </summary>
        /// <returns></returns>
        public static Task<List<TEntity>> ToFixedListAsync<TEntity>(this ISugarQueryable<TEntity> query, IQueryFixedDto dto, CancellationToken cancellationToken)
        where TEntity : class, new()
        {
            if (dto.QueryCount == 0) dto.QueryCount = 50;
            return query.Skip(dto.QueryIndex * dto.QueryCount).Take(dto.QueryCount).ToListAsync(cancellationToken);
        }

        public static Task<List<TEntity>> ToFixedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int queryIndex, int? queryCount = null, CancellationToken cancellationToken = default)
            where TEntity : class, new()
        {
            if (queryCount is null or 0) queryCount = 50;
            return query.Skip(queryIndex * queryCount.Value).Take(queryCount.Value).ToListAsync(cancellationToken);
        }

        public static Task<List<TEntity>> ToPageListWithoutTotalAsync<TEntity>(this ISugarQueryable<TEntity> query, PagedRequest request, CancellationToken cancellationToken)
            where TEntity : class, new()
        {
            if (request.PageSize > 500) request.PageSize = 500;
            return query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToListAsync(cancellationToken);
        }

        public static Task<List<TEntity>> ToPageListWithoutTotalAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int? pageSize = null, CancellationToken cancellationToken = default)
            where TEntity : class, new()
        {
            if (pageSize is null or 0) pageSize = 20;
            if (pageSize > 500) pageSize = 500;
            return query.Skip((pageIndex - 1) * pageSize.Value).Take(pageSize.Value).ToListAsync(cancellationToken);
        }
    }
}
