sunpengfei
2025-08-14 567401d7d0338c582e22ed24399dabc6803e3ec8
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
{
    /// <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 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();
            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 (models.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);
                    }
                    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);
                        }
                    }
                }
                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>
        /// 编辑灵工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<Guid> Handle(EditEnterpriseEmployeeCommand request, CancellationToken cancellationToken)
        {
            var logier = JwtUtils.GetCurrentLogier();
            return request.SaveData<EnterpriseEmployee, EditEnterpriseEmployeeCommand>(
                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, "该灵工已报名无法修改信息");
                    }
                    request.Adapt(entity);
                },
                cancellationToken);
        }
    }
}