123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using JsonRpcLite.Services;
- using WingInterfaceLibrary.DTO.Device;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.DTO.Dictionary;
- using WingServerCommon.Log;
- using WingServerCommon.Service;
- using WingInterfaceLibrary.Interface.DBInterface;
- using WingInterfaceLibrary.DB.Request;
- using WingServerCommon.Mapper;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Authentication;
- using WingInterfaceLibrary.DTO.User;
- using WingInterfaceLibrary.DTO.Management;
- using WingServerCommon.Config;
- using WingDeviceService.Common;
- using WingInterfaceLibrary.Request.User;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.DTO.Organization;
- using WingInterfaceLibrary.DTO.DiagnosisModule;
- using WingServerCommon.Config.Parameters;
- using WingServerCommon.Interfaces.Cache;
- namespace WingDeviceService.Service
- {
- /// <summary>
- /// 设备管理服务
- /// </summary>
- public partial class DeviceService : JsonRpcService, IDeviceService
- {
- private IUserDBService _userServiceProxy;
- private IDeviceInfoDBService _deviceInfoDBServiceProxy;
- private IAuthenticationService _authenticationService;
- private IOrganizationDBService _organizationDBService;
- private INotificationService _notificationService;
- private IRecordsInfoDBService _recordsInfoDBService;
- private IPatientInfoDBService _patientInfoDBService;
- private IDiagnosisModuleDBService _diagnosisModuleService;
- private IRemedicalService _remedicalService;
- private int _heartRateSeconds;
- private DeviceHeartRateManager _deviceHeartRateManager;
- /// <summary>
- /// Init service
- /// </summary>
- public override void Load(JsonRpcClientPool jsonRpcClientPool)
- {
- base.Load(jsonRpcClientPool);
- _userServiceProxy = GetProxy<IUserDBService>();
- _authenticationService = GetProxy<IAuthenticationService>();
- _deviceInfoDBServiceProxy = GetProxy<IDeviceInfoDBService>();
- _organizationDBService = GetProxy<IOrganizationDBService>();
- _notificationService = GetProxy<INotificationService>();
- _recordsInfoDBService = GetProxy<IRecordsInfoDBService>();
- _patientInfoDBService = GetProxy<IPatientInfoDBService>();
- _diagnosisModuleService = GetProxy<IDiagnosisModuleDBService>();
- _remedicalService = GetProxy<IRemedicalService>();
- _heartRateSeconds = ConfigurationManager.GetParammeter<IntParameter>("Device", "HeartRateSeconds").Value;
- _deviceHeartRateManager = new DeviceHeartRateManager(SetOnlineState);
- }
- /// <summary>
- /// 设备心跳
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> HeartRateAsync(TokenRequest request)
- {
- _deviceHeartRateManager.AddOrUpdate(request.Token);
- var result = SetOnlineState(request.Token, true);
- return result;
- }
- private bool SetOnlineState(string token, bool isOnline)
- {
- return _authenticationService.SetOnlineStateAsync(new SetOnlineStateRequest { Token = token, IsOnline = isOnline }).Result;
- }
- /// <summary>
- /// 添加分享设备关联用户请求
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> CreateShareDeviceToUserAsync(CreateShareDeviceToUserRequest request)
- {
- var userCodes = request.UserCodes;
- var deviceCode = request.DeviceCode;
- //检查设备编码不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
- }
- if (userCodes == null || userCodes.Count <= 0)
- {
- ThrowCustomerException(CustomerRpcCode.UserCodeEmptyError, "Required parameter:UserCodes cannot be empty");
- }
- //查询设备对象
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
- if (deviceDb == null)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- foreach (var userCode in userCodes)
- {
- var existUser = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (existUser == null)
- {
- ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
- }
- }
- await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
- {
- DeviceCodes = new List<string> { deviceCode },
- UserCodes = userCodes.ToList()
- });
- return true;
- }
- /// <summary>
- /// 根据设备code获取设备信息
- /// </summary>
- /// <param name="request">设备code请求实体</param>
- /// <returns>返回查询到的设备对象信息</returns>
- public async Task<DeviceExtendInfoDTO> GetDeviceInfoAsync(GetDeviceRequest request)
- {
- //验证
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeEmpty, "DeviceCode empty error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null || deviceDb.DeviceCode != request.DeviceCode)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- //根据机构编号查询机构名称
- var organizationName = "";
- if (!string.IsNullOrEmpty(deviceDb.OrganizationCode))
- {
- var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.OrganizationCode });
- organizationName = organization?.OrganizationName ?? string.Empty;
- }
- //根据机构编号查询科室名称
- var departmentName = "";
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- var department = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.DepartmentCode });
- departmentName = department?.OrganizationName ?? string.Empty;
- }
- //是否在线 todo
- var deviceTypeModel = await _deviceInfoDBServiceProxy.FindDictionaryByCodeAsync(deviceDb.DeviceType);
- var result = new DeviceExtendInfoDTO()
- {
- DepartmentName = departmentName,
- OrganizationName = organizationName,
- IsOnline = await GetDeviceOnlineState(deviceDb.DeviceCode),
- DeviceCode = deviceDb.DeviceCode,
- SerialNumber = deviceDb.SerialNumber,
- Name = deviceDb.Name,
- Description = deviceDb.Description,
- DeviceModel = deviceDb.DeviceModel,
- DeviceType = deviceDb.DeviceType,
- HeadPicUrl = deviceDb.HeadPicUrl,
- DeviceSoftwareVersion = deviceDb.DeviceSoftwareVersion,
- SDKSoftwareVersion = deviceDb.SDKSoftwareVersion,
- OrganizationCode = deviceDb.OrganizationCode,
- DepartmentCode = deviceDb.DepartmentCode,
- ShortCode = deviceDb.ShortCode,
- LanguageConfigs = deviceTypeModel.LanguageConfigs
- };
- return result;
- }
- /// <summary>
- /// 根据设备动态唯一码获取设备信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<DeviceExtendInfoDTO> GetDeviceByShortCodeAsync(GetDeviceByShortCodeRequest request)
- {
- //验证
- if (string.IsNullOrWhiteSpace(request.ShortCode))
- {
- ThrowCustomerException(CustomerRpcCode.ShortCodeEmpty, "ShortCode empty error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(request.ShortCode);
- if (deviceDb == null || deviceDb.ShortCode != request.ShortCode)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- return await MapToExtendDTO(deviceDb);
- }
- public async Task<PageCollection<DeviceInfoDTO>> GetDeviceInfoPageAsync(PageFilterRequest request)
- {
- throw new System.NotImplementedException();
- }
- public async Task<bool> DeleteShareDeviceToUserAsync(DeleteShareDeviceToUserRequest request)
- {
- var userCodes = request.UserCodes;
- var deviceCode = request.DeviceCode;
- //检查设备编码不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
- }
- //用户编码集合检查
- if (userCodes == null || userCodes.Count <= 0)
- {
- ThrowCustomerException(CustomerRpcCode.UserCodeEmptyError, "Required parameter:UserCodes cannot be empty");
- }
- //查询设备对象
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
- if (deviceDb == null)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
- foreach (var userCode in userCodes)
- {
- var user = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (user != null)
- {
- if (user.BindDevices.Contains(deviceCode))
- {
- user.BindDevices.Remove(deviceCode);
- updateUsers.Add(new TableDataItem<UserPasswordDTO>()
- {
- Data = user.MappingTo<UserPasswordDTO>(),
- });
- }
- }
- }
- if (updateUsers.Count > 0)
- {
- var updateUsersDBRequest = new UpdateUsersDBRequest() { Users = updateUsers };
- await _userServiceProxy.UpdateUsersAsync(updateUsersDBRequest);
- }
- return true;
- }
- /// <summary>
- /// 机构设备移除-根据机构code和设备code解除其绑定关系
- /// </summary>
- /// <param name="request">移除设备请求实体</param>
- /// <returns>移除是否成功:true-成功;false-失败</returns>
- public async Task<bool> RemoveDeviceRelevancyAsync(RemoveDeviceRelevancyRequest request)
- {
- //验证
- if (string.IsNullOrWhiteSpace(request.DeviceCode) || string.IsNullOrWhiteSpace(request.OrganizationCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeOrOrganizationCodeEmpty, "DeviceCode Or OrganizationCode empty error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null || deviceDb.OrganizationCode != request.OrganizationCode || deviceDb.DepartmentCode != request.DepartmentCode)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "DeviceInfo not exist error");
- }
- //根据设备code和机构code查询相关数据,查到则删除关联
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- deviceDb.DepartmentCode = "";
- deviceDb.OrganizationCode = "";
- }
- else
- {
- deviceDb.OrganizationCode = "";
- }
- bool removeDeviceResult = await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty, true);
- return removeDeviceResult;
- }
- private async Task<string> GetClientIdByTokenAsync(string token)
- {
- var request = new TokenRequest() { Token = token };
- var result = await _authenticationService.GetTokenAsync(request);
- return result.ClientId; //return User code
- }
- /// <summary>
- /// 查询个人名下所有设备列表接口
- /// </summary>
- /// <param name="request">查询个人名下所有设备列表请求实体</param>
- /// <returns>返回设备信息列表</returns>
- public async Task<PageCollection<DeviceExtendInfoDTO>> GetPersonDeviceListAsync(GetPersonDeviceRequest request)
- {
- //验证
- var userCode = await GetClientIdByTokenAsync(request.Token);
- //查询用户是否存在
- var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (userInfoDO == null)
- {
- ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
- }
- //查询用户的医院是否存在
- if (string.IsNullOrWhiteSpace(userInfoDO.OrganizationCode))
- {
- ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
- }
- var deviceInfoDOList = new List<DeviceInfoDTO>();
- if (!userInfoDO.IsDirector)
- {
- //普通用户,查询用户绑定的设备
- if (userInfoDO.BindDevices != null && userInfoDO.BindDevices.Any())
- {
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
- }
- }
- else
- {
- //机构负责人
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
- }
- if (!deviceInfoDOList.Any())
- {
- return new PageCollection<DeviceExtendInfoDTO>() { CurrentPage = request.PageIndex, PageSize = request.PageSize, DataCount = 0, PageData = new List<DeviceExtendInfoDTO>() };
- }
- var deviceList = await MapToExtendDTO(deviceInfoDOList);
- //条件筛选
- if (!string.IsNullOrWhiteSpace(request.DeviceType))
- {
- deviceList = deviceList.Where(d => d.DeviceType == request.DeviceType)?.ToList() ?? new List<DeviceExtendInfoDTO>();
- }
- if (!string.IsNullOrWhiteSpace(request.DeviceModel))
- {
- deviceList = deviceList.Where(d => d.DeviceModel == request.DeviceModel)?.ToList() ?? new List<DeviceExtendInfoDTO>();
- }
- if (!string.IsNullOrWhiteSpace(request.KeyWord))
- {
- var keyword = request.KeyWord.Trim().ToLower();
- deviceList = deviceList.Where(d => d.Name.ToLower().Contains(keyword) || d.ShortCode.ToLower().Contains(keyword))?.ToList() ?? new List<DeviceExtendInfoDTO>();
- }
- if (request.IsOnline != null)
- {
- deviceList = deviceList.Where(d => d.IsOnline == request.IsOnline)?.ToList() ?? new List<DeviceExtendInfoDTO>();
- }
- var pagedResult = new PageCollection<DeviceExtendInfoDTO>
- {
- CurrentPage = request.PageIndex,
- PageSize = request.PageSize,
- DataCount = deviceList.Count,
- PageData = deviceList.Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize)?.ToList() ?? new List<DeviceExtendInfoDTO>()
- };
- return pagedResult;
- }
- /// <summary>
- /// 绑定设备
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> BindDeviceAsync(BindDeviceRequest request)
- {
- //验证
- var userCode = await GetClientIdByTokenAsync(request.Token);
- //查询用户是否存在
- var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (userInfoDO == null)
- {
- ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(request.ShortCode);
- if (deviceDb == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- if (userInfoDO.BindDevices != null && userInfoDO.BindDevices.Contains(deviceDb.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.HasAddedDevice, "The device has been Added");
- }
- try
- {
- var isInvented = (string.IsNullOrWhiteSpace(deviceDb.OrganizationCode) || deviceDb.OrganizationCode == $"invented_{deviceDb.ShortCode}") ? true : false;
- deviceDb.Name = request.Name;
- deviceDb.SerialNumber = request.SerialNumber;
- deviceDb.Description = request.Description;
- if (!string.IsNullOrEmpty(request.HeadPicUrl))
- {
- deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
- }
- deviceDb.DepartmentCode = request.DepartmentCode;
- deviceDb.IsAutoShared = request.IsAutoShared;
- deviceDb.OrganizationCode = request.OrganizationCode;
- await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty);
- if (isInvented && !string.IsNullOrWhiteSpace(request.OrganizationCode)) //虚拟机构改为正常机构
- {
- var newOrgCode = string.IsNullOrWhiteSpace(request.DepartmentCode) ? request.OrganizationCode : request.DepartmentCode;
- //迁移数据超声机上传检查记录
- await _recordsInfoDBService.UpdateRecordsWithOrgCodeAsync(new List<string>() { deviceDb.DeviceCode }, newOrgCode);
- //迁移数据超声机上传病人
- await _patientInfoDBService.UpdatePatientWithOrgCodeAsync(new List<string>() { deviceDb.DeviceCode }, newOrgCode);
- }
- if (deviceDb.IsAutoShared)
- {
- var users = new List<UserDTO>();
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.DepartmentCode });
- }
- else
- {
- users = await _userServiceProxy.GetUserListAsync(new GetUserListDBRequest { OrganizationQueryType = OrganizationQueryTypeEnum.All, OrganizationCode = deviceDb.OrganizationCode });
- }
- var userCodes = users.Select(f => f.UserCode).ToList();
- await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
- {
- DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
- UserCodes = userCodes.Select(f => f.ToString()).ToList()
- });
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"BindDeviceAsync error {ex}");
- }
- return false;
- }
- /// <summary>
- /// 修改设备
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> ModifyDeviceAsync(ModifyDeviceRequest request)
- {
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- if (string.IsNullOrWhiteSpace(request.Name))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNameEmpty, "Device name cannot be empty");
- }
- deviceDb.Name = request.Name;
- if (!string.IsNullOrWhiteSpace(request.HeadPicUrl))
- {
- deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
- }
- else
- {
- deviceDb.HeadPicUrl = string.Empty;
- }
- deviceDb.DepartmentCode = request.DepartmentCode;
- //修改自动分享
- deviceDb.IsAutoShared = request.IsAutoShared;
- if (deviceDb.IsAutoShared)
- {
- var users = new List<UserDTO>();
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.DepartmentCode });
- }
- else
- {
- users = await _userServiceProxy.GetUserListAsync(new GetUserListDBRequest { OrganizationQueryType = OrganizationQueryTypeEnum.All, OrganizationCode = deviceDb.OrganizationCode });
- }
- var userCodes = users.Select(f => f.UserCode).ToList();
- //先解绑,再绑定,避免绑定重复设备code
- var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
- foreach (var user in users)
- {
- var userPasswordDTO = user.MappingTo<UserPasswordDTO>();
- user.BindDevices?.Remove(deviceDb.DeviceCode);
- await _userServiceProxy.UpdateUserAsync(userPasswordDTO.UserCode, userPasswordDTO, string.Empty);
- }
- await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
- {
- DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
- UserCodes = userCodes.Select(f => f.ToString()).ToList()
- });
- }
- return await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty, true);
- }
- /// <summary>
- /// 创建字典
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<string> CreateDictionaryItemAsync(CreateDictionaryItemRequest request)
- {
- return await _deviceInfoDBServiceProxy.CreateDictionaryItemAsync(new CreateDictionaryItemDBRequest
- {
- Data = new DictionaryDTO { DictionaryType = request.DictionaryType, Value = request.DictionaryItemValue, ParentCode = request.ParentCode },
- ExtensionData = string.Empty
- });
- }
- /// <summary>
- /// 查找设备型号所有字典项
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<DictionaryDTO>> FindDeviceModelItemsAsync(FindDeviceModelItemsRequest request)
- {
- var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
- {
- DictionaryType = (int)request.DictionaryType,
- ParentCode = request.DeviceTypeCode
- });
- return dictionaryDOList;
- }
- /// <summary>
- /// 查找设备类型所有字典项
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<DictionaryDTO>> FindDeviceTypeItemsAsync(FindDeviceTypeItemsRequest request)
- {
- var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
- {
- DictionaryType = (int)request.DictionaryType
- });
- return dictionaryDOList;
- }
- /// <summary>
- /// 根据组织编码查询所属全部设备信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<DeviceExtendInfoDTO>> FindDevicesByOrganizationCodeAsync(FindDevicesByOrganizationCodeRequest request)
- {
- var result = new List<DeviceExtendInfoDTO>();
- var deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(request.OrganizationCode);
- if (deviceInfoDOList?.Count > 0)
- {
- //根据机构编号查询机构名称
- var organizationCode = deviceInfoDOList.FindAll(c => !string.IsNullOrEmpty(c.OrganizationCode)).Select(c => c.OrganizationCode).FirstOrDefault() ?? string.Empty;
- var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = organizationCode });
- var organizationName = organization?.OrganizationName ?? string.Empty;
- //根据机构编号查询科室名称
- var organizationFilter = new Dictionary<string, string>();
- organizationFilter.Add("RootCode", organizationCode);
- var organizations = await _organizationDBService.GetOrganizationPageAsync((new PageFilterRequest { CurrentPage = 1, PageSize = 999999, Filter = organizationFilter, IsFuzzy = false }));
- var deviceCodes = deviceInfoDOList.Select(x => x.DeviceCode).ToList();
- var deviceTokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest { ClientIds = deviceCodes });
- var deviceTypeList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest { DictionaryType = (int)DictionaryTypeEnum.DeviceType });
- foreach (var c in deviceInfoDOList)
- {
- var deviceTypeModel = deviceTypeList.FirstOrDefault(t => t.DictionaryCode == c.DeviceType);
- result.Add(new DeviceExtendInfoDTO
- {
- DepartmentName = organizations.PageData.FirstOrDefault(o => o.OrganizationCode == c.DepartmentCode)?.OrganizationName,
- OrganizationName = organizationName,
- IsOnline = deviceTokens.FirstOrDefault(x => x.ClientId == c.DeviceCode)?.IsOnline ?? false,
- DeviceCode = c.DeviceCode,
- SerialNumber = c.SerialNumber,
- Name = c.Name,
- Description = c.Description,
- DeviceModel = c.DeviceModel,
- DeviceType = c.DeviceType,
- HeadPicUrl = c.HeadPicUrl,
- DeviceSoftwareVersion = c.DeviceSoftwareVersion,
- SDKSoftwareVersion = c.SDKSoftwareVersion,
- OrganizationCode = c.OrganizationCode,
- DepartmentCode = c.DepartmentCode,
- ShortCode = c.ShortCode,
- LanguageConfigs = deviceTypeModel?.LanguageConfigs
- });
- }
- }
- return result;
- }
- public Task<DeviceInfoDTO> CreateDeviceInfoAsync(CreateDeviceRequest request)
- {
- throw new NotImplementedException();
- }
- public async Task<List<SelectItemDTO>> GetPersonDeviceDropdownListAsync(TokenRequest request)
- {
- //验证
- var userCode = await GetClientIdByTokenAsync(request.Token);
- //查询用户是否存在
- var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (userInfoDO == null)
- {
- ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
- }
- var result = new List<SelectItemDTO>();
- //查询用户的医院是否存在
- if (string.IsNullOrEmpty(userInfoDO.OrganizationCode))
- {
- ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
- }
- var deviceInfoDOList = new List<DeviceInfoDTO>();
- if (userInfoDO.IsDirector)
- {
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
- }
- else if (!userInfoDO.RoleCodes.Contains("Role_ExpertAssistant"))
- {
- //查询用户绑定的设备
- if (userInfoDO.BindDevices == null || userInfoDO.BindDevices.Count <= 0)
- {
- return result;
- }
- //根据用户名下的设备id查询所有的设备信息
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
- }
- else
- {
- var experts = await _userServiceProxy.GetBindExpertsAsync(userCode);
- if (experts.Any())
- {
- var bindDeviceCodes = experts.SelectMany(x => x.BindDevices ?? new List<string>()).Distinct().ToList();
- if (bindDeviceCodes.Any())
- {
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(bindDeviceCodes);
- }
- }
- else
- {
- deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
- }
- }
- if (deviceInfoDOList?.Count > 0)
- {
- result = deviceInfoDOList.Select(c => new SelectItemDTO()
- {
- Key = c.DeviceCode,
- Value = c.Name
- }).ToList();
- }
- return result;
- }
- /// <summary>
- /// 设备端调用,查询获取服务器配置信息接口
- /// </summary>
- /// <param name="request">Token验证</param>
- /// <returns>服务器配置信息结果:key:字段,value:数据值,复杂类型可为json</returns>
- public async Task<Dictionary<string, string>> QueryServerConfigAsync(TokenRequest request)
- {
- var deviceCode = await GetClientIdByTokenAsync(request.Token);
- var deviceInfo = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
- if (deviceInfo == null || string.IsNullOrWhiteSpace(deviceInfo.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- var org = new OrganizationDTO();
- if (!string.IsNullOrWhiteSpace(deviceInfo.OrganizationCode))
- {
- org = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceInfo.OrganizationCode });
- }
- var dictionary = new Dictionary<string, string>();
- //定义配置字段 并扩充字段,返回数据可持续更新接口文档,如果数据是复杂类型可以转成json
- //TODO 配置信息: 直播配置、存储配置、
- //存储上传图像或者视频是 是否本地上传缩略图
- var isUploadThumbnail = ConfigurationManager.RemedicalSettings.IsUploadThumbnail;
- dictionary.Add("IsUploadThumbnail", isUploadThumbnail.ToString());
- dictionary.Add("PatientType", org.PatientType.ToString());
- dictionary.Add("HeartRateSeconds", _heartRateSeconds.ToString());
- return dictionary;
- }
- /// <summary>
- /// 添加或迁移设备到医院请求
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDeviceToOrgAsync(AddDeviceToOrgRequest request)
- {
- var shortCode = request.UniqueCode;
- #region Params Check
- var userCode = await GetClientIdByTokenAsync(request.Token);
- if (string.IsNullOrEmpty(shortCode))
- {
- ThrowCustomerException(CustomerRpcCode.UniqueCodeIsEmpty, "UniqueCode is empty");
- }
- var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(shortCode);
- if (device == null)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- var oldOrg = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = device.OrganizationCode });
- if (oldOrg == null || string.IsNullOrWhiteSpace(oldOrg.OrganizationCode))
- {
- ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
- }
- //用户
- var userDTO = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (userDTO == null)
- {
- ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
- }
- if (!userDTO.IsDirector)
- {
- ThrowCustomerException(CustomerRpcCode.NotOrgAdmin, "not org admin");
- }
- var org = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = userDTO.OrganizationCode });
- if (org == null || string.IsNullOrWhiteSpace(org.OrganizationCode))
- {
- ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
- }
- #endregion
- try
- {
- var newOrgode = org.OrganizationCode;
- //设备更新医院
- if (!string.IsNullOrEmpty(device.DepartmentCode))
- {
- device.DepartmentCode = "";
- }
- if (!string.IsNullOrEmpty(device.Name))
- {
- device.Name = "";
- }
- device.HeadPicUrl = "";
- device.Description = "";
- device.IsAutoShared = false;
- device.OrganizationCode = newOrgode;
- bool success = await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(device.DeviceCode, device, string.Empty, true);
- if (success)
- {
- // 删除已知绑定的用户
- var users = await _userServiceProxy.GetUsersByDeviceAsync(device.DeviceCode);
- var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
- foreach (var user in users)
- {
- user.BindDevices.Remove(device.DeviceCode);
- updateUsers.Add(new TableDataItem<UserPasswordDTO>()
- {
- Data = user.MappingTo<UserPasswordDTO>(),
- });
- }
- if (updateUsers.Count > 0)
- {
- var updateUsersDBRequest = new UpdateUsersDBRequest() { Users = updateUsers };
- await _userServiceProxy.UpdateUsersAsync(updateUsersDBRequest);
- }
- if (oldOrg.Isinvented) // 虚拟医院
- {
- //迁移数据超声机上传检查记录
- await _recordsInfoDBService.UpdateRecordsWithOrgCodeAsync(new List<string>() { device.DeviceCode }, newOrgode);
- //迁移数据超声机上传病人
- await _patientInfoDBService.UpdatePatientWithOrgCodeAsync(new List<string>() { device.DeviceCode }, newOrgode);
- }
- }
- return success;
- }
- catch (System.Exception ex)
- {
- Logger.WriteLineError($"AddDeviceToOrgAsync error {ex}");
- }
- return false;
- }
- /// <summary>
- /// 补齐设备扩展信息
- /// </summary>
- /// <param name="deviceInfoDOList"></param>
- /// <returns></returns>
- private async Task<List<DeviceExtendInfoDTO>> MapToExtendDTO(List<DeviceInfoDTO> deviceInfoDOList)
- {
- var deviceList = new List<DeviceExtendInfoDTO>();
- if (deviceInfoDOList != null && deviceInfoDOList.Any())
- {
- //查询机构
- var orgCodes = deviceInfoDOList.Select(x => x.OrganizationCode).ToList();
- orgCodes.AddRange(deviceInfoDOList.Select(x => x.DepartmentCode));
- var organizations = await _organizationDBService.FindOrganizationListByCodesAsync(orgCodes.Distinct().ToList());
- //查询机构负责人
- var organizationDirectorCodes = new List<string>();
- foreach (var org in organizations)
- {
- if (org.Directors != null && org.Directors.Any())
- {
- organizationDirectorCodes.AddRange(org.Directors);
- }
- }
- var organizationDirectors = await _userServiceProxy.FindUserListByUserCodesAsync(organizationDirectorCodes);
- var deviceCodes = deviceInfoDOList.Select(x => x.DeviceCode).ToList();
- var deviceTokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest { ClientIds = deviceCodes });
- var deviceTypeList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest { DictionaryType = (int)DictionaryTypeEnum.DeviceType });
- foreach (var dto in deviceInfoDOList)
- {
- var device = dto.MappingTo<DeviceExtendInfoDTO>();
- device.IsOnline = deviceTokens.FirstOrDefault(x => x.ClientId == dto.DeviceCode)?.IsOnline ?? false;
- device.DepartmentName = organizations.FirstOrDefault(x => x.OrganizationCode == dto.DepartmentCode)?.OrganizationName;
- device.LanguageConfigs = deviceTypeList.FirstOrDefault(t => t.DictionaryCode == device.DeviceType)?.LanguageConfigs;
- var organization = organizations.FirstOrDefault(x => x.OrganizationCode == dto.OrganizationCode);
- if (organization != null)
- {
- device.OrganizationName = organization.OrganizationName;
- if (organization.Directors != null && organization.Directors.Any())
- {
- var director = organizationDirectors.FirstOrDefault(x => x.UserCode == organization.Directors.FirstOrDefault());
- device.OrganizationDirectorCode = director?.UserCode;
- device.OrganizationDirectorUserName = director?.UserName;
- device.OrganizationDirectorFullName = director?.FullName;
- }
- }
- deviceList.Add(device);
- }
- }
- return deviceList;
- }
- /// <summary>
- /// 补齐设备扩展信息
- /// </summary>
- /// <param name="deviceInfoDOList"></param>
- /// <returns></returns>
- private async Task<DeviceExtendInfoDTO> MapToExtendDTO(DeviceInfoDTO deviceInfoDO)
- {
- var deviceList = await MapToExtendDTO(new List<DeviceInfoDTO> { deviceInfoDO });
- return deviceList.FirstOrDefault();
- }
- /// <summary>
- /// 获取设备绑定用户codes
- /// </summary>
- /// <param name="request">获取设备绑定用户codes请求实体</param>
- /// <returns>test01,test02</returns>
- public async Task<List<string>> GetDeviceBindUsersCodesAsync(GetDeviceRequest request)
- {
- //检查设备编码不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
- }
- //查询设备对象
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null)
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- var userListResult = await _userServiceProxy.GetUsersByDeviceAsync(request.DeviceCode);
- if (userListResult != null && userListResult.Count > 0)
- {
- return userListResult.Select(u => u.UserCode).ToList();
- }
- return new List<string>();
- }
- /// <summary>
- /// 查询设备AI应用的状态
- /// </summary>
- /// <param name="request">请求参数</param>
- /// <returns>AI应用集合</returns>
- public async Task<List<DiagnosisModuleDTO>> FindDeviceDiagnosisModulesAsync(FindDeviceDiagnosisModulesRequest request)
- {
- //检查设备编码不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
- }
- return await _diagnosisModuleService.GetDeviceDiagnosisModulesAsync(new GetDeviceDiagnosisModulesDBRequest { DeviceCode = request.DeviceCode });
- }
- /// <summary>
- /// 修改设备AI应用启用状态
- /// </summary>
- /// <param name="request">修改设备AI应用启用状态请求实体</param>
- /// <returns>是否成功</returns>
- /// <para>DeviceErrorCodeEnum</para>
- public async Task<bool> ModifyDeviceDiagnosisModuleStateAsync(ModifyDeviceDiagnosisModuleStateRequest request)
- {
- //检查设备编码不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
- }
- if (string.IsNullOrWhiteSpace(request.DiagnosisModule))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceAIDiagnosisModule, "Required parameter:DeviceCode cannot be empty");
- }
- var result = await _diagnosisModuleService.UpdateDeviceDiagnosisModuleStateAsync(new UpdateDeviceDiagnosisModuleStateDBRequest
- {
- DeviceCode = request.DeviceCode,
- DiagnosisModule = request.DiagnosisModule,
- Enabled = request.Enabled
- });
- CacheMaintenance.Instance.Get<IDeviceInfosManager>().Remove(request.DeviceCode);
- return result;
- }
- /// <summary>
- /// 通用异常处理方法
- /// </summary>
- /// <param name="customerRpcCodeEnum">异常标识码</param>
- /// <param name="msg">异常信息</param>
- /// <param name="internalMessage">内部异常信息,非必填</param>
- private void ThrowCustomerException(CustomerRpcCode customerRpcCodeEnum, string msg, string internalMessage = null)
- {
- ThrowRpcException((int)customerRpcCodeEnum, msg, internalMessage);
- }
- }
- }
|