using Hotline.IndustryClassification;
using Hotline.SeedData;
using Hotline.Settings.Hotspots;
using SqlSugar;
using System.ComponentModel;
using XF.Domain.Exceptions;
using XF.Domain.Repository;
using XF.Utility.EnumExtensions;

namespace Hotline.Settings;

// [SugarIndex("unique_org_code", nameof(SystemOrganize.Code), OrderByType.Desc, true)]
[Description("组织架构")]
[SugarIndex("index_systemOrganize_parentId", nameof(SystemOrganize.ParentId), OrderByType.Desc)]
public class SystemOrganize : CreationSoftDeleteEntity
{
    // /// <summary>
    // /// 编码
    // /// <remarks>
    // /// （001001001） 解析：001,001,001
    // /// 部门编码结构中，中心为顶级
    // /// </remarks>
    // /// </summary>
    // [SugarColumn(ColumnDescription = "编码")]
    // public string Code { get; set; }

    /// <summary>
    /// 组织架构名称
    /// </summary>
    [SugarColumn(ColumnDescription = "组织架构名称")]
    public string Name { get; set; }

    /// <summary>
    /// 组织架构简称
    /// </summary>
    [SugarColumn(ColumnDescription = "组织架构简称")]
    public string ShortName { get; set; }

    /// <summary>
    /// 区域Code（行政区域代码）
    /// </summary>
    [SugarColumn(ColumnDescription = "区域Code（行政区域代码）")]
    public string? AreaCode { get; set; }

    /// <summary>
    /// 区域名称（行政区域名称）
    /// </summary>
    [SugarColumn(ColumnDescription = "区域名称（行政区域名称）")]
    public string? AreaName { get; set; }

    /// <summary>
    /// 部门级别
    /// </summary>
    [SugarColumn(ColumnDescription = "部门级别")]
    public int Level { get; set; }

    /// <summary>
    /// 部门类型
    /// </summary>
    [SugarColumn(ColumnDescription = "部门类型")]
    public EOrgType OrgType { get; set; }

    /// <summary>
    /// 上级ID
    /// </summary>
    [SugarColumn(ColumnDescription = "上级ID")]
    public string? ParentId { get; set; }

    /// <summary>
    /// 上级名称
    /// </summary>
    [SugarColumn(ColumnDescription = "上级名称")]
    public string? ParentName { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    [SugarColumn(ColumnDescription = "是否启用")]
    public bool IsEnable { get; set; }

    /// <summary>
    /// 是否为中心
    /// </summary>
    [SugarColumn(ColumnDescription = "是否为中心")]
    public bool IsCenter { get; set; }

    [SugarColumn(IsIgnore = true)]
    public List<SystemOrganize> Children { get; set; }

    [SugarColumn(IsIgnore = true , ColumnDescription = "部门类型描述")]
    public string OrgTypeText => OrgType.GetDescription();

    public void InitOrgLevel() => Level = Id.CalcOrgLevel();

    /// <summary>
    /// 旧系统id
    /// </summary>
    [SugarColumn(ColumnDescription = "旧系统id")]
    public int? oldBmid { get; set; }

    [Navigate(typeof(IndustryAssociations), nameof(IndustryAssociations.OrganizeId), nameof(IndustryAssociations.IndustrialManagementId))]
    public List<IndustrialManagement> IndustrialManagements { get; set; }
}

// ... (Rest of the OrgExtensions class unchanged)
public static class OrgExtensions
{
    public static int CalcOrgLevel(this string orgCode)
    {
        if(string.IsNullOrEmpty(orgCode))
            throw new UserFriendlyException("非法部门Id");
        //中心算做一级部门
        if (orgCode == OrgSeedData.CenterId) return 1;
        if (orgCode.Length % 3 != 0)
            throw new UserFriendlyException("非法部门Id");
        return (orgCode.Length / 3) - 1;
    }

    public static bool CheckIfOrgLevelIs(this string orgCode, int orgLevel) =>
        orgLevel == CalcOrgLevel(orgCode);

    /// <summary>
    /// 根据orgCode获取该部门所属对应级别的上级部门code(中心直属下级部门无效)
    /// </summary>
    /// <param name="orgId"></param>
    /// <param name="orgLevel">默认获取一级部门</param>
    /// <returns></returns>
    public static string GetHigherOrgId(this string orgId, int orgLevel = 1)
    {
        if (string.IsNullOrEmpty(orgId))
            throw UserFriendlyException.SameMessage("无效部门编码");
        //中心算做一级部门
        if (orgLevel == 1 && orgId.IsCenter()) return orgId;

        return orgId.Substring(0, 3 * (orgLevel + 1));
        //return orgId.Substring(0, 3 * orgLevel);
    }

    /// <summary>
    /// 获取最末级部门对应编号(最末3位)
    /// </summary>
    public static string GetLastOrgId(this string orgCode)
    {
        if (orgCode.Length < 3)
            throw new UserFriendlyException("非法部门编码");
        return orgCode.Substring(orgCode.Length - 3, 3);
    }

    public static bool IsCenter(this string orgId)
    {
        if (string.IsNullOrEmpty(orgId))
            throw UserFriendlyException.SameMessage("无效部门编码");
        return orgId == OrgSeedData.CenterId;
    }
}
