Browse Source

修改设备自动分享

fly 2 years ago
parent
commit
4cc707000a
1 changed files with 29 additions and 1 deletions
  1. 29 1
      Service/DeviceService.cs

+ 29 - 1
Service/DeviceService.cs

@@ -462,7 +462,7 @@ namespace WingDeviceService.Service
             {
                 throw new RpcException(1001, "Not find device", "Not find device");
             }
-            if(string.IsNullOrWhiteSpace(request.Name))
+            if (string.IsNullOrWhiteSpace(request.Name))
             {
                 throw new RpcException(1001, "Device name cannot be empty", "Device name cannot be empty");
             }
@@ -476,6 +476,34 @@ namespace WingDeviceService.Service
                 deviceDb.HeadPicUrl = string.Empty;
             }
             deviceDb.DepartmentCode = request.DepartmentCode;
+            //修改自动分享
+            deviceDb.IsAutoShared = request.IsAutoShared;
+            if (deviceDb.IsAutoShared)
+            {
+                var users = new List<UserDTO>();
+                if (!string.IsNullOrEmpty(deviceDb.DepartmentCode))
+                {
+                    users = await _userServiceProxy.FindUsersByOrganizationCodeAsync(deviceDb.DepartmentCode);
+                }
+                else
+                {
+                    users = await _userServiceProxy.FindUsersByOrganizationCodeAsync(deviceDb.OrganizationCode);
+                }
+                var userCodes = users.Select(f => f.UserCode).ToList();
+                //先解绑,再绑定,避免绑定重复设备code
+                var updateUsers = new List<TableDataItem<UserPasswordDTO>>();
+                foreach (var user in users)
+                {
+                    var userPasswordDTO = user.MappingTo<UserPasswordDTO>();
+                    user.BindDevices?.Remove(deviceDb.DeviceCode);
+                    await _userServiceProxy.UpdateUserAsync(userPasswordDTO.UserCode, userPasswordDTO, string.Empty);
+                }
+                await _userServiceProxy.BingDevicesToUsersAsync(new BingDevicesToUsersRequest
+                {
+                    DeviceCodes = new List<string> { deviceDb.DeviceCode.ToString() },
+                    UserCodes = userCodes.Select(f => f.ToString()).ToList()
+                });
+            }
             return await _deviceInfoDBServiceProxy.UpdateDeviceInfoByCodeAsync(deviceDb.DeviceCode, deviceDb, string.Empty);
         }