zhengyiming
2025-03-19 aef74aff62bd9fc2d615f8b15a100432f2bca44f
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
using LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.Models;
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 Z.EntityFramework.Plus;
using ZeroD.Util;
 
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;
 
        public UserRoleService(
               IRepository<User, Guid> userRepository,
               IRepository<Role, Guid> roleRepository,
               IRepository<LifePayChannles, Guid> channleRepository,
               IRepository<UserRole, Guid> userRoleRep,
               IRepository<UserChannle, Guid> userChannleRep)
        {
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _channleRepository = channleRepository;
            _userRoleRep = userRoleRep;
            _userChannleRep = userChannleRep;
        }
 
        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.Id == uc.ChannleId)
                        select new UserChannleDto
                        {
                            Id = c.Id,
                            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)
            {
                userChannles.Add(new UserChannle()
                {
                    Id = Guid.NewGuid(),
                    ChannleId = item,
                    UserId = entity.Id
                });
            }
 
            await _userChannleRep.InsertManyAsync(userChannles);
 
            return Constant.SUCCESS;
        }
 
        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)
        {
            var entity = ObjectMapper.Map<CreateOrUpdateRoleInput, Role>(input);
            return await _roleRepository.InsertAndGetIdAsync(entity);
        }
 
        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;
        }
    }
}