同步框架代码

master
smartwyy 6 months ago
parent c2b3dd03ab
commit 70d00183dd

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkCore.Attribute
{
/// <summary>
/// 常量特性
/// </summary>
//[SuppressSniffer]
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class ConstAttribute : System.Attribute
{
public string Name { get; set; }
public ConstAttribute(string name)
{
Name = name;
}
}
}

@ -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
{
/// <summary>
/// 验证值类型不能0
/// </summary>
public class NotEmptyAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value is long id)
{
return id != 0;
}
return false;
}
}
}

@ -8,7 +8,15 @@ public class PropertyAttribute : System.Attribute
public readonly double Scale;
public readonly int Start;
public readonly PropertyReadConstant Type;
/// <summary>
///
/// </summary>
/// <param name="start"></param>
/// <param name="length"></param>
/// <param name="type"></param>
/// <param name="scale">缩放</param>
/// <param name="round">舍入方式,用于处理浮点数的精度问题</param>
/// <param name="offset">偏移量</param>
public PropertyAttribute(int start, int length, PropertyReadConstant type = PropertyReadConstant.Bit,
double scale = 1, int round = 0, double offset = 0)
{

@ -4,7 +4,11 @@ public class ScopeAttribute : System.Attribute
{
public readonly string Scope;
public ScopeAttribute(string scope)
/// <summary>
/// 向容器注入bean默认单例模式
/// </summary>
/// <param name="scope"></param>
public ScopeAttribute(string scope = "SingleInstance")
{
Scope = scope;
}

@ -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);
}
}

@ -0,0 +1,39 @@
namespace HybirdFrameworkCore.Const;
/// <summary>
/// 大小端常量c#默认小端 DCBA
/// </summary>
public class EndingConst
{
/// <summary>
/// 字节序,C# 默认BA
/// </summary>
public enum ByteSeq
{
/// <summary>
///
/// </summary>
AB,
/// <summary>
///
/// </summary>
BA
}
/// <summary>
/// 字序 c# 默认DC
/// </summary>
public enum WordSeq
{
/// <summary>
///
/// </summary>
CD,
/// <summary>
///
/// </summary>
DC
}
}

@ -8,6 +8,7 @@
/// <summary>
/// 状态码
/// </summary>
public int Status { get; set; } = 200;
/// <summary>
/// 操作是否成功
@ -53,7 +54,7 @@
/// </summary>
/// <param name="msg">消息</param>
/// <returns></returns>
public static Result<T> Fail(string msg = "失败")
public static Result<T> Fail(string? msg = "失败")
{
return Message(false, msg, default);
}

@ -83,18 +83,31 @@ public class RedisHelper : IDisposable
/// </summary>
/// <param name="key">键</param>
/// <returns>对应的值如果键不存在则返回null</returns>
public string GetStrValue(string key)
public string? GetStrValue(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.StringGet(key);
}
/// <summary>
/// 先进先出队列
/// </summary>
/// <param name="topic"></param>
/// <param name="value"></param>
/// <returns></returns>
public void PublishAsync(string topic, string value)
{
ISubscriber sub = GetConnect().GetSubscriber();
sub.PublishAsync(topic, value);
}
/// <summary>
/// 获取Redis中的值异步版本
/// </summary>
/// <param name="key">键</param>
/// <returns>一个表示异步操作的任务任务的结果是对应的值如果键不存在则返回null</returns>
public async Task<string> GetAsync(string key)
public async Task<string?> GetAsync(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringGetAsync(key);
@ -105,11 +118,12 @@ public class RedisHelper : IDisposable
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expiry">值</param>
/// <returns>操作是否成功</returns>
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);
}
/// <summary>
@ -123,4 +137,40 @@ public class RedisHelper : IDisposable
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringSetAsync(key, value);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public List<string> GetStrListValue(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
RedisValue[] redisValues = db.ListRange(key);
// 将RedisValue数组转换为string的List
return redisValues.Select(value => (string)value).ToList();
}
/// <summary>
/// 设置一个 Redis LIST
/// </summary>
/// <param name="key"></param>
/// <param name="values"></param>
public void SetListInRedis(string key, List<string> values)
{
var db = GetConnect().GetDatabase(_defaultDB);
// 清空已存在的key如果需要的话
db.KeyDelete(key); // 注意这将会删除key及其关联的所有数据
// 将List<string>中的每个元素推送到Redis LIST的右边
values.ForEach(value => db.ListRightPush(key, value));
}
public bool Remove(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.KeyDelete(key);
}
}

@ -1,4 +1,5 @@
using System.Text;
using HybirdFrameworkCore.Const;
namespace HybirdFrameworkCore.Utils;
@ -130,6 +131,65 @@ public static class BitUtls
#region Func
/// <summary>
/// 按字节序反序
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 按字序反序
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
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;
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
/// <param name="byteSeq"></param>
/// <param name="wordSeq"></param>
/// <returns></returns>
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)
{
return string.Join(" ", bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));

@ -0,0 +1,9 @@
namespace HybirdFrameworkCore.Utils;
public interface IEnum<TK, TV>
{
public TK Key { get; set; }
public TV Value { get; set; }
public TV GetByKey(TK key);
}

@ -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;

@ -1,20 +1,24 @@
using HslCommunication.Core;
using HybirdFrameworkCore.Const;
using HybirdFrameworkCore.Utils;
using log4net;
namespace HybirdFrameworkDriver.ModbusTcpMaster;
/// <summary>
///
/// </summary>
public static class ModbusDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
public static T Decode<T>(byte[] bytes) where T : class, new()
{
T t = new T();
return Decode<T>(bytes, t);
}
public static T Decode<T>(byte[] bytes, T t) where T : class, new()
public static T Decode<T>(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)
if (byteSeq == EndingConst.ByteSeq.AB)
{
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];
decodeUseBytes = BitUtls.ReverseByteSeq(bytes);
}
break;
case DataFormat.BADC:
decodeUseBytes = bytes;
break;
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<T>(int startRegisterNo, ModbusProperty<T> field, byte[] bytes, bool unSign = true)
private static void SetPropertyValue<T>(int startRegisterNo, ModbusProperty<T> field, byte[] bytes,
bool unSign = true)
{
var registerNo = field.RegisterNo;
var start = field.Start;

@ -1,12 +1,16 @@
namespace HybirdFrameworkDriver.ModbusTcpMaster;
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class ModbusProperty<T> : IModbusProperty
{
/// <summary>
/// </summary>
/// <param name="registerNo">寄存器编号</param>
/// <param name="start">根据类型Register为第几个寄存器</param>
/// <param name="length">根据类型Register 为 几个寄存器</param>
/// <param name="length">根据类型Register 为 几个寄存器(2byte)</param>
/// <param name="type">Register/Bit</param>
/// <param name="scale">精度</param>
/// <param name="round">保留几位小数</param>

@ -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;
/// <summary>
/// 字节序
/// </summary>
public EndingConst.ByteSeq ByteSeq { get; set; } = EndingConst.ByteSeq.AB;
/// <summary>
/// 字序
/// </summary>
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);
}
/// <summary>
/// 将数据写入到Modbus的寄存器上去需要指定起始地址和数据内容如果富文本地址不指定默认使用的功能码是 0x10<br />
/// 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
/// </summary>
/// <param name="address">起始地址,比如"100""x=4;100""s=1;100","s=1;x=4;100"</param>
/// <param name="value">写入的数据长度根据data的长度来指示</param>
/// <returns>返回写入结果</returns>
/// <remarks>富地址格式,支持携带站号信息,功能码信息,具体参照类的示例代码</remarks>
/// <example>
/// 此处演示批量写入的示例
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\Modbus.cs" region="WriteExample1" title="Write示例" />
/// </example>
public OperateResult Write(string address, byte[] value)
{
return ModbusTcpNet.Write(address, value);
}
public bool WriteValue<T>(ModbusProperty<T> property)
{
@ -213,14 +242,13 @@ public class ModbusTcpMaster
OperateResult<byte[]> 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;
}

@ -6,8 +6,6 @@ namespace Repository;
public abstract class BaseRepository<T> where T : class, new()
{
private readonly ISqlSugarClient DbBaseClient;
//private readonly IUnitOfWork _unitOfWork;
protected BaseRepository(ISqlSugarClient sqlSugar)
{
@ -15,6 +13,8 @@ public abstract class BaseRepository<T> where T : class, new()
DbBaseClient = sqlSugar;
}
public ISqlSugarClient DbBaseClient;
/// <summary>
/// 根据主值查询单条数据
/// </summary>
@ -32,7 +32,7 @@ public abstract class BaseRepository<T> where T : class, new()
/// <summary>
/// 根据主值查询单条数据
/// </summary>
/// <param name="objId">id必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <param name="objId">Id必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns>数据实体</returns>
public async Task<T> QueryByIdAsync(object objId, bool blUseNoLock = false)
@ -89,97 +89,97 @@ public abstract class BaseRepository<T> where T : class, new()
.ToList();
}
/// <summary>
/// 根据条件查询表单数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPageByCause(QueryPageModel page, Expression<Func<T, bool>> predicate)
/// <summary>
/// 根据条件查询表单数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPageByCause(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate)
{
if (null == predicate) {
return this.QueryIPage(page);
}
int totalCount = 0;
return this.QueryIPage(page);
}
int totalCount = 0;
List<T> pageList = DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
List<T> pageList = DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
return new IPage<T>(totalCount, page, pageList);
}
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据条件查询表单数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageByCauseAsync(QueryPageModel page, Expression<Func<T, bool>> predicate)
/// <summary>
/// 根据条件查询表单数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageByCauseAsync(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate)
{
if (null == predicate)
{
return await this.QueryIPageAsync(page);
}
RefAsync<int> totalCount = 0;
return await this.QueryIPageAsync(page);
}
RefAsync<int> totalCount = 0;
List<T> pageList = await DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
List<T> pageList = await DbBaseClient
.Queryable<T>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
return new IPage<T>(totalCount, page, pageList);
}
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 查询表单所有数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPage(QueryPageModel page)
{
int totalCount = 0;
//page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据
//page.PageSize = page.PageSize == 0 ? 10 : page.PageSize;
/// <summary>
/// 查询表单所有数据(分页)
/// </summary>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPage<T> QueryIPage(QueryPageModel page)
{
int totalCount = 0;
//page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据
//page.PageSize = page.PageSize == 0 ? 10 : page.PageSize;
List<T> pageList = DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
List<T> pageList = DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
return new IPage<T>(totalCount, page, pageList);
}
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 查询表单所有数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageAsync(QueryPageModel page)
{
RefAsync<int> totalCount = 0;
/// <summary>
/// 查询表单所有数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public async Task<IPage<T>> QueryIPageAsync(QueryPageModel page)
{
RefAsync<int> totalCount = 0;
List<T> pageList = await DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
List<T> pageList = await DbBaseClient
.Queryable<T>()
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
return new IPage<T>(totalCount, page, pageList);
}
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据主值列表查询单条数据
@ -239,6 +239,7 @@ public abstract class BaseRepository<T> where T : class, new()
.ToList();
}
/// <summary>
/// 根据条件查询数据
/// </summary>
@ -256,6 +257,66 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public async Task<List<TResult>> QueryListByClauseAsync<TResult>(
bool isWhere, Expression<Func<T, bool>> expression,
Expression<Func<T, object>> orderBy,
bool blUseNoLock = false)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(orderBy, OrderByType.Asc)
.WhereIF(isWhere, expression)
.WithNoLockOrNot(blUseNoLock)
.Select<TResult>()
.ToListAsync();
}
public async Task<List<TResult>> QueryListByClauseAsync<TResult>(
Expression<Func<T, bool>> expression, Expression<Func<T, TResult>> expression1
)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.ToListAsync(expression1);
}
public async Task<List<TResult>> QueryListBySelectClauseAsync<TResult>(Expression<Func<T, TResult>> selectExpression)
{
return await DbBaseClient
.Queryable<T>()
.Select(selectExpression)
.ToListAsync();
}
public async Task<List<T>> QueryListByOrderClauseAsync(Expression<Func<T, object>> expression)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(expression, OrderByType.Asc)
.ToListAsync();
}
public async Task<List<T>> QueryListByClauseAsync<TReturn1>(
Expression<Func<T, List<TReturn1>>> include1,
Expression<Func<T, object>> expression)
{
return await DbBaseClient
.Queryable<T>()
.Includes(include1)
.OrderBy(expression, OrderByType.Asc)
.ToListAsync();
}
public async Task<List<T>> QueryListByInludeClauseAsync<TReturn1>(
Expression<Func<T, TReturn1>> include1, Expression<Func<T, bool>> expression, Expression<Func<T, object>> expression2
)
{
return await DbBaseClient
.Queryable<T>()
.Includes(include1)
.Where(expression)
.OrderBy(expression2, OrderByType.Asc)
.ToListAsync();
}
/// <summary>
/// 根据条件查询数据
@ -292,6 +353,89 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public async Task<List<T>> QueryListByClauseAsync(
bool isWhere, Expression<Func<T, bool>> expression,
bool isWhere1, Expression<Func<T, bool>> expression1,
bool isWhere2, Expression<Func<T, bool>> expression2,
Expression<Func<T, object>> expression3,
bool blUseNoLock = false)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(expression3, OrderByType.Asc)
.WhereIF(isWhere, expression)
.WhereIF(isWhere1, expression1)
.WhereIF(isWhere2, expression2)
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public async Task<List<T>> QueryListByClauseAsync(
Expression<Func<T, bool>> expression,
bool isWhere1, Expression<Func<T, bool>> expression1,
bool isWhere2, Expression<Func<T, bool>> expression2,
Expression<Func<T, object>> expression3,
int pageNumber, int pageSize, RefAsync<int> totalNumber,
bool blUseNoLock = false)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(expression3, OrderByType.Asc)
.Where(expression)
.WhereIF(isWhere1, expression1)
.WhereIF(isWhere2, expression2)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageNumber, pageSize, totalNumber);
}
public async Task<List<T>> QueryTreeByClauseAsync(
Expression<Func<T, object>> expression,
Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(expression, OrderByType.Asc)
.ToTreeAsync(childListExpression, parentIdExpression, rootValue);
}
public async Task<List<T>> QueryTreeByClauseAsync(
Expression<Func<T, object>> expression,
Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds)
{
return await DbBaseClient
.Queryable<T>()
.OrderBy(expression, OrderByType.Asc)
.ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds);
}
public async Task<List<T>> QueryTreeByClauseAsync(
Expression<Func<T, object>> parentIdExpression, object primaryKeyValue, bool isContainOneself = true)
{
return await DbBaseClient
.Queryable<T>()
.ToChildListAsync(parentIdExpression, primaryKeyValue, isContainOneself);
}
public async Task<List<T>> QueryTreeByClauseAsync(
Expression<Func<T, bool>> expression,
Expression<Func<T, object>> expression1,
Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue
)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.OrderBy(expression1, OrderByType.Asc)
.ToTreeAsync(childListExpression, parentIdExpression, rootValue);
}
public async Task<List<T>> QueryTreeByClauseAsync(
Expression<Func<T, bool>> expression,
Expression<Func<T, object>> expression1,
Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue, object[] childIds
)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.OrderBy(expression1, OrderByType.Asc)
.ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds);
}
/// <summary>
/// 根据条件查询数据
@ -330,6 +474,19 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public async Task<List<TResult>> QueryListByClauseAsync<TResult>(
Expression<Func<T, bool>> expression,
bool isWhere, Expression<Func<T, bool>> whereIfExpression,
Expression<Func<T, TResult>> selectExpression
)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.WhereIF(isWhere, whereIfExpression)
.Select(selectExpression)
.ToListAsync();
}
/// <summary>
/// 根据条件查询一定数量数据
@ -440,6 +597,47 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.FirstAsync(predicate);
}
public async Task<List<TResult>> QueryByGroupByAsync<TResult>(
Expression<Func<T, object>> expression,
Expression<Func<T, TResult>> expression2
)
{
return await DbBaseClient
.Queryable<T>()
.GroupBy(expression)
.Select(expression2)
.ToListAsync();
}
public async Task<List<long>> QueryByClauseAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, long>> selectExpression, bool blUseNoLock = false)
{
return await DbBaseClient
.Queryable<T>()
.Where(predicate)
.Select(selectExpression)
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public async Task<int> UpdateColumnsAsync(
T updateObj,
Expression<Func<T, object>> columns
)
{
return await DbBaseClient
.Updateable(updateObj)
.UpdateColumns(columns)
.ExecuteCommandAsync();
}
public async Task<List<T>> QueryListByClauseAsync(
Expression<Func<T, bool>> expression)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.ToListAsync();
}
/// <summary>
/// 根据条件查询数据
@ -476,17 +674,120 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.FirstAsync(predicate);
}
public async Task<List<T>> QueryByOrderByClauseAsync
(
Expression<Func<T, bool>> expression,
Expression<Func<T, object>> expression1,
bool blUseNoLock = false)
{
return await DbBaseClient
.Queryable<T>()
.Where(expression)
.OrderBy(expression1, OrderByType.Asc)
.WithNoLockOrNot(blUseNoLock)
.ToListAsync();
}
public List<T> QueryByClauseToList(
Expression<Func<T, bool>> expression, Expression<Func<T, bool>> expression2,
Expression<Func<T, object>> expression1, bool blUseNoLock = false)
{
return DbBaseClient
.Queryable<T>()
.Where(expression)
.Where(expression2)
.OrderBy(expression1, OrderByType.Asc)
.WithNoLockOrNot(blUseNoLock)
.ToList();
}
public List<T> QueryByClauseToList(Expression<Func<T, bool>> expression, Expression<Func<T, bool>> expression2, bool blUseNoLock = false)
{
return DbBaseClient
.Queryable<T>()
.Where(expression)
.Where(expression2)
.WithNoLockOrNot(blUseNoLock)
.ToList();
}
public List<T> QueryByClauseToList(Expression<Func<T, bool>> expression, Expression<Func<T, object>> expression1, bool blUseNoLock = false)
{
return DbBaseClient
.Queryable<T>()
.Where(expression)
.OrderBy(expression1, OrderByType.Asc)
.WithNoLockOrNot(blUseNoLock)
.ToList();
}
public List<T> QueryByClauseToList(Expression<Func<T, bool>> expression)
{
return DbBaseClient
.Queryable<T>()
.Where(expression)
.ToList();
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="joinExpression"></param>
/// <param name="whereExpression"></param>
/// <param name="orderExpression"></param>
/// <param name="selectExpression"></param>
/// <returns></returns>
public async Task<List<TResult>> QueryByClauseAsync<T, T2, TResult>(
Expression<Func<T, T2, bool>> joinExpression,
Expression<Func<T, T2, bool>> whereExpression,
Expression<Func<T, T2, object>> orderExpression,
Expression<Func<T, T2, TResult>> selectExpression
)
{
return await DbBaseClient
.Queryable<T>()
.LeftJoin<T2>(joinExpression)
.Where(whereExpression)
.OrderBy(orderExpression, OrderByType.Asc)
.Select(selectExpression)
.ToListAsync();
}
public async Task<List<TResult>> QueryByClauseAsync<T, T2, TResult>(
Expression<Func<T, T2, bool>> joinExpression,
Expression<Func<T, T2, bool>> whereExpression,
bool isWhere, Expression<Func<T, T2, bool>> whereifExpression,
Expression<Func<T, T2, object>> orderExpression,
Expression<Func<T, T2, TResult>> selectExpression
)
{
return await DbBaseClient
.Queryable<T>()
.LeftJoin<T2>(joinExpression)
.Where(whereExpression)
.WhereIF(isWhere, whereifExpression)
.OrderBy(orderExpression, OrderByType.Asc)
.Select(selectExpression)
.ToListAsync();
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">实体数据</param>
/// <returns></returns>
public int Insert(T entity)
public T Insert(T entity)
{
return DbBaseClient
.Insertable(entity)
.ExecuteReturnIdentity();
.ExecuteReturnEntity();
}
/// <summary>
/// 写入或者更新实体数据
/// </summary>
/// <param name="entity">实体数据</param>
/// <returns></returns>
public int InsertOrUpdate(T entity)
{
return DbBaseClient.Storageable(entity).ExecuteCommand();
}
/// <summary>
@ -494,25 +795,30 @@ public abstract class BaseRepository<T> where T : class, new()
/// </summary>
/// <param name="entity">实体数据</param>
/// <returns></returns>
public async Task<int> InsertAsync(T entity)
public async Task<T> InsertAsync(T entity)
{
return await DbBaseClient
.Insertable(entity)
.ExecuteReturnIdentityAsync();
.ExecuteReturnEntityAsync();
}
public async Task<T> InsertReturnEntityAsync(T entity)
{
return await DbBaseClient
.Insertable(entity)
.ExecuteReturnEntityAsync();
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">实体数据</param>
/// <param name="insertColumns">插入的列</param>
/// <returns></returns>
public int Insert(T entity, Expression<Func<T, object>> insertColumns = null)
public T Insert(T entity, Expression<Func<T, object>> 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();
}
/// <summary>
@ -521,12 +827,12 @@ public abstract class BaseRepository<T> where T : class, new()
/// <param name="entity">实体数据</param>
/// <param name="insertColumns">插入的列</param>
/// <returns></returns>
public async Task<int> InsertAsync(T entity, Expression<Func<T, object>> insertColumns = null)
public async Task<T> InsertAsync(T entity, Expression<Func<T, object>> 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();
}
/// <summary>
@ -562,9 +868,10 @@ public abstract class BaseRepository<T> where T : class, new()
/// </summary>
/// <param name="entity">实体类</param>
/// <returns></returns>
public int Insert(List<T> entity)
public bool Insert(List<T> entity)
{
return DbBaseClient.Insertable(entity.ToArray()).ExecuteReturnIdentity();
return DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntity();
;
}
/// <summary>
@ -572,9 +879,9 @@ public abstract class BaseRepository<T> where T : class, new()
/// </summary>
/// <param name="entity">实体类</param>
/// <returns></returns>
public async Task<int> InsertAsync(List<T> entity)
public async Task<bool> InsertAsync(List<T> entity)
{
return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync();
return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntityAsync();
}
/// <summary>
@ -607,6 +914,64 @@ public abstract class BaseRepository<T> where T : class, new()
return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync();
}
public async Task<bool> UpdateAsync(
Expression<Func<T, bool>> expression
)
{
return await DbBaseClient
.Queryable<T>()
.ClearFilter()
.AnyAsync(expression);
}
public int Update(Expression<Func<T, bool>> columns,
Expression<Func<T, bool>> expression)
{
return DbBaseClient.Updateable<T>().SetColumns(columns).Where(expression).ExecuteCommand();
}
public async Task<int> UpdateAsync(
Expression<Func<T, bool>> columns,
Expression<Func<T, bool>> expression
)
{
return await DbBaseClient
.Updateable<T>()
.SetColumns(columns)
.Where(expression)
.ExecuteCommandAsync();
}
public async Task<int> UpdateAsync(
Expression<Func<T, object>> columns
)
{
return await DbBaseClient
.Updateable<T>()
.IgnoreColumns(columns)
.ExecuteCommandAsync();
}
public async Task<int> UpdateAsync(
T updateObj,
bool ignoreAllNullColumns
)
{
return await DbBaseClient
.Updateable(updateObj)
.IgnoreColumns(ignoreAllNullColumns)
.ExecuteCommandAsync();
}
public async Task<int> UpdateAsync(
T updateObj, bool ignoreAllNullColumns, Expression<Func<T, object>> columns
)
{
return await DbBaseClient
.Updateable<T>(updateObj)
.IgnoreColumns(ignoreAllNullColumns)
.IgnoreColumns(columns)
.ExecuteCommandAsync();
}
/// <summary>
/// 更新实体数据
/// </summary>
@ -627,6 +992,7 @@ public abstract class BaseRepository<T> where T : class, new()
return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 根据手写条件更新
/// </summary>
@ -771,7 +1137,23 @@ public abstract class BaseRepository<T> where T : class, new()
{
return await DbBaseClient.Deleteable<T>(entity).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 新增方法
/// 该方法用于根据角色Id删除用户角色
/// </summary>
/// <param name="predicate"></param>
/// <param name="selectExpression"></param>
/// <param name="action"></param>
/// <returns></returns>
public async Task DeleteUserRoleByRoleId(
Expression<Func<T, bool>> predicate,
Expression<Func<T, long>> selectExpression, Action<long> action)
{
await DbBaseClient.Queryable<T>()
.Where(predicate)
.Select(selectExpression)
.ForEachAsync(action);
}
/// <summary>
/// 删除数据
/// </summary>
@ -1097,7 +1479,26 @@ public abstract class BaseRepository<T> where T : class, new()
return await DbBaseClient.Queryable<T>().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field);
}
public async Task<List<T>> QueryPageAsync(
Expression<Func<T, bool>> whereExpression,
bool isWhere1, Expression<Func<T, bool>> expression1,
bool isWhere2, Expression<Func<T, bool>> expression2,
bool isWhere3, Expression<Func<T, bool>> expression3,
Expression<Func<T, object>> orderBy,
int pageNumber, int pageSize, RefAsync<int> totalNumber,
bool blUseNoLock = false)
{
var page = await DbBaseClient
.Queryable<T>()
.Where(whereExpression)
.WhereIF(isWhere1, expression1)
.WhereIF(isWhere2, expression2)
.WhereIF(isWhere3, expression3)
.OrderBy(orderBy, OrderByType.Asc)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageNumber, pageSize, totalNumber);
return page;
}
/// <summary>
/// 查询-2表查询
@ -1300,4 +1701,7 @@ public abstract class BaseRepository<T> where T : class, new()
var list = await DbBaseClient.SqlQueryable<T>(sql).ToListAsync();
return list;
}
}

@ -289,7 +289,7 @@ public class BaseServices<T> where T : class, new()
/// </summary>
/// <param name="entity">实体数据</param>
/// <returns></returns>
public int Insert(T entity)
public T Insert(T entity)
{
return BaseDal.Insert(entity);
}
@ -299,7 +299,7 @@ public class BaseServices<T> where T : class, new()
/// </summary>
/// <param name="entity">实体数据</param>
/// <returns></returns>
public async Task<int> InsertAsync(T entity)
public async Task<T> InsertAsync(T entity)
{
return await BaseDal.InsertAsync(entity);
}
@ -310,7 +310,7 @@ public class BaseServices<T> where T : class, new()
/// <param name="entity">实体数据</param>
/// <param name="insertColumns">插入的列</param>
/// <returns></returns>
public int Insert(T entity, Expression<Func<T, object>> insertColumns = null)
public T Insert(T entity, Expression<Func<T, object>> insertColumns = null)
{
return BaseDal.Insert(entity, insertColumns);
}
@ -321,7 +321,7 @@ public class BaseServices<T> where T : class, new()
/// <param name="entity">实体数据</param>
/// <param name="insertColumns">插入的列</param>
/// <returns></returns>
public async Task<int> InsertAsync(T entity, Expression<Func<T, object>> insertColumns = null)
public async Task<T> InsertAsync(T entity, Expression<Func<T, object>> insertColumns = null)
{
return await BaseDal.InsertAsync(entity, insertColumns);
}
@ -353,7 +353,7 @@ public class BaseServices<T> where T : class, new()
/// </summary>
/// <param name="entity">实体类</param>
/// <returns></returns>
public int Insert(List<T> entity)
public bool Insert(List<T> entity)
{
return BaseDal.Insert(entity);
}
@ -363,7 +363,7 @@ public class BaseServices<T> where T : class, new()
/// </summary>
/// <param name="entity">实体类</param>
/// <returns></returns>
public async Task<int> InsertAsync(List<T> entity)
public async Task<bool> InsertAsync(List<T> entity)
{
return await BaseDal.InsertAsync(entity);
}

@ -20,8 +20,8 @@ public class SysBatteryReplaceLogServices : BaseServices<sysBatteryReplaceLog>
var result = false;
if (_sysBatteryReplaceLog != null)
{
var resuklt = _dal.Insert(_sysBatteryReplaceLog);
if (resuklt > 0) result = true;
_dal.Insert(_sysBatteryReplaceLog);
result = true;
}
return result;

@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5034",
"applicationUrl": "http://localhost:5036",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"profiles_active": "dev"

@ -26,7 +26,7 @@
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://*:15036"
"Url": "http://*:5036"
}
}
},

@ -1,19 +0,0 @@
{
"runtimeOptions": {
"tfm": "net6.0",
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "6.0.0"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "6.0.0"
}
],
"configProperties": {
"System.GC.Server": true,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -1,32 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- 控制台日志配置 -->
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level [%thread] (%file:%line) - %message%newline"/>
</layout>
</appender>
<!-- 文件存储日志配置 -->
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!-- 保存文件的名称 -->
<file value="logs\WebApi.log"/>
<appendToFile value="true"/>
<!-- 文件的编码方式 -->
<param name="Encoding" value="UTF-8"/>
<!-- 每个文件的大小 -->
<maximumFileSize value="100MB"/>
<!-- 保存文件数量 -->
<maxSizeRollBackups value="2"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile"/>
</root>
</log4net>

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save