lingling
2025-04-16 10496309bfe3a0d65ed012c5598732f5bfac2efd
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Z.EntityFramework.Plus;
using ZeroD.Util;
using static LifePayment.Domain.Shared.LifePaymentConstant;
 
namespace HumanResourcesServices.Application
{
    public class UserRoleService : ApplicationService, IUserRoleService
    {
        private readonly IRepository<User, Guid> _userRepository;
        private readonly IRepository<Role, Guid> _roleRepository;
        private readonly IRepository<LifePayChannles, Guid> _channleRepository;
        private readonly IRepository<UserRole, Guid> _userRoleRep;
        private readonly IRepository<UserChannle, Guid> _userChannleRep;
        private readonly IIdentityUserAppService _identityUserService;
 
        public UserRoleService(
               IRepository<User, Guid> userRepository,
               IRepository<Role, Guid> roleRepository,
               IRepository<LifePayChannles, Guid> channleRepository,
               IRepository<UserRole, Guid> userRoleRep,
               IRepository<UserChannle, Guid> userChannleRep,
               IIdentityUserAppService identityUserService)
        {
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _channleRepository = channleRepository;
            _userRoleRep = userRoleRep;
            _userChannleRep = userChannleRep;
            _identityUserService = identityUserService;
        }
 
        public async Task<PageOutput<UserDto>> GetBackClientUsers(GetBackClientUsersInput input)
        {
            var query = _userRepository.Where(s => s.ClientId == Constant.ClientType.Back).Include(i => i.UserRoles).Include(i => i.UserChannle).Select(u => new UserDto
            {
                Id = u.Id,
                UserName = u.UserName,
                Name = u.Name,
                PhoneNumber = u.PhoneNumber,
                IsLocked = u.IsLocked ?? default,
                Roles = from ur in u.UserRoles
                        from r in _roleRepository.Where(s => s.Id == ur.RoleId)
                        select new RoleDto
                        {
                            Id = r.Id,
                            Name = r.Name,
                        },
                Channles = from uc in u.UserChannle
                           from c in _channleRepository.Where(s => s.ChannlesNum == uc.ChannleId)
                        select new UserChannleDto
                        {
                            Id = c.Id,
                            ChannlesNum = c.ChannlesNum,
                            Name = c.ChannlesName,
                        },
                Remark = u.Remark,
                CompanyOrgId = u.CompanyOrgId,
                DepartmentOrgId = u.DepartmentOrgId
            });
            if (input.IsLocked.HasValue)
            {
                query = query.Where(s => s.IsLocked == input.IsLocked);
            }
 
            if (!input.QueryCondition.IsNullOrEmpty())
            {
                query = query.Where(s => s.UserName.Contains(input.QueryCondition) ||
                                         s.Name.Contains(input.QueryCondition));
            }
 
            var result = await query.GetPageResult(input.PageModel);
            return result;
        }
 
        public async Task<int> UpdateBackClientUser(UpdateBackClientUserInput input)
        {
            var entity = await _userRepository.FirstOrDefaultAsync(s => s.Id == input.Id && s.ClientId == Constant.ClientType.Back);
            if (entity == null)
            {
                throw new UserFriendlyException("未找到对应用户");
            }
 
            entity.Name = input.Name;
            entity.PhoneNumber = input.PhoneNumber;
            entity.UserName = input.UserName;
            entity.Remark = input.Remark;
            entity.DepartmentOrgId = input.DepartmentOrgId;
            entity.CompanyOrgId = input.CompanyOrgId;
 
            var userchannle = await _userChannleRep.Where(s => s.UserId == input.Id).DeleteAsync();
            List<UserChannle> userChannles = new List<UserChannle>();
            foreach (var item in input.ChannlesId)
            {
                var channleNum = item;
                if (IsGuid(channleNum))
                {
                    channleNum = await _channleRepository.Where(x => x.Id == Guid.Parse(item)).Select(s => s.ChannlesNum).FirstOrDefaultAsync();
                }
 
                userChannles.Add(new UserChannle()
                {
                    Id = Guid.NewGuid(),
                    ChannleId = channleNum,
                    UserId = entity.Id
                });
            }
 
            await _userChannleRep.InsertManyAsync(userChannles);
 
            return Constant.SUCCESS;
        }
 
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task ResetPassword(ResetPasswordBaseInput input)
        {
            var user = await _userRepository.FirstOrDefaultAsync(x => x.Id == input.UserId);
            CheckExtensions.IfTrueThrowUserFriendlyException(user == null, CustomeErrorMessage.IsNoExistUser);
            CheckExtensions.IfTrueThrowUserFriendlyException(string.IsNullOrEmpty(input.Password), "请输入密码");
            var identiResetInput = new ResetPassWordInput
            {
                UserId = input.UserId,
                Name = user.Name,
                UserName = user.UserName,
                Password = input.Password,
                PhoneNumber = user.PhoneNumber
            };
            var result = await _identityUserService.ResetPassword(identiResetInput);
            CheckExtensions.IfTrueThrowUserFriendlyException(result != Constant.SUCCESS,
                                                             CustomeErrorMessage.ResetPasswordFail);
        }
 
        public async Task<int> DeleteBackClientUser(Guid id)
        {
            var entity = await _userRepository.FirstOrDefaultAsync(s => s.Id == id && s.ClientId == Constant.ClientType.Back);
            if (entity == null)
            {
                throw new UserFriendlyException("未找到对应用户");
            }
 
            entity.IsDeleted = true;
            return Constant.SUCCESS;
        }
 
        public async Task<Guid> CreateRole(CreateOrUpdateRoleInput input)
        {
            Role role = new Role()
            {
                Id = input.Id,
                Name = input.Name,
                Sequence = input.Sequence,
                DepartmentId = input.DepartmentId,
                DataRange = input.DataRange,
                Remark = input.Remark
            };
            return await _roleRepository.InsertAndGetIdAsync(role);
        }
 
        public async Task<PageOutput<RoleInfo>> GetRoles(GetRolesInput input)
        {
            var query = _roleRepository.Select(r => new RoleInfo
            {
                Id = r.Id,
                Name = r.Name,
                Sequence = r.Sequence,
                IsEnable = r.IsEnable,
                DepartmentId = r.DepartmentId,
                DataRange = r.DataRange,
                Remark = r.Remark,
            });
            if (!input.QueryCondition.IsNullOrEmpty())
            {
                query = query.Where(s => s.Name.Contains(input.QueryCondition));
            }
 
            var result = await query.GetPageResult(input.PageModel);
            var listrols = new List<UserRole>();
            if (result.Data.Any())
            {
                listrols = await (from ur in _userRoleRep.Where(r => result.Data.Select(d => d.Id).Contains(r.RoleId))
                                  join u in _userRepository.Where(r => !r.IsDeleted) on ur.UserId equals u.Id
                                  select ur).ToListAsync();
            }
            foreach (var item in result.Data)
            {
                item.UserCount = listrols.Where(r => r.RoleId == item.Id).Count();
            }
            return result;
        }
 
        public async Task<int> UpdateRole(CreateOrUpdateRoleInput input)
        {
            var entity = await _roleRepository.FirstOrDefaultAsync(s => s.Id == input.Id);
            if (entity == null)
            {
                throw new UserFriendlyException("未找到对应角色");
            }
 
            entity.Name = input.Name;
            entity.Sequence = input.Sequence;
            entity.DepartmentId = input.DepartmentId;
            entity.DataRange = input.DataRange;
            entity.Remark = input.Remark;
            return Constant.SUCCESS;
        }
 
        public async Task<int> RoleEnableOrForbid(Guid id, bool isEnable)
        {
            var entity = await _roleRepository.FirstOrDefaultAsync(s => s.Id == id);
            if (entity == null)
            {
                throw new UserFriendlyException("未找到对应角色");
            }
 
            entity.IsEnable = isEnable;
            return Constant.SUCCESS;
        }
 
        public async Task<int> DeleteRole(Guid id)
        {
            await _roleRepository.DeleteAsync(id);
            return Constant.SUCCESS;
        }
 
        private bool IsGuid(string input)
        {
            Guid guidOutput;
            return Guid.TryParse(input, out guidOutput);
        }
    }
}