From 8280ee1102bb17248ddc5575511c95d38a034022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=AF=85?= <2667210109@qq.com> Date: Sun, 1 Dec 2024 13:13:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=B8=E5=A4=9Abug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- asg_form/Controllers/Dbset.cs | 26 +++++++++++++++++++++++ asg_form/Controllers/excel.cs | 37 +++++++++++++++++++++++---------- asg_form/Controllers/form_cs.cs | 2 +- asg_form/asg_form.csproj | 8 +++---- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/asg_form/Controllers/Dbset.cs b/asg_form/Controllers/Dbset.cs index cf0cebc..b6e0604 100644 --- a/asg_form/Controllers/Dbset.cs +++ b/asg_form/Controllers/Dbset.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.DataEncryption; using Microsoft.EntityFrameworkCore.DataEncryption.Providers; using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using System.Security.Cryptography; using System.Text; using static asg_form.Controllers.Budget.BgCountController; @@ -296,12 +297,25 @@ namespace asg_form.Controllers protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connStr = @"Host=localhost;Port=2345;Database=asg;Username=postgres;Password=luolan12323;"; + optionsBuilder.UseNpgsql(connStr); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) + { + property.SetValueConverter(new ValueConverter( + v => v.ToUniversalTime(), + v => DateTime.SpecifyKind(v, DateTimeKind.Unspecified))); + } + } + } modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } @@ -324,6 +338,18 @@ namespace asg_form.Controllers protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) + { + property.SetValueConverter(new ValueConverter( + v => v.ToUniversalTime(), + v => DateTime.SpecifyKind(v, DateTimeKind.Unspecified))); + } + } + } modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } diff --git a/asg_form/Controllers/excel.cs b/asg_form/Controllers/excel.cs index 7cdbea1..bbc0359 100644 --- a/asg_form/Controllers/excel.cs +++ b/asg_form/Controllers/excel.cs @@ -35,7 +35,7 @@ namespace asg_form.Controllers - public static void ExportToExcel_noadmin(List
data, string fileName) + public static void ExportToExcel_noadmin(List data,string eventname, string fileName) { using (var package = new ExcelPackage()) { @@ -56,9 +56,16 @@ namespace asg_form.Controllers worksheet.Cells[1, a].Style.Fill.BackgroundColor.SetColor(Color.RoyalBlue); a++; } - var pic = worksheet.Drawings.AddPicture(form.team_name, $@"{AppDomain.CurrentDomain.BaseDirectory}loge\{form.team_name}.png"); - pic.SetSize(50, 50); - pic.SetPosition(26, 0); + try + { + var pic = worksheet.Drawings.AddPicture(form.team_name, $@"{AppDomain.CurrentDomain.BaseDirectory}loge/{eventname}/{form.team_name}.png"); + pic.SetSize(50, 50); + pic.SetPosition(26, 0); + } + catch (Exception) + { + Console.WriteLine($@"{AppDomain.CurrentDomain.BaseDirectory}loge/{eventname}/{form.team_name}.png"); + } worksheet.Cells[2, 2].Value = form.Id; worksheet.Cells[2, 3].Value = form.team_name; worksheet.Cells[2, 4].Value = form.team_tel; @@ -107,7 +114,7 @@ namespace asg_form.Controllers - public static void ExportToExcel(List data, string fileName) + public static void ExportToExcel(List data, string eventname, string fileName) { using (var package = new ExcelPackage()) { @@ -131,9 +138,17 @@ namespace asg_form.Controllers worksheet.Cells[1, a].Style.Fill.BackgroundColor.SetColor(Color.RoyalBlue); a++; } - var pic = worksheet.Drawings.AddPicture(form.team_name, $@"{AppDomain.CurrentDomain.BaseDirectory}loge\{form.team_name}.png"); - pic.SetSize(50, 50); - pic.SetPosition(26, 0); + try + { + var pic = worksheet.Drawings.AddPicture(form.team_name, $@"{AppDomain.CurrentDomain.BaseDirectory}loge/{eventname}/{form.team_name}.png"); + pic.SetSize(50, 50); + pic.SetPosition(26, 0); + } + catch (Exception) + { + + } + worksheet.Cells[2, 2].Value = form.Id; worksheet.Cells[2, 3].Value = form.team_name; worksheet.Cells[2, 4].Value = form.team_tel; @@ -341,16 +356,16 @@ namespace asg_form.Controllers { string guid = Guid.NewGuid().ToString(); TestDbContext testDb = new TestDbContext(); - List result = testDb.Forms.ToList(); + List result = testDb.Forms.Include(a=>a.role).Include(a=>a.events).Where(a=>a.events.name==event_name).ToList(); if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin")) { - ExportToExcel(result, $"{AppDomain.CurrentDomain.BaseDirectory}excel/{guid}.xlsx"); + ExportToExcel(result,event_name, $"{AppDomain.CurrentDomain.BaseDirectory}excel/{guid}.xlsx"); return Ok(guid); } else if (this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin")) { - ExportToExcel_noadmin(result, $"{AppDomain.CurrentDomain.BaseDirectory}excel/{guid}.xlsx"); + ExportToExcel_noadmin(result,event_name, $"{AppDomain.CurrentDomain.BaseDirectory}excel/{guid}.xlsx"); return Ok(guid); } else diff --git a/asg_form/Controllers/form_cs.cs b/asg_form/Controllers/form_cs.cs index 3020696..a2594e3 100644 --- a/asg_form/Controllers/form_cs.cs +++ b/asg_form/Controllers/form_cs.cs @@ -423,7 +423,7 @@ namespace asg_form.Controllers if (imageFile == null || imageFile.Length == 0) return BadRequest("Invalid image file."); // 将文件保存到磁盘 - var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{for1.events_name}/", $"{imageFile.FileName}"); + var filePath = Path.Combine(Directory.GetCurrentDirectory(), $"loge/{for1.events_name}/", $"{for1.team_name}.png"); using (var stream = new FileStream(filePath, FileMode.Create)) { await imageFile.CopyToAsync(stream); diff --git a/asg_form/asg_form.csproj b/asg_form/asg_form.csproj index c1166e0..0188af3 100644 --- a/asg_form/asg_form.csproj +++ b/asg_form/asg_form.csproj @@ -30,6 +30,7 @@ + @@ -37,7 +38,6 @@ - @@ -53,10 +53,10 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + - + @@ -69,11 +69,9 @@ - -