using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace CrazyStudio.Core.Common.Tools.Extensions
{
    public static class SqlSugarExtension
    {
        /// <summary>
        /// 绑定SqlSugar数据库信息
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddSqlSugar(this IServiceCollection services, IConfiguration configuration)
        {
            var conn = configuration.GetConnectionString("DBConnStr");

            var configConnection = new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = conn,
                IsAutoCloseConnection = true,    //自动释放
                InitKeyType = InitKeyType.Attribute  // 一定要配置此属性 ，否则生成数据库会报错
            };

            SqlSugarScope sqlSugar = new SqlSugarScope(configConnection, db =>
            {
//#if DEBUG
//                //单例参数配置，所有上下文生效
//                db.Aop.OnLogExecuting = (sql, pars) =>
//                {
//                    Console.WriteLine(sql);//输出sql
//                };
//#endif
            });

            services.AddSingleton<ISqlSugarClient>(sqlSugar);
        }
    }
}
