From c6931e349266f766a936ff6c4876ce66daff86ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 23 Apr 2022 00:57:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=88=86?= =?UTF-8?q?=E8=A1=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebFirst/database/sqlite.db | Bin 139264 -> 139264 bytes .../Config/SwaggerDoc.xml | 14 ++++- .../Controllers/LogController.cs | 55 ++++++++++++++++++ .../yi-sqlsugar-dev.db | Bin 28672 -> 69632 bytes .../Yi.Framework.Interface/ILogService.cs | 13 +++++ .../IServiceTemplate/ILogService.cs | 12 ++++ .../Yi.Framework.Model/LogEntity.cs | 17 ++++++ .../Yi.Framework.Model/Models/LogEntity.cs | 23 ++++++++ .../Yi.Framework.Service/LogService.cs | 23 ++++++++ .../ServiceTemplate/LogService.cs | 14 +++++ .../MiddlewareExtend/SqlsugarExtension.cs | 21 ++++++- 11 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/LogController.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Interface/ILogService.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/ILogService.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Model/LogEntity.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Model/Models/LogEntity.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Service/LogService.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Service/ServiceTemplate/LogService.cs diff --git a/WebFirst/database/sqlite.db b/WebFirst/database/sqlite.db index 54a747c154c26f6bded128eb9c60649d23980a0b..0f0c339e48f5611e48a1daf523457e076c468cc5 100644 GIT binary patch delta 292 zcmZoTz|nAkV}i6`ECU0B5)i{c#zY-s#@LMsV)~5slgssG8TW6Vs-M4sS&2z$GW!E( zCMKoLjsn*hEn@7s85o6y<(-Xv?o zJ+rBPE6@&UX$}q!Re44R-_+vb#Pn1s1+nVM{)NvP*B6%*W#*+D85r1WwAxLdFUF+K zY_Hck-SIP{9*ez6tIhO{(-@6GEbHlxUqCF~R;%fX#~3xH7l<*ju-J>XT25am&m;np Ru$cbwD5J)9H&LctTmU5FSpWb4 delta 111 zcmZoTz|nAkV}i6`C<6n75)i{c+C&{=#?XxkV)~4BlgssG8Fy@+s-M4sS&^w>GW!GP z&5i=vOx&!-3`ZCxCFPxsjhh=DZEtwQ*rLnq$SOUZU4W?>sAM7wvu>-!^p8gwHMYBn IGVS6501&YrTL1t6 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 b852b0be791296ea07fddc85718ef665c5ddf27e..906d48707b349476aa402db41416b93284a1eb7e 100644 GIT binary patch delta 1696 zcmchX%}*0S7>8$DSlay>Hc^|1(M=OWn@~FY*)3iuqz7ZE5ld7K)}?G{l@z7bcmo| z`|Ep9E5dsakW3_U*@urD|qrA+I1sM)>F`3{Fm`Q@OO1OHE|bQn+J19O0Sp z_4%+gS6V2Qs|}I6=*NML4gQLjv&&Yw`hQW%&SWx@G=l#zWmhWJf*qEgS<8i@wH#4& zJt}2y<{IN%h;C)=3P(S)?EMu~|cQlvp+>cl* z^1Te8;|54UXc-vpqJUg*#tMYfDj?0ZD3atDB!1{Rt)y& zUrrwD-zzXZA7XF)?Ajl|?X%y&pZeOK<1i7*KMnN8cQ(30LmI!;0HnCn-d-ZU{q~X) z&%Fcz62+V{3QHKm?|xgBCx+z?K%%~;`iAMoXL!l^#R&yR9RCXI8*)E|A%WKHGQ;B; z1swn#T-t(nZcq%Zf~$B(AaEYq2XAVF0QZ-S$w~~#5<+9-d}tLOC1qVpAf(`r5MI+_ zis~+_$uZp+RbEL0m$KD2T+Y!-QIGZKxC;*cld-J2qcEdZ^ zvMS?KRZO)TUDb41N$4lw-DnTr1h9$m@8G>Df*(G@Z+Ups1#g%MO+m;od(kyBaRS{< q_UJnR?qD~Y@NVW%d~5UXC&=dIF5*?)OzI{wdcu=k%yWn4-Qa)xrp-bC delta 90 zcmZozz|!!5ae}m<4g&)NI}pPF>qH%6NgW0~>yNzLw;0&CHZ$-!@t^0~%qPgp!gFP_ oqJlNo=FMChjLd%-eoel|tvT6{_YAY9I@{!n{4$$A@$e-B0B-OW(EtDd 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("_______________________________________________"); };