sunpengfei
2025-08-07 313c5cbe5c63fa07f78fa24d8cc33b75435a266f
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
using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.User.Application
{
    /// <summary>
    /// 企业命令处理器
    /// </summary>
    public class EnterpriseCommandHandler(
            IRepository<Enterprise> rep,
            IRepository<UserInfo> repUserInfo
        ) :
        IRequestHandler<SaveEnterpriseCommand, Guid>,
        IRequestHandler<SetEnterpriseElectronSignSettingCommand, Guid>,
        IRequestHandler<SetEnterpriseSmsSettingCommand, Guid>
 
    {
        private readonly IRepository<Enterprise> rep = rep;
        private readonly IRepository<UserInfo> repUserInfo = repUserInfo;
 
        /// <summary>
        /// 保存企业
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(SaveEnterpriseCommand request, CancellationToken cancellationToken)
        {
            var entity = await rep.AsQueryable()
                .Include(it => it.EnterpriseAuth)
                .FirstOrDefaultAsync(it => it.EnterpriseAuth.SocietyCreditCode == request.EnterpriseAuth.SocietyCreditCode);
            if (entity == null)
            {
                entity = new Enterprise();
                entity.EnterpriseAuth = new EnterpriseAuth();
                request.Adapt(entity);
                await rep.InsertAsync(entity);
            }
            else
            {
                request.Adapt(entity);
                await rep.UpdateAsync(entity);
            }
 
            var checkExist = await repUserInfo.AsQueryable()
                .AnyAsync(it =>
                    it.Type == EnumUserType.Enterprise
                    && it.EnterpriseId != entity.Id
                    && it.UserAuth.UserName == request.UserName);
            if (checkExist) throw Oops.Oh(EnumErrorCodeType.s405, "该账号");
 
            var userInfo = await repUserInfo.AsQueryable()
                .Include(it => it.UserAuth)
                .FirstOrDefaultAsync(it =>
                    it.Type == EnumUserType.Enterprise
                    && it.EnterpriseId == entity.Id);
            if (userInfo == null)
            {
                userInfo = new UserInfo
                {
                    EnterpriseId = entity.Id,
                    Type = EnumUserType.Enterprise,
                    Status = EnumUserInfoStatus.Normal,
                    UserAuth = new UserAuth
                    {
                        Name = request.Contacts,
                        UserName = request.UserName,
                        PhoneNumber = request.ContactPhoneNumber,
                        Password = PBKDF2Encryption.Encrypt(MD5Encryption.Encrypt(request.Password))
                    }
                };
                await repUserInfo.InsertAsync(userInfo);
            }
            else
            {
                userInfo.UserAuth.PhoneNumber = request.ContactPhoneNumber;
                await repUserInfo.UpdateAsync(userInfo);
            }
 
            return entity.Id;
        }
 
        /// <summary>
        /// 设置企业电子签配置
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(SetEnterpriseElectronSignSettingCommand request, CancellationToken cancellationToken)
        {
            var entity = await rep.AsQueryable().FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken);
            if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "该企业");
            request.Adapt(entity);
            await rep.UpdateAsync(entity);
            return entity.Id;
        }
 
        /// <summary>
        /// 设置企业短信配置
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(SetEnterpriseSmsSettingCommand request, CancellationToken cancellationToken)
        {
            var entity = await rep.AsQueryable().FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken);
            if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "该企业");
            request.Adapt(entity);
            await rep.UpdateAsync(entity);
            return entity.Id;
        }
    }
}