using SqlSugar;
using System.Runtime.CompilerServices;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Settings
{
    public class SystemArea: CreationSoftDeleteEntity
    {
        /// <summary>
        /// 区域名称
        /// </summary>
        public string AreaName { get; set; }

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

        /// <summary>
        /// 简称
        /// </summary>
        public string AreaNameAbbreviation { get; set; }

        /// <summary>
        /// 是否可以修改
        /// </summary>
        [SugarColumn(DefaultValue = "f")]
        public bool IsCanModify { get; set; }


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

    }

    public static class AreaExtensions
    {
        /// <summary>
        /// 获取最末级区域对应编码（最后2位）
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public static string GetLastAreaCode(this string areaCode)
        {
            if (areaCode.Length < 6)
                throw new UserFriendlyException("非法区域编码");
            return areaCode.Substring(areaCode.Length - 2, 2);
        }
    }
}
