using FreeSql;
using Microsoft.Extensions.Configuration;
using Volo.Abp.DependencyInjection;

namespace JiShe.CollectBus.FreeSql
{
    public class FreeSqlProvider : IFreeSqlProvider, ISingletonDependency
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FreeSqlProvider"/> class.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        public FreeSqlProvider(IConfiguration configuration)
        {
            GetInstance(configuration);
        }

        public FreeSqlCloud<DbEnum> Instance { get; set; } = new();
        public FreeSqlCloud<DbEnum> GetInstance(IConfiguration configuration)
        {
            Instance = new FreeSqlCloud<DbEnum>
            {
                DistributeTrace = log => Console.WriteLine(log.Split('\n')[0].Trim())
            };

            Instance.Register(DbEnum.EnergyDB, () => new FreeSqlBuilder()
                .UseConnectionString(DataType.SqlServer, configuration.GetConnectionString(DbEnum.EnergyDB.ToString()))
                .Build());

            Instance.Register(DbEnum.PrepayDB, () => new FreeSqlBuilder()
                .UseConnectionString(DataType.SqlServer, configuration.GetConnectionString(DbEnum.PrepayDB.ToString()))
                .Build());
            return Instance;
        }

    }
}
