框架更新

master
lxw 6 months ago
parent 18ad4af3ef
commit 37dd5f1b5d

@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup>
<ItemGroup>

@ -1,11 +0,0 @@
namespace Entity.Common;
public interface IPageList<T> : IList<T>
{
int PageIndex { get; }
int PageSize { get; }
int TotalCount { get; }
int TotalPages { get; }
bool HasPreviousPage { get; }
bool HasNextPage { get; }
}

@ -1,99 +0,0 @@
namespace Entity.Common;
/// <summary>
/// 分页组件实体类
/// </summary>
/// <typeparam name="T">泛型实体</typeparam>
[Serializable]
public class PageList<T> : List<T>, IPageList<T>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IQueryable<T> source, int pageIndex, int pageSize)
{
var total = source.Count();
TotalCount = total;
TotalPages = total / pageSize;
if (total % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
public PageList(IList<T> source, int pageIndex, int pageSize)
{
TotalCount = source.Count();
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">数据源</param>
/// <param name="pageIndex">分页索引</param>
/// <param name="pageSize">分页大小</param>
/// <param name="totalCount">总记录数</param>
public PageList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
{
TotalCount = totalCount;
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
PageSize = pageSize;
PageIndex = pageIndex;
AddRange(source);
}
/// <summary>
/// 分页索引
/// </summary>
public int PageIndex { get; }
/// <summary>
/// 分页大小
/// </summary>
public int PageSize { get; private set; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalCount { get; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; }
/// <summary>
/// 是否有上一页
/// </summary>
public bool HasPreviousPage => PageIndex > 0;
/// <summary>
/// 是否有下一页
/// </summary>
public bool HasNextPage => PageIndex + 1 < TotalPages;
}

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup>
</Project>

@ -0,0 +1,27 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace HybirdFrameworkCore.Entity;
/// <summary>
/// 序列化
/// </summary>
public class DatetimeJsonConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}

@ -0,0 +1,20 @@
namespace HybirdFrameworkCore.Entity;
public class IPage<T>
{
public int Total;
public int PageNum;
public int PageSize;
public List<T>? Rows;
public IPage(int total, QueryPageModel page, List<T>? rows)
{
Total = total;
PageNum = page.Page;
PageSize = page.PageSize;
Rows = rows;
}
}

@ -0,0 +1,58 @@
using System.Diagnostics;
using AutoMapper;
namespace HybirdFrameworkCore.Entity
{
public class PageResult<T>
{
/// <summary>
/// 当前页标
/// </summary>
public int PageNum { get; set; }
/// <summary>
/// 每页大小
/// </summary>
public int PageSize { set; get; }
/// <summary>
/// 返回数据
/// </summary>
public List<T>? Rows { get; set; }
/// <summary>
/// 当前页标
/// </summary>
public long ToTal { get; set; }
public PageResult()
{
PageSize = 0;
PageNum = 0;
ToTal = 0;
Rows = new List<T>();
}
public static PageResult<T> ConvertPage<TS>(IPage<TS> page) where TS : class, new()
{
if (page.Total <= 0 || page.Rows == null)
{
return new PageResult<T>();
}
MapperConfiguration configuration = new MapperConfiguration(cfg => cfg.CreateMap<TS, T>());
Debug.Assert(page.Rows != null, "iPage.Rows != null");
List<T> listDest = configuration.CreateMapper().Map<List<TS>, List<T>>(page.Rows);
return new PageResult<T>()
{
PageSize = page.PageSize,
PageNum = page.PageNum,
ToTal = page.Total,
Rows = listDest,
};
}
}
}

@ -0,0 +1,13 @@
namespace HybirdFrameworkCore.Entity;
public class QueryPageModel
{
/// <summary>
///页码
/// </summary>
public int Page { get; set; } = 1;
/// <summary>
/// 页数
/// </summary>
public int PageSize { get; set; } = 10;
}

@ -0,0 +1,104 @@
namespace HybirdFrameworkCore.Entity
{
/// <summary>
/// 通用返回信息类
/// </summary>
public class Result<T>
{
/// <summary>
/// 状态码
/// </summary>
public int Status { get; set; } = 200;
/// <summary>
/// 操作是否成功
/// </summary>
public bool IsSuccess { get; set; } = false;
/// <summary>
/// 返回信息
/// </summary>
public string? Msg { get; set; }
/// <summary>
/// 返回数据集合
/// </summary>
public T? Data { get; set; }
/// <summary>
/// 返回成功
/// </summary>
/// <param name="msg">消息</param>
/// <returns></returns>
public static Result<T> Success(string msg = "成功")
{
return Message(true, msg, default);
}
/// <summary>
/// 返回成功
/// </summary>
/// <param name="data">数据</param>
/// <param name="msg">消息</param>
/// <returns></returns>
public static Result<T> Success(T data, string msg = "成功")
{
return Message(true, msg, data);
}
/// <summary>
/// 返回失败
/// </summary>
/// <param name="msg">消息</param>
/// <returns></returns>
public static Result<T> Fail(string msg = "失败")
{
return Message(false, msg, default);
}
/// <summary>
/// 返回失败
/// </summary>
/// <param name="msg">消息</param>
/// <param name="data">数据</param>
/// <returns></returns>
public static Result<T> Fail(T data, string msg = "失败")
{
return Message(false, msg, data);
}
/// <summary>
/// 返回消息
/// </summary>
/// <param name="success">失败/成功</param>
/// <param name="msg">消息</param>
/// <param name="data">数据</param>
/// <returns></returns>
public static Result<T> Message(bool success, string msg, T data)
{
return new Result<T>() { Msg = msg, Data = data, IsSuccess = success };
}
/// <summary>
/// 返回消息
/// </summary>
/// <param name="success">失败/成功</param>
/// <param name="msg">消息</param>
/// <param name="data">数据</param>
/// <returns></returns>
public static Result<T> Message(bool success, T data)
{
return new Result<T>() { Msg="查询成功", Data = data, IsSuccess = success };
}
}
}

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="7.0.1"/>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>

@ -1,5 +1,5 @@
using System.Linq.Expressions;
using Entity.Common;
using HybirdFrameworkCore.Entity;
using SqlSugar;
namespace Repository;
@ -1004,106 +1004,98 @@ public abstract class BaseRepository<T> where T : class, new()
{
return await DbBaseClient.Queryable<T>().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field);
}
/// <summary>
/// 根据条件查询分页数据
/// 根据条件查询表单数据(分页)
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate, string orderBy = "", int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
public IPage<T> QueryIPageByCause(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
var totalCount = 0;
var page = DbBaseClient
if (null == predicate) {
return this.QueryIPage(page);
}
int totalCount = 0;
List<T> pageList = DbBaseClient
.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageList(pageIndex, pageSize, ref totalCount);
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据条件查询分页数据
/// 根据条件查询表单数据(分页) 异步
/// </summary>
/// <param name="page"></param>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate, string orderBy = "",
int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false)
public async Task<IPage<T>> QueryIPageByCauseAsync(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate)
{
return await this.QueryIPageAsync(page);
}
RefAsync<int> totalCount = 0;
var page = await DbBaseClient
List<T> pageList = await DbBaseClient
.Queryable<T>()
.OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageIndex, pageSize, totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
.Where(predicate)
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据条件查询分页数据
/// 查询表单所有数据(分页)
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="page"> 页数</param>
/// <param name="pageSize">每页几条数据</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
public IPage<T> QueryIPage(QueryPageModel page)
{
var totalCount = 0;
var page = DbBaseClient
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>()
.OrderByIF(orderByExpression != null, orderByExpression, orderByType)
.WhereIF(predicate != null, predicate)
.WithNoLockOrNot(blUseNoLock)
.ToPageList(pageIndex, pageSize, ref totalCount);
.WithNoLockOrNot(false)
.ToPageList(page.Page, page.PageSize, ref totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 根据条件查询分页数据
/// 查询表单所有数据(分页) 异步
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <param name="page"></param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
public async Task<IPage<T>> QueryIPageAsync(QueryPageModel page)
{
RefAsync<int> totalCount = 0;
var page = await DbBaseClient
List<T> pageList = await DbBaseClient
.Queryable<T>()
.WhereIF(predicate != null, predicate)
.OrderByIF(orderByExpression != null, orderByExpression, orderByType)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageIndex, pageSize, totalCount);
.WithNoLockOrNot(false)
.ToPageListAsync(page.Page, page.PageSize, totalCount);
var list = new PageList<T>(page, pageIndex, pageSize, totalCount);
return list;
return new IPage<T>(totalCount, page, pageList);
}
/// <summary>
/// 查询-2表查询
/// </summary>

@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="SqlSugar.IOC" Version="2.0.0"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup>
<ItemGroup>

@ -1,5 +1,4 @@
using System.Linq.Expressions;
using Entity.Common;
using Repository;
using SqlSugar;
@ -876,70 +875,6 @@ public class BaseServices<T> where T : class, new()
return await BaseDal.GetSumAsync(predicate, field, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate, string orderBy = "", int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return BaseDal.QueryPage(predicate, orderBy, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate"></param>
/// <param name="orderBy"></param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate, string orderBy = "",
int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false)
{
return await BaseDal.QueryPageAsync(predicate, orderBy, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public IPageList<T> QueryPage(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return BaseDal.QueryPage(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
}
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="pageIndex">当前页面索引</param>
/// <param name="pageSize">分布大小</param>
/// <param name="orderByExpression"></param>
/// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
/// <returns></returns>
public async Task<IPageList<T>> QueryPageAsync(Expression<Func<T, bool>> predicate,
Expression<Func<T, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20, bool blUseNoLock = false)
{
return await BaseDal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize,
blUseNoLock);
}
/// <summary>
/// 查询-多表查询

@ -14,7 +14,7 @@
<PackageReference Include="DotNetty.Handlers" Version="0.7.5"/>
<PackageReference Include="DotNetty.Transport" Version="0.7.5"/>
<PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup>
<ItemGroup>

@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Mvc;
using Repository.Station;
using SqlSugar;
namespace WebStarter.Controllers.Test;
[ApiController]
[Route("[controller]")]
public class GenController : ControllerBase
{
private readonly ISqlSugarClient _sqlSugarClient;
public GenController(ISqlSugarClient sqlSugarClient)
{
_sqlSugarClient = sqlSugarClient;
}
/// <summary>
/// 生成文件
/// </summary>
/// <returns></returns>
[HttpGet("gen/t/{id}")]
public void Get(int id)
{
Console.WriteLine();
_sqlSugarClient.DbFirst
.IsCreateAttribute() //创建sqlsugar自带特性
.FormatFileName(it => ToPascalCase(it)) //格式化文件名(文件名和表名不一样情况)
.FormatClassName(it => ToPascalCase(it)) //格式化类名 (类名和表名不一样的情况)
.FormatPropertyName(it => ToPascalCase(it)) //格式化属性名 (属性名和字段名不一样情况)
.CreateClassFile("D:\\lxw\\work\\pro\\c#\\hn_back_main\\Entity\\DbModel\\Station",
"Entity.DbModel.Station");
Console.WriteLine("生成完毕");
}
static string ToPascalCase(string input)
{
if (string.IsNullOrEmpty(input))
return input;
string[] strings = input.Split("_");
string res = "";
foreach (var s in strings)
{
string first = s.First().ToString().ToUpper();
string te = first + s.Substring(1);
res += te;
}
return res;
}
}

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Service.System;
namespace WebStarter.Controllers;
namespace WebStarter.Controllers.Test;
[ApiController]
[Route("[controller]")]

@ -2,6 +2,7 @@ using Autofac;
using Autofac.Extensions.DependencyInjection;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis;
using SqlSugar;
using SqlSugar.IOC;
@ -43,9 +44,36 @@ if (redisConnectionString != null && instanceName != null)
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Logging.AddLog4Net("log4net.config");
builder.Services.AddSwaggerGen(c =>
{
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
//c.IncludeXmlComments(Path.Combine(basePath, Assembly.GetExecutingAssembly().GetName().Name+".xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
});
//跨域
builder.Services.AddCors(options =>
{
options.AddPolicy
(name: "myCors",
builde =>
{
builde.WithOrigins("*", "*", "*")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
);
});
builder.Services.AddControllers().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
var app = builder.Build();
@ -54,10 +82,11 @@ if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseStaticFiles();
}
app.UseAuthorization();
app.UseCors("myCors");
app.MapControllers();
AppInfo.Container = app.Services.GetAutofacRoot();

@ -12,7 +12,7 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="log4net" Version="2.0.15"/>
<PackageReference Include="SqlSugar.IOC" Version="2.0.0"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.95"/>
<PackageReference Include="SqlSugarCore" Version="5.1.4.115"/>
</ItemGroup>
<ItemGroup>

Loading…
Cancel
Save