DALHelper.cs 4.11 KB
using FreeSql.Aop;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace HHECS.Dal
{
    public class DALHelper
    {
        /// <summary>
        /// hack:正式使用请删除自动同步
        /// </summary>
        private static IFreeSql fsql;

        private static readonly object lockObj = new object();

        public static string Constr { get; set; }

        /// <summary>
        /// 获取freesql实例
        /// </summary>
        /// <returns></returns>
        public static IFreeSql GetFreeSql()
        {
            if (fsql == null)
            {
                lock (lockObj)
                {
                    if (fsql == null)
                    {
                        //   fsql = new FreeSql.FreeSqlBuilder()
                        //       .UseConnectionString(FreeSql.DataType.SqlServer, Constr)
                        //.UseAutoSyncStructure(true) //自动同步实体结构到数据库
                        //.Build(); //请务必定义成 Singleton 单例模式


                        #region SqlServer
                        fsql = new FreeSql.FreeSqlBuilder()
                       .UseConnectionString(FreeSql.DataType.SqlServer, Constr)
                       // .UseAutoSyncStructure(true) //自动同步实体结构到数据库
                       .Build(); //请务必定义成 Singleton 单例模式


                        fsql.Aop.AuditValue += Aop_AuditValue;
                        fsql.Aop.CurdAfter += Aop_CurdAfter;
                        #endregion
                    }
                }
            }
            return fsql;
        }

        /// <summary>
        /// 记录超时Sql
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Aop_CurdAfter(object sender, CurdAfterEventArgs e)
        {
            if (e.ElapsedMilliseconds > 2000)
            {
                Console.WriteLine($"SQL语句耗时过长:{e.ElapsedMilliseconds} -- {e.Sql}");
                NoticeBus.Notice($"SQL语句耗时过长:{e.ElapsedMilliseconds} -- {e.Sql}", Level.Warning);
            }
        }

        /// <summary>
        /// 插入更新时,自动赋值created和updated
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Aop_AuditValue(object sender, AuditValueEventArgs e)
        {
            if (e.AuditValueType == AuditValueType.Insert)
            {
                if (e.Property.Name == "Created")
                {
                    e.Value = DateTime.Now;
                }
            }
            if (e.AuditValueType == AuditValueType.Update)
            {
                if (e.Property.Name == "Updated")
                {
                    e.Value = DateTime.Now;
                }
            }
            if (e.AuditValueType == AuditValueType.InsertOrUpdate)
            {
                if (e.Property.Name == "Created" && e.Value == null)
                {
                    e.Value = DateTime.Now;
                }
                if (e.Property.Name == "Updated")
                {
                    e.Value = DateTime.Now;
                }
            }
        }

        /// <summary>
        /// 同步表结构
        /// </summary>
        public static void SyncTable()
        {
            GetFreeSql().CodeFirst.SyncStructure(GetTypesByNameSpace());
        }

        public static Type[] GetTypesByNameSpace()
        {
            List<Type> tableAssembies = new List<Type>();
            List<string> entitiesFullName = new List<string>()
            {
                "HHECS.Model.Entities"
            };
            foreach (Type type in Assembly.GetAssembly(typeof(BaseEntity<>)).GetExportedTypes())
                foreach (var fullname in entitiesFullName)
                    if (type.FullName.StartsWith(fullname) && type.IsClass && type.IsAbstract == false)
                        tableAssembies.Add(type);

            return tableAssembies.ToArray();
        }

    }
}