sunpengfei
2025-08-14 173a8e6a757e8c7c324d5d00cd1ec90d0f13d281
FlexJobApi.UserServer.Application/EnterpriseEmployees/Commands/EnterpriseEmployeesCommandHandler.cs
@@ -1,16 +1,20 @@
using 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
@@ -21,7 +25,7 @@
    public class EnterpriseEmployeesCommandHandler(
            IRepository<EnterpriseEmployee> rep
        ) :
        IRequestHandler<ImportEnterpriseEmployeesCommand, int>,
        IRequestHandler<ImportEnterpriseEmployeesCommand, ImportEnterpriseEmployeesCommandResult>,
        IRequestHandler<EditEnterpriseEmployeeCommand, Guid>
    {
        private readonly IRepository<EnterpriseEmployee> rep = rep;
@@ -32,11 +36,97 @@
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(ImportEnterpriseEmployeesCommand request, CancellationToken cancellationToken)
        public async Task<ImportEnterpriseEmployeesCommandResult> Handle(ImportEnterpriseEmployeesCommand request, CancellationToken cancellationToken)
        {
            var logier = JwtUtils.GetCurrentLogier();
            var result = new ImportEnterpriseEmployeesCommandResult();
            var models = await request.ExcelUrl.ImportExcelFromOSS<ImportEnterpriseEmployeesCommandModel>();
            Console.WriteLine();
            throw new NotImplementedException();
            var identities = models.DistinctSelect(it => it.Identity);
            var enterpriseEmployees = await rep.AsQueryable()
                .Where(it => it.EnterpriseId == logier.EnterpriseId && identities.Contains(it.Identity))
                .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("手机号格式不正确");
                }
                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
                    {
                        if (enterpriseEmployee.UserId.HasValue)
                        {
                            errors.Add("该灵工已报名无法修改信息");
                        }
                        else
                        {
                            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>
@@ -47,9 +137,10 @@
        /// <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)