diff --git a/HybirdFrameworkCore/Attribute/ConstAttribute.cs b/HybirdFrameworkCore/Attribute/ConstAttribute.cs
new file mode 100644
index 0000000..8440e08
--- /dev/null
+++ b/HybirdFrameworkCore/Attribute/ConstAttribute.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HybirdFrameworkCore.Attribute
+{
+ ///
+ /// 常量特性
+ ///
+ //[SuppressSniffer]
+ [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
+ public class ConstAttribute : System.Attribute
+ {
+ public string Name { get; set; }
+
+ public ConstAttribute(string name)
+ {
+ Name = name;
+ }
+ }
+}
diff --git a/HybirdFrameworkCore/Attribute/NotEmptyAttribute.cs b/HybirdFrameworkCore/Attribute/NotEmptyAttribute.cs
new file mode 100644
index 0000000..108e6e4
--- /dev/null
+++ b/HybirdFrameworkCore/Attribute/NotEmptyAttribute.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HybirdFrameworkCore.Attribute
+{
+ ///
+ /// 验证值类型不能0
+ ///
+ public class NotEmptyAttribute : ValidationAttribute
+ {
+ public override bool IsValid(object value)
+ {
+ if (value is long id)
+ {
+ return id != 0;
+ }
+ return false;
+ }
+ }
+}
diff --git a/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs b/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs
index d1a6ab4..bca041d 100644
--- a/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs
+++ b/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs
@@ -8,7 +8,15 @@ public class PropertyAttribute : System.Attribute
public readonly double Scale;
public readonly int Start;
public readonly PropertyReadConstant Type;
-
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// 缩放
+ /// 舍入方式,用于处理浮点数的精度问题
+ /// 偏移量
public PropertyAttribute(int start, int length, PropertyReadConstant type = PropertyReadConstant.Bit,
double scale = 1, int round = 0, double offset = 0)
{
diff --git a/HybirdFrameworkCore/Autofac/Attribute/ScopeAttribute.cs b/HybirdFrameworkCore/Autofac/Attribute/ScopeAttribute.cs
index 5e34d5a..2651c76 100644
--- a/HybirdFrameworkCore/Autofac/Attribute/ScopeAttribute.cs
+++ b/HybirdFrameworkCore/Autofac/Attribute/ScopeAttribute.cs
@@ -4,7 +4,11 @@ public class ScopeAttribute : System.Attribute
{
public readonly string Scope;
- public ScopeAttribute(string scope)
+ ///
+ /// 向容器注入bean,默认单例模式
+ ///
+ ///
+ public ScopeAttribute(string scope = "SingleInstance")
{
Scope = scope;
}
diff --git a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
index 9510427..b47c643 100644
--- a/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
+++ b/HybirdFrameworkCore/Autofac/AutofacModuleRegister.cs
@@ -50,17 +50,14 @@ public class AutofacModuleRegister : Module
{
if (ScopeConst.InstancePerLifetimeScope == scope.Scope)
{
- Log.Debug($"register InstancePerLifetimeScope {type}");
instancePerLifetimeScopeList.Add(type);
}
else if (ScopeConst.InstancePerDependency == scope.Scope)
{
- Log.Debug($"register InstancePerDependency {type}");
instancePerDependencyList.Add(type);
}
else
{
- Log.Debug($"register SingleInstance {type}");
defaultList.Add(type);
}
}
diff --git a/HybirdFrameworkCore/Const/EndingConst.cs b/HybirdFrameworkCore/Const/EndingConst.cs
new file mode 100644
index 0000000..fa0b8bb
--- /dev/null
+++ b/HybirdFrameworkCore/Const/EndingConst.cs
@@ -0,0 +1,39 @@
+namespace HybirdFrameworkCore.Const;
+
+///
+/// 大小端常量,c#默认小端 DCBA
+///
+public class EndingConst
+{
+ ///
+ /// 字节序,C# 默认BA
+ ///
+ public enum ByteSeq
+ {
+ ///
+ ///
+ ///
+ AB,
+
+ ///
+ ///
+ ///
+ BA
+ }
+
+ ///
+ /// 字序 c# 默认DC
+ ///
+ public enum WordSeq
+ {
+ ///
+ ///
+ ///
+ CD,
+
+ ///
+ ///
+ ///
+ DC
+ }
+}
\ No newline at end of file
diff --git a/HybirdFrameworkCore/Entity/Result.cs b/HybirdFrameworkCore/Entity/Result.cs
index 7865213..3cd3831 100644
--- a/HybirdFrameworkCore/Entity/Result.cs
+++ b/HybirdFrameworkCore/Entity/Result.cs
@@ -8,6 +8,7 @@
///
/// 状态码
///
+
public int Status { get; set; } = 200;
///
/// 操作是否成功
@@ -53,7 +54,7 @@
///
/// 消息
///
- public static Result Fail(string msg = "失败")
+ public static Result Fail(string? msg = "失败")
{
return Message(false, msg, default);
}
diff --git a/HybirdFrameworkCore/Redis/RedisHelper.cs b/HybirdFrameworkCore/Redis/RedisHelper.cs
index d932523..45d9455 100644
--- a/HybirdFrameworkCore/Redis/RedisHelper.cs
+++ b/HybirdFrameworkCore/Redis/RedisHelper.cs
@@ -83,18 +83,31 @@ public class RedisHelper : IDisposable
///
/// 键
/// 对应的值,如果键不存在则返回null
- public string GetStrValue(string key)
+ public string? GetStrValue(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.StringGet(key);
}
+ ///
+ /// 先进先出队列
+ ///
+ ///
+ ///
+ ///
+ public void PublishAsync(string topic, string value)
+ {
+ ISubscriber sub = GetConnect().GetSubscriber();
+ sub.PublishAsync(topic, value);
+ }
+
+
///
/// 获取Redis中的值(异步版本)
///
/// 键
/// 一个表示异步操作的任务,任务的结果是对应的值,如果键不存在则返回null
- public async Task GetAsync(string key)
+ public async Task GetAsync(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringGetAsync(key);
@@ -105,11 +118,12 @@ public class RedisHelper : IDisposable
///
/// 键
/// 值
+ /// 值
/// 操作是否成功
- public bool SetKeyValueStr(string key, string value)
+ public bool SetKeyValueStr(string key, string value, TimeSpan? expiry = default(TimeSpan?))
{
var db = GetConnect().GetDatabase(_defaultDB);
- return db.StringSet(key, value);
+ return db.StringSet(key, value, expiry);
}
///
@@ -123,4 +137,40 @@ public class RedisHelper : IDisposable
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringSetAsync(key, value);
}
+
+
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ public List GetStrListValue(string key)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ RedisValue[] redisValues = db.ListRange(key);
+ // 将RedisValue数组转换为string的List
+ return redisValues.Select(value => (string)value).ToList();
+ }
+ ///
+ /// 设置一个 Redis LIST
+ ///
+ ///
+ ///
+ public void SetListInRedis(string key, List values)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ // 清空已存在的key(如果需要的话)
+ db.KeyDelete(key); // 注意:这将会删除key及其关联的所有数据
+ // 将List中的每个元素推送到Redis LIST的右边
+ values.ForEach(value => db.ListRightPush(key, value));
+ }
+
+
+ public bool Remove(string key)
+ {
+ var db = GetConnect().GetDatabase(_defaultDB);
+ return db.KeyDelete(key);
+ }
+
}
\ No newline at end of file
diff --git a/HybirdFrameworkCore/Utils/BitUtls.cs b/HybirdFrameworkCore/Utils/BitUtls.cs
index fcc87b8..2cd9048 100644
--- a/HybirdFrameworkCore/Utils/BitUtls.cs
+++ b/HybirdFrameworkCore/Utils/BitUtls.cs
@@ -1,4 +1,5 @@
using System.Text;
+using HybirdFrameworkCore.Const;
namespace HybirdFrameworkCore.Utils;
@@ -129,6 +130,65 @@ public static class BitUtls
#endregion
#region Func
+
+
+ ///
+ /// 按字节序反序
+ ///
+ ///
+ ///
+ public static byte[] ReverseByteSeq(byte[] bytes)
+ {
+ byte[] result = new byte [bytes.Length];
+ for (int i = 0, count = bytes.Length - 1; i < count; i += 2)
+ {
+ result[i] = bytes[i + 1];
+ result[i + 1] = bytes[i];
+ }
+
+ return result;
+ }
+
+ ///
+ /// 按字序反序
+ ///
+ ///
+ ///
+ public static byte[] ReverseWordSeq(byte[] bytes)
+ {
+ byte[] result = new byte [bytes.Length];
+ for (int i = 0, count = bytes.Length - 3; i < count; i += 4)
+ {
+ result[i] = bytes[i + 2];
+ result[i + 1] = bytes[i+3];
+ result[i + 2] = bytes[i];
+ result[i + 3] = bytes[i+1];
+ }
+
+ return result;
+ }
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static byte[] ProcessEnding(byte[] bytes, EndingConst.ByteSeq byteSeq, EndingConst.WordSeq wordSeq)
+ {
+ if (byteSeq == EndingConst.ByteSeq.BA)
+ {
+ bytes = ReverseByteSeq(bytes);
+ }
+
+ if (wordSeq == EndingConst.WordSeq.DC)
+ {
+ bytes = ReverseWordSeq(bytes);
+ }
+
+ return bytes;
+ }
public static string Bytes2BinaryString(byte[] bytes)
{
diff --git a/HybirdFrameworkCore/Utils/IEnum.cs b/HybirdFrameworkCore/Utils/IEnum.cs
new file mode 100644
index 0000000..cb11b55
--- /dev/null
+++ b/HybirdFrameworkCore/Utils/IEnum.cs
@@ -0,0 +1,9 @@
+namespace HybirdFrameworkCore.Utils;
+
+public interface IEnum
+{
+ public TK Key { get; set; }
+ public TV Value { get; set; }
+
+ public TV GetByKey(TK key);
+}
\ No newline at end of file
diff --git a/HybirdFrameworkCore/Winform/ModelBindControlAttribute.cs b/HybirdFrameworkCore/Winform/ModelBindControlAttribute.cs
index d9800a9..3712c12 100644
--- a/HybirdFrameworkCore/Winform/ModelBindControlAttribute.cs
+++ b/HybirdFrameworkCore/Winform/ModelBindControlAttribute.cs
@@ -1,7 +1,7 @@
namespace HybirdFrameworkCore.Winform;
[AttributeUsage(AttributeTargets.Property)]
-public class ModelBindControlAttribute : Attribute
+public class ModelBindControlAttribute : System.Attribute
{
private readonly string ModelName;
@@ -17,7 +17,7 @@ public class ModelBindControlAttribute : Attribute
}
[AttributeUsage(AttributeTargets.Property)]
-public class NoEditAttribute : Attribute
+public class NoEditAttribute : System.Attribute
{
private readonly string ModelName;
diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
index 08521d0..b8c2345 100644
--- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
+++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs
@@ -1,20 +1,24 @@
-using HslCommunication.Core;
+using HybirdFrameworkCore.Const;
using HybirdFrameworkCore.Utils;
using log4net;
namespace HybirdFrameworkDriver.ModbusTcpMaster;
+///
+///
+///
public static class ModbusDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
+
public static T Decode(byte[] bytes) where T : class, new()
{
T t = new T();
return Decode(bytes, t);
}
- public static T Decode(byte[] bytes, T t) where T : class, new()
+ public static T Decode(byte[] bytes, T t, EndingConst.ByteSeq byteSeq = EndingConst.ByteSeq.AB, EndingConst.WordSeq wordSeq = EndingConst.WordSeq.CD) where T : class, new()
{
var fields = t.GetType().GetProperties()
.Where(it => it.PropertyType.GetGenericTypeDefinition() == typeof(ModbusProperty<>))
@@ -29,19 +33,15 @@ public static class ModbusDecoder
}
var decodeUseBytes = new byte[bytes.Length];
- switch (ModbusTcpMaster.DataFormat)
- {
- case DataFormat.ABCD:
- for (var i = 0; i < bytes.Length; i++)
- if (i % 2 == 0)
- decodeUseBytes[i + 1] = bytes[i];
- else
- decodeUseBytes[i - 1] = bytes[i];
- break;
- case DataFormat.BADC:
- decodeUseBytes = bytes;
- break;
+ if (byteSeq == EndingConst.ByteSeq.AB)
+ {
+ decodeUseBytes = BitUtls.ReverseByteSeq(bytes);
+ }
+
+ if (wordSeq == EndingConst.WordSeq.CD)
+ {
+ decodeUseBytes = BitUtls.ReverseWordSeq(decodeUseBytes);
}
foreach (var field in fields)
@@ -112,7 +112,8 @@ public static class ModbusDecoder
return t;
}
- private static void SetPropertyValue(int startRegisterNo, ModbusProperty field, byte[] bytes, bool unSign = true)
+ private static void SetPropertyValue(int startRegisterNo, ModbusProperty field, byte[] bytes,
+ bool unSign = true)
{
var registerNo = field.RegisterNo;
var start = field.Start;
diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusProperty.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusProperty.cs
index 3cdc04f..c534cde 100644
--- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusProperty.cs
+++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusProperty.cs
@@ -1,12 +1,16 @@
namespace HybirdFrameworkDriver.ModbusTcpMaster;
+///
+///
+///
+///
public class ModbusProperty : IModbusProperty
{
///
///
/// 寄存器编号
/// 根据类型:Register为第几个寄存器
- /// 根据类型:Register 为 几个寄存器
+ /// 根据类型:Register 为 几个寄存器(2byte)
/// Register/Bit
/// 精度
/// 保留几位小数
diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
index 2787bca..498c201 100644
--- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
+++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs
@@ -1,6 +1,6 @@
using HslCommunication;
-using HslCommunication.Core;
using HslCommunication.ModBus;
+using HybirdFrameworkCore.Const;
using HybirdFrameworkCore.Utils;
using log4net;
@@ -18,7 +18,15 @@ public class ModbusTcpMaster
public int Port { get; set; } = 502;
- public static DataFormat DataFormat { get; set; } = DataFormat.ABCD;
+ ///
+ /// 字节序
+ ///
+ public EndingConst.ByteSeq ByteSeq { get; set; } = EndingConst.ByteSeq.AB;
+
+ ///
+ /// 字序
+ ///
+ public EndingConst.WordSeq WordSeq { get; set; } = EndingConst.WordSeq.CD;
public int Duration { get; set; } = 1000;
public bool Connected { get; set; }
@@ -48,7 +56,6 @@ public class ModbusTcpMaster
if (ModbusTcpNet == null)
{
ModbusTcpNet = new ModbusTcpNet(Ip, Port);
- ModbusTcpNet.DataFormat = DataFormat;
var result = ModbusTcpNet.ConnectServer();
connectId = ModbusTcpNet.ConnectionId;
if (result.IsSuccess)
@@ -114,6 +121,11 @@ public class ModbusTcpMaster
GetLog().Info("stop listen");
}
+ public void Reset()
+ {
+ ReadAction(this);
+ }
+
public byte[]? ReadRegister(int registerNo, int start, int length)
{
start = start % 16 == 0 ? start / 16 : start / 16 + 1;
@@ -184,6 +196,23 @@ public class ModbusTcpMaster
return ModbusTcpNet.ReadDiscrete(address, length);
}
+ ///
+ /// 将数据写入到Modbus的寄存器上去,需要指定起始地址和数据内容,如果富文本地址不指定,默认使用的功能码是 0x10
+ /// To write data to Modbus registers, you need to specify the start address and data content. If the rich text address is not specified, the default function code is 0x10
+ ///
+ /// 起始地址,比如"100","x=4;100","s=1;100","s=1;x=4;100"
+ /// 写入的数据,长度根据data的长度来指示
+ /// 返回写入结果
+ /// 富地址格式,支持携带站号信息,功能码信息,具体参照类的示例代码
+ ///
+ /// 此处演示批量写入的示例
+ ///
+ ///
+ public OperateResult Write(string address, byte[] value)
+ {
+ return ModbusTcpNet.Write(address, value);
+ }
+
public bool WriteValue(ModbusProperty property)
{
@@ -213,14 +242,13 @@ public class ModbusTcpMaster
OperateResult readResultRegister = ModbusTcpNet.Read("x=3;" + registerNo, 1);
if (readResultRegister.IsSuccess)
{
- switch (DataFormat)
+ if (ByteSeq == EndingConst.ByteSeq.AB)
{
- case DataFormat.ABCD:
readResultRegister.Content[1] = setValue[0];
- break;
- case DataFormat.BADC:
+ }
+ else
+ {
readResultRegister.Content[0] = setValue[0];
- break;
}
operateResult = ModbusTcpNet.Write("x=16;" + registerNo, readResultRegister.Content);
@@ -230,22 +258,7 @@ public class ModbusTcpMaster
//其他类型 String 占用几个寄存器 直接补0
else
{
- var preWriteCont = new byte[length * 2];
-
- switch (DataFormat)
- {
- case DataFormat.ABCD:
- for (var i = 0; i < setValue.Length; i++)
- if (i % 2 == 0)
- preWriteCont[i + 1] = setValue[i];
- else
- preWriteCont[i - 1] = setValue[i];
-
- break;
- case DataFormat.BADC:
- Array.Copy(setValue, preWriteCont, setValue.Length);
- break;
- }
+ var preWriteCont = BitUtls.ProcessEnding(setValue, ByteSeq, WordSeq);
operateResult = ModbusTcpNet.Write("x=16;" + registerNo, preWriteCont);
result = operateResult.IsSuccess;
@@ -253,23 +266,7 @@ public class ModbusTcpMaster
}
else if (setValue.Length == length * 2)
{
- var preWriteCont = new byte[setValue.Length];
-
- switch (DataFormat)
- {
- case DataFormat.ABCD:
- for (var i = 0; i < setValue.Length; i++)
- if (i % 2 == 0)
- preWriteCont[i + 1] = setValue[i];
- else
- preWriteCont[i - 1] = setValue[i];
-
- break;
- case DataFormat.BADC:
- //Array.Copy(setValue, preWriteCont, setValue.Length);
- break;
- }
-
+ var preWriteCont = BitUtls.ProcessEnding(setValue, ByteSeq, WordSeq);
operateResult = ModbusTcpNet.Write("x=16;" + registerNo, preWriteCont);
result = operateResult.IsSuccess;
}
diff --git a/Repository/BaseRepository.cs b/Repository/BaseRepository.cs
index dfa0cda..770092e 100644
--- a/Repository/BaseRepository.cs
+++ b/Repository/BaseRepository.cs
@@ -6,8 +6,6 @@ namespace Repository;
public abstract class BaseRepository where T : class, new()
{
- private readonly ISqlSugarClient DbBaseClient;
-
//private readonly IUnitOfWork _unitOfWork;
protected BaseRepository(ISqlSugarClient sqlSugar)
{
@@ -15,6 +13,8 @@ public abstract class BaseRepository where T : class, new()
DbBaseClient = sqlSugar;
}
+ public ISqlSugarClient DbBaseClient;
+
///
/// 根据主值查询单条数据
///
@@ -32,7 +32,7 @@ public abstract class BaseRepository where T : class, new()
///
/// 根据主值查询单条数据
///
- /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
+ /// Id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
/// 是否使用WITH(NOLOCK)
/// 数据实体
public async Task QueryByIdAsync(object objId, bool blUseNoLock = false)
@@ -88,98 +88,98 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToList();
}
-
-
- ///
- /// 根据条件查询表单数据(分页)
- ///
- /// 页数
- /// 每页几条数据
- /// 是否使用WITH(NOLOCK)
- ///
- public IPage QueryIPageByCause(QueryPageModel page, Expression> predicate)
+
+ ///
+ /// 根据条件查询表单数据(分页)
+ ///
+ /// 页数
+ /// 每页几条数据
+ /// 是否使用WITH(NOLOCK)
+ ///
+ public IPage QueryIPageByCause(QueryPageModel page, Expression> predicate)
+ {
+ if (null == predicate)
{
- if (null == predicate) {
- return this.QueryIPage(page);
- }
- int totalCount = 0;
+ return this.QueryIPage(page);
+ }
+ int totalCount = 0;
- List pageList = DbBaseClient
- .Queryable()
- .Where(predicate)
- .WithNoLockOrNot(false)
- .ToPageList(page.Page, page.PageSize, ref totalCount);
-
+ List pageList = DbBaseClient
+ .Queryable()
+ .Where(predicate)
+ .WithNoLockOrNot(false)
+ .ToPageList(page.Page, page.PageSize, ref totalCount);
- return new IPage(totalCount, page, pageList);
- }
- ///
- /// 根据条件查询表单数据(分页) 异步
- ///
- ///
- ///
- ///
- public async Task> QueryIPageByCauseAsync(QueryPageModel page, Expression> predicate)
- {
- if (null == predicate)
- {
- return await this.QueryIPageAsync(page);
- }
- RefAsync totalCount = 0;
+ return new IPage(totalCount, page, pageList);
+ }
+ ///
+ /// 根据条件查询表单数据(分页) 异步
+ ///
+ ///
+ ///
+ ///
+ public async Task> QueryIPageByCauseAsync(QueryPageModel page, Expression> predicate)
+ {
+ if (null == predicate)
+ {
+ return await this.QueryIPageAsync(page);
+ }
+ RefAsync totalCount = 0;
- List pageList = await DbBaseClient
- .Queryable()
- .Where(predicate)
- .WithNoLockOrNot(false)
- .ToPageListAsync(page.Page, page.PageSize, totalCount);
+ List pageList = await DbBaseClient
+ .Queryable()
+ .Where(predicate)
+ .WithNoLockOrNot(false)
+ .ToPageListAsync(page.Page, page.PageSize, totalCount);
- return new IPage(totalCount, page, pageList);
- }
-
-
- ///
- /// 查询表单所有数据(分页)
- ///
- /// 页数
- /// 每页几条数据
- /// 是否使用WITH(NOLOCK)
- ///
- public IPage QueryIPage(QueryPageModel page)
- {
- int totalCount = 0;
- //page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据
- //page.PageSize = page.PageSize == 0 ? 10 : page.PageSize;
- List pageList = DbBaseClient
- .Queryable()
- .WithNoLockOrNot(false)
- .ToPageList(page.Page, page.PageSize, ref totalCount);
+ return new IPage(totalCount, page, pageList);
+ }
- return new IPage(totalCount, page, pageList);
- }
- ///
- /// 查询表单所有数据(分页) 异步
- ///
- ///
- ///
- public async Task> QueryIPageAsync(QueryPageModel page)
- {
- RefAsync totalCount = 0;
+ ///
+ /// 查询表单所有数据(分页)
+ ///
+ /// 页数
+ /// 每页几条数据
+ /// 是否使用WITH(NOLOCK)
+ ///
+ public IPage QueryIPage(QueryPageModel page)
+ {
+ int totalCount = 0;
+ //page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据
+ //page.PageSize = page.PageSize == 0 ? 10 : page.PageSize;
- List pageList = await DbBaseClient
+ List pageList = DbBaseClient
.Queryable()
.WithNoLockOrNot(false)
- .ToPageListAsync(page.Page, page.PageSize, totalCount);
+ .ToPageList(page.Page, page.PageSize, ref totalCount);
- return new IPage(totalCount, page, pageList);
- }
+ return new IPage(totalCount, page, pageList);
+ }
+
+ ///
+ /// 查询表单所有数据(分页) 异步
+ ///
+ ///
+ ///
+ public async Task> QueryIPageAsync(QueryPageModel page)
+ {
+ RefAsync totalCount = 0;
+
+ List pageList = await DbBaseClient
+ .Queryable()
+ .WithNoLockOrNot(false)
+ .ToPageListAsync(page.Page, page.PageSize, totalCount);
+
+ return new IPage(totalCount, page, pageList);
+ }
///
/// 根据主值列表查询单条数据
@@ -239,6 +239,7 @@ public abstract class BaseRepository where T : class, new()
.ToList();
}
+
///
/// 根据条件查询数据
///
@@ -256,6 +257,66 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
+ public async Task> QueryListByClauseAsync(
+ bool isWhere, Expression> expression,
+ Expression> orderBy,
+ bool blUseNoLock = false)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(orderBy, OrderByType.Asc)
+ .WhereIF(isWhere, expression)
+ .WithNoLockOrNot(blUseNoLock)
+ .Select()
+ .ToListAsync();
+ }
+
+ public async Task> QueryListByClauseAsync(
+ Expression> expression, Expression> expression1
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .ToListAsync(expression1);
+ }
+
+ public async Task> QueryListBySelectClauseAsync(Expression> selectExpression)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Select(selectExpression)
+ .ToListAsync();
+ }
+
+ public async Task> QueryListByOrderClauseAsync(Expression> expression)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(expression, OrderByType.Asc)
+ .ToListAsync();
+ }
+ public async Task> QueryListByClauseAsync(
+ Expression>> include1,
+ Expression> expression)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Includes(include1)
+ .OrderBy(expression, OrderByType.Asc)
+ .ToListAsync();
+ }
+ public async Task> QueryListByInludeClauseAsync(
+ Expression> include1, Expression> expression, Expression> expression2
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Includes(include1)
+ .Where(expression)
+ .OrderBy(expression2, OrderByType.Asc)
+ .ToListAsync();
+ }
///
/// 根据条件查询数据
@@ -292,6 +353,89 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
+ public async Task> QueryListByClauseAsync(
+ bool isWhere, Expression> expression,
+ bool isWhere1, Expression> expression1,
+ bool isWhere2, Expression> expression2,
+ Expression> expression3,
+ bool blUseNoLock = false)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(expression3, OrderByType.Asc)
+ .WhereIF(isWhere, expression)
+ .WhereIF(isWhere1, expression1)
+ .WhereIF(isWhere2, expression2)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToListAsync();
+ }
+ public async Task> QueryListByClauseAsync(
+ Expression> expression,
+ bool isWhere1, Expression> expression1,
+ bool isWhere2, Expression> expression2,
+ Expression> expression3,
+ int pageNumber, int pageSize, RefAsync totalNumber,
+ bool blUseNoLock = false)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(expression3, OrderByType.Asc)
+ .Where(expression)
+ .WhereIF(isWhere1, expression1)
+ .WhereIF(isWhere2, expression2)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToPageListAsync(pageNumber, pageSize, totalNumber);
+ }
+ public async Task> QueryTreeByClauseAsync(
+ Expression> expression,
+ Expression>> childListExpression, Expression> parentIdExpression, object rootValue)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(expression, OrderByType.Asc)
+ .ToTreeAsync(childListExpression, parentIdExpression, rootValue);
+ }
+ public async Task> QueryTreeByClauseAsync(
+ Expression> expression,
+ Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .OrderBy(expression, OrderByType.Asc)
+ .ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds);
+ }
+ public async Task> QueryTreeByClauseAsync(
+ Expression> parentIdExpression, object primaryKeyValue, bool isContainOneself = true)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .ToChildListAsync(parentIdExpression, primaryKeyValue, isContainOneself);
+ }
+ public async Task> QueryTreeByClauseAsync(
+ Expression> expression,
+ Expression> expression1,
+ Expression>> childListExpression, Expression> parentIdExpression, object rootValue
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .OrderBy(expression1, OrderByType.Asc)
+ .ToTreeAsync(childListExpression, parentIdExpression, rootValue);
+ }
+ public async Task> QueryTreeByClauseAsync(
+ Expression> expression,
+ Expression> expression1,
+ Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .OrderBy(expression1, OrderByType.Asc)
+ .ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds);
+ }
+
///
/// 根据条件查询数据
@@ -330,6 +474,19 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
+ public async Task> QueryListByClauseAsync(
+ Expression> expression,
+ bool isWhere, Expression> whereIfExpression,
+ Expression> selectExpression
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .WhereIF(isWhere, whereIfExpression)
+ .Select(selectExpression)
+ .ToListAsync();
+ }
///
/// 根据条件查询一定数量数据
@@ -440,6 +597,47 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.FirstAsync(predicate);
}
+ public async Task> QueryByGroupByAsync(
+ Expression> expression,
+ Expression> expression2
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .GroupBy(expression)
+ .Select(expression2)
+ .ToListAsync();
+ }
+ public async Task> QueryByClauseAsync(Expression> predicate, Expression> selectExpression, bool blUseNoLock = false)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(predicate)
+ .Select(selectExpression)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToListAsync();
+ }
+
+ public async Task UpdateColumnsAsync(
+ T updateObj,
+ Expression> columns
+ )
+ {
+ return await DbBaseClient
+ .Updateable(updateObj)
+ .UpdateColumns(columns)
+ .ExecuteCommandAsync();
+ }
+
+
+ public async Task> QueryListByClauseAsync(
+ Expression> expression)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .ToListAsync();
+ }
///
/// 根据条件查询数据
@@ -476,17 +674,120 @@ public abstract class BaseRepository where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.FirstAsync(predicate);
}
+ public async Task> QueryByOrderByClauseAsync
+ (
+ Expression> expression,
+ Expression> expression1,
+ bool blUseNoLock = false)
+ {
+ return await DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .OrderBy(expression1, OrderByType.Asc)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToListAsync();
+ }
+ public List QueryByClauseToList(
+ Expression> expression, Expression> expression2,
+ Expression> expression1, bool blUseNoLock = false)
+ {
+ return DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .Where(expression2)
+ .OrderBy(expression1, OrderByType.Asc)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToList();
+ }
+ public List QueryByClauseToList(Expression> expression, Expression> expression2, bool blUseNoLock = false)
+ {
+ return DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .Where(expression2)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToList();
+ }
+ public List QueryByClauseToList(Expression> expression, Expression> expression1, bool blUseNoLock = false)
+ {
+ return DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .OrderBy(expression1, OrderByType.Asc)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToList();
+ }
+ public List QueryByClauseToList(Expression> expression)
+ {
+ return DbBaseClient
+ .Queryable()
+ .Where(expression)
+ .ToList();
+ }
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public async Task> QueryByClauseAsync(
+ Expression> joinExpression,
+ Expression> whereExpression,
+ Expression> orderExpression,
+ Expression> selectExpression
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .LeftJoin(joinExpression)
+ .Where(whereExpression)
+ .OrderBy(orderExpression, OrderByType.Asc)
+ .Select(selectExpression)
+ .ToListAsync();
+ }
+ public async Task> QueryByClauseAsync(
+ Expression> joinExpression,
+ Expression> whereExpression,
+ bool isWhere, Expression> whereifExpression,
+ Expression> orderExpression,
+ Expression> selectExpression
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .LeftJoin(joinExpression)
+ .Where(whereExpression)
+ .WhereIF(isWhere, whereifExpression)
+ .OrderBy(orderExpression, OrderByType.Asc)
+ .Select(selectExpression)
+ .ToListAsync();
+ }
///
/// 写入实体数据
///
/// 实体数据
///
- public int Insert(T entity)
+ public T Insert(T entity)
{
return DbBaseClient
.Insertable(entity)
- .ExecuteReturnIdentity();
+ .ExecuteReturnEntity();
+ }
+
+ ///
+ /// 写入或者更新实体数据
+ ///
+ /// 实体数据
+ ///
+ public int InsertOrUpdate(T entity)
+ {
+ return DbBaseClient.Storageable(entity).ExecuteCommand();
}
///
@@ -494,25 +795,30 @@ public abstract class BaseRepository where T : class, new()
///
/// 实体数据
///
- public async Task InsertAsync(T entity)
+ public async Task InsertAsync(T entity)
{
return await DbBaseClient
.Insertable(entity)
- .ExecuteReturnIdentityAsync();
+ .ExecuteReturnEntityAsync();
+ }
+ public async Task InsertReturnEntityAsync(T entity)
+ {
+ return await DbBaseClient
+ .Insertable(entity)
+ .ExecuteReturnEntityAsync();
}
-
///
/// 写入实体数据
///
/// 实体数据
/// 插入的列
///
- public int Insert(T entity, Expression> insertColumns = null)
+ public T Insert(T entity, Expression> insertColumns = null)
{
var insert = DbBaseClient.Insertable(entity);
if (insertColumns == null)
- return insert.ExecuteReturnIdentity();
- return insert.InsertColumns(insertColumns).ExecuteReturnIdentity();
+ return insert.ExecuteReturnEntity();
+ return insert.InsertColumns(insertColumns).ExecuteReturnEntity();
}
///
@@ -521,12 +827,12 @@ public abstract class BaseRepository where T : class, new()
/// 实体数据
/// 插入的列
///
- public async Task InsertAsync(T entity, Expression> insertColumns = null)
+ public async Task InsertAsync(T entity, Expression> insertColumns = null)
{
var insert = DbBaseClient.Insertable(entity);
if (insertColumns == null)
- return await insert.ExecuteReturnIdentityAsync();
- return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync();
+ return await insert.ExecuteReturnEntityAsync();
+ return await insert.InsertColumns(insertColumns).ExecuteReturnEntityAsync();
}
///
@@ -562,9 +868,10 @@ public abstract class BaseRepository where T : class, new()
///
/// 实体类
///
- public int Insert(List entity)
+ public bool Insert(List entity)
{
- return DbBaseClient.Insertable(entity.ToArray()).ExecuteReturnIdentity();
+ return DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntity();
+ ;
}
///
@@ -572,9 +879,9 @@ public abstract class BaseRepository where T : class, new()
///
/// 实体类
///
- public async Task InsertAsync(List entity)
+ public async Task InsertAsync(List entity)
{
- return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync();
+ return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntityAsync();
}
///
@@ -607,6 +914,64 @@ public abstract class BaseRepository where T : class, new()
return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync();
}
+ public async Task UpdateAsync(
+ Expression> expression
+ )
+ {
+ return await DbBaseClient
+ .Queryable()
+ .ClearFilter()
+ .AnyAsync(expression);
+ }
+
+ public int Update(Expression> columns,
+ Expression> expression)
+ {
+ return DbBaseClient.Updateable().SetColumns(columns).Where(expression).ExecuteCommand();
+ }
+
+
+ public async Task UpdateAsync(
+ Expression> columns,
+ Expression> expression
+ )
+ {
+ return await DbBaseClient
+ .Updateable()
+ .SetColumns(columns)
+ .Where(expression)
+ .ExecuteCommandAsync();
+ }
+
+ public async Task UpdateAsync(
+ Expression> columns
+ )
+ {
+ return await DbBaseClient
+ .Updateable()
+ .IgnoreColumns(columns)
+ .ExecuteCommandAsync();
+ }
+ public async Task UpdateAsync(
+ T updateObj,
+ bool ignoreAllNullColumns
+ )
+ {
+ return await DbBaseClient
+ .Updateable(updateObj)
+ .IgnoreColumns(ignoreAllNullColumns)
+ .ExecuteCommandAsync();
+ }
+ public async Task UpdateAsync(
+ T updateObj, bool ignoreAllNullColumns, Expression> columns
+ )
+ {
+ return await DbBaseClient
+ .Updateable(updateObj)
+ .IgnoreColumns(ignoreAllNullColumns)
+ .IgnoreColumns(columns)
+ .ExecuteCommandAsync();
+ }
///
/// 更新实体数据
///
@@ -627,6 +992,7 @@ public abstract class BaseRepository where T : class, new()
return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync();
}
+
///
/// 根据手写条件更新
///
@@ -771,7 +1137,23 @@ public abstract class BaseRepository where T : class, new()
{
return await DbBaseClient.Deleteable(entity).ExecuteCommandHasChangeAsync();
}
-
+ ///
+ /// 新增方法
+ /// 该方法用于:根据角色Id删除用户角色
+ ///
+ ///
+ ///
+ ///
+ ///
+ public async Task DeleteUserRoleByRoleId(
+ Expression> predicate,
+ Expression> selectExpression, Action action)
+ {
+ await DbBaseClient.Queryable()
+ .Where(predicate)
+ .Select(selectExpression)
+ .ForEachAsync(action);
+ }
///
/// 删除数据
///
@@ -1097,7 +1479,26 @@ public abstract class BaseRepository where T : class, new()
return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field);
}
-
+ public async Task> QueryPageAsync(
+ Expression> whereExpression,
+ bool isWhere1, Expression> expression1,
+ bool isWhere2, Expression> expression2,
+ bool isWhere3, Expression> expression3,
+ Expression> orderBy,
+ int pageNumber, int pageSize, RefAsync totalNumber,
+ bool blUseNoLock = false)
+ {
+ var page = await DbBaseClient
+ .Queryable()
+ .Where(whereExpression)
+ .WhereIF(isWhere1, expression1)
+ .WhereIF(isWhere2, expression2)
+ .WhereIF(isWhere3, expression3)
+ .OrderBy(orderBy, OrderByType.Asc)
+ .WithNoLockOrNot(blUseNoLock)
+ .ToPageListAsync(pageNumber, pageSize, totalNumber);
+ return page;
+ }
///
/// 查询-2表查询
@@ -1300,4 +1701,7 @@ public abstract class BaseRepository