From ab92e760e8461f0a48b37786b0f5a78db744dca6 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期一, 18 八月 2025 16:37:06 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs                                                        |   60 +++++++++++++++++++++++++++++-
 FlexJobApi.Core/Utils/ResourceUtils/ResourceActionFilter.cs                                         |    6 ++
 FlexJobApi.UserServer.Application/FlexJobApi.UserServer.Application.xml                             |    2 
 FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs |   27 ++++++++++++-
 FlexJobApi.UserServer.Application/Users/Commands/SyncUserCommandHandler.cs                          |   12 +++++-
 5 files changed, 98 insertions(+), 9 deletions(-)

diff --git a/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs b/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs
index e024dd2..d395149 100644
--- a/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs
+++ b/FlexJobApi.Core/Utils/JwtUtils/JwtHandler.cs
@@ -1,7 +1,18 @@
-锘縰sing Furion.Authorization;
+锘縰sing FlexJobApi.Core.Entities.Common;
+using Furion;
+using Furion.Authorization;
+using Furion.DatabaseAccessor;
 using Furion.DataEncryption;
+using Furion.DistributedIDGenerator;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Controllers;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
 using System.Threading.Tasks;
 
 namespace FlexJobApi.Core;
@@ -15,7 +26,52 @@
         {
             await AuthorizeHandleAsync(context);
         }
-        else context.Fail();
+        else
+        {
+
+            var resourceLog = new ResourceLog();
+            resourceLog.CreatedTime = DateTimeOffset.Now;
+            var stopwatch = Stopwatch.StartNew();
+            var serviceScopeFactory = App.GetService<IServiceScopeFactory>();
+            var serviceScope = serviceScopeFactory.CreateScope();
+            var rep = serviceScope.ServiceProvider.GetRequiredService<IRepository<ResourceLog, LogDbContextLocator>>();
+            resourceLog.Id = IDGen.NextID();
+            resourceLog.TraceId = App.GetTraceId();
+            resourceLog.Method = EnumUtils.GetEnum<EnumResourceMethod>(httpContext.Request.Method);
+            resourceLog.Domain = $"{httpContext.Request.Scheme}://{httpContext.Request.Host}";
+            resourceLog.Path = httpContext.Request.Path;
+            resourceLog.ClientIpAddress = httpContext.GetRemoteIpAddressToIPv4();
+            if (httpContext.Request.ContentType?.Contains("application/json") == true)
+            {
+                httpContext.Request.EnableBuffering(); // 鍏佽澶氭璇诲彇
+                var body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync();
+                httpContext.Request.Body.Position = 0; // 閲嶇疆娴佷綅缃�
+                resourceLog.Request = body;
+            }
+            else
+            {
+                resourceLog.Request = httpContext.Request.Query.ToJson();
+            }
+            resourceLog.RequestHeaders = httpContext.Request.Headers.ToJson();
+
+            context.Fail();
+
+            resourceLog.UpdatedTime = DateTimeOffset.Now;
+            resourceLog.Response = new FriendlyResult<object>
+            {
+                TraceId = resourceLog.TraceId,
+                Code = 401,
+                Success = false,
+                Timestamp = DateTime.Now.ToTimeStamp()
+            }.ToJson();
+            resourceLog.ResponseHeaders = httpContext.Response.Headers.ToJson();
+            resourceLog.IsSuccess = false;
+
+            stopwatch.Stop();
+            resourceLog.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
+
+            await rep.InsertNowAsync(resourceLog);
+        }
     }
 
     /// <summary>
diff --git a/FlexJobApi.Core/Utils/ResourceUtils/ResourceActionFilter.cs b/FlexJobApi.Core/Utils/ResourceUtils/ResourceActionFilter.cs
index c08cbc2..1393015 100644
--- a/FlexJobApi.Core/Utils/ResourceUtils/ResourceActionFilter.cs
+++ b/FlexJobApi.Core/Utils/ResourceUtils/ResourceActionFilter.cs
@@ -4,6 +4,7 @@
 using Furion.DistributedIDGenerator;
 using Furion.Schedule;
 using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.Controllers;
 using Microsoft.AspNetCore.Mvc.Filters;
 using Microsoft.Extensions.DependencyInjection;
@@ -48,7 +49,10 @@
             var actionContext = await next();
 
             resourceLog.UpdatedTime = DateTimeOffset.Now;
-            resourceLog.Response = actionContext.Result.ToJson();
+            if (actionContext.Result is JsonResult jsonResult)
+            {
+                resourceLog.Response = jsonResult.Value.ToJson();
+            }
             resourceLog.ResponseHeaders = context.HttpContext.Response.Headers.ToJson();
             resourceLog.IsSuccess = actionContext.Exception == null;
 
diff --git a/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs b/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
index a6aebe5..e342e1d 100644
--- a/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
+++ b/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
@@ -23,12 +23,14 @@
     /// 鐏靛伐鍛戒护澶勭悊鍣�
     /// </summary>
     public class EnterpriseEmployeesCommandHandler(
-            IRepository<EnterpriseEmployee> rep
+            IRepository<EnterpriseEmployee> rep,
+            IRepository<User> repUser
         ) :
         IRequestHandler<ImportEnterpriseEmployeesCommand, ImportEnterpriseEmployeesCommandResult>,
         IRequestHandler<EditEnterpriseEmployeeCommand, Guid>
     {
         private readonly IRepository<EnterpriseEmployee> rep = rep;
+        private readonly IRepository<User> repUser = repUser;
 
         /// <summary>
         /// 瀵煎叆鐏靛伐淇℃伅
@@ -44,6 +46,10 @@
             var identities = models.DistinctSelect(it => it.Identity);
             var enterpriseEmployees = await rep.AsQueryable()
                 .Where(it => it.EnterpriseId == logier.EnterpriseId && identities.Contains(it.Identity))
+                .ToListAsync();
+            var userIds = enterpriseEmployees.DistinctSelect(it => it.UserId.HasValue, it => it.UserId!.Value);
+            var users = await repUser.AsQueryable()
+                .Where(it => userIds.Contains(it.Id))
                 .ToListAsync();
             var successList = new List<ImportEnterpriseEmployeesCommandModel>();
             foreach (var model in models)
@@ -100,11 +106,26 @@
                     }
                     else
                     {
+                        var canUpdate = true;
                         if (enterpriseEmployee.UserId.HasValue)
                         {
-                            errors.Add("宸叉姤鍚嶆棤娉曚慨鏀逛俊鎭�");
+                            var user = users.FirstOrDefault(it => it.Id == enterpriseEmployee.UserId.Value)!;
+                            if (user.IsReal == true)
+                            {
+                                canUpdate = false;
+                                errors.Add("宸插疄鍚嶆棤娉曚慨鏀逛俊鎭�");
+                            }
+                            else
+                            {
+                                user.Name = model.Name;
+                                user.ContactPhoneNumber = model.ContactPhoneNumber;
+                                user.Gender = model.Gender;
+                                user.Birthday = model.Birthday;
+                                user.Age = model.Age;
+                                await repUser.UpdateAsync(user);
+                            }
                         }
-                        else
+                        if (canUpdate)
                         {
                             enterpriseEmployee.Name = model.Name;
                             enterpriseEmployee.ContactPhoneNumber = model.ContactPhoneNumber;
diff --git a/FlexJobApi.UserServer.Application/FlexJobApi.UserServer.Application.xml b/FlexJobApi.UserServer.Application/FlexJobApi.UserServer.Application.xml
index c2c5b0c..658df5a 100644
--- a/FlexJobApi.UserServer.Application/FlexJobApi.UserServer.Application.xml
+++ b/FlexJobApi.UserServer.Application/FlexJobApi.UserServer.Application.xml
@@ -692,7 +692,7 @@
             鍚屾鐢ㄦ埛鍛戒护澶勭悊鍣�
             </summary>
         </member>
-        <member name="M:FlexJobApi.UserServer.Application.SyncUserCommandHandler.#ctor(Furion.DatabaseAccessor.IRepository{FlexJobApi.Core.User},Furion.DatabaseAccessor.IRepository{FlexJobApi.Core.Enterprise})">
+        <member name="M:FlexJobApi.UserServer.Application.SyncUserCommandHandler.#ctor(Furion.DatabaseAccessor.IRepository{FlexJobApi.Core.User},Furion.DatabaseAccessor.IRepository{FlexJobApi.Core.Enterprise},Furion.DatabaseAccessor.IRepository{FlexJobApi.Core.Role})">
             <summary>
             鍚屾鐢ㄦ埛鍛戒护澶勭悊鍣�
             </summary>
diff --git a/FlexJobApi.UserServer.Application/Users/Commands/SyncUserCommandHandler.cs b/FlexJobApi.UserServer.Application/Users/Commands/SyncUserCommandHandler.cs
index 22771dd..62f87f6 100644
--- a/FlexJobApi.UserServer.Application/Users/Commands/SyncUserCommandHandler.cs
+++ b/FlexJobApi.UserServer.Application/Users/Commands/SyncUserCommandHandler.cs
@@ -65,9 +65,10 @@
                 await repEnterprise.InsertAsync(enterprise);
             else
                 await repEnterprise.UpdateAsync(enterprise);
+
             var user = await repUser.AsQueryable()
                     .Include(it => it.UserRoles)
-                    .Where(it => it.Type == EnumUserType.Enterprise && it.UserName == request.UserName)
+                    .Where(it => it.Type == EnumUserType.Enterprise && it.DataSource == request.DataSource && it.DataSourceId == request.DataSourceId)
                     .FirstOrDefaultAsync(cancellationToken);
             if (user == null)
             {
@@ -78,6 +79,10 @@
             {
                 throw Oops.Oh(EnumErrorCodeType.s405, "璐﹀彿");
             }
+            var checkUserNameExist = await repUser.AsQueryable().AsNoTracking()
+                .AnyAsync(it => it.Type == EnumUserType.Enterprise && it.UserName == request.UserName && it.Id != user.Id);
+            if (checkUserNameExist) throw Oops.Oh(EnumErrorCodeType.s405, "璐﹀彿");
+
             user.EnterpriseId = enterprise.Id;
             user.Level = 100;
             user.UserRoles = await repRole.AsQueryable().AsNoTracking()
@@ -106,7 +111,7 @@
             var user = await repUser.AsQueryable()
                 .Include(it => it.UserRoles)
                 .Include(it => it.UserManageIndustrialParks)
-                .Where(it => it.Type == EnumUserType.Operation && it.UserName == request.UserName)
+                .Where(it => it.Type == EnumUserType.Operation && it.DataSource == request.DataSource && it.DataSourceId == request.DataSourceId)
                 .FirstOrDefaultAsync(cancellationToken);
             if (user == null)
             {
@@ -114,6 +119,9 @@
                 user.Type = EnumUserType.Operation;
                 user.Status = EnumUserStatus.Normal;
             }
+            var checkUserNameExist = await repUser.AsQueryable().AsNoTracking()
+                .AnyAsync(it => it.Type == EnumUserType.Operation && it.UserName == request.UserName && it.Id != user.Id);
+            if (checkUserNameExist) throw Oops.Oh(EnumErrorCodeType.s405, "璐﹀彿");
             request.Adapt(user);
             user.UserManageIndustrialParks = request.IndustrialParkIds.Select(it => new UserManageIndustrialPark
             {

--
Gitblit v1.9.1