using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.Extensions
* 唯一标识：59de67c2-e9f9-4aac-b985-ba5c001f9f88
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-15 09:53:53
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Extensions
{
    public static class SqlsugarSetup
    {
        /// <summary>
        /// 注册SqlSugar
        /// </summary>
        /// <param name="services"></param>
        public static void AddSqlSugarSetup(this IServiceCollection services)
        {
            services.AddSingleton<ISqlSugarClient>(x =>
            {
                var appConfig = x.GetService<AppConfig>();

                var connectConfigList = new List<ConnectionConfig>();
                if (appConfig.sqlConfig != null)
                {
                    foreach (var item in appConfig.sqlConfig)
                    {
                        #region 加载sqlite数据库地址
                        if (item.configId == "local")
                        {
                            item.connStr = $"Data Source={System.Environment.CurrentDirectory}//data//data.db";
                        }
                        #endregion
                        var config = new ConnectionConfig()
                        {
                            ConfigId = item.configId,
                            DbType = (DbType)item.dbType,
                            ConnectionString = item.connStr,
                            InitKeyType = InitKeyType.Attribute,
                            IsAutoCloseConnection = true,
                        };
                        connectConfigList.Add(config);
                    }
                }
                SqlSugarScope Db = new SqlSugarScope(connectConfigList, db =>
                {
                    db.Aop.OnLogExecuting = (sql, pars) => { };
                });

                return Db;
            });

            //缓存本机配置
            services.AddSingleton<List<BaseConfigInfo>>(x =>
            {
                var service = x.GetService<IBaseConfigInfoService>();

                var info = service.Query(x => x.IsFlag == 1);

                return info;
            });

            //缓存托盘队列
            services.AddSingleton<List<RealPalletTask>>(x =>
            {
                var service = x.GetService<IRealPalletTaskService>();

                var info = service.Query();

                return info;
            });

            //缓存条码队列
            services.AddSingleton<List<RealBarCodeTask>>(x =>
            {
                var service = x.GetService<IRealBarCodeTaskService>();

                var info = service.Query();

                return info;
            });
        }
    }
}
