public async Task <BaseResponse> ChangeDeviceProject(string account, string DeviceSn, string GroupId, int?projectId) { var entity = await _dr.FindAsync(DeviceSn); var migration = new DeviceMigrationModel { Create = account, CreateTime = DateTime.Now, CurrentPId = projectId, PrePId = entity.ProjectId, DeviceSn = entity.DeviceSn, DeviceNo = entity.DeviceNo, GroupId = GroupId, TypeId = 1 }; string message = ""; try { entity.ProjectId = projectId; entity.Modify = account; entity.ModifyTime = DateTime.Now; if (projectId.HasValue && projectId.Value != 0) { message = "迁移设备"; var p = await _ps.GetProjectAsync(projectId.Value); if (p != null) { entity.FullId = p.PathId == null?p.Id.ToString() : p.PathId; entity.FullName = p.PathName == null ? p.Name : p.PathName; entity.GroupId = GroupId;//超级管理员可以跨组织迁移设备 } else { return(new BaseResponse { Success = false, Message = "输入的项目不存在" }); } } else { message = "设备移入回收站"; entity.FullId = null; entity.FullName = null; } await _dr.SaveDeviceWithMigrationAsync(entity, migration); _log.LogInformation($"{account}{message}成功"); return(new BaseResponse { Success = true, Message = $"{message}成功" }); } catch (Exception ex) { _log.LogError($"{account}{message}标识为{DeviceSn}的设备失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}"); return(new BaseResponse { Success = false, Message = $"{message}失败,请联系管理员" }); } }
/// <summary> /// 迁移设备 /// </summary> /// <param name="entity">设备信息</param> /// <param name="migration">设备迁移信息</param> /// <returns></returns> public async Task SaveDeviceWithMigrationAsync(DeviceModel entity, DeviceMigrationModel migration) { using (var trans = _db.Database.BeginTransaction()) { try { _db.Entry <DeviceModel>(entity).State = EntityState.Modified; _db.DeviceMigrations.Add(migration); await _db.SaveChangesAsync(); await trans.CommitAsync(); } catch (Exception ex) { trans.Rollback(); throw ex; } } }