using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using Tiobon.Core.Common;
using Tiobon.Core.Model;

namespace Tiobon.Core.IServices.BASE;

public interface IBaseServices<TEntity> where TEntity : class
{
    ISqlSugarClient Db { get; }

    Task<TEntity> QueryById(object objId);
    Task<TEntity> QueryById(object objId, bool blnUseCache = false);
    Task<List<TEntity>> QueryByIDs(object[] lstIds);

    Task<long> Add(TEntity model);

    Task<List<long>> Add(List<TEntity> listEntity);

    Task<bool> DeleteById(object id);

    Task<bool> Delete(TEntity model);

    Task<bool> DeleteByIds(object[] ids);

    Task<bool> Update(TEntity model);
    Task<bool> Update(List<TEntity> model);
    Task<bool> Update(TEntity entity, string where);

    Task<bool> Update(object operateAnonymousObjects);

    Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");

    Task<List<TEntity>> Query();
    Task<List<TEntity>> Query(string where);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
    Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression);
    Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
    Task<List<TEntity>> Query(string where, string orderByFields);
    Task<List<TEntity>> QuerySql(string sql, SugarParameter[] parameters = null);
    Task<DataTable> QueryTable(string sql, SugarParameter[] parameters = null);

    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields);
    Task<List<TEntity>> Query(string where, int top, string orderByFields);

    Task<List<TEntity>> Query(
        Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields);
    Task<List<TEntity>> Query(string where, int pageIndex, int pageSize, string orderByFields);


    Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null);

    Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(
        Expression<Func<T, T2, T3, object[]>> joinExpression,
        Expression<Func<T, T2, T3, TResult>> selectExpression,
        Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();
    Task<PageModel<TEntity>> QueryPage(PaginationModel pagination);

    #region 分表
    Task<TEntity> QueryByIdSplit(object objId);
    Task<List<long>> AddSplit(TEntity entity);
    Task<bool> DeleteSplit(TEntity entity, DateTime dateTime);
    Task<bool> UpdateSplit(TEntity entity, DateTime dateTime);
    Task<PageModel<TEntity>> QueryPageSplit(Expression<Func<TEntity, bool>> whereExpression, DateTime beginTime, DateTime endTime, int pageIndex = 1, int pageSize = 20, string orderByFields = null);
    #endregion
}


/// <summary>
/// 增删改查接口
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TEntityDto"></typeparam>
/// <typeparam name="TInsertDto"></typeparam>
/// <typeparam name="TEditDto"></typeparam>
public interface IBaseServices<TEntity, TEntityDto, TInsertDto, TEditDto> where TEntity : class
{
    ISqlSugarClient Db { get; }

    Task<bool> AnyAsync(object objId);

    Task<bool> AnyAsync(Expression<Func<TEntity, bool>> whereExpression);

    /// <summary>
    /// 根据ID查询实体数据是否存在
    /// </summary>
    /// <param name="objId"></param>
    /// <returns></returns>
    Task<TEntityDto> QueryById(object objId);
    Task<TEntityDto> QueryById(object objId, bool blnUseCache = false);
    Task<List<TEntityDto>> QueryByIDs(object[] lstIds);

    Task<ServiceFormResult<TEntityDto>> QueryForm(QueryForm body);

    Task<long> Add(TInsertDto model);

    Task<List<long>> Add(List<TInsertDto> listEntity);

    Task<bool> DeleteById(object id);
    Task<bool> DeleteById1(object id);

    Task<bool> Delete(TEntity model);

    Task<bool> DeleteByIds(object[] ids);
    Task<bool> DeleteByIds1(long[] ids);

    /// <summary>
    /// 根据表达式，删除实体
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    Task<bool> Delete(Expression<Func<TEntity, bool>> whereExpression);

    Task<bool> Update(long Id, TEditDto model);
    Task<bool> Update(long Id, TEditDto model, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");

    Task<bool> Update(Dictionary<long, TEditDto> editModels);
    Task<bool> Update(List<TEntity> model);
    Task<bool> Update(TEntity entity, string where);

    Task<bool> Update(object operateAnonymousObjects);

    Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");
    Task<bool> Update(List<TEntity> entitys, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string where = "");

    Task<List<TEntity>> Query();
    Task<List<TEntity>> Query(string where);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);

    Task<List<TEntityDto>> QueryDto(Expression<Func<TEntity, bool>> whereExpression);
    Task<TEntity> QuerySingle(object objId);
    Task<TEntity> QuerySingle(Expression<Func<TEntity, bool>> whereExpression);
    Task<TEntityDto> QuerySingleDto(Expression<Func<TEntity, bool>> whereExpression);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
    Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression);
    Task<List<TResult>> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields);
    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
    Task<List<TEntity>> Query(string where, string orderByFields);
    Task<List<TEntity>> QuerySql(string sql, SugarParameter[] parameters = null);
    Task<DataTable> QueryTable(string sql, SugarParameter[] parameters = null);

    Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields);
    Task<List<TEntity>> Query(string where, int top, string orderByFields);

    Task<List<TEntity>> Query(
        Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields);
    Task<List<TEntity>> Query(string where, int pageIndex, int pageSize, string orderByFields);


    Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null);
    Task<ServicePageResult<TEntityDto>> QueryFilterPage([FromBody] QueryBody body);
    Task<ServicePageResult<TEntityDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true);
    Task<ServiceResult<ExcelData>> ExportExcel(QueryExport body);

    Task<ServiceResult<ExcelData>> ImportExcel(IFormFile file, string menuName = null, long? MasterId = null);

    Task<ServiceResult<string>> DownloadExcel(string menuName);

    Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(
        Expression<Func<T, T2, T3, object[]>> joinExpression,
        Expression<Func<T, T2, T3, TResult>> selectExpression,
        Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();
    Task<PageModel<TEntity>> QueryPage(PaginationModel pagination);

    #region 分表
    Task<TEntity> QueryByIdSplit(object objId);
    Task<List<long>> AddSplit(TEntity entity);
    Task<bool> DeleteSplit(TEntity entity, DateTime dateTime);
    Task<bool> UpdateSplit(TEntity entity, DateTime dateTime);
    Task<PageModel<TEntity>> QueryPageSplit(Expression<Func<TEntity, bool>> whereExpression, DateTime beginTime, DateTime endTime, int pageIndex = 1, int pageSize = 20, string orderByFields = null);
    #endregion
}
