using System;
using Autofac;
using Tiobon.Core.Common.Extensions;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.Model.Models;
//using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using SqlSugar;
using Xunit;
using Xunit.Abstractions;

namespace Tiobon.Core.Tests;

public class OrmTest
{
    private readonly ITestOutputHelper _testOutputHelper;
    private readonly IBaseRepository<TiobonArticle> _baseRepository;
    DI_Test dI_Test = new DI_Test();

    public OrmTest(ITestOutputHelper testOutputHelper)
    {
        _testOutputHelper = testOutputHelper;

        var container = dI_Test.DICollections();

        _baseRepository = container.Resolve<IBaseRepository<TiobonArticle>>();
        _baseRepository.Db.Aop.OnLogExecuting = (sql, p) =>
        {
            _testOutputHelper.WriteLine("");
            _testOutputHelper.WriteLine("==================FullSql=====================", "", new string[] { sql.GetType().ToString(), GetParas(p), "【SQL语句】：" + sql });
            _testOutputHelper.WriteLine("【SQL语句】：" + sql);
            _testOutputHelper.WriteLine(GetParas(p));
            _testOutputHelper.WriteLine("==============================================");
            _testOutputHelper.WriteLine("");
        };
    }

    private static string GetParas(SugarParameter[] pars)
    {
        string key = "【SQL参数】：";
        foreach (var param in pars)
        {
            key += $"{param.ParameterName}:{param.Value}\n";
        }

        return key;
    }

    [Fact]
    public void MultiTables()
    {
        var sql = _baseRepository.Db.Queryable<TiobonArticle>()
            .AS($@"{nameof(TiobonArticle)}_TenantA")
            .ToSqlString();
        //_testOutputHelper.WriteLine(sql);

        _baseRepository.Db.MappingTables.Add(nameof(TiobonArticle), $@"{nameof(TiobonArticle)}_TenantA");

        var query = _baseRepository.Db.Queryable<TiobonArticle>()
            .LeftJoin<TiobonArticleComment>((a, c) => a.bID == c.bID);
        // query.QueryBuilder.AsTables.AddOrModify(nameof(TiobonArticle), $@"{nameof(TiobonArticle)}_TenantA");
        //query.QueryBuilder.AsTables.AddOrModify(nameof(TiobonArticleComment), $@"{nameof(TiobonArticleComment)}_TenantA");
        // query.QueryBuilder.AsTables.AddOrModify(nameof(TiobonArticleComment), $@"{nameof(TiobonArticleComment)}_TenantA");
        // query.QueryBuilder.AsTables.AddOrModify(nameof(SysUserInfo), $@"{nameof(SysUserInfo)}_TenantA");


        sql = query.ToSqlString();

        _testOutputHelper.WriteLine(sql);

        sql = _baseRepository.Db.Deleteable<TiobonArticle>().ToSqlString();
        _testOutputHelper.WriteLine(sql);
    }
}