DeviceService.cs 47 KB

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