refactor: ♻️ 国家代码表重构

[skip ci]
This commit is contained in:
tk 2024-12-06 10:27:08 +08:00 committed by nsnail
parent b24642e5c9
commit 4228bd5c32
15 changed files with 2927 additions and 2595 deletions

View File

@ -7,7 +7,7 @@ namespace NetAdmin.Infrastructure.Attributes;
/// https://github.com/countries/countries
/// </remarks>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
public sealed class CountryAttribute : Attribute
public sealed class CountryInfoAttribute : Attribute
{
/// <summary>
/// 三个字母的国家代码
@ -22,7 +22,7 @@ public sealed class CountryAttribute : Attribute
/// <summary>
/// 国际电话子呼号(区分同一呼号不同国家)
/// </summary>
public string CallingSubCode { get; set; }
public string[] CallingSubCode { get; set; }
/// <summary>
/// 货币代码
@ -37,7 +37,7 @@ public sealed class CountryAttribute : Attribute
/// <summary>
/// 官方语言代码
/// </summary>
public string Languages { get; set; }
public string[] Languages { get; set; }
/// <summary>
/// 国家全称
@ -49,8 +49,13 @@ public sealed class CountryAttribute : Attribute
/// </summary>
public string ShortName { get; set; }
/// <summary>
/// 时区
/// </summary>
public string[] Timezones { get; set; }
/// <summary>
/// 非正式名称
/// </summary>
public string UnofficialNames { get; set; }
public string[] UnofficialNames { get; set; }
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,17 @@
using Microsoft.OpenApi.Extensions;
namespace NetAdmin.Infrastructure.Extensions;
/// <summary>
/// CountryCodes 扩展方法
/// </summary>
public static class CountryCodesExtensions
{
/// <summary>
/// 获取国际电话呼号
/// </summary>
public static int GetCallingCode(this CountryCodes me)
{
return me.GetAttributeOfType<CountryInfoAttribute>().CallingCode;
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.OpenApi.Extensions;
namespace NetAdmin.Infrastructure.Utils;
/// <summary>
/// 电话号相关工具类
/// </summary>
public static class PhoneNumberHelper
{
private static readonly IEnumerable<(string CallingCode, CountryCodes CountryCode)> _countryList;
#pragma warning disable S3963
static PhoneNumberHelper()
#pragma warning restore S3963
{
_countryList = Enum.GetValues<CountryCodes>()
.SelectMany(x => {
var attribute = x.GetAttributeOfType<CountryInfoAttribute>();
// ReSharper disable once UseCollectionExpression
return (attribute.CallingSubCode ?? new[] { string.Empty }).Select(y => (attribute.CallingCode + y, x));
})
.OrderBy(x => x.Item1)
.ThenByDescending(x => x.x.GetAttributeOfType<CountryInfoAttribute>().IsPreferred)
.DistinctBy(x => x.Item1)
.OrderByDescending(x => x.Item1.Length);
}
/// <summary>
/// 电话号码转国家代码
/// </summary>
public static CountryCodes PhoneNumberToCountryCode(string phoneNumber)
{
return _countryList.First(x => phoneNumber.Replace("+", string.Empty).Trim().StartsWith(x.CallingCode, StringComparison.Ordinal)).CountryCode;
}
}

View File

@ -48,7 +48,7 @@ public sealed class ConfigService(BasicRepository<Sys_Config, long> rpo) //
{
req.ThrowIfInvalid();
#if DBTYPE_SQLSERVER
return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryConfigRsp>();
return (await UpdateReturnListAsync(req).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryConfigRsp>();
#else
return await UpdateAsync(req, null).ConfigureAwait(false) > 0
? await GetAsync(new QueryConfigReq { Id = req.Id }).ConfigureAwait(false)

View File

@ -31,7 +31,11 @@ public sealed class ConstantService : ServiceBase<IConstantService>, IConstantSe
static string[] GetDicValue(Enum y)
{
var ret = new[] { Convert.ToInt64(y, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), y.ResDesc<Ln>() };
var ret = new[] { Convert.ToInt64(y, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture), y.ResDesc<Ln>() };
if (y is CountryCodes) {
return [..ret, y.GetAttributeOfType<CountryInfoAttribute>().CallingCode.ToInvString()];
}
var indicate = y.GetAttributeOfType<IndicatorAttribute>()?.Indicate.ToLowerInvariant();
return indicate.NullOrEmpty() ? ret : [..ret, indicate];
}

View File

@ -66,7 +66,7 @@ public sealed class DeptService(BasicRepository<Sys_Dept, long> rpo) //
{
req.ThrowIfInvalid();
#if DBTYPE_SQLSERVER
return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDeptRsp>();
return (await UpdateReturnListAsync(req).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDeptRsp>();
#else
return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryDeptReq { Id = req.Id }).ConfigureAwait(false) : null;
#endif

View File

@ -55,7 +55,7 @@ public sealed class DicCatalogService(BasicRepository<Sys_DicCatalog, long> rpo)
{
req.ThrowIfInvalid();
return req.ParentId == 0 || await Rpo.Where(a => a.Id == req.ParentId).WithNoLockNoWait().AnyAsync().ConfigureAwait(false)
? await UpdateAsync(req, null).ConfigureAwait(false)
? await UpdateAsync(req).ConfigureAwait(false)
: throw new NetAdminInvalidOperationException(Ln.);
}

View File

@ -58,7 +58,7 @@ public sealed class DicContentService(BasicRepository<Sys_DicContent, long> rpo)
}
#if DBTYPE_SQLSERVER
return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDicContentRsp>();
return (await UpdateReturnListAsync(req).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDicContentRsp>();
#else
return await UpdateAsync(req, null).ConfigureAwait(false) > 0
? await GetAsync(new QueryDicContentReq { Id = req.Id }).ConfigureAwait(false)

View File

@ -55,7 +55,7 @@ public sealed class DocCatalogService(BasicRepository<Sys_DocCatalog, long> rpo)
{
req.ThrowIfInvalid();
return req.ParentId == 0 || await Rpo.Where(a => a.Id == req.ParentId).WithNoLockNoWait().AnyAsync().ConfigureAwait(false)
? await UpdateAsync(req, null).ConfigureAwait(false)
? await UpdateAsync(req).ConfigureAwait(false)
: throw new NetAdminInvalidOperationException(Ln.);
}

View File

@ -62,7 +62,7 @@ public sealed class DocContentService(BasicRepository<Sys_DocContent, long> rpo)
}
#if DBTYPE_SQLSERVER
return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDocContentRsp>();
return (await UpdateReturnListAsync(req).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryDocContentRsp>();
#else
return await UpdateAsync(req, null, [nameof(IFieldOwner.OwnerId), nameof(IFieldOwner.OwnerDeptId)]).ConfigureAwait(false) > 0
? await GetAsync(new QueryDocContentReq { Id = req.Id }).ConfigureAwait(false)

View File

@ -50,7 +50,7 @@ public sealed class MenuService(BasicRepository<Sys_Menu, long> rpo, IUserServic
{
req.ThrowIfInvalid();
#if DBTYPE_SQLSERVER
return (await UpdateReturnListAsync(req, null).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryMenuRsp>();
return (await UpdateReturnListAsync(req).ConfigureAwait(false)).FirstOrDefault()?.Adapt<QueryMenuRsp>();
#else
return await UpdateAsync(req, null).ConfigureAwait(false) > 0 ? await GetAsync(new QueryMenuReq { Id = req.Id }).ConfigureAwait(false) : null;
#endif

View File

@ -72,7 +72,7 @@ public sealed class SiteMsgService(BasicRepository<Sys_SiteMsg, long> rpo, Conte
// 主表
var entity = req.Adapt<Sys_SiteMsg>();
_ = await UpdateAsync(entity, null).ConfigureAwait(false);
_ = await UpdateAsync(entity).ConfigureAwait(false);
// 分表
await Rpo.SaveManyAsync(entity, nameof(entity.Roles)).ConfigureAwait(false);

View File

@ -67,7 +67,7 @@ public sealed class UserProfileService(BasicRepository<Sys_UserProfile, long> rp
public Task<int> EditAsync(EditUserProfileReq req)
{
req.ThrowIfInvalid();
return UpdateAsync(req.Adapt<Sys_UserProfile>(), null);
return UpdateAsync(req.Adapt<Sys_UserProfile>());
}
/// <inheritdoc />