255 lines
9.5 KiB
C#
255 lines
9.5 KiB
C#
using HslCommunication;
|
|
using SqlSugar;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace guoke
|
|
{
|
|
/// <summary>
|
|
/// 扩展类 所有扩展写在这里
|
|
/// </summary>
|
|
public static class guokeExtensionClass
|
|
{
|
|
#region 数据库扩展
|
|
/// <summary>
|
|
/// 检查表是否存在,如果不存在则创建表
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="db"></param>
|
|
public static bool guokeCheckToCreate<T>(this SqlSugarScope db)
|
|
{
|
|
try
|
|
{
|
|
string tableName = db.guokeGetTableName(typeof(T));//获取表名
|
|
|
|
if (!db.guokeCheckTable(tableName))
|
|
{
|
|
if (db.MappingTables == null)
|
|
{
|
|
db.MappingTables = new MappingTableList();
|
|
}
|
|
db.MappingTables.Add(typeof(T).Name, tableName); // 类名 表名
|
|
db.CodeFirst.InitTables(typeof(T)); //创建表
|
|
LogService.Log.Info("仓库", $"创建表:{tableName},模型:{typeof(T).Name}");
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogService.Log.Error("数据库", $"检查创建表错误:{e.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检查表
|
|
/// </summary>
|
|
/// <param name="tableName"></param>
|
|
public static bool guokeCheckTable(this SqlSugarScope db, string tableName)
|
|
{
|
|
return db.DbMaintenance.IsAnyTable(tableName);//判断状态表是否存在
|
|
}
|
|
/// <summary>
|
|
/// 根据类型获取表名
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public static string guokeGetTableName(this SqlSugarScope db, Type type)
|
|
{
|
|
object[] attributes = type.GetCustomAttributes(typeof(SugarTable), false);
|
|
if (attributes.Length > 0)
|
|
{
|
|
SugarTable sugarTable = (SugarTable)attributes[0];
|
|
return sugarTable.TableName;
|
|
}
|
|
return type.Name;
|
|
}
|
|
/// <summary>
|
|
/// 检查表是否存在,如果不存在则创建表
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="db"></param>
|
|
public static bool guokeCheckToCreate<T>(this SqlSugarClient db)
|
|
{
|
|
try
|
|
{
|
|
string tableName = db.guokeGetTableName(typeof(T));//获取表名
|
|
|
|
if (!db.guokeCheckTable(tableName))
|
|
{
|
|
if (db.MappingTables == null)
|
|
{
|
|
db.MappingTables = new MappingTableList();
|
|
}
|
|
db.MappingTables.Add(typeof(T).Name, tableName); // 类名 表名
|
|
db.CodeFirst.InitTables(typeof(T)); //创建表
|
|
LogService.Log.Info("仓库", $"创建表:{tableName},模型:{typeof(T).Name}");
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogService.Log.Error("数据库", $"检查创建表错误:{e.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检查表
|
|
/// </summary>
|
|
/// <param name="tableName"></param>
|
|
public static bool guokeCheckTable(this SqlSugarClient db,string tableName)
|
|
{
|
|
return db.DbMaintenance.IsAnyTable(tableName);//判断状态表是否存在
|
|
}
|
|
/// <summary>
|
|
/// 根据类型获取表名
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public static string guokeGetTableName(this SqlSugarClient db,Type type)
|
|
{
|
|
object[] attributes = type.GetCustomAttributes(typeof(SugarTable), false);
|
|
if (attributes.Length > 0)
|
|
{
|
|
SugarTable sugarTable = (SugarTable)attributes[0];
|
|
return sugarTable.TableName;
|
|
}
|
|
return type.Name;
|
|
}
|
|
#endregion
|
|
#region 字节数组扩展
|
|
/// <summary>
|
|
/// 扩展方法,用于将一个 byte[] 追加到另一个 byte[] 后面
|
|
/// </summary>
|
|
/// <param name="originalArray"></param>
|
|
/// <param name="arrayToAppend"></param>
|
|
/// <returns>追加后的数组</returns>
|
|
public static byte[] Append(this byte[] originalArray, byte[] arrayToAppend)
|
|
{
|
|
if (originalArray == null)
|
|
{
|
|
return arrayToAppend;
|
|
}
|
|
if (arrayToAppend == null)
|
|
{
|
|
return originalArray;
|
|
}
|
|
|
|
// 创建一个 List<byte> 并将原始数组元素添加进去
|
|
List<byte> combinedList = new List<byte>(originalArray);
|
|
// 将待追加的数组元素添加到列表中
|
|
combinedList.AddRange(arrayToAppend);
|
|
|
|
// 将列表转换为 byte[] 数组并返回
|
|
return combinedList.ToArray();
|
|
}
|
|
#endregion
|
|
|
|
#region 字符串扩展
|
|
/// <summary>
|
|
/// 字符串转换为整数,失败时返回默认值
|
|
/// </summary>
|
|
/// <param name="str">字符串</param>
|
|
/// <param name="defaultValue">默认值</param>
|
|
/// <returns></returns>
|
|
public static int ToIntOrDefault(this string str, int defaultValue = 0)
|
|
{
|
|
return int.TryParse(str, out int result) ? result : defaultValue;
|
|
}
|
|
/// <summary>
|
|
/// 将字符串转换为长整数,失败时返回默认值
|
|
/// </summary>
|
|
/// <param name="str">字符串 </param>
|
|
/// <param name="defaultValue">默认值</param>
|
|
/// <returns></returns>
|
|
public static long ToLongOrDefault(this string str, long defaultValue = 0)
|
|
{
|
|
return long.TryParse(str, out long result) ? result : defaultValue;
|
|
}
|
|
/// <summary>
|
|
/// 将字符串转换为整数,带操作结果
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static OperateResult<int> guokeToInt(this string str)
|
|
{
|
|
return int.TryParse(str, out int result) ? OperateResult.CreateSuccessResult(result) : OperateResult.CreateFailedResult<int>(new OperateResult("转换失败"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用正则表达式从字符串中提取匹配的部分,带操作结果
|
|
/// </summary>
|
|
/// <param name="input">输入字符串</param>
|
|
/// <param name="pattern">正则表达式模式</param>
|
|
/// <returns>返回结果</returns>
|
|
public static OperateResult<string> guokeExtractWithRegex(this string input, string pattern)
|
|
{
|
|
// 创建正则表达式对象
|
|
Regex regex = new Regex(pattern);
|
|
|
|
// 匹配输入字符串
|
|
Match match = regex.Match(input);
|
|
|
|
// 如果匹配成功,返回匹配的值
|
|
if (match.Success && match.Groups.Count > 1)
|
|
{
|
|
return OperateResult.CreateSuccessResult(match.Groups[1].Value);
|
|
}
|
|
|
|
// 如果没有匹配,返回空字符串
|
|
return OperateResult.CreateFailedResult<string>(new OperateResult("提取失败"));
|
|
}
|
|
/// <summary>
|
|
/// 提取字符串右边的连续数字,带操作结果
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static OperateResult<int> guokeExtractRightNumbers(this string input)
|
|
{
|
|
// 定义正则表达式模式,用于匹配字符串右边的连续数字
|
|
string pattern = @"\d+$";
|
|
// 使用 Regex.Match 方法进行匹配
|
|
Match match = Regex.Match(input, pattern);
|
|
|
|
// 检查是否匹配成功
|
|
if (match.Success)
|
|
{
|
|
// 如果匹配成功,尝试将匹配到的字符串转换为 int 类型
|
|
if (int.TryParse(match.Value, out int result))
|
|
{
|
|
return OperateResult.CreateSuccessResult(result);
|
|
}
|
|
}
|
|
|
|
// 如果没有匹配到或者转换失败,返回 0
|
|
return OperateResult.CreateFailedResult<int>(new OperateResult("提取失败"));
|
|
}
|
|
/// <summary>
|
|
/// 提取字符串左边的连续数字,带操作结果
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static OperateResult<int> guokeExtractLeftNumbers(this string input)
|
|
{
|
|
// 定义正则表达式模式,用于匹配字符串右边的连续数字
|
|
string pattern = @"^\d+";
|
|
// 使用 Regex.Match 方法进行匹配
|
|
Match match = Regex.Match(input, pattern);
|
|
|
|
// 检查是否匹配成功
|
|
if (match.Success)
|
|
{
|
|
// 如果匹配成功,尝试将匹配到的字符串转换为 int 类型
|
|
if (int.TryParse(match.Value, out int result))
|
|
{
|
|
return OperateResult.CreateSuccessResult(result);
|
|
}
|
|
}
|
|
|
|
// 如果没有匹配到或者转换失败,返回 0
|
|
return OperateResult.CreateFailedResult<int>(new OperateResult("提取失败"));
|
|
}
|
|
#endregion
|
|
}
|
|
}
|