DeviceService.cs 47 KB

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