diff --git a/services/Syc.Basic.Web.WMS.Application/Service/UserService.cs b/services/Syc.Basic.Web.WMS.Application/Service/UserService.cs new file mode 100644 index 0000000..eb3d474 --- /dev/null +++ b/services/Syc.Basic.Web.WMS.Application/Service/UserService.cs @@ -0,0 +1,199 @@ +using DotNetCore.CAP; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Syc.Authorize.JwtBearer; + +//using Syc.Basic.Web.Contracts; +using Syc.Basic.Web.WMS.Dtos; +using Syc.Basic.Web.WMS.Dtos.Auth; +using Syc.Basic.Web.WMS.Dtos.Role; +using Syc.Basic.Web.WMS.Dtos.User; +using Syc.Basic.Web.WMS.Entitys; +using Syc.Basic.Web.WMS.IServices; +using Syc.Basic.Web.WMS.Repository; +using Syc.Basic.Web.Services; +using Syc.Core.Tools; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.Application.Services; +using Volo.Abp.Domain.Repositories; + +namespace Syc.Basic.Web.WMS.Service +{ + [ApiExplorerSettings(GroupName = "用户服务"), Authorize] + public class UserAppService : ApiService, IUserService + { + private readonly IUserDomainService userDomainService; + private readonly IRepository repository; + private readonly IUserRepository userRepository; + private readonly IRepository roleRepository; + private readonly IRepository orgRepository; + private readonly IDictionaryService dictionaryService; + private readonly IRepository positionRepository; + private readonly IRepository userPositionRepository; + + public UserAppService(IUserDomainService userDomainService + , IRepository repository + , IUserRepository userRepository + ,IRepository roleRepository + ,IRepository orgRepository + ,IDictionaryService dictionaryService, + IRepository positionRepository + ,IRepository userPositionRepository) + { + this.userDomainService = userDomainService; + this.repository = repository; + this.userRepository = userRepository; + this.roleRepository = roleRepository; + this.orgRepository = orgRepository; + this.dictionaryService = dictionaryService; + this.positionRepository = positionRepository; + this.userPositionRepository = userPositionRepository; + } + + public async Task Add(UserInsertInput input) + { + var user = ObjectMapper.Map(input,new User()); + var accountExist = await repository.AnyAsync(e => e.Account == user.Account); + if (accountExist) + throw Oops.Oh("账号已存在,请重复使用"); + + var roles = await roleRepository.GetListAsync(e => input.roleIds.Contains(e.Code)); + var organizations = await orgRepository.GetAsync(e => input.departmentId == e.Id); + var positions = await positionRepository.GetListAsync(e => input.positionIds.Contains(e.Code)); + var password = await dictionaryService.GetAsync(ConstPool.Dict_Code_UserDefaultPassword,ConstPool.DictItem_Key_UserDefaultPassword); + user.Createtime = DateTime.Now; + user.Creator_Id = Identity.Id; + user.Password = Tools.SecurityUtil.MD5Encrypt(password, Encoding.ASCII); + user.Roles = roles; + user.Organizations = new List() { organizations }; + user.Positions = positions; + await repository.InsertAsync(user); + } + + public async Task DelUser(UserRemoveDto userInsertDto) + { + var users = await repository.GetListAsync(e=> userInsertDto.Ids.Contains(e.Id)); + if (users.Any(e => e.Account == ConstPool.AdminAccount)) + throw Oops.Oh($"{ConstPool.AdminAccount} 账号禁止删除"); + if (users.Count > 0) + await repository.DeleteManyAsync(users); + } + + public async Task Edit(UserInsertInput input) + { + var accountExist = await repository.AnyAsync(e => e.Account == input.username && e.Id != input.id); + if (accountExist) + throw Oops.Oh("账号已存在,请重复使用"); + var userDO = await userRepository.GetUserByAccountAsync(input.username); + if(userDO.IsNullOrEmpty()) + throw Oops.Oh("用户不存在"); + + var roles = await roleRepository.GetListAsync(e => input.roleIds.Contains(e.Code)); + var organizations = await orgRepository.GetAsync(e => input.departmentId == e.Id); + var positions = await positionRepository.GetListAsync(e => input.positionIds.Contains(e.Code)); + + var user = userDO.User(); + user.LastUpdateTime = DateTime.UtcNow; + user.RealName = input.realname; + user.NickName = input.nickname; + user.Telephone = input.mobile; + user.Email = input.email; + user.IsEnable = input.status; + user.Roles.Clear(); + user.Roles.AddRange(roles); + user.Organizations.Clear(); + user.Organizations.AddRange(organizations); + user.Positions.Clear(); + user.Positions.AddRange(positions); + await repository.UpdateAsync(user); + } + + /// + /// 个人信息 + /// + /// + public async Task Profile() + { + + } + + /// + /// 更改用户状态 + /// + /// + [HttpPost] + public async Task UpdateStatus(UpdateUserStatInput input) + { + var user = await repository.GetAsync(e => e.Id == input.id); + if (user.IsNullOrEmpty()) + throw Oops.Oh("用户不存在或已被删除"); + user.IsEnable = input.status; + await repository.UpdateAsync(user); + } + + /// + /// 获取用户信息 + /// + /// + [HttpGet] + public async Task CurrentUserInfo() + { + var userDO = await userDomainService.GetUserByAccountAsync(Identity.Account); + var result = ObjectMapper.Map(Identity, new GetUserInfoDto()); + result.departmentName = userDO.Department; + result.roleName = userDO.Roles().Select(e => e.Name).ToList(); + return result; + } + + /// + /// 用户搜索 + /// + /// + [HttpGet] + public async Task> SearchUserAsync(SearchUserInput input) + { + var result = await userRepository.SearchUserAsync(input.departmentId, input.email, input.mobile, input.nickname, input.username, input.roleIds, input.Page, input.PageSize); + var items = ObjectMapper.Map(result.items, new List()); + return new PageOutput() + { + PageIndex = input.Page, + Data = items, + PageSize = input.PageSize, + Total = result.total, + }; + } + + /// + /// 根据 id 获取用户信息 + /// + /// + /// + /// + public async Task> GetByIdAsync(List ids) + { + var users = await userRepository.GetUsers(ids.ToArray(),false); + return ObjectMapper.Map(users, new List()); + } + /// + /// 获取用户职位 + /// + /// + /// + [HttpGet] + public async Task> GetUserPosition() + { + var users = await userPositionRepository.GetQueryableAsync(); + users = users.Where(e => e.UsersId == Identity.Id); + List positions = new List(); + foreach (var user in users) { + positions.Add(user.PositionsCode); + } + return positions; + } + } +}