diff --git a/IRaCIS.Core.Application/Service/Management/DTO/UserModel.cs b/IRaCIS.Core.Application/Service/Management/DTO/UserModel.cs index dc272bd2f..c8255a067 100644 --- a/IRaCIS.Core.Application/Service/Management/DTO/UserModel.cs +++ b/IRaCIS.Core.Application/Service/Management/DTO/UserModel.cs @@ -25,7 +25,11 @@ namespace IRaCIS.Application.Contracts public class TJUserInfoDto { public string Code { get; set; } + public string Msg { get; set; } + + public bool Success { get; set; } + public TJUserInfoData Data { get; set; } } @@ -38,6 +42,8 @@ namespace IRaCIS.Application.Contracts public string Birthday { get; set; } public string Title { get; set; } public string UserType { get; set; } + + public List Roles { get; set; } } diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 6f63871f6..a5e3fdd24 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -1,5 +1,7 @@ -using IP2Region.Net.Abstractions; +using DocumentFormat.OpenXml.Spreadsheet; +using IP2Region.Net.Abstractions; using IRaCIS.Application.Contracts; +using IRaCIS.Core.Application.Auth; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.Helper.OtherTool; @@ -10,6 +12,7 @@ using Medallion.Threading; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using NPOI.SS.Formula.Functions; using Panda.DynamicWebApi.Attributes; using System.Text.RegularExpressions; using ZiggyCreatures.Caching.Fusion; @@ -29,6 +32,7 @@ namespace IRaCIS.Core.Application.Service IOptionsMonitor _verifyConfig, IOptionsMonitor systemEmailConfig, IRepository _hirHospitalRepository, + ITokenService _tokenService, ISearcher _searcher, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer, IFusionCache _fusionCache) : BaseService, IUserService { @@ -635,28 +639,131 @@ namespace IRaCIS.Core.Application.Service [AllowAnonymous] [HttpGet] - public async Task TJUserLoginInfo(string token) + public async Task TJUserLoginInfo(string token, + [FromServices] IRepository _userTypeRepository, + [FromServices] IRepository _hirHospitalRepository) { - //同济医院token 地址:http://192.168.40.88:8080 appid:third-hirs + #region MyRegion + ////同济医院token 地址:http://192.168.40.88:8080 appid:third-hirs - //本地测试地址接口 + ////本地测试地址接口 - var apiUrl = "http://192.168.40.88:8080/dock/userinfo"; - var headers = new Dictionary + //var apiUrl = "http://192.168.40.88:8080/dock/userinfo"; + //var headers = new Dictionary + //{ + // { "Content-Type", "application/json" } // 根据需要添加其他头部信息 + //}; + + //var requestData = new + //{ + // token = token, + // appId = "third-hirs", + //}; + + //var tjUserInfo = await RestClientAPI.PostAsync(apiUrl, requestData, headers); + #endregion + + + var tjUserInfo = new TJUserInfoDto { - { "Content-Type", "application/json" } // 根据需要添加其他头部信息 + Code = "200", + Msg = "操作成功", + Success = true, + Data = new TJUserInfoData + { + UserCode = "YS101389", + UserName = "胡学梅", + DeptCode = "2121", + Sex = "未知", + Birthday = "", + Title = "", + UserType = "doctor", + Roles = new List { "PM", "CRC", "CRC", "PI", "QA", "Other" } + } }; - var requestData = new + var generateRoles = new List() { "PM", "CRC", "SR", "PI", "QA" }; + + if (tjUserInfo != null && tjUserInfo.Data?.Roles != null && + tjUserInfo.Data.Roles.Any(role => generateRoles.Any(gRole => string.Equals(gRole, role, StringComparison.OrdinalIgnoreCase)))) { - userToken = token, - appId = "third-hirs", - }; - var tjUserInfo = await RestClientAPI.PostAsync(apiUrl, requestData, headers); + var sysUserTypeList = _userTypeRepository.Where().Select(t => new { t.UserTypeShortName, t.UserTypeEnum, t.Id }).ToList(); + + var matchedRoles = generateRoles.Where(role => tjUserInfo.Data.Roles.Any(r => string.Equals(r, role, StringComparison.OrdinalIgnoreCase))).ToList(); + + + var hospital = await _hirHospitalRepository.Where(t => t.IsDefault == true).ProjectTo(_mapper.ConfigurationProvider).FirstNotNullAsync(); + + var thirdUId = tjUserInfo.Data.UserCode; + + var email = $"{tjUserInfo.Data.UserCode}@tjhir.com"; + + var @lock = _distributedLockProvider.CreateLock($"UserAccount"); + + using (await @lock.AcquireAsync()) + { + //匹配的角色 都匹配一遍账号,没有便生成 + foreach (var roleName in matchedRoles) + { + var userType = sysUserTypeList.Where(t => t.UserTypeShortName == roleName).First(); + + var userTypeId = userType.Id; + + if (!_userRepository.Any(t => t.ThirdUserCode == thirdUId && t.UserTypeId == userTypeId)) + { + var newCode = await _userRepository.Select(t => t.Code).DefaultIfEmpty().MaxAsync() + 1; + + var newUser = new User() + { + FirstName = $"{tjUserInfo.Data.UserName}", + LastName = "tj", + + IsFirstAdd = false, + IsZhiZhun = true, + OrganizationName = hospital.HospitalName, + Code = newCode, + Password = MD5Helper.Md5("123456"), + CheckCode = "123456", + UserCode = AppSettings.GetCodeStr(newCode, nameof(User)), + + UserTypeId = userTypeId, + UserTypeEnum = userType.UserTypeEnum, + + EMail = email, + UserName = $"{tjUserInfo.Data.UserName}-{roleName}", + ThirdUserCode = thirdUId, + }; + + await _userRepository.AddAsync(newUser); + } + + + } + await _userRepository.SaveChangesAsync(); + } + + //返回该账号的在系统的中的所有角色,如果单个角色,就直接登录,如果是多个角色,那么前端进行选择后进行登录 + + var accountList = _userRepository.Where(t => t.EMail == email && t.UserTypeEnum != UserTypeEnum.SuperAdmin) + .ProjectTo(_mapper.ConfigurationProvider).OrderBy(t => t.UserTypeShortName).ToList(); + + var hirToken = _tokenService.GetToken(IRaCISClaims.Create(new UserBasicInfo() { UserName = tjUserInfo.Data.UserName })); + + return ResponseOutput.Ok(accountList, hirToken); + + } + else + { + return ResponseOutput.NotOk("跳转系统对接的用户未配置当前系统的角色,无法自动生成账号,请联系管理员配置该跳转账户信息"); + } + + + + + - return ResponseOutput.Ok(tjUserInfo); }