DeviceService.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using WingInterfaceLibrary.DTO.Device;
  6. using WingInterfaceLibrary.Interface;
  7. using WingInterfaceLibrary.Request.Device;
  8. using WingInterfaceLibrary.DTO.Dictionary;
  9. using WingServerCommon.Log;
  10. using WingServerCommon.Service;
  11. using WingInterfaceLibrary.Interface.DBInterface;
  12. using WingInterfaceLibrary.DB.Request;
  13. using WingServerCommon.Mapper;
  14. using WingInterfaceLibrary.Request;
  15. using WingInterfaceLibrary.Request.Authentication;
  16. using WingInterfaceLibrary.DTO.User;
  17. using WingInterfaceLibrary.DTO.Management;
  18. using WingServerCommon.Config;
  19. using WingDeviceService.Common;
  20. using WingInterfaceLibrary.Request.User;
  21. using WingInterfaceLibrary.Enum;
  22. using WingInterfaceLibrary.DTO.Organization;
  23. using WingInterfaceLibrary.DTO.DiagnosisModule;
  24. using WingServerCommon.Config.Parameters;
  25. using WingServerCommon.Interfaces.Cache;
  26. using WingInterfaceLibrary.LiveConsultation;
  27. using WingInterfaceLibrary.Result.Languge;
  28. using WingInterfaceLibrary.Internal.Interface;
  29. namespace WingDeviceService.Service
  30. {
  31. /// <summary>
  32. /// 设备管理服务
  33. /// </summary>
  34. public partial class DeviceService : JsonRpcService, IDeviceService
  35. {
  36. private IUserDBService _userServiceProxy;
  37. private IDeviceInfoDBService _deviceInfoDBServiceProxy;
  38. private IAuthenticationService _authenticationService;
  39. private IOrganizationDBService _organizationDBService;
  40. private INotificationService _notificationService;
  41. private IRecordsInfoDBService _recordsInfoDBService;
  42. private IPatientInfoDBService _patientInfoDBService;
  43. private IDiagnosisModuleDBService _diagnosisModuleService;
  44. private IRemedicalService _remedicalService;
  45. private int _heartRateSeconds;
  46. private DeviceHeartRateManager _deviceHeartRateManager;
  47. private string _webSocketUrl = string.Empty;
  48. private int _liveConsultationRateSeconds = 0;
  49. /// <summary>
  50. /// Init service
  51. /// </summary>
  52. public override void Load(JsonRpcClientPool jsonRpcClientPool)
  53. {
  54. base.Load(jsonRpcClientPool);
  55. _userServiceProxy = GetProxy<IUserDBService>();
  56. _authenticationService = GetProxy<IAuthenticationService>();
  57. _deviceInfoDBServiceProxy = GetProxy<IDeviceInfoDBService>();
  58. _organizationDBService = GetProxy<IOrganizationDBService>();
  59. _notificationService = GetProxy<INotificationService>();
  60. _recordsInfoDBService = GetProxy<IRecordsInfoDBService>();
  61. _patientInfoDBService = GetProxy<IPatientInfoDBService>();
  62. _diagnosisModuleService = GetProxy<IDiagnosisModuleDBService>();
  63. _remedicalService = GetProxy<IRemedicalService>();
  64. _heartRateSeconds = ConfigurationManager.GetParammeter<IntParameter>("Device", "HeartRateSeconds").Value;
  65. _deviceHeartRateManager = new DeviceHeartRateManager(SetOnlineState);
  66. _webSocketUrl = ConfigurationManager.GetParammeter<StringParameter>("Notification", "WebSocketUrl").Value;
  67. _liveConsultationRateSeconds = ConfigurationManager.GetParammeter<IntParameter>("Live", "HeartRateSeconds").Value;
  68. }
  69. /// <summary>
  70. /// 设备心跳
  71. /// </summary>
  72. /// <param name="request"></param>
  73. /// <returns></returns>
  74. public async Task<bool> HeartRateAsync(TokenRequest request)
  75. {
  76. _deviceHeartRateManager.AddOrUpdate(request.Token);
  77. var result = SetOnlineState(request.Token, true);
  78. return result;
  79. }
  80. private bool SetOnlineState(string token, bool isOnline)
  81. {
  82. return _authenticationService.SetOnlineStateAsync(new SetOnlineStateRequest { Token = token, IsOnline = isOnline }).Result;
  83. }
  84. /// <summary>
  85. /// 添加分享设备关联用户请求
  86. /// </summary>
  87. /// <param name="request"></param>
  88. /// <returns></returns>
  89. public async Task<bool> CreateShareDeviceToUserAsync(CreateShareDeviceToUserRequest request)
  90. {
  91. var userCodes = request.UserCodes;
  92. var deviceCode = request.DeviceCode;
  93. //检查设备编码不为空
  94. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  95. {
  96. ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
  97. }
  98. if (userCodes == null || userCodes.Count <= 0)
  99. {
  100. ThrowCustomerException(CustomerRpcCode.UserCodeEmptyError, "Required parameter:UserCodes cannot be empty");
  101. }
  102. //查询设备对象
  103. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
  104. if (deviceDb == null)
  105. {
  106. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  107. }
  108. foreach (var userCode in userCodes)
  109. {
  110. var existUser = await _userServiceProxy.FindUserByCodeAsync(userCode);
  111. if (existUser == null)
  112. {
  113. ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
  114. }
  115. }
  116. await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
  117. {
  118. DeviceCodes = new List<string> { deviceCode },
  119. UserCodes = userCodes.ToList()
  120. });
  121. return true;
  122. }
  123. /// <summary>
  124. /// 根据设备code获取设备信息
  125. /// </summary>
  126. /// <param name="request">设备code请求实体</param>
  127. /// <returns>返回查询到的设备对象信息</returns>
  128. public async Task<DeviceExtendInfoDTO> GetDeviceInfoAsync(GetDeviceRequest request)
  129. {
  130. //验证
  131. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  132. {
  133. ThrowCustomerException(CustomerRpcCode.DeviceCodeEmpty, "DeviceCode empty error");
  134. }
  135. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  136. if (deviceDb == null || deviceDb.DeviceCode != request.DeviceCode)
  137. {
  138. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  139. }
  140. //根据机构编号查询机构名称
  141. var organizationName = "";
  142. if (!string.IsNullOrEmpty(deviceDb.OrganizationCode))
  143. {
  144. var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.OrganizationCode });
  145. organizationName = organization?.OrganizationName ?? string.Empty;
  146. }
  147. //根据机构编号查询科室名称
  148. var departmentName = "";
  149. if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
  150. {
  151. var department = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.DepartmentCode });
  152. departmentName = department?.OrganizationName ?? string.Empty;
  153. }
  154. //是否在线 todo
  155. var deviceTypeModel = await _deviceInfoDBServiceProxy.FindDictionaryByCodeAsync(deviceDb.DeviceType);
  156. var result = new DeviceExtendInfoDTO()
  157. {
  158. DepartmentName = departmentName,
  159. OrganizationName = organizationName,
  160. IsOnline = await GetDeviceOnlineState(deviceDb.DeviceCode),
  161. DeviceCode = deviceDb.DeviceCode,
  162. SerialNumber = deviceDb.SerialNumber,
  163. Name = deviceDb.Name,
  164. Description = deviceDb.Description,
  165. DeviceModel = deviceDb.DeviceModel,
  166. DeviceType = deviceDb.DeviceType,
  167. HeadPicUrl = deviceDb.HeadPicUrl,
  168. DeviceSoftwareVersion = deviceDb.DeviceSoftwareVersion,
  169. SDKSoftwareVersion = deviceDb.SDKSoftwareVersion,
  170. OrganizationCode = deviceDb.OrganizationCode,
  171. DepartmentCode = deviceDb.DepartmentCode,
  172. ShortCode = deviceDb.ShortCode,
  173. LanguageConfigs = deviceTypeModel.LanguageConfigs
  174. };
  175. return result;
  176. }
  177. /// <summary>
  178. /// 根据设备动态唯一码获取设备信息
  179. /// </summary>
  180. /// <param name="request"></param>
  181. /// <returns></returns>
  182. public async Task<DeviceExtendInfoDTO> GetDeviceByShortCodeAsync(GetDeviceByShortCodeRequest request)
  183. {
  184. //验证
  185. if (string.IsNullOrWhiteSpace(request.ShortCode))
  186. {
  187. ThrowCustomerException(CustomerRpcCode.ShortCodeEmpty, "ShortCode empty error");
  188. }
  189. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(request.ShortCode);
  190. if (deviceDb == null || deviceDb.ShortCode != request.ShortCode)
  191. {
  192. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  193. }
  194. return await MapToExtendDTO(deviceDb);
  195. }
  196. public async Task<PageCollection<DeviceInfoDTO>> GetDeviceInfoPageAsync(PageFilterRequest request)
  197. {
  198. throw new System.NotImplementedException();
  199. }
  200. public async Task<bool> DeleteShareDeviceToUserAsync(DeleteShareDeviceToUserRequest request)
  201. {
  202. var userCodes = request.UserCodes;
  203. var deviceCode = request.DeviceCode;
  204. //检查设备编码不为空
  205. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  206. {
  207. ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
  208. }
  209. //用户编码集合检查
  210. if (userCodes == null || userCodes.Count <= 0)
  211. {
  212. ThrowCustomerException(CustomerRpcCode.UserCodeEmptyError, "Required parameter:UserCodes cannot be empty");
  213. }
  214. //查询设备对象
  215. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
  216. if (deviceDb == null)
  217. {
  218. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  219. }
  220. var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
  221. foreach (var userCode in userCodes)
  222. {
  223. var user = await _userServiceProxy.FindUserByCodeAsync(userCode);
  224. if (user != null)
  225. {
  226. if (user.BindDevices.Contains(deviceCode))
  227. {
  228. user.BindDevices.Remove(deviceCode);
  229. updateUsers.Add(new TableDataItem<UserPasswordDTO>()
  230. {
  231. Data = user.MappingTo<UserPasswordDTO>(),
  232. });
  233. }
  234. }
  235. }
  236. if (updateUsers.Count > 0)
  237. {
  238. var updateUsersDBRequest = new UpdateUsersDBRequest() { Users = updateUsers };
  239. await _userServiceProxy.UpdateUsersAsync(updateUsersDBRequest);
  240. }
  241. return true;
  242. }
  243. /// <summary>
  244. /// 机构设备移除-根据机构code和设备code解除其绑定关系
  245. /// </summary>
  246. /// <param name="request">移除设备请求实体</param>
  247. /// <returns>移除是否成功:true-成功;false-失败</returns>
  248. public async Task<bool> RemoveDeviceRelevancyAsync(RemoveDeviceRelevancyRequest request)
  249. {
  250. //验证
  251. if (string.IsNullOrWhiteSpace(request.DeviceCode) || string.IsNullOrWhiteSpace(request.OrganizationCode))
  252. {
  253. ThrowCustomerException(CustomerRpcCode.DeviceCodeOrOrganizationCodeEmpty, "DeviceCode Or OrganizationCode empty error");
  254. }
  255. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  256. if (deviceDb == null || deviceDb.OrganizationCode != request.OrganizationCode || deviceDb.DepartmentCode != request.DepartmentCode)
  257. {
  258. ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "DeviceInfo not exist error");
  259. }
  260. //根据设备code和机构code查询相关数据,查到则删除关联
  261. if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
  262. {
  263. deviceDb.DepartmentCode = "";
  264. deviceDb.OrganizationCode = "";
  265. }
  266. else
  267. {
  268. deviceDb.OrganizationCode = "";
  269. }
  270. bool removeDeviceResult = await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty, true);
  271. return removeDeviceResult;
  272. }
  273. private async Task<string> GetClientIdByTokenAsync(string token)
  274. {
  275. var request = new TokenRequest() { Token = token };
  276. var result = await _authenticationService.GetTokenAsync(request);
  277. return result.ClientId; //return User code
  278. }
  279. /// <summary>
  280. /// 查询个人名下所有设备列表接口
  281. /// </summary>
  282. /// <param name="request">查询个人名下所有设备列表请求实体</param>
  283. /// <returns>返回设备信息列表</returns>
  284. public async Task<PageCollection<DeviceExtendInfoDTO>> GetPersonDeviceListAsync(GetPersonDeviceRequest request)
  285. {
  286. //验证
  287. var userCode = await GetClientIdByTokenAsync(request.Token);
  288. //查询用户是否存在
  289. var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
  290. if (userInfoDO == null)
  291. {
  292. ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
  293. }
  294. //查询用户的医院是否存在
  295. if (string.IsNullOrWhiteSpace(userInfoDO.OrganizationCode))
  296. {
  297. ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
  298. }
  299. var deviceInfoDOList = new List<DeviceInfoDTO>();
  300. if (!userInfoDO.IsDirector)
  301. {
  302. //普通用户,查询用户绑定的设备
  303. if (userInfoDO.BindDevices != null && userInfoDO.BindDevices.Any())
  304. {
  305. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
  306. }
  307. }
  308. else
  309. {
  310. //机构负责人
  311. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
  312. }
  313. if (!deviceInfoDOList.Any())
  314. {
  315. return new PageCollection<DeviceExtendInfoDTO>() { CurrentPage = request.PageIndex, PageSize = request.PageSize, DataCount = 0, PageData = new List<DeviceExtendInfoDTO>() };
  316. }
  317. var deviceList = await MapToExtendDTO(deviceInfoDOList);
  318. //条件筛选
  319. if (!string.IsNullOrWhiteSpace(request.DeviceType))
  320. {
  321. deviceList = deviceList.Where(d => d.DeviceType == request.DeviceType)?.ToList() ?? new List<DeviceExtendInfoDTO>();
  322. }
  323. if (!string.IsNullOrWhiteSpace(request.DeviceModel))
  324. {
  325. deviceList = deviceList.Where(d => d.DeviceModel == request.DeviceModel)?.ToList() ?? new List<DeviceExtendInfoDTO>();
  326. }
  327. if (!string.IsNullOrWhiteSpace(request.KeyWord))
  328. {
  329. var keyword = request.KeyWord.Trim().ToLower();
  330. deviceList = deviceList.Where(d => d.Name.ToLower().Contains(keyword) || d.ShortCode.ToLower().Contains(keyword))?.ToList() ?? new List<DeviceExtendInfoDTO>();
  331. }
  332. if (request.IsOnline != null)
  333. {
  334. deviceList = deviceList.Where(d => d.IsOnline == request.IsOnline)?.ToList() ?? new List<DeviceExtendInfoDTO>();
  335. }
  336. var pagedResult = new PageCollection<DeviceExtendInfoDTO>
  337. {
  338. CurrentPage = request.PageIndex,
  339. PageSize = request.PageSize,
  340. DataCount = deviceList.Count,
  341. PageData = deviceList.Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize)?.ToList() ?? new List<DeviceExtendInfoDTO>()
  342. };
  343. return pagedResult;
  344. }
  345. /// <summary>
  346. /// 绑定设备
  347. /// </summary>
  348. /// <param name="request"></param>
  349. /// <returns></returns>
  350. public async Task<bool> BindDeviceAsync(BindDeviceRequest request)
  351. {
  352. //验证
  353. var userCode = await GetClientIdByTokenAsync(request.Token);
  354. //查询用户是否存在
  355. var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
  356. if (userInfoDO == null)
  357. {
  358. ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
  359. }
  360. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(request.ShortCode);
  361. if (deviceDb == null)
  362. {
  363. ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
  364. }
  365. if (userInfoDO.BindDevices != null && userInfoDO.BindDevices.Contains(deviceDb.DeviceCode))
  366. {
  367. ThrowCustomerException(CustomerRpcCode.HasAddedDevice, "The device has been Added");
  368. }
  369. try
  370. {
  371. var isInvented = (string.IsNullOrWhiteSpace(deviceDb.OrganizationCode) || deviceDb.OrganizationCode == $"invented_{deviceDb.ShortCode}") ? true : false;
  372. deviceDb.Name = request.Name;
  373. deviceDb.SerialNumber = request.SerialNumber;
  374. deviceDb.Description = request.Description;
  375. if (!string.IsNullOrEmpty(request.HeadPicUrl))
  376. {
  377. deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
  378. }
  379. deviceDb.DepartmentCode = request.DepartmentCode;
  380. deviceDb.IsAutoShared = request.IsAutoShared;
  381. deviceDb.OrganizationCode = request.OrganizationCode;
  382. await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty);
  383. if (isInvented && !string.IsNullOrWhiteSpace(request.OrganizationCode)) //虚拟机构改为正常机构
  384. {
  385. var newOrgCode = string.IsNullOrWhiteSpace(request.DepartmentCode) ? request.OrganizationCode : request.DepartmentCode;
  386. //迁移数据超声机上传检查记录
  387. await _recordsInfoDBService.UpdateRecordsWithOrgCodeAsync(new List<string>() { deviceDb.DeviceCode }, newOrgCode);
  388. //迁移数据超声机上传病人
  389. await _patientInfoDBService.UpdatePatientWithOrgCodeAsync(new List<string>() { deviceDb.DeviceCode }, newOrgCode);
  390. }
  391. if (deviceDb.IsAutoShared)
  392. {
  393. var users = new List<UserDTO>();
  394. if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
  395. {
  396. users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.DepartmentCode });
  397. }
  398. else
  399. {
  400. users = await _userServiceProxy.GetUserListAsync(new GetUserListDBRequest { OrganizationQueryType = OrganizationQueryTypeEnum.All, OrganizationCode = deviceDb.OrganizationCode });
  401. }
  402. var userCodes = users.Select(f => f.UserCode).ToList();
  403. await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
  404. {
  405. DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
  406. UserCodes = userCodes.Select(f => f.ToString()).ToList()
  407. });
  408. }
  409. return true;
  410. }
  411. catch (Exception ex)
  412. {
  413. Logger.WriteLineError($"BindDeviceAsync error {ex}");
  414. }
  415. return false;
  416. }
  417. /// <summary>
  418. /// 修改设备
  419. /// </summary>
  420. /// <param name="request"></param>
  421. /// <returns></returns>
  422. public async Task<bool> ModifyDeviceAsync(ModifyDeviceRequest request)
  423. {
  424. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  425. if (deviceDb == null)
  426. {
  427. ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
  428. }
  429. if (string.IsNullOrWhiteSpace(request.Name))
  430. {
  431. ThrowCustomerException(CustomerRpcCode.DeviceNameEmpty, "Device name cannot be empty");
  432. }
  433. deviceDb.Name = request.Name;
  434. if (!string.IsNullOrWhiteSpace(request.HeadPicUrl))
  435. {
  436. deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
  437. }
  438. else
  439. {
  440. deviceDb.HeadPicUrl = string.Empty;
  441. }
  442. deviceDb.DepartmentCode = request.DepartmentCode;
  443. //修改自动分享
  444. deviceDb.IsAutoShared = request.IsAutoShared;
  445. if (deviceDb.IsAutoShared)
  446. {
  447. var users = new List<UserDTO>();
  448. if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
  449. {
  450. users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.DepartmentCode });
  451. }
  452. else
  453. {
  454. users = await _userServiceProxy.GetUserListAsync(new GetUserListDBRequest { OrganizationQueryType = OrganizationQueryTypeEnum.All, OrganizationCode = deviceDb.OrganizationCode });
  455. }
  456. var userCodes = users.Select(f => f.UserCode).ToList();
  457. //先解绑,再绑定,避免绑定重复设备code
  458. var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
  459. foreach (var user in users)
  460. {
  461. var userPasswordDTO = user.MappingTo<UserPasswordDTO>();
  462. user.BindDevices?.Remove(deviceDb.DeviceCode);
  463. await _userServiceProxy.UpdateUserAsync(userPasswordDTO.UserCode, userPasswordDTO, string.Empty);
  464. }
  465. await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
  466. {
  467. DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
  468. UserCodes = userCodes.Select(f => f.ToString()).ToList()
  469. });
  470. }
  471. return await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty, true);
  472. }
  473. /// <summary>
  474. /// 创建字典
  475. /// </summary>
  476. /// <param name="request"></param>
  477. /// <returns></returns>
  478. public async Task<string> CreateDictionaryItemAsync(CreateDictionaryItemRequest request)
  479. {
  480. return await _deviceInfoDBServiceProxy.CreateDictionaryItemAsync(new CreateDictionaryItemDBRequest
  481. {
  482. Data = new DictionaryDTO { DictionaryType = request.DictionaryType, Value = request.DictionaryItemValue, ParentCode = request.ParentCode },
  483. ExtensionData = string.Empty
  484. });
  485. }
  486. /// <summary>
  487. /// 查找设备型号所有字典项
  488. /// </summary>
  489. /// <param name="request"></param>
  490. /// <returns></returns>
  491. public async Task<List<DictionaryDTO>> FindDeviceModelItemsAsync(FindDeviceModelItemsRequest request)
  492. {
  493. var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
  494. {
  495. DictionaryType = (int)request.DictionaryType,
  496. ParentCode = request.DeviceTypeCode
  497. });
  498. return dictionaryDOList;
  499. }
  500. /// <summary>
  501. /// 查找设备类型所有字典项
  502. /// </summary>
  503. /// <param name="request"></param>
  504. /// <returns></returns>
  505. public async Task<List<DictionaryDTO>> FindDeviceTypeItemsAsync(FindDeviceTypeItemsRequest request)
  506. {
  507. var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
  508. {
  509. DictionaryType = (int)request.DictionaryType
  510. });
  511. return dictionaryDOList;
  512. }
  513. /// <summary>
  514. /// 根据组织编码查询所属全部设备信息
  515. /// </summary>
  516. /// <param name="request"></param>
  517. /// <returns></returns>
  518. public async Task<List<DeviceExtendInfoDTO>> FindDevicesByOrganizationCodeAsync(FindDevicesByOrganizationCodeRequest request)
  519. {
  520. var result = new List<DeviceExtendInfoDTO>();
  521. var deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(request.OrganizationCode);
  522. if (deviceInfoDOList?.Count > 0)
  523. {
  524. //根据机构编号查询机构名称
  525. var organizationCode = deviceInfoDOList.FindAll(c => !string.IsNullOrEmpty(c.OrganizationCode)).Select(c => c.OrganizationCode).FirstOrDefault() ?? string.Empty;
  526. var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = organizationCode });
  527. var organizationName = organization?.OrganizationName ?? string.Empty;
  528. //根据机构编号查询科室名称
  529. var organizationFilter = new Dictionary<string, string>();
  530. organizationFilter.Add("RootCode", organizationCode);
  531. var organizations = await _organizationDBService.GetOrganizationPageAsync((new PageFilterRequest { CurrentPage = 1, PageSize = 999999, Filter = organizationFilter, IsFuzzy = false }));
  532. var deviceCodes = deviceInfoDOList.Select(x => x.DeviceCode).ToList();
  533. var deviceTokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest { ClientIds = deviceCodes });
  534. var deviceTypeList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest { DictionaryType = (int)DictionaryTypeEnum.DeviceType });
  535. foreach (var c in deviceInfoDOList)
  536. {
  537. var deviceTypeModel = deviceTypeList.FirstOrDefault(t => t.DictionaryCode == c.DeviceType);
  538. result.Add(new DeviceExtendInfoDTO
  539. {
  540. DepartmentName = organizations.PageData.FirstOrDefault(o => o.OrganizationCode == c.DepartmentCode)?.OrganizationName,
  541. OrganizationName = organizationName,
  542. IsOnline = deviceTokens.FirstOrDefault(x => x.ClientId == c.DeviceCode)?.IsOnline ?? false,
  543. DeviceCode = c.DeviceCode,
  544. SerialNumber = c.SerialNumber,
  545. Name = c.Name,
  546. Description = c.Description,
  547. DeviceModel = c.DeviceModel,
  548. DeviceType = c.DeviceType,
  549. HeadPicUrl = c.HeadPicUrl,
  550. DeviceSoftwareVersion = c.DeviceSoftwareVersion,
  551. SDKSoftwareVersion = c.SDKSoftwareVersion,
  552. OrganizationCode = c.OrganizationCode,
  553. DepartmentCode = c.DepartmentCode,
  554. ShortCode = c.ShortCode,
  555. LanguageConfigs = deviceTypeModel?.LanguageConfigs
  556. });
  557. }
  558. }
  559. return result;
  560. }
  561. public Task<DeviceInfoDTO> CreateDeviceInfoAsync(CreateDeviceRequest request)
  562. {
  563. throw new NotImplementedException();
  564. }
  565. public async Task<List<SelectItemDTO>> GetPersonDeviceDropdownListAsync(TokenRequest request)
  566. {
  567. //验证
  568. var userCode = await GetClientIdByTokenAsync(request.Token);
  569. //查询用户是否存在
  570. var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(userCode);
  571. if (userInfoDO == null)
  572. {
  573. ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
  574. }
  575. var result = new List<SelectItemDTO>();
  576. //查询用户的医院是否存在
  577. if (string.IsNullOrEmpty(userInfoDO.OrganizationCode))
  578. {
  579. ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
  580. }
  581. var deviceInfoDOList = new List<DeviceInfoDTO>();
  582. if (userInfoDO.IsDirector)
  583. {
  584. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
  585. }
  586. else if (!userInfoDO.RoleCodes.Contains("Role_ExpertAssistant"))
  587. {
  588. //查询用户绑定的设备
  589. if (userInfoDO.BindDevices == null || userInfoDO.BindDevices.Count <= 0)
  590. {
  591. return result;
  592. }
  593. //根据用户名下的设备id查询所有的设备信息
  594. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
  595. }
  596. else
  597. {
  598. var experts = await _userServiceProxy.GetBindExpertsAsync(userCode);
  599. if (experts.Any())
  600. {
  601. var bindDeviceCodes = experts.SelectMany(x => x.BindDevices ?? new List<string>()).Distinct().ToList();
  602. if (bindDeviceCodes.Any())
  603. {
  604. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(bindDeviceCodes);
  605. }
  606. }
  607. else
  608. {
  609. deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(userInfoDO.RootOrganizationCode);
  610. }
  611. }
  612. if (deviceInfoDOList?.Count > 0)
  613. {
  614. result = deviceInfoDOList.Select(c => new SelectItemDTO()
  615. {
  616. Key = c.DeviceCode,
  617. Value = c.Name
  618. }).ToList();
  619. }
  620. return result;
  621. }
  622. /// <summary>
  623. /// 设备端调用,查询获取服务器配置信息接口
  624. /// </summary>
  625. /// <param name="request">Token验证</param>
  626. /// <returns>服务器配置信息结果:key:字段,value:数据值,复杂类型可为json</returns>
  627. public async Task<DeviceServerSettingResult> QueryServerConfigAsync(TokenRequest request)
  628. {
  629. var deviceCode = await GetClientIdByTokenAsync(request.Token);
  630. var deviceInfo = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
  631. if (deviceInfo == null || string.IsNullOrWhiteSpace(deviceInfo.DeviceCode))
  632. {
  633. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  634. }
  635. var org = new OrganizationDTO();
  636. if (!string.IsNullOrWhiteSpace(deviceInfo.OrganizationCode))
  637. {
  638. org = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceInfo.OrganizationCode });
  639. if (org == null)
  640. {
  641. org = new OrganizationDTO();
  642. }
  643. }
  644. var dictionary = new Dictionary<string, string>();
  645. //定义配置字段 并扩充字段,返回数据可持续更新接口文档,如果数据是复杂类型可以转成json
  646. //TODO 配置信息: 直播配置、存储配置、
  647. //存储上传图像或者视频是 是否本地上传缩略图
  648. var isUploadThumbnail = ConfigurationManager.RemedicalSettings.IsUploadThumbnail;
  649. dictionary.Add("IsUploadThumbnail", isUploadThumbnail.ToString());
  650. dictionary.Add("PatientType", org.PatientType.ToString());
  651. dictionary.Add("HeartRateSeconds", _heartRateSeconds.ToString());
  652. dictionary.Add("NotificationUrl", _webSocketUrl);
  653. dictionary.Add("MergedChannel", deviceInfo.MergedChannel.ToString());
  654. dictionary.Add("LiveConsultationRateSeconds", _liveConsultationRateSeconds.ToString());
  655. return new DeviceServerSettingResult()
  656. {
  657. ServerConfigList = dictionary,
  658. IsUploadThumbnail = isUploadThumbnail,
  659. PatientType = org.PatientType,
  660. HeartRateSeconds = _heartRateSeconds,
  661. NotificationUrl = _webSocketUrl,
  662. MergedChannel = deviceInfo.MergedChannel,
  663. LiveConsultationRateSeconds = _liveConsultationRateSeconds
  664. };
  665. }
  666. /// <summary>
  667. /// 添加或迁移设备到医院请求
  668. /// </summary>
  669. /// <param name="request"></param>
  670. /// <returns></returns>
  671. public async Task<bool> AddDeviceToOrgAsync(AddDeviceToOrgRequest request)
  672. {
  673. var shortCode = request.UniqueCode;
  674. #region Params Check
  675. var userCode = await GetClientIdByTokenAsync(request.Token);
  676. if (string.IsNullOrEmpty(shortCode))
  677. {
  678. ThrowCustomerException(CustomerRpcCode.UniqueCodeIsEmpty, "UniqueCode is empty");
  679. }
  680. var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(shortCode);
  681. if (device == null)
  682. {
  683. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  684. }
  685. var oldOrg = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = device.OrganizationCode });
  686. if (oldOrg == null || string.IsNullOrWhiteSpace(oldOrg.OrganizationCode))
  687. {
  688. ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
  689. }
  690. //用户
  691. var userDTO = await _userServiceProxy.FindUserByCodeAsync(userCode);
  692. if (userDTO == null)
  693. {
  694. ThrowCustomerException(CustomerRpcCode.UserNotExistError, "User does not exist");
  695. }
  696. if (!userDTO.IsDirector)
  697. {
  698. ThrowCustomerException(CustomerRpcCode.NotOrgAdmin, "not org admin");
  699. }
  700. var org = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = userDTO.OrganizationCode });
  701. if (org == null || string.IsNullOrWhiteSpace(org.OrganizationCode))
  702. {
  703. ThrowCustomerException(CustomerRpcCode.OrganizationNotExist, "Org does not exist");
  704. }
  705. #endregion
  706. try
  707. {
  708. var newOrgode = org.OrganizationCode;
  709. //设备更新医院
  710. if (!string.IsNullOrEmpty(device.DepartmentCode))
  711. {
  712. device.DepartmentCode = "";
  713. }
  714. if (!string.IsNullOrEmpty(device.Name))
  715. {
  716. device.Name = "";
  717. }
  718. device.HeadPicUrl = "";
  719. device.Description = "";
  720. device.IsAutoShared = false;
  721. device.OrganizationCode = newOrgode;
  722. bool success = await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(device.DeviceCode, device, string.Empty, true);
  723. if (success)
  724. {
  725. // 删除已知绑定的用户
  726. var users = await _userServiceProxy.GetUsersByDeviceAsync(device.DeviceCode);
  727. var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
  728. foreach (var user in users)
  729. {
  730. user.BindDevices.Remove(device.DeviceCode);
  731. updateUsers.Add(new TableDataItem<UserPasswordDTO>()
  732. {
  733. Data = user.MappingTo<UserPasswordDTO>(),
  734. });
  735. }
  736. if (updateUsers.Count > 0)
  737. {
  738. var updateUsersDBRequest = new UpdateUsersDBRequest() { Users = updateUsers };
  739. await _userServiceProxy.UpdateUsersAsync(updateUsersDBRequest);
  740. }
  741. if (oldOrg.Isinvented) // 虚拟医院
  742. {
  743. //迁移数据超声机上传检查记录
  744. await _recordsInfoDBService.UpdateRecordsWithOrgCodeAsync(new List<string>() { device.DeviceCode }, newOrgode);
  745. //迁移数据超声机上传病人
  746. await _patientInfoDBService.UpdatePatientWithOrgCodeAsync(new List<string>() { device.DeviceCode }, newOrgode);
  747. }
  748. }
  749. return success;
  750. }
  751. catch (System.Exception ex)
  752. {
  753. Logger.WriteLineError($"AddDeviceToOrgAsync error {ex}");
  754. }
  755. return false;
  756. }
  757. /// <summary>
  758. /// 补齐设备扩展信息
  759. /// </summary>
  760. /// <param name="deviceInfoDOList"></param>
  761. /// <returns></returns>
  762. private async Task<List<DeviceExtendInfoDTO>> MapToExtendDTO(List<DeviceInfoDTO> deviceInfoDOList)
  763. {
  764. var deviceList = new List<DeviceExtendInfoDTO>();
  765. if (deviceInfoDOList != null && deviceInfoDOList.Any())
  766. {
  767. //查询机构
  768. var orgCodes = deviceInfoDOList.Select(x => x.OrganizationCode).ToList();
  769. orgCodes.AddRange(deviceInfoDOList.Select(x => x.DepartmentCode));
  770. var organizations = await _organizationDBService.FindOrganizationListByCodesAsync(orgCodes.Distinct().ToList());
  771. //查询机构负责人
  772. var organizationDirectorCodes = new List<string>();
  773. foreach (var org in organizations)
  774. {
  775. if (org.Directors != null && org.Directors.Any())
  776. {
  777. organizationDirectorCodes.AddRange(org.Directors);
  778. }
  779. }
  780. var organizationDirectors = await _userServiceProxy.FindUserListByUserCodesAsync(organizationDirectorCodes);
  781. var deviceCodes = deviceInfoDOList.Select(x => x.DeviceCode).ToList();
  782. var deviceTokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest { ClientIds = deviceCodes });
  783. var deviceTypeList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest { DictionaryType = (int)DictionaryTypeEnum.DeviceType });
  784. foreach (var dto in deviceInfoDOList)
  785. {
  786. var device = dto.MappingTo<DeviceExtendInfoDTO>();
  787. device.IsOnline = deviceTokens.FirstOrDefault(x => x.ClientId == dto.DeviceCode)?.IsOnline ?? false;
  788. device.DepartmentName = organizations.FirstOrDefault(x => x.OrganizationCode == dto.DepartmentCode)?.OrganizationName;
  789. device.LanguageConfigs = deviceTypeList.FirstOrDefault(t => t.DictionaryCode == device.DeviceType)?.LanguageConfigs;
  790. var organization = organizations.FirstOrDefault(x => x.OrganizationCode == dto.OrganizationCode);
  791. if (organization != null)
  792. {
  793. device.OrganizationName = organization.OrganizationName;
  794. if (organization.Directors != null && organization.Directors.Any())
  795. {
  796. var director = organizationDirectors.FirstOrDefault(x => x.UserCode == organization.Directors.FirstOrDefault());
  797. device.OrganizationDirectorCode = director?.UserCode;
  798. device.OrganizationDirectorUserName = director?.UserName;
  799. device.OrganizationDirectorFullName = director?.FullName;
  800. }
  801. }
  802. deviceList.Add(device);
  803. }
  804. }
  805. return deviceList;
  806. }
  807. /// <summary>
  808. /// 补齐设备扩展信息
  809. /// </summary>
  810. /// <param name="deviceInfoDOList"></param>
  811. /// <returns></returns>
  812. private async Task<DeviceExtendInfoDTO> MapToExtendDTO(DeviceInfoDTO deviceInfoDO)
  813. {
  814. var deviceList = await MapToExtendDTO(new List<DeviceInfoDTO> { deviceInfoDO });
  815. return deviceList.FirstOrDefault();
  816. }
  817. /// <summary>
  818. /// 获取设备绑定用户codes
  819. /// </summary>
  820. /// <param name="request">获取设备绑定用户codes请求实体</param>
  821. /// <returns>test01,test02</returns>
  822. public async Task<List<string>> GetDeviceBindUsersCodesAsync(GetDeviceRequest request)
  823. {
  824. //检查设备编码不为空
  825. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  826. {
  827. ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
  828. }
  829. //查询设备对象
  830. var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  831. if (deviceDb == null)
  832. {
  833. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  834. }
  835. var userListResult = await _userServiceProxy.GetUsersByDeviceAsync(request.DeviceCode);
  836. if (userListResult != null && userListResult.Count > 0)
  837. {
  838. return userListResult.Select(u => u.UserCode).ToList();
  839. }
  840. return new List<string>();
  841. }
  842. /// <summary>
  843. /// 查询设备AI应用的状态
  844. /// </summary>
  845. /// <param name="request">请求参数</param>
  846. /// <returns>AI应用集合</returns>
  847. public async Task<List<DiagnosisModuleDTO>> FindDeviceDiagnosisModulesAsync(FindDeviceDiagnosisModulesRequest request)
  848. {
  849. //检查设备编码不为空
  850. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  851. {
  852. ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
  853. }
  854. return await _diagnosisModuleService.GetDeviceDiagnosisModulesAsync(new GetDeviceDiagnosisModulesDBRequest { DeviceCode = request.DeviceCode });
  855. }
  856. /// <summary>
  857. /// 修改设备AI应用启用状态
  858. /// </summary>
  859. /// <param name="request">修改设备AI应用启用状态请求实体</param>
  860. /// <returns>是否成功</returns>
  861. /// <para>DeviceErrorCodeEnum</para>
  862. public async Task<bool> ModifyDeviceDiagnosisModuleStateAsync(ModifyDeviceDiagnosisModuleStateRequest request)
  863. {
  864. //检查设备编码不为空
  865. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  866. {
  867. ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "Required parameter:DeviceCode cannot be empty");
  868. }
  869. if (string.IsNullOrWhiteSpace(request.DiagnosisModule))
  870. {
  871. ThrowCustomerException(CustomerRpcCode.DeviceAIDiagnosisModule, "Required parameter:DeviceCode cannot be empty");
  872. }
  873. var result = await _diagnosisModuleService.UpdateDeviceDiagnosisModuleStateAsync(new UpdateDeviceDiagnosisModuleStateDBRequest
  874. {
  875. DeviceCode = request.DeviceCode,
  876. DiagnosisModule = request.DiagnosisModule,
  877. Enabled = request.Enabled
  878. });
  879. CacheMaintenance.Instance.Get<IDeviceInfosManager>().Remove(request.DeviceCode);
  880. return result;
  881. }
  882. /// <summary>
  883. /// 设备离开会诊房间
  884. /// </summary>
  885. /// <param name="request">设备离开会诊房间请求实体</param>
  886. /// <returns></returns>
  887. public async Task<DeviceLeaveLiveConsultationResult> DeviceLeaveLiveConsultationAsync(DeviceLeaveLiveConsultationRequest request)
  888. {
  889. var deviceCode = await GetClientIdByTokenAsync(request.Token);
  890. var result = new DeviceLeaveLiveConsultationResult
  891. {
  892. Success = false,
  893. };
  894. return result;
  895. }
  896. /// <summary>
  897. /// 通用异常处理方法
  898. /// </summary>
  899. /// <param name="customerRpcCodeEnum">异常标识码</param>
  900. /// <param name="msg">异常信息</param>
  901. /// <param name="internalMessage">内部异常信息,非必填</param>
  902. private void ThrowCustomerException(CustomerRpcCode customerRpcCodeEnum, string msg, string internalMessage = null)
  903. {
  904. ThrowRpcException((int)customerRpcCodeEnum, msg, internalMessage);
  905. }
  906. /// <summary>
  907. /// 上报设备画面尺寸相关信息
  908. /// </summary>
  909. /// <param name="request"></param>
  910. /// <docFirstDirectory>设备端API</docFirstDirectory>
  911. /// <docSecondDirectory>实时会诊服务接口</docSecondDirectory>
  912. /// <docThirdDirectory>会诊</docThirdDirectory>
  913. /// <returns></returns>
  914. public async Task<ReportVideoDeviceInfoResult> ReportVideoDeviceInfoAsync(ReportVideoDeviceInfoRequest request)
  915. {
  916. var result = new ReportVideoDeviceInfoResult();
  917. if (request.VideoDeviceInfos == null || request.VideoDeviceInfos.Count == 0)
  918. {
  919. //视屏设备画面信息为空
  920. ThrowCustomerException(CustomerRpcCode.VideoDeviceInfosEmpty, "VideoDeviceInfosEmpty");
  921. }
  922. if (request.VideoDeviceInfos.Any(x => string.IsNullOrWhiteSpace(x.VideoDeviceId)))
  923. {
  924. //视屏设备ID为空
  925. ThrowCustomerException(CustomerRpcCode.VideoDeviceIdEmpty, "VideoDeviceIdEmpty");
  926. }
  927. var videoDeviceIdCount = request.VideoDeviceInfos.Select(x => x.VideoDeviceId).Distinct().Count();
  928. if (videoDeviceIdCount < request.VideoDeviceInfos.Count)
  929. {
  930. //视屏设备ID重复
  931. ThrowCustomerException(CustomerRpcCode.VideoDeviceIdRepeatError, "VideoDeviceIdRepeatError");
  932. }
  933. var deviceCode = await GetClientIdByTokenAsync(request.Token);
  934. var deviceDTO = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
  935. var videoDeviceInfos = new List<VideoDeviceDTO>();
  936. foreach (var video in request.VideoDeviceInfos)
  937. {
  938. var videoDeviceInfo = new VideoDeviceDTO
  939. {
  940. VideoDeviceId = video.VideoDeviceId,
  941. VideoDeviceSourceType = video.VideoDeviceSourceType,
  942. Width = video.Width,
  943. Height = video.Height,
  944. OutputWidth = video.Width,
  945. OutputHeight = video.Height,
  946. };
  947. if (deviceDTO.VideoDeviceInfos != null && deviceDTO.VideoDeviceInfos.Any(x => x.VideoDeviceId == video.VideoDeviceId))
  948. {
  949. var oldVideo = deviceDTO.VideoDeviceInfos.FirstOrDefault(x => x.VideoDeviceId == video.VideoDeviceId);
  950. videoDeviceInfo.OutputWidth = oldVideo.OutputWidth;
  951. videoDeviceInfo.OutputHeight = oldVideo.OutputHeight;
  952. }
  953. }
  954. deviceDTO.VideoDeviceInfos = videoDeviceInfos;
  955. await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDTO.DeviceCode, deviceDTO, null);
  956. CacheMaintenance.Instance.Get<IDeviceInfosManager>().Remove(deviceDTO.DeviceCode);
  957. result.Success = true;
  958. return result;
  959. }
  960. /// <summary>
  961. /// 查找有效设备总数量
  962. /// </summary>
  963. /// <returns></returns>
  964. public async Task<long> GetActiveDeviceCount()
  965. {
  966. return await _deviceInfoDBServiceProxy.GetActiveDeviceCount();
  967. }
  968. }
  969. }