sunpengfei
2025-08-14 ba6a9c246898ecf04f40c827db27a1729f1b0f87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using FlexJobApi.Core;
using Furion;
using Furion.DatabaseAccessor;
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.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace FlexJobApi.UserServer.Application
{
    /// <summary>
    /// 灵工命令处理器
    /// </summary>
    public class EnterpriseEmployeesCommandHandler(
            IRepository<EnterpriseEmployee> rep
        ) :
        IRequestHandler<ImportEnterpriseEmployeesCommand, ImportEnterpriseEmployeesCommandResult>,
        IRequestHandler<EditEnterpriseEmployeeCommand, Guid>
    {
        private readonly IRepository<EnterpriseEmployee> rep = rep;
 
        /// <summary>
        /// 导入灵工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ImportEnterpriseEmployeesCommandResult> Handle(ImportEnterpriseEmployeesCommand request, CancellationToken cancellationToken)
        {
            var result = new ImportEnterpriseEmployeesCommandResult();
            var models = await request.ExcelUrl.ImportExcelFromOSS<ImportEnterpriseEmployeesCommandModel>();
            foreach (var model in models)
            {
                var error = new ImportEnterpriseEmployeesCommandResultError();
                if (model.ContactPhoneNumber.IsNull())
                {
                    error.ErrorMessage += "请填写手机号";
                }
                else if (!Regex.IsMatch(model.ContactPhoneNumber, @"^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$"))
                {
                    error.ErrorMessage += "手机号格式不正确";
                }
            }
            return result;
        }
 
        /// <summary>
        /// 编辑灵工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<Guid> Handle(EditEnterpriseEmployeeCommand request, CancellationToken cancellationToken)
        {
            return request.SaveData<EnterpriseEmployee, EditEnterpriseEmployeeCommand>(
                null,
                null,
                (entity) =>
                {
                    if (request.Id.HasValue && entity.UserId.HasValue)
                    {
                        throw Oops.Oh(EnumErrorCodeType.s510, "该灵工已报名无法修改信息");
                    }
                    request.Adapt(entity);
                },
                cancellationToken);
        }
    }
}