diff --git a/ConsoleStarter/ConsoleStarter.csproj b/ConsoleStarter/ConsoleStarter.csproj index 57b5dae..5849726 100644 --- a/ConsoleStarter/ConsoleStarter.csproj +++ b/ConsoleStarter/ConsoleStarter.csproj @@ -16,7 +16,7 @@ - + diff --git a/Entity/Common/IPageList.cs b/Entity/Common/IPageList.cs deleted file mode 100644 index 438c220..0000000 --- a/Entity/Common/IPageList.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Entity.Common; - -public interface IPageList : IList -{ - int PageIndex { get; } - int PageSize { get; } - int TotalCount { get; } - int TotalPages { get; } - bool HasPreviousPage { get; } - bool HasNextPage { get; } -} \ No newline at end of file diff --git a/Entity/Common/PageList.cs b/Entity/Common/PageList.cs deleted file mode 100644 index 89eb1ba..0000000 --- a/Entity/Common/PageList.cs +++ /dev/null @@ -1,99 +0,0 @@ -namespace Entity.Common; - -/// -/// 分页组件实体类 -/// -/// 泛型实体 -[Serializable] -public class PageList : List, IPageList -{ - /// - /// 构造函数 - /// - /// 数据源 - /// 分页索引 - /// 分页大小 - public PageList(IQueryable 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()); - } - - /// - /// 构造函数 - /// - /// 数据源 - /// 分页索引 - /// 分页大小 - public PageList(IList 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()); - } - - /// - /// 构造函数 - /// - /// 数据源 - /// 分页索引 - /// 分页大小 - /// 总记录数 - public PageList(IEnumerable source, int pageIndex, int pageSize, int totalCount) - { - TotalCount = totalCount; - TotalPages = TotalCount / pageSize; - - if (TotalCount % pageSize > 0) - TotalPages++; - - PageSize = pageSize; - PageIndex = pageIndex; - AddRange(source); - } - - /// - /// 分页索引 - /// - public int PageIndex { get; } - - /// - /// 分页大小 - /// - public int PageSize { get; private set; } - - /// - /// 总记录数 - /// - public int TotalCount { get; } - - /// - /// 总页数 - /// - public int TotalPages { get; } - - /// - /// 是否有上一页 - /// - public bool HasPreviousPage => PageIndex > 0; - - /// - /// 是否有下一页 - /// - public bool HasNextPage => PageIndex + 1 < TotalPages; -} \ No newline at end of file diff --git a/Entity/Entity.csproj b/Entity/Entity.csproj index 9352d52..2a3a2c0 100644 --- a/Entity/Entity.csproj +++ b/Entity/Entity.csproj @@ -7,7 +7,7 @@ - + diff --git a/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs b/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs new file mode 100644 index 0000000..4e0facd --- /dev/null +++ b/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs @@ -0,0 +1,27 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace HybirdFrameworkCore.Entity; + + +/// +/// 序列化 +/// +public class DatetimeJsonConverter : JsonConverter +{ + 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")); + } +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Entity/IPage.cs b/HybirdFrameworkCore/Entity/IPage.cs new file mode 100644 index 0000000..b9a8d4a --- /dev/null +++ b/HybirdFrameworkCore/Entity/IPage.cs @@ -0,0 +1,20 @@ +namespace HybirdFrameworkCore.Entity; + +public class IPage +{ + public int Total; + + public int PageNum; + + public int PageSize; + + public List? Rows; + + public IPage(int total, QueryPageModel page, List? rows) + { + Total = total; + PageNum = page.Page; + PageSize = page.PageSize; + Rows = rows; + } +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Entity/PageResult.cs b/HybirdFrameworkCore/Entity/PageResult.cs new file mode 100644 index 0000000..80d49fc --- /dev/null +++ b/HybirdFrameworkCore/Entity/PageResult.cs @@ -0,0 +1,58 @@ +using System.Diagnostics; +using AutoMapper; + +namespace HybirdFrameworkCore.Entity +{ + public class PageResult + { + /// + /// 当前页标 + /// + public int PageNum { get; set; } + + /// + /// 每页大小 + /// + public int PageSize { set; get; } + + /// + /// 返回数据 + /// + public List? Rows { get; set; } + + /// + /// 当前页标 + /// + public long ToTal { get; set; } + + public PageResult() + { + PageSize = 0; + PageNum = 0; + ToTal = 0; + Rows = new List(); + } + + + public static PageResult ConvertPage(IPage page) where TS : class, new() + { + if (page.Total <= 0 || page.Rows == null) + { + return new PageResult(); + } + + MapperConfiguration configuration = new MapperConfiguration(cfg => cfg.CreateMap()); + + Debug.Assert(page.Rows != null, "iPage.Rows != null"); + List listDest = configuration.CreateMapper().Map, List>(page.Rows); + + return new PageResult() + { + PageSize = page.PageSize, + PageNum = page.PageNum, + ToTal = page.Total, + Rows = listDest, + }; + } + } +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Entity/QueryPageModel.cs b/HybirdFrameworkCore/Entity/QueryPageModel.cs new file mode 100644 index 0000000..b8bfd4a --- /dev/null +++ b/HybirdFrameworkCore/Entity/QueryPageModel.cs @@ -0,0 +1,13 @@ +namespace HybirdFrameworkCore.Entity; + +public class QueryPageModel +{ + /// + ///页码 + /// + public int Page { get; set; } = 1; + /// + /// 页数 + /// + public int PageSize { get; set; } = 10; +} \ No newline at end of file diff --git a/HybirdFrameworkCore/Entity/Result.cs b/HybirdFrameworkCore/Entity/Result.cs new file mode 100644 index 0000000..3bc3134 --- /dev/null +++ b/HybirdFrameworkCore/Entity/Result.cs @@ -0,0 +1,104 @@ +namespace HybirdFrameworkCore.Entity +{ + /// + /// 通用返回信息类 + /// + public class Result + { + /// + /// 状态码 + /// + + public int Status { get; set; } = 200; + /// + /// 操作是否成功 + /// + public bool IsSuccess { get; set; } = false; + /// + /// 返回信息 + /// + public string? Msg { get; set; } + + /// + /// 返回数据集合 + /// + public T? Data { get; set; } + + /// + /// 返回成功 + /// + /// 消息 + /// + public static Result Success(string msg = "成功") + { + return Message(true, msg, default); + } + + + + /// + /// 返回成功 + /// + /// 数据 + /// 消息 + /// + public static Result Success(T data, string msg = "成功") + { + return Message(true, msg, data); + } + + + + /// + /// 返回失败 + /// + /// 消息 + /// + public static Result Fail(string msg = "失败") + { + return Message(false, msg, default); + } + + + /// + /// 返回失败 + /// + /// 消息 + /// 数据 + /// + public static Result Fail(T data, string msg = "失败") + { + return Message(false, msg, data); + } + + /// + /// 返回消息 + /// + /// 失败/成功 + /// 消息 + /// 数据 + /// + public static Result Message(bool success, string msg, T data) + { + return new Result() { Msg = msg, Data = data, IsSuccess = success }; + } + + /// + /// 返回消息 + /// + /// 失败/成功 + /// 消息 + /// 数据 + /// + public static Result Message(bool success, T data) + { + return new Result() { Msg="查询成功", Data = data, IsSuccess = success }; + } + + + + } + + + +} diff --git a/HybirdFrameworkCore/HybirdFrameworkCore.csproj b/HybirdFrameworkCore/HybirdFrameworkCore.csproj index ae3db33..ae5b4e4 100644 --- a/HybirdFrameworkCore/HybirdFrameworkCore.csproj +++ b/HybirdFrameworkCore/HybirdFrameworkCore.csproj @@ -8,6 +8,7 @@ + diff --git a/Repository/BaseRepository.cs b/Repository/BaseRepository.cs index c1e94fc..2cf9e53 100644 --- a/Repository/BaseRepository.cs +++ b/Repository/BaseRepository.cs @@ -1,5 +1,5 @@ using System.Linq.Expressions; -using Entity.Common; +using HybirdFrameworkCore.Entity; using SqlSugar; namespace Repository; @@ -1004,105 +1004,97 @@ public abstract class BaseRepository where T : class, new() { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } - - /// - /// 根据条件查询分页数据 - /// - /// - /// - /// 当前页面索引 - /// 分布大小 - /// 是否使用WITH(NOLOCK) - /// - public IPageList QueryPage(Expression> predicate, string orderBy = "", int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - var totalCount = 0; - var page = DbBaseClient - .Queryable() - .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate) - .WithNoLockOrNot(blUseNoLock) - .ToPageList(pageIndex, pageSize, ref totalCount); - - var list = new PageList(page, pageIndex, pageSize, totalCount); - return list; - } - - /// - /// 根据条件查询分页数据 - /// - /// - /// - /// 当前页面索引 - /// 分布大小 - /// 是否使用WITH(NOLOCK) - /// - public async Task> QueryPageAsync(Expression> predicate, string orderBy = "", - int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) - { - RefAsync totalCount = 0; - var page = await DbBaseClient - .Queryable() - .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate) - .WithNoLockOrNot(blUseNoLock) - .ToPageListAsync(pageIndex, pageSize, totalCount); - var list = new PageList(page, pageIndex, pageSize, totalCount); - return list; - } - - /// - /// 根据条件查询分页数据 - /// - /// 判断集合 - /// 排序方式 - /// 当前页面索引 - /// 分布大小 - /// - /// 是否使用WITH(NOLOCK) - /// - public IPageList QueryPage(Expression> predicate, - Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - var totalCount = 0; - var page = DbBaseClient - .Queryable() - .OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .WhereIF(predicate != null, predicate) - .WithNoLockOrNot(blUseNoLock) - .ToPageList(pageIndex, pageSize, ref totalCount); - - var list = new PageList(page, pageIndex, pageSize, totalCount); - return list; - } - - /// - /// 根据条件查询分页数据 - /// - /// 判断集合 - /// 排序方式 - /// 当前页面索引 - /// 分布大小 - /// - /// 是否使用WITH(NOLOCK) - /// - public async Task> QueryPageAsync(Expression> predicate, - Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - RefAsync totalCount = 0; - var page = await DbBaseClient - .Queryable() - .WhereIF(predicate != null, predicate) - .OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .WithNoLockOrNot(blUseNoLock) - .ToPageListAsync(pageIndex, pageSize, totalCount); - - var list = new PageList(page, pageIndex, pageSize, totalCount); - return list; - } + /// + /// 根据条件查询表单数据(分页) + /// + /// 页数 + /// 每页几条数据 + /// 是否使用WITH(NOLOCK) + /// + public IPage QueryIPageByCause(QueryPageModel page, Expression> predicate) + { + if (null == predicate) { + return this.QueryIPage(page); + } + int totalCount = 0; + + + 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; + + + 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); + } + + /// + /// 查询表单所有数据(分页) 异步 + /// + /// + /// + 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); + } + /// /// 查询-2表查询 diff --git a/Repository/Repository.csproj b/Repository/Repository.csproj index 9c691e6..7e55f6e 100644 --- a/Repository/Repository.csproj +++ b/Repository/Repository.csproj @@ -8,7 +8,7 @@ - + diff --git a/Service/BaseServices.cs b/Service/BaseServices.cs index afaaeb7..3fdd212 100644 --- a/Service/BaseServices.cs +++ b/Service/BaseServices.cs @@ -1,5 +1,4 @@ using System.Linq.Expressions; -using Entity.Common; using Repository; using SqlSugar; @@ -875,71 +874,7 @@ public class BaseServices where T : class, new() { return await BaseDal.GetSumAsync(predicate, field, blUseNoLock); } - - /// - /// 根据条件查询分页数据 - /// - /// - /// - /// 当前页面索引 - /// 分布大小 - /// 是否使用WITH(NOLOCK) - /// - public IPageList QueryPage(Expression> predicate, string orderBy = "", int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - return BaseDal.QueryPage(predicate, orderBy, pageIndex, pageSize, blUseNoLock); - } - - /// - /// 根据条件查询分页数据 - /// - /// - /// - /// 当前页面索引 - /// 分布大小 - /// 是否使用WITH(NOLOCK) - /// - public async Task> QueryPageAsync(Expression> predicate, string orderBy = "", - int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) - { - return await BaseDal.QueryPageAsync(predicate, orderBy, pageIndex, pageSize, blUseNoLock); - } - - /// - /// 根据条件查询分页数据 - /// - /// 判断集合 - /// 排序方式 - /// 当前页面索引 - /// 分布大小 - /// - /// 是否使用WITH(NOLOCK) - /// - public IPageList QueryPage(Expression> predicate, - Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - return BaseDal.QueryPage(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock); - } - - /// - /// 根据条件查询分页数据 - /// - /// 判断集合 - /// 排序方式 - /// 当前页面索引 - /// 分布大小 - /// - /// 是否使用WITH(NOLOCK) - /// - public async Task> QueryPageAsync(Expression> predicate, - Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, - int pageSize = 20, bool blUseNoLock = false) - { - return await BaseDal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, - blUseNoLock); - } + /// /// 查询-多表查询 diff --git a/Service/Service.csproj b/Service/Service.csproj index 1ee60b6..433e1ae 100644 --- a/Service/Service.csproj +++ b/Service/Service.csproj @@ -14,7 +14,7 @@ - + diff --git a/WebStarter/Controllers/Test/GenController.cs b/WebStarter/Controllers/Test/GenController.cs new file mode 100644 index 0000000..843ff1b --- /dev/null +++ b/WebStarter/Controllers/Test/GenController.cs @@ -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; + + } + + /// + /// 生成文件 + /// + /// + [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; + } +} \ No newline at end of file diff --git a/WebStarter/Controllers/WeatherForecastController.cs b/WebStarter/Controllers/Test/WeatherForecastController.cs similarity index 96% rename from WebStarter/Controllers/WeatherForecastController.cs rename to WebStarter/Controllers/Test/WeatherForecastController.cs index f3f784d..bd80138 100644 --- a/WebStarter/Controllers/WeatherForecastController.cs +++ b/WebStarter/Controllers/Test/WeatherForecastController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Service.System; -namespace WebStarter.Controllers; +namespace WebStarter.Controllers.Test; [ApiController] [Route("[controller]")] diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index 5cfa57f..08010f6 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -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.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(); diff --git a/WpfStarter/WpfStarter.csproj b/WpfStarter/WpfStarter.csproj index bb66c09..acf2725 100644 --- a/WpfStarter/WpfStarter.csproj +++ b/WpfStarter/WpfStarter.csproj @@ -12,7 +12,7 @@ - +