using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Xml.Serialization;

namespace Ewide.Core
{
    /// <summary>
    /// 角色表
    /// </summary>
    [Table("sys_role")]
    [Comment("角色表")]
    public class SysRole : DEntityBase, IEntityTypeBuilder<SysRole>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称")]
        public string Name { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        [Comment("编码")]
        public string Code { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        public int Sort { get; set; }

        /// <summary>
        /// 数据范围类型（字典 1全部数据 2本部门及以下数据 3本部门数据 4仅本人数据 5自定义数据）
        /// </summary>
        [Comment("数据范围类型")]
        public int DataScopeType { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Comment("备注")]
        public string Remark { get; set; }

        /// <summary>
        /// 状态（字典 0正常 1停用 2删除）
        /// </summary>
        [Comment("状态")]
        public CommonStatus Status { get; set; } = CommonStatus.ENABLE;

        /// <summary>
        /// 多对多（用户）
        /// </summary>
        [XmlIgnore]
        public ICollection<SysUser> SysUsers { get; set; }

        /// <summary>
        /// 多对多中间表（用户角色）
        /// </summary>
        [XmlIgnore]
        public List<SysUserRole> SysUserRoles { get; set; }

        /// <summary>
        /// 多对多（机构）
        /// </summary>
        [XmlIgnore]
        public ICollection<SysOrg> SysOrgs { get; set; }

        /// <summary>
        /// 多对多中间表（角色-机构 数据范围）
        /// </summary>
        [XmlIgnore]
        public List<SysRoleDataScope> SysRoleDataScopes { get; set; }

        [XmlIgnore]
        public ICollection<SysAreaCode> AreaCodes { get; set; }

        [XmlIgnore]
        public List<SysRoleArea> SysRoleAreas { get; set; }

        /// <summary>
        /// 多对多（菜单）
        /// </summary>
        [XmlIgnore]
        public ICollection<SysMenu> SysMenus { get; set; }

        /// <summary>
        /// 多对多中间表（角色-菜单）
        /// </summary>
        [XmlIgnore]
        public List<SysRoleMenu> SysRoleMenus { get; set; }

        /// <summary>
        /// 配置多对多关系
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        public void Configure(EntityTypeBuilder<SysRole> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(p => p.SysOrgs)
                .WithMany(p => p.SysRoles)
                .UsingEntity<SysRoleDataScope>(
                u => u.HasOne(c => c.SysOrg).WithMany(c => c.SysRoleDataScopes).HasForeignKey(c => c.SysOrgId),
                u => u.HasOne(c => c.SysRole).WithMany(c => c.SysRoleDataScopes).HasForeignKey(c => c.SysRoleId),
                u =>
                {
                    u.HasKey(c => new { c.SysRoleId, c.SysOrgId });
                });
            entityBuilder.HasMany(p => p.AreaCodes)
                .WithMany(p => p.SysRoles)
                .UsingEntity<SysRoleArea>(
                u => u.HasOne(c => c.Area).WithMany(c => c.SysRoleAreas).HasForeignKey(c => c.AreaCode),
                u => u.HasOne(c => c.SysRole).WithMany(c => c.SysRoleAreas).HasForeignKey(c => c.SysRoleId),
                u =>
                {
                    u.HasKey(c => new { c.SysRoleId, c.AreaCode });
                });
            entityBuilder.HasMany(p => p.SysMenus)
                .WithMany(p => p.SysRoles)
                .UsingEntity<SysRoleMenu>(
                u => u.HasOne(c => c.SysMenu).WithMany(c => c.SysRoleMenus).HasForeignKey(c => c.SysMenuId),
                u => u.HasOne(c => c.SysRole).WithMany(c => c.SysRoleMenus).HasForeignKey(c => c.SysRoleId),
                u =>
                {
                    u.HasKey(c => new { c.SysRoleId, c.SysMenuId });
                });
        }
    }
}
