diff --git a/WebFirst/database/sqlite.db b/WebFirst/database/sqlite.db index 54a747c1..0f0c339e 100644 Binary files a/WebFirst/database/sqlite.db and b/WebFirst/database/sqlite.db differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index 6bf89910..de809849 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -51,6 +51,18 @@ + + + 自动分表,日志添加 + + + + + + 查询近20年与21年的日志表 + + + 仓储上下文对象测试 @@ -95,7 +107,7 @@ - 启动一个定时任务,每5秒访问一次百度 + 启动一个定时任务 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/LogController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/LogController.cs new file mode 100644 index 00000000..0b4d7231 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/LogController.cs @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Yi.Framework.Common.Models; +using Yi.Framework.Interface; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; +using Yi.Framework.WebCore; +using Yi.Framework.WebCore.AttributeExtend; +using Yi.Framework.WebCore.AuthorizationPolicy; + +namespace Yi.Framework.ApiMicroservice.Controllers +{ + [ApiController] + [Route("api/[controller]/[action]")] + public class LogController : ControllerBase + { + private ILogService _iLogService; + //大量日志,将采用自动分表形式,默认1年分一次表 + public LogController(ILogger logger, ILogService iLogService) + { + _iLogService = iLogService; + } + + /// + /// 自动分表,日志添加 + /// + /// + [HttpPost] + public async Task Add() + { + Random random = new Random(); + var logList = new List() { + new LogEntity() { LogCreateTime = Convert.ToDateTime("2019-12-1"), Message = "jack"+random.Next() } , + new LogEntity() { LogCreateTime = Convert.ToDateTime("2022-02-1"), Message = "jack"+random.Next() }, + new LogEntity() { LogCreateTime = Convert.ToDateTime("2020-02-1"), Message = "jack"+random.Next() }, + new LogEntity() { LogCreateTime = Convert.ToDateTime("2021-12-1"), Message = "jack"+random.Next() } }; + return Result.Success().SetData(await _iLogService.AddListTest(logList)); + } + + /// + /// 查询近20年与21年的日志表 + /// + /// + [HttpGet] + public async Task GetList() + { + return Result.Success().SetData(await _iLogService.GetListTest()); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db index b852b0be..906d4870 100644 Binary files a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db differ diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs new file mode 100644 index 00000000..911d4b4f --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; + +namespace Yi.Framework.Interface +{ + public partial interface ILogService + { + Task> AddListTest(List logEntities); + Task> GetListTest(); + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/ILogService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/ILogService.cs new file mode 100644 index 00000000..7320c588 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/ILogService.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; + +namespace Yi.Framework.Interface +{ + public partial interface ILogService : IBaseService + { + + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs b/Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs new file mode 100644 index 00000000..11a4db0d --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using SqlSugar; +namespace Yi.Framework.Model.Models +{ + /// + /// 日志表 + /// + [SplitTable(SplitType.Year)] + [SugarTable("SplitLog_{year}{month}{day}")] + public partial class LogEntity + { + [SplitField] //分表字段 在插入的时候会根据这个字段插入哪个表,在更新删除的时候用这个字段找出相关表 + public DateTime? LogCreateTime { get; set; } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs b/Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs new file mode 100644 index 00000000..7975b7ef --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using SqlSugar; +namespace Yi.Framework.Model.Models +{ + /// + /// 日志表 + /// + public partial class LogEntity:BaseModelEntity + { + /// + /// 租户Id + /// + [SugarColumn(ColumnName="TenantId" )] + public long? TenantId { get; set; } + /// + /// 日志信息 + /// + [SugarColumn(ColumnName="Message" )] + public string Message { get; set; } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Service/LogService.cs b/Yi.Framework.Net6/Yi.Framework.Service/LogService.cs new file mode 100644 index 00000000..045568fe --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Service/LogService.cs @@ -0,0 +1,23 @@ +using SqlSugar; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Yi.Framework.Interface; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; + +namespace Yi.Framework.Service +{ + public partial class LogService + { + public async Task> AddListTest(List logEntities) + { + return await _repository._Db.Insertable(logEntities).SplitTable().ExecuteReturnSnowflakeIdListAsync(); + } + + public async Task> GetListTest() + { + return await _repository._Db.Queryable().SplitTable(tas => tas.Where(u => u.TableName.Contains("2020") || u.TableName.Contains("2021"))).ToListAsync(); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Service/ServiceTemplate/LogService.cs b/Yi.Framework.Net6/Yi.Framework.Service/ServiceTemplate/LogService.cs new file mode 100644 index 00000000..d88d2d7d --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Service/ServiceTemplate/LogService.cs @@ -0,0 +1,14 @@ +using SqlSugar; +using Yi.Framework.Interface; +using Yi.Framework.Model.Models; +using Yi.Framework.Repository; + +namespace Yi.Framework.Service +{ + public partial class LogService : BaseService, ILogService + { + public LogService(IRepository repository) : base(repository) + { + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs index ece1a0c4..806f0622 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs @@ -12,6 +12,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend { public static void AddSqlsugarServer(this IServiceCollection services) { + + + DbType dbType; var slavaConFig = new List(); if (Appsettings.appBool("MutiDB_Enabled")) @@ -42,7 +45,19 @@ namespace Yi.Framework.WebCore.MiddlewareExtend DisableNvarchar = true }, SlaveConnectionConfigs = slavaConFig, - + //设置codefirst非空值判断 + ConfigureExternalServices = new ConfigureExternalServices + { + EntityService = (c, p) => + { + // int? decimal?这种 isnullable=true + if (c.PropertyType.IsGenericType && + c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + p.IsNullable = true; + } + } + } }, db => { @@ -80,6 +95,10 @@ namespace Yi.Framework.WebCore.MiddlewareExtend Console.WriteLine("_______________________________________________"); Console.WriteLine("执行SQL:"+s.ToString()); + foreach (var i in p) + { + Console.WriteLine("参数:" +i.ParameterName+",参数值"+i.Value); + } Console.WriteLine("_______________________________________________"); };