123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using WingInterfaceLibrary.Enum;
- using System.Threading.Tasks;
- using JsonRpcLite.Services;
- using WingInterfaceLibrary.DTO.Device;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.DTO.Dictionary;
- using WingServerCommon.Log;
- using WingServerCommon.Service;
- using WingInterfaceLibrary.Interface.DBInterface;
- using WingInterfaceLibrary.DB.Request;
- using WingServerCommon.Mapper;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.DTO.User;
- using System.IO;
- using WingInterfaceLibrary.DTO.Management;
- using WingServerCommon.Config;
- using WingDeviceService.Common;
- namespace WingDeviceService.Service
- {
-
-
-
- public partial class DeviceService : JsonRpcService, IDeviceService
- {
- private IUserDBService _userServiceProxy;
- private IDeviceInfoDBService _deviceInfoDBServiceProxy;
- private IAuthenticationService _authenticationServiceProxy;
- private IOrganizationDBService _organizationDBService;
- private INotificationService _notificationService;
- private CacheManager<CacheDeviceDTO> _cacheDeviceManager
- {
- get
- {
- return CacheManagerSingle<CacheDeviceDTO>.Instance;
- }
- }
-
-
-
- public override void Load(JsonRpcClientPool jsonRpcClientPool)
- {
- base.Load(jsonRpcClientPool);
- _userServiceProxy = GetProxy<IUserDBService>();
- _authenticationServiceProxy = GetProxy<IAuthenticationService>();
- _deviceInfoDBServiceProxy = GetProxy<IDeviceInfoDBService>();
- _organizationDBService = GetProxy<IOrganizationDBService>();
- _notificationService = GetProxy<INotificationService>();
- }
-
-
-
-
-
- public async Task<bool> CreateShareDeviceToUserAsync(CreateShareDeviceToUserRequest request)
- {
- var userCodes = request.UserCodes;
- var deviceCode = request.DeviceCode;
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
-
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- throw new RpcException(1004, "Required parameter:DeviceCode cannot be empty", "Required parameter:DeviceCode cannot be empty");
- }
- if (userCodes == null || userCodes.Count <= 0)
- {
- throw new RpcException(1004, "Required parameter:UserCodes cannot be empty", "Required parameter:UserCodes cannot be empty");
- }
-
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
- if (deviceDb == null)
- {
- throw new RpcException(1007, "Device not exist.", "Device not exist.");
- }
- foreach (var userCode in userCodes)
- {
- var existUser = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (existUser == null)
- {
- throw new RpcException(1007, " Users not exist.", " Users not exist.");
- }
- }
- await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
- {
- DeviceCodes = new List<string> { deviceCode },
- UserCodes = userCodes.ToList()
- });
- return true;
- }
-
-
-
-
-
- public async Task<DeviceExtendInfoDTO> GetDeviceInfoAsync(GetDeviceRequest request)
- {
-
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- throw new RpcException(1001, "DeviceCode empty error", "DeviceCode empty error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null || deviceDb.DeviceCode != request.DeviceCode)
- {
- throw new RpcException(1001, "DeviceInfo not exist error", "DeviceInfo not exist error");
- }
-
- var organizationName = "";
- if (!string.IsNullOrEmpty(deviceDb.OrganizationCode))
- {
- var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.OrganizationCode });
- organizationName = organization?.OrganizationName ?? string.Empty;
- }
-
- var departmentName = "";
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- var department = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = deviceDb.DepartmentCode });
- departmentName = department?.OrganizationName ?? string.Empty;
- }
-
- var result = new DeviceExtendInfoDTO()
- {
- DepartmentName = departmentName,
- OrganizationName = organizationName,
- IsOnline = (deviceDb.Name == null ? false : ((deviceDb.Name.ToString().Length % 2 == 0) ? true : false)),
- DeviceCode = deviceDb.DeviceCode,
- SerialNumber = deviceDb.SerialNumber,
- Name = deviceDb.Name,
- Description = deviceDb.Description,
- DeviceModel = deviceDb.DeviceModel,
- DeviceType = deviceDb.DeviceType,
- HeadPicUrl = deviceDb.HeadPicUrl,
- DeviceSoftwareVersion = deviceDb.DeviceSoftwareVersion,
- SDKSoftwareVersion = deviceDb.SDKSoftwareVersion,
- OrganizationCode = deviceDb.OrganizationCode,
- DepartmentCode = deviceDb.DepartmentCode,
- ShortCode = deviceDb.ShortCode
- };
- return result;
- }
- public async Task<PageCollection<DeviceInfoDTO>> GetDeviceInfoPageAsync(PageFilterRequest request)
- {
- throw new System.NotImplementedException();
- }
- public async Task<bool> DeleteShareDeviceToUserAsync(DeleteShareDeviceToUserRequest request)
- {
- var userCodes = request.UserCodes;
- var deviceCode = request.DeviceCode;
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
-
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- throw new RpcException(1004, "Required parameter:DeviceCode cannot be empty", "Required parameter:DeviceCode cannot be empty");
- }
-
- if (userCodes == null || userCodes.Count <= 0)
- {
- throw new RpcException(1004, "Required parameter:UserCodes cannot be empty", "Required parameter:UserCodes cannot be empty");
- }
-
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
- if (deviceDb == null)
- {
- throw new RpcException(1007, "Device not exist.", "Device not exist.");
- }
- var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
- foreach (var userCode in userCodes)
- {
- var user = await _userServiceProxy.FindUserByCodeAsync(userCode);
- if (user != null)
- {
- if (user.BindDevices.Contains(deviceCode))
- {
- user.BindDevices.Remove(deviceCode);
- updateUsers.Add(new TableDataItem<UserPasswordDTO>()
- {
- Data = user.MappingTo<UserPasswordDTO>(),
- });
- }
- }
- }
- if (updateUsers.Count > 0)
- {
- var updateUsersDBRequest = new UpdateUsersDBRequest() { Users = updateUsers };
- await _userServiceProxy.UpdateUsersAsync(updateUsersDBRequest);
- }
- return true;
- }
-
-
-
-
-
- public async Task<bool> RemoveDeviceRelevancyAsync(RemoveDeviceRelevancyRequest request)
- {
-
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- if (string.IsNullOrWhiteSpace(request.DeviceCode) || string.IsNullOrWhiteSpace(request.OrganizationCode))
- {
- throw new RpcException(1001, "DeviceCode Or OrganizationCode empty error", "DeviceCode Or OrganizationCode empty error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null || deviceDb.OrganizationCode != request.OrganizationCode || deviceDb.DepartmentCode != request.DepartmentCode)
- {
- throw new RpcException(1001, "DeviceInfo not exist error", "DeviceInfo not exist error");
- }
-
-
-
-
-
-
-
-
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- deviceDb.DepartmentCode = "";
- deviceDb.OrganizationCode = "";
- }
- else
- {
- deviceDb.OrganizationCode = "";
- }
- bool removeDeviceResult = await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty, true);
- return removeDeviceResult;
- }
-
-
-
-
-
- public async Task<List<DeviceExtendInfoDTO>> GetPersonDeviceListAsync(WingInterfaceLibrary.Request.TokenRequest request)
- {
-
- var tokenInfo = await _authenticationServiceProxy.GetTokenInfoAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token });
- if (tokenInfo == null)
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
-
- var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(tokenInfo.UserCode);
- if (userInfoDO == null)
- {
- throw new RpcException(1007, "User not exist.", "User not exist.");
- }
- var result = new List<DeviceExtendInfoDTO>();
-
- if (string.IsNullOrEmpty(userInfoDO.OrganizationCode))
- {
- throw new RpcException(1007, "User's organization not exist.", "User's organization not exist.");
- }
-
- if (userInfoDO.BindDevices == null || userInfoDO.BindDevices.Count <= 0)
- {
- return result;
- }
-
- var deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
- if (deviceInfoDOList?.Count > 0)
- {
-
- var organizationCode = deviceInfoDOList.FindAll(c => !string.IsNullOrEmpty(c.OrganizationCode)).Select(c => c.OrganizationCode).FirstOrDefault() ?? string.Empty;
- var organization = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = organizationCode });
- var organizationName = organization?.OrganizationName ?? string.Empty;
-
- var department = await _organizationDBService.GetOrganizationDetailAsync(new GetOrganizationDBRequest { OrganizationCode = userInfoDO.OrganizationCode });
- var departmentName = department?.OrganizationName ?? string.Empty;
-
- result = deviceInfoDOList.Select(c => new DeviceExtendInfoDTO()
- {
- DepartmentName = departmentName,
- OrganizationName = organizationName,
- IsOnline = (c.Name == null ? false : ((c.Name.ToString().Length % 2 == 0) ? true : false)),
- DeviceCode = c.DeviceCode,
- SerialNumber = c.SerialNumber,
- Name = c.Name,
- Description = c.Description,
- DeviceModel = c.DeviceModel,
- DeviceType = c.DeviceType,
- HeadPicUrl = c.HeadPicUrl,
- DeviceSoftwareVersion = c.DeviceSoftwareVersion,
- SDKSoftwareVersion = c.SDKSoftwareVersion,
- OrganizationCode = c.OrganizationCode,
- DepartmentCode = c.DepartmentCode,
- ShortCode = c.ShortCode
- }).ToList();
- }
- return result;
- }
-
-
-
-
-
- public async Task<bool> BindDeviceAsync(BindDeviceRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByShortCodeAsync(request.ShortCode);
- if (deviceDb == null)
- {
- throw new RpcException(1001, "Not find device", "Not find device");
- }
- try
- {
- deviceDb.Name = request.Name;
- deviceDb.SerialNumber = request.SerialNumber;
- deviceDb.Description = request.Description;
- if (!string.IsNullOrEmpty(request.HeadPicUrl))
- {
-
-
-
-
-
-
-
-
-
-
-
- deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
- }
- deviceDb.OrganizationCode = request.OrganizationCode;
- deviceDb.DepartmentCode = request.DepartmentCode;
- deviceDb.IsAutoShared = request.IsAutoShared;
- if (deviceDb.IsAutoShared)
- {
- if (await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty))
- {
- var users = new List<UserDTO>();
- if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
- {
- users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.DepartmentCode });
- }
- else
- {
- users = await _userServiceProxy.FindUserListByOrganizationCodesAsync(new List<string> { deviceDb.OrganizationCode });
- }
- var userCodes = users.Select(f => f.UserCode).ToList();
- await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
- {
- DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
- UserCodes = userCodes.Select(f => f.ToString()).ToList()
- });
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"BindDeviceAsync error {ex}");
- }
- return false;
- }
-
-
-
-
-
- public async Task<bool> AlterDeviceAsync(AlterDeviceRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var deviceDb = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (deviceDb == null)
- {
- throw new RpcException(1001, "Not find device", "Not find device");
- }
- deviceDb.Name = request.Name;
- deviceDb.SerialNumber = request.SerialNumber;
- deviceDb.Description = request.Description;
- deviceDb.HeadPicUrl = request.HeadPicUrl.ToUrlToken();
- deviceDb.OrganizationCode = request.OrganizationCode;
- return await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty);
- }
-
-
-
-
-
- public async Task<string> CreateDictionaryItemAsync(CreateDictionaryItemRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var tokenInfo = await _authenticationServiceProxy.GetTokenInfoAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token });
- return await _deviceInfoDBServiceProxy.CreateDictionaryItemAsync(new CreateDictionaryItemDBRequest
- {
- Data = new DictionaryDTO { DictionaryType = request.DictionaryType, Value = request.DictionaryItemValue, ParentCode = request.ParentCode },
- PlatformDatas = tokenInfo.HeaderMap,
- ExtensionData = string.Empty
- });
- }
-
-
-
-
-
- public async Task<List<DictionaryDTO>> FindDeviceModelItemsAsync(FindDeviceModelItemsRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
- {
- DictionaryType = (int)request.DictionaryType,
- ParentCode = request.DeviceTypeCode
- });
- return dictionaryDOList;
- }
-
-
-
-
-
- public async Task<List<DictionaryDTO>> FindDeviceTypeItemsAsync(FindDeviceTypeItemsRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var dictionaryDOList = await _deviceInfoDBServiceProxy.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest
- {
- DictionaryType = (int)request.DictionaryType
- });
- return dictionaryDOList;
- }
-
-
-
-
-
- public async Task<List<DeviceInfoDTO>> FindDevicesByOrganizationCodeAsync(FindDevicesByOrganizationCodeRequest request)
- {
- if (!await _authenticationServiceProxy.CheckTokenAsync(new WingInterfaceLibrary.Request.TokenRequest() { Token = request.Token }))
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
- var deviceInfosDO = await _deviceInfoDBServiceProxy.FindDeviceInfoListByOrganizationCodeAsync(request.OrganizationCode);
- return deviceInfosDO;
- }
- public Task<DeviceInfoDTO> CreateDeviceInfoAsync(CreateDeviceRequest request)
- {
- throw new NotImplementedException();
- }
- public async Task<List<SelectItemDTO>> GetPersonDeviceDropdownListAsync(TokenRequest request)
- {
-
- var tokenInfo = await _authenticationServiceProxy.GetTokenInfoAsync(request);
- if (tokenInfo == null)
- {
- throw new RpcException(1002, "Permission validation error", "Permission validation error");
- }
-
- var userInfoDO = await _userServiceProxy.FindUserByCodeAsync(tokenInfo.UserCode);
- if (userInfoDO == null)
- {
- throw new RpcException(1007, "User not exist.", "User not exist.");
- }
- var result = new List<SelectItemDTO>();
-
- if (string.IsNullOrEmpty(userInfoDO.OrganizationCode))
- {
- throw new RpcException(1007, "User's organization not exist.", "User's organization not exist.");
- }
-
- if (userInfoDO.BindDevices == null || userInfoDO.BindDevices.Count <= 0)
- {
- return result;
- }
-
- var deviceInfoDOList = await _deviceInfoDBServiceProxy.FindDeviceInfoListByCodeListAsync(userInfoDO.BindDevices);
- if (deviceInfoDOList?.Count > 0)
- {
- result = deviceInfoDOList.Select(c => new SelectItemDTO()
- {
- Key = c.DeviceCode,
- Value = c.Name
- }).ToList();
- }
- return result;
- }
-
-
-
-
-
- public async Task<Dictionary<string, string>> QueryServerConfig(TokenRequest request)
- {
- var cacheDeviceDTO = await GetDeviceByToken(request);
-
- var code = cacheDeviceDTO.DeviceCode;
- var dictionary = new Dictionary<string, string>();
-
-
-
- var isUploadThumbnail = ConfigurationManager.RemedicalSettings.IsUploadThumbnail;
- dictionary.Add("IsUploadThumbnail", isUploadThumbnail.ToString());
- return dictionary;
- }
- }
- }
|