From 22f443f24ea48e8bf3940d0432f00edbb0a86c2f Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期四, 11 九月 2025 10:55:49 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.Core/Utils/StringUtils/StringUtils.cs |  115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 1 deletions(-)

diff --git a/FlexJobApi.Core/Utils/StringUtils/StringUtils.cs b/FlexJobApi.Core/Utils/StringUtils/StringUtils.cs
index 35b74c5..6a3a4b1 100644
--- a/FlexJobApi.Core/Utils/StringUtils/StringUtils.cs
+++ b/FlexJobApi.Core/Utils/StringUtils/StringUtils.cs
@@ -1,5 +1,9 @@
-锘縰sing System;
+锘縰sing Furion.DataValidation;
+using Furion.FriendlyException;
+using Mapster.Utils;
+using System;
 using System.Collections.Generic;
+using System.Data.SqlTypes;
 using System.Diagnostics.CodeAnalysis;
 using System.Linq;
 using System.Text;
@@ -32,6 +36,33 @@
             return !str.IsNull();
         }
 
+        public static Guid? ToGuid(this string str, string requiredMessage = null)
+        {
+            if (str.IsNotNull() && Guid.TryParse(str, out var guid) && guid != Guid.Empty) return guid;
+            if (requiredMessage.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s400, requiredMessage);
+            return null;
+        }
+
+        public static int? ToInt(this string str)
+        {
+            if (str.IsNotNull() && int.TryParse(str, out var @int)) return @int;
+            return null;
+        }
+
+        public static decimal? ToDecimal(this string str)
+        {
+            if (str.IsNotNull() && decimal.TryParse(str, out var @int)) return @int;
+            return null;
+        }
+
+        public static T? ToEnum<T>(this string str, string requiredMessage = null)
+             where T : struct
+        {
+            if (str.IsNotNull() && Enum.TryParse<T>(str, out var @enum)) return @enum;
+            if (requiredMessage.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s400, requiredMessage);
+            return null;
+        }
+
         /// <summary>
         /// 鑾峰彇澶嶆暟鑻辨枃鍚�
         /// </summary>
@@ -53,5 +84,87 @@
                 return singularName + "s";
             }
         }
+
+        public static bool CheckIsIdentityNumber18(this string identity)
+        {
+            return identity.IsNotNull() && identity.TryValidate(EnumValidationTypes.ValiIdentity).IsValid && identity.Length == 18;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鎬у埆
+        /// </summary>
+        /// <param name="identity"></param>
+        /// <returns></returns>
+        public static EnumUserGender? GetGender(this string identity)
+        {
+            if (identity.CheckIsIdentityNumber18())
+            {
+                return identity[16] % 2 == 0
+                    ? EnumUserGender.Female
+                    : EnumUserGender.Male;
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇鐢熸棩
+        /// </summary>
+        /// <param name="identity"></param>
+        /// <returns></returns>
+        public static DateTime? GetBirthday(this string identity)
+        {
+            if (identity.CheckIsIdentityNumber18())
+            {
+                return new DateTime(
+                    identity.Substring(6, 4).ToInt()!.Value,
+                    identity.Substring(10, 2).ToInt()!.Value,
+                    identity.Substring(12, 2).ToInt()!.Value);
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇骞撮緞
+        /// </summary>
+        /// <param name="identity"></param>
+        /// <returns></returns>
+        public static int? GetAge(this string identity)
+        {
+            if (identity.CheckIsIdentityNumber18())
+            {
+                var birthday = identity.GetBirthday();
+                return birthday.GetAge();
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鐢熸垚闅忔満瀛楃涓�
+        /// </summary>
+        /// <param name="length"></param>
+        /// <returns></returns>
+        public static string GenerateRandomString(int length)
+        {
+            if (length <= 0) return null;
+            var random = new Random();
+            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+            var result = new StringBuilder(length);
+            for (int i = 0; i < length; i++)
+            {
+                // 浠庡瓧绗﹂泦涓殢鏈洪�夋嫨涓�涓瓧绗�
+                int index = random.Next(chars.Length);
+                result.Append(chars[index]);
+            }
+            return result.ToString();
+        }
     }
 }

--
Gitblit v1.9.1