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.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs |  137 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 130 insertions(+), 7 deletions(-)

diff --git a/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs b/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
index fbda053..e342e1d 100644
--- a/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
+++ b/FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
@@ -1,13 +1,20 @@
 锘縰sing FlexJobApi.Core;
+using Furion;
 using Furion.DatabaseAccessor;
+using Furion.DataValidation;
 using Furion.FriendlyException;
+using Furion.HttpRemote;
 using Mapster;
 using MediatR;
 using Microsoft.EntityFrameworkCore;
+using MiniExcelLibs;
+using NetTopologySuite.Index.HPRtree;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Principal;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 
 namespace FlexJobApi.UserServer.Application
@@ -16,16 +23,131 @@
     /// 鐏靛伐鍛戒护澶勭悊鍣�
     /// </summary>
     public class EnterpriseEmployeesCommandHandler(
-            IRepository<EnterpriseEmployee> rep
+            IRepository<EnterpriseEmployee> rep,
+            IRepository<User> repUser
         ) :
-        IRequestHandler<ImportEnterpriseEmployeesCommand, int>,
+        IRequestHandler<ImportEnterpriseEmployeesCommand, ImportEnterpriseEmployeesCommandResult>,
         IRequestHandler<EditEnterpriseEmployeeCommand, Guid>
     {
         private readonly IRepository<EnterpriseEmployee> rep = rep;
+        private readonly IRepository<User> repUser = repUser;
 
-        public Task<int> Handle(ImportEnterpriseEmployeesCommand request, CancellationToken cancellationToken)
+        /// <summary>
+        /// 瀵煎叆鐏靛伐淇℃伅
+        /// </summary>
+        /// <param name="request"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        public async Task<ImportEnterpriseEmployeesCommandResult> Handle(ImportEnterpriseEmployeesCommand request, CancellationToken cancellationToken)
         {
-            throw new NotImplementedException();
+            var logier = JwtUtils.GetCurrentLogier();
+            var result = new ImportEnterpriseEmployeesCommandResult();
+            var models = await request.ExcelUrl.ImportExcelFromOSS<ImportEnterpriseEmployeesCommandModel>();
+            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)
+            {
+                var errors = new List<string>();
+                if (model.Name.IsNull())
+                {
+                    errors.Add("璇峰~鍐欏鍚�");
+                }
+                if (model.ContactPhoneNumber.IsNull())
+                {
+                    errors.Add("璇峰~鍐欐墜鏈哄彿");
+                }
+                else if (!model.ContactPhoneNumber.TryValidate(EnumValidationTypes.ValidPhoneNumber).IsValid)
+                {
+                    errors.Add("鎵嬫満鍙锋牸寮忎笉姝g‘");
+                }
+                if (model.Identity.IsNull())
+                {
+                    errors.Add("璇峰~鍐欒韩浠借瘉鍙�");
+                }
+                else if (!model.Identity.TryValidate(EnumValidationTypes.ValiIdentity).IsValid)
+                {
+                    errors.Add("韬唤璇佸彿鏍煎紡涓嶆纭�");
+                }
+                else if (successList.Any(it => it.Identity == model.Identity))
+                {
+                    errors.Add("韬唤璇佸彿閲嶅");
+                }
+                else
+                {
+                    model.Gender = model.Identity.GetGender();
+                    model.Birthday = model.Identity.GetBirthday();
+                    model.Age = model.Identity.GetAge();
+                }
+
+                if (errors.IsNull())
+                {
+                    var enterpriseEmployee = enterpriseEmployees.FirstOrDefault(it => it.Identity == model.Identity);
+                    if (enterpriseEmployee == null)
+                    {
+                        enterpriseEmployee = new EnterpriseEmployee
+                        {
+                            EnterpriseId = logier.EnterpriseId!.Value,
+                            Name = model.Name,
+                            Identity = model.Identity,
+                            ContactPhoneNumber = model.ContactPhoneNumber,
+                            Gender = model.Gender,
+                            Birthday = model.Birthday,
+                            Age = model.Age,
+                        };
+                        await rep.InsertAsync(enterpriseEmployee);
+                        successList.Add(model);
+                    }
+                    else
+                    {
+                        var canUpdate = true;
+                        if (enterpriseEmployee.UserId.HasValue)
+                        {
+                            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);
+                            }
+                        }
+                        if (canUpdate)
+                        {
+                            enterpriseEmployee.Name = model.Name;
+                            enterpriseEmployee.ContactPhoneNumber = model.ContactPhoneNumber;
+                            enterpriseEmployee.Gender = model.Gender;
+                            enterpriseEmployee.Birthday = model.Birthday;
+                            enterpriseEmployee.Age = model.Age;
+                            await rep.UpdateAsync(enterpriseEmployee);
+                            successList.Add(model);
+                        }
+                    }
+                }
+                if (errors.IsNotNull())
+                {
+                    var error = model.Adapt<ImportEnterpriseEmployeesCommandResultError>();
+                    error.ErrorMessage = errors.SplitJoin("锛�");
+                    result.Errors.Add(error);
+                }
+            }
+            result.TotalCount = models.Count;
+            result.FailCount = result.Errors.Count;
+            result.SuccessCount = result.TotalCount - result.FailCount;
+            return result;
         }
 
         /// <summary>
@@ -36,14 +158,15 @@
         /// <returns></returns>
         public Task<Guid> Handle(EditEnterpriseEmployeeCommand request, CancellationToken cancellationToken)
         {
+            var logier = JwtUtils.GetCurrentLogier();
             return request.SaveData<EnterpriseEmployee, EditEnterpriseEmployeeCommand>(
-                null,
-                null,
+                q => q.Where(it => it.EnterpriseId == logier.EnterpriseId),
+                it => it.EnterpriseId == logier.EnterpriseId && it.Id != request.Id && it.Identity == request.Identity,
                 (entity) =>
                 {
                     if (request.Id.HasValue && entity.UserId.HasValue)
                     {
-                        throw Oops.Oh(EnumErrorCodeType.s510, "璇ョ伒宸ュ凡鎶ュ悕鏃犳硶淇敼淇℃伅");
+                        throw Oops.Oh(EnumErrorCodeType.s510, "宸叉姤鍚嶆棤娉曚慨鏀逛俊鎭�");
                     }
                     request.Adapt(entity);
                 },

--
Gitblit v1.9.1