using CloudBuilder.Core.DependencyInjection.EFCore;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace CloudBuilder.Security.Entity
{
    public class CloudBuilderSecurityRegistEntity : IMasterRegistEntityType<ModelBuilder>
        {
        ///<summary>
        ///注入模块内的所有实体类
        ///</summary>
        ///<param name="builder"></param>
        public void RegistEntity(ModelBuilder builder)
        {
            string _assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
            //注入所有Entity实体类
            AddEntityType(builder, _assemblyName);
            //如果Entity实体类不是单一主键，要单独重写注入
            RegistKeys(builder);
        }

        public void AddEntityType(ModelBuilder builder, string _assemblyName)
        {
            RegistEntityHelper.AddMasterEntityType(builder, _assemblyName);
        }

        ///<summary>
        ///如果存在多个主键的，要在RegistKeys方法注入
        ///builder.Entity<xxxxxxEntity>().HasKey(c => new { c.TableName, c.FieldName });                
        ///builder.Entity<xxxxxxEntity>(eb => { eb.HasNoKey(); });
        ///</summary>
        ///<param name="builder"></param>
        public void RegistKeys(ModelBuilder builder)
        {
            //***BEGIN***

            builder.Entity<SecurityActorGroupEntity>().HasKey(c => new {c.ActorName,c.GroupName});
            builder.Entity<SecurityActorGroupViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionDependentEntity>().HasKey(c => new {c.GroupName,c.PermissionName,c.DependentDetailName});
            builder.Entity<SecurityFunctionMapEntity>().HasKey(c => new {c.FunctionNameParent,c.FunctionNameSon});
            builder.Entity<SecurityFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionDependentViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionEntity>().HasKey(c => new { c.GroupName, c.PermissionName });
            builder.Entity<SecurityMasterEntity>().HasKey(c => new {c.MasterCategory,c.MasterSubcategory,c.MasterCodeItem});
            builder.Entity<SecurityPermissionDependentEntity>().HasKey(c => new {c.PermissionName,c.DependentDetailName});
            builder.Entity<SecurityPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityTableDescriptionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserActorEntity>().HasKey(c => new {c.UserName,c.ActorName});
            builder.Entity<SecurityUserActorViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserDelegatedPermissionEntity>().HasKey(c => new {c.UserName,c.DelegatedByUser,c.PermissionName});
            builder.Entity<SecurityUserViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserWebLoginEntity>().HasKey(c => new {c.SessionId,c.Username});
            builder.Entity<SecurityPermissionFunctionMapEntity>().HasKey(c => new {c.PermissionName,c.FunctionName});
            builder.Entity<SysTableSchemaViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<LocaleEntity>().HasKey(c => new {c.GroupName,c.Lang,c.Name});
            builder.Entity<SecurityUserPreferenceEntity>().HasKey(c => new {c.Username,c.PreferenceKey});
            builder.Entity<SecurityFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityPermissionFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityActorGroupViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionDependentViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityInitializerTickViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityTableDescriptionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserActorViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserWebPagePreferenceEntity>().HasKey(c => new {c.Username,c.PreferenceKey,c.ProfileName});
            builder.Entity<SysTableSchemaViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityPermissionFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityActorGroupViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionDependentViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityGroupPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityInitializerTickViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityPermissionFunctionMapViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityPermissionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityTableDescriptionViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserActorViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityUserViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SysTableSchemaViewEntity>(eb => { eb.HasNoKey(); });
            builder.Entity<SecurityLockEntity>().HasKey(c => new {c.EntityName,c.PkeyId,c.LockedByActor});
            //***END***
        }
    }
}

