using CrazyStudio.Core.Common.Tools.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XPrintServer.DataModel.Models;

namespace XPrintServer.DataModel
{
    public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
    {
        private SqlSugarClient? _dbContext = null;
        public SqlSugarRepository(ConnectionConfig config)
        {
            if (_dbContext == null)
            {
                _dbContext = new SqlSugarClient(config);
                ////调式代码 用来打印SQL 
                //_dbConfig.Aop.OnLogExecuting = (sql, pars) =>
                //{
                //    Console.WriteLine(sql + "\r\n" +
                //        _dbConfig.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                //    Console.WriteLine();
                //};
                _dbContext.Aop.DataExecuting = (_, entityInfo) =>
                {
                    switch (entityInfo.OperationType)
                    {
                        // 新增操作
                        case DataFilterType.InsertByObject:
                            // 主键Id
                            if (entityInfo.EntityColumnInfo.IsPrimarykey &&
                                entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(Guid))
                            {
                                var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
                                if (id.IsNullOrGuidEmpty()) entityInfo.SetValue(Guid.CreateVersion7());
                            }
                            break;
                    }
                };
                Context = _dbContext;
            }
        }
        /// <summary>
        /// 转换为首字母大写的驼峰命名规则
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns></returns>
        public string ConvertTF(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            string[] strItems = str.Split('_');
            string strItemTarget = string.Empty;
            if (strItems.Length > 1)
            {
                if (strItems[0].Length > 0)
                {
                    strItemTarget = strItems[0][0].ToString().ToUpper();
                    if (strItems[0].Length > 1)
                    {
                        strItemTarget += strItems[0].ToString().Substring(1);
                    }
                }
                for (int j = 1; j < strItems.Length; j++)
                {
                    string temp = strItems[j].ToString();
                    string temp1 = temp[0].ToString().ToUpper();
                    string temp2 = "";
                    temp2 = temp1 + temp.Remove(0, 1);
                    strItemTarget += temp2;
                }
                return strItemTarget;
            }
            else
            {
                if (str.Length > 1)
                {
                    strItemTarget = str[0].ToString().ToUpper() + str.Substring(1);
                }
                else
                {
                    strItemTarget = str[0].ToString().ToUpper();
                }
                return strItemTarget;
            }
        }
    }
}
