using Hotline.SeedData;
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("组织架构")]
public class SystemOrganize : CreationSoftDeleteEntity
{
    // /// <summary>
    // /// 编码
    // /// <remarks>
    // /// （001001001） 解析：001,001,001
    // /// 部门编码结构中，中心为顶级
    // /// </remarks>
    // /// </summary>
    // public string Code { get; set; }

    /// <summary>
    /// 组织架构名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 组织架构简称
    /// </summary>
    public string ShortName { get; set; }

    /// <summary>
    /// 区域Code（行政区域代码）
    /// </summary>
    public string? AreaCode { get; set; }

    /// <summary>
    /// 区域名称（行政区域名称）
    /// </summary>
    public string? AreaName { get; set; }

    /// <summary>
    /// 部门级别
    /// </summary>
    public int Level { get; set; }

    /// <summary>
    /// 部门类型
    /// </summary>
    public EOrgType OrgType { get; set; }

    /// <summary>
    /// 上级ID
    /// </summary>
    public string? ParentId { get; set; }

    /// <summary>
    /// 上级名称
    /// </summary>
    public string? ParentName { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool IsEnable { get; set; }

    /// <summary>
    /// 是否为中心
    /// </summary>
    public bool IsCenter { get; set; }

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

    [SugarColumn(IsIgnore = true)]
    public string OrgTypeText => OrgType.GetDescription();

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

    /// <summary>
    /// 旧系统id
    /// </summary>
    public int? oldBmid { get; set; }

}

public static class OrgExtensions
{
    public static int CalcOrgLevel(this string orgCode)
    {
        //中心算做一级部门
        if (orgCode == OrgSeedData.CenterId) return 1;
        if (orgCode.Length % 3 != 0)
            throw new UserFriendlyException("非法部门Code");
        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;
    }
}