Seamus 3 ヶ月 前
コミット
8f0a88009d

+ 0 - 26
ERP.Application/Program.cs

@@ -26,32 +26,6 @@ namespace ERP
             app.InitConfigure();
             app.Run();
 
-            //var builder = WebApplication.CreateBuilder(args);
-
-            //// Add services to the czxczzxcontainer.
-
-            //builder.Services.AddControllers();
-            //// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
-            //builder.Services.AddEndpointsApiExplorer();
-            //builder.Services.AddSwaggerGen();
-
-            //var app = builder.Build();
-
-            //// Configure the HTTP request pipeline.
-            //if (app.Environment.IsDevelopment())
-            //{
-            //    app.UseSwagger();
-            //    app.UseSwaggerUI();
-            //}
-
-            //app.UseHttpsRedirection();
-
-            //app.UseAuthorization();
-
-
-            //app.MapControllers();
-
-            //app.Run();
         }
     }
 }

+ 2 - 14
ERP.Application/appsettings.Development.json

@@ -1,18 +1,6 @@
 {
-  "DBConfig": {
-    "ConnectionConfigs": [
-      {
-        "DbType": 0,
-        "ConnectionString": "server=8.222.216.184;Database=erp;Uid=root;Pwd=zyh@0728;"
-      }
-    ]
-  },
-  " ConnectionStrings": {
-    "CoreDBContext": "server=127.0.0.1;user id=root;database= ;password= ", //my sql
-    "CoreDBContext2": "server=127.0.0.1;user id=root;database= ;password= ", //my sql
-    "CoreDBContext3": "server=127.0.0.1;user id=root;database= ;password= ", //my sql
-    "CoreDBContext4": "server=127.0.0.1;user id=root;database= ;password= ", //my sql
-    "CoreDBContext5": "server=127.0.0.1;user id=root;database= ;password= " //my sql
+  "ConnectionStrings": {
+    "CoreDbContext": "server=8.222.216.184;user id=root;database=erp;password=zyh@0728" //my sql
   },
   "Redis": {
     "Host": "localhost",

+ 4 - 1
ERP.Core/Application.cs

@@ -9,6 +9,7 @@ using ERP.Framework.Constants;
 using ERP.Framework.Extensions;
 using ERP.Framework.Filter;
 using Newtonsoft.Json;
+using ERP.Core.Context;
 
 namespace ERP.Core
 {
@@ -81,6 +82,9 @@ namespace ERP.Core
             // 可添加 AddOauth2() 第三方授权或SSO
             // AddElasticSerch() 配置ElasticSearc
             // AddRateLimiting 限流
+            builder.Services.AddEndpointsApiExplorer();
+
+            builder.Services.AddDbContext<CoreDbContext>();
 
             return builder;
         }
@@ -99,7 +103,6 @@ namespace ERP.Core
             {
                 //  endpoints.MapMetrics();
                 endpoints.MapControllers();
-                //endpoints.MapHealthChecks("/health");
             });
         }
     }

+ 40 - 3
ERP.Core/Context/CoreDbContext.cs

@@ -1,6 +1,10 @@
 using ERP.Core.Entity;
+using ERP.Framework.WebApi;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using Serilog;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -18,12 +22,37 @@ namespace ERP.Core.Context
             this._configuration = configuration;
         }
 
+        public override int SaveChanges()
+        {
+            var entityEntries = ChangeTracker.Entries().ToList();
+            foreach(var entry in entityEntries)
+            {
+                if (entry.State == EntityState.Added)
+                {
+                    Entry(entry.Entity).Property(nameof(BaseEntity.CreateTime)).CurrentValue = DateTime.Now;
+
+                    //Entry(entry.Entity).Property(nameof(BaseEntity.CreateBy)).CurrentValue = DateTime.Now;
+                }
+
+                if (entry.State == EntityState.Modified)
+                {
+                    Entry(entry.Entity).Property(nameof(BaseEntity.UpdateTime)).CurrentValue = DateTime.Now;
+                }
+            }
+
+            return base.SaveChanges();
+        }
+
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
+            var name = GetType().Name;
             // 可扩展为多租户,从当前请求头拿租户Id,根据租户Id获取对应数据库配置名,替换
-            string connect = _configuration.GetConnectionString(GetType().Name)!;
-
+            string connect = _configuration.GetConnectionString(name)!;
+            // 可扩展为使用多种数据库,添加配置项数组根据不同租户Id,配置不同库类型,Key为租户Id/库名,对象包含数据库类型等
             optionsBuilder.UseMySql(connect,new MySqlServerVersion(new Version(8,0,26)));
+
+            UseLog(optionsBuilder);
+
             base.OnConfiguring(optionsBuilder);
         }
 
@@ -33,10 +62,18 @@ namespace ERP.Core.Context
         /// <param name="modelBuilder"></param>
         protected override void OnModelCreating(ModelBuilder modelBuilder)
         {
+        }
 
+        private void UseLog(DbContextOptionsBuilder optionsBuilder)
+        {
+            optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder =>
+            {
+                builder.AddConsole();
+                builder.AddSerilog();
+            }));
         }
 
-        public virtual DbSet<SysUser> SysUsers { get; set; }
+        public virtual DbSet<SysUser> SysUser { get; set; }
 
 
     }

+ 1 - 0
ERP.Core/Entity/SysUser.cs

@@ -1,5 +1,6 @@
 using ERP.Core.Enum;
 using ERP.Framework.WebApi;
+using System.ComponentModel.DataAnnotations.Schema;
 
 namespace ERP.Core.Entity
 {

+ 12 - 12
ERP.Core/Service/AuthService.cs

@@ -43,24 +43,24 @@ namespace ERP.Core.Service
                 throw new Exception(string.Format(_localizer["User.NotExist"], loginName));
             }
 
-            CheckUserStatus(user);
+            //CheckUserStatus(user);
 
             CheckLogin(securityConfig, password, user);
 
             return new LoginUser();
         }
 
-        private void CheckUserStatus(SysUser user)
-        {
-            if (user.UserStatus == StatusEnum.Enable)
-            {
-                return;
-            }
-            else if (user.UserStatus == StatusEnum.Disable)
-            {
-                throw new Exception(string.Format(_localizer["User.Bloked"], user.UserName));
-            }
-        }
+        //private void CheckUserStatus(SysUser user)
+        //{
+        //    if (user.UserStatus == StatusEnum.Enable)
+        //    {
+        //        return;
+        //    }
+        //    else if (user.UserStatus == StatusEnum.Disable)
+        //    {
+        //        throw new Exception(string.Format(_localizer["User.Bloked"], user.UserName));
+        //    }
+        //}
 
         private void CheckLogin(SecurityConfig securityConfig, string password, SysUser loginUser)
         {

+ 2 - 0
ERP.Framework/ERP.Framework.csproj

@@ -22,6 +22,8 @@
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
+    <PackageReference Include="Serilog" Version="4.0.0" />
+    <PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
     <PackageReference Include="StackExchange.Redis" Version="2.6.122" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 1 - 1
ERP.Framework/Extensions/AutofacExtension.cs

@@ -18,7 +18,7 @@ namespace ERP.Framework.Extensions
         {
             var assemblyList = AppDomain.CurrentDomain.GetAssemblies()
                 .Where(t => t.FullName != null && t.FullName.Contains("ERP"));
-
+            
             //根据名称约定(业务处理层以Service结尾,仓储层以Repository结尾),实现服务接口和服务实现的依赖
             builder.RegisterAssemblyTypes(assemblyList.ToArray())
                 .Where(t => t.IsClass)

+ 0 - 1
ERP.Framework/WebApi/BaseEntity.cs

@@ -12,7 +12,6 @@ namespace ERP.Framework.WebApi
         public BaseEntity()
         {
             this.Id = YitIdHelper.NextId();
-            this.CreateTime = DateTime.Now;
         }
 
         /// <summary>