翻译代码 @Override public MessageData update(TUserDept tUserDept) throws Exception { if(tUserDept==null || tUserDept.getId() == null || tUserDept.getId() < 1){ throw new ServiceException(WholeConstants.CommonStatus.MISSING_PARAMETERS); } if (StringUtils.isBlank(tUserDept.getName()) || !tUserDept.getName().matches(PathConstant.STRING_TEN)) { throw new ServiceException(WholeConstants.CommonStatus.DEPT_NAME_FORMAT_ERROR); } if (tUserDeptMapper.queryByName(tUserDept.getName(),tUserDept.getId()) > 0) { throw new ServiceException(WholeConstants.CommonStatus.DEPT_NAME_EXIST); } tUserDept.setUpdateTime(new Date()); tUserDept.setUpdateUserId(ShiroUtil.getUserId()); tUserDeptMapper.update(tUserDept); return MessageData.buildSuccess(); }
时间: 2024-04-21 20:26:19 浏览: 21
这是一个 Java 方法,其作用是更新 TUserDept 对象并返回一个 MessageData 对象。
方法头部的 @Override 表示这个方法重写了父类或接口的方法。
方法的参数为一个 TUserDept 对象,抛出异常类型为 Exception。
方法体中,先判断 tUserDept 对象是否为空或其 id 属性为空或小于1,如果是则抛出 ServiceException 异常,异常信息为 "missing parameters"。
然后再判断 tUserDept 对象的 name 属性是否为空或不符合一定的格式,如果不符合则抛出 ServiceException 异常,异常信息为 "dept name format error"。
接着判断数据库中是否已经存在同名的部门,如果存在则也抛出 ServiceException 异常,异常信息为 "dept name exist"。
然后设置 tUserDept 对象的 updateTime 和 updateUserId 属性,并调用 tUserDeptMapper 的 update 方法将 tUserDept 对象更新到数据库中。
最后返回一个成功的 MessageData 对象。
相关问题
@Transactional(rollbackFor = Exception.class) @Override public MessageData updateUser(User user) throws Exception { if (user == null || user.getId() == null || user.getId() < 1) { throw new ServiceException(WholeConstants.CommonStatus.MISSING_PARAMETERS); } if (StringUtils.isBlank(user.getName()) || !user.getName().matches(PathConstant.STRING_TEN)) { throw new ServiceException(WholeConstants.CommonStatus.NAME_FORMAT_ERROR); } if (StringUtils.isBlank(user.getUsername()) || !user.getUsername().matches(PathConstant.STRING_TEN)) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_NAME_FORMAT_ERROR); } Integer[] params = {1,2};//userType if (user.getUserType() == null || !ObjectUtils.containsElement(params,user.getUserType())) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_STATUS_PARAMS); } if (StringUtils.isAnyBlank(user.getPassword(),user.getCheckPass())) { if (StringUtils.isAllBlank(user.getPassword(),user.getCheckPass())) { user.setPassword(null);//两次密码都为空则不修改 }else { throw new ServiceException(WholeConstants.CommonStatus.PASSWORD_FORMAT_ERROR); } } if (!user.getPassword().matches(PathConstant.STRING_TWENTYS) || !user.getCheckPass().matches(PathConstant.STRING_TWENTYS)) { throw new ServiceException(WholeConstants.CommonStatus.PASSWORD_FORMAT_ERROR); }else if (!user.getPassword().equals(user.getCheckPass())){ throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_DIFF_PASSWORD); }else { user.setPassword(Md5Util.getStrMd5(user.getPassword())); } //判断用户名称是否重复 if(webUserMapper.selectByUserName(user.getUsername(),user.getId()) > 0) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_NAME_EXIST); } user.setUpdateTime(new Date()); user.setUpdateUserId(ShiroUtil.getUserId()); //web平台修改用户角色 if(user.getUserType().equals(PathConstant.USER_TYPE_WEB)) { if(user.getRoleId() == null) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_ROLE_NULL); } if (userRoleMapper.updateByUserId(user.getId(), user.getRoleId()) < 1) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_UPDATE); } } else if(user.getUserType().equals(PathConstant.USER_TYPE_APP)){ userRoleMapper.deleteByUserId(user.getId()); } if (webUserMapper.updateUser(user) != 1) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_UPDATE); } logService.recordLog(LogType.UPDATE_TYPE.getCode(), "修改用户信息"); return MessageData.buildSuccess(); }翻译代码
这是一段Java代码,主要是一个名为updateUser的函数,其作用是更新用户信息。函数使用了@Transactional(rollbackFor = Exception.class)注解,表示在执行函数时会开启一个事务,如果出现异常则会回滚。函数接收一个User对象作为参数,并返回一个MessageData对象。
函数中进行了一系列的参数校验,包括用户ID、用户名称、账号名称、用户类型、密码等。如果检测到参数有误,则会抛出ServiceException异常。如果校验通过,则会更新用户的更新时间、更新者ID等信息,并将更新后的用户信息存储到数据库中。
在更新用户信息时,如果用户类型为web平台用户,则还会进行用户角色的修改操作。
最后,函数会记录日志并返回一个执行成功的MessageData对象。
翻译下面的代码 @Transactional(rollbackFor = Exception.class) @Override public MessageData updateUser(User user) throws Exception { if (user == null || user.getId() == null || user.getId() < 1) { throw new ServiceException(WholeConstants.CommonStatus.MISSING_PARAMETERS); } if (StringUtils.isBlank(user.getName()) || !user.getName().matches(PathConstant.STRING_TEN)) { throw new ServiceException(WholeConstants.CommonStatus.NAME_FORMAT_ERROR); } if (StringUtils.isBlank(user.getUsername()) || !user.getUsername().matches(PathConstant.STRING_TEN)) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_NAME_FORMAT_ERROR); } Integer[] params = {1,2};//userType if (user.getUserType() == null || !ObjectUtils.containsElement(params,user.getUserType())) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_STATUS_PARAMS); } if (StringUtils.isAnyBlank(user.getPassword(),user.getCheckPass())) { if (StringUtils.isAllBlank(user.getPassword(),user.getCheckPass())) { user.setPassword(null);//两次密码都为空则不修改 }else { throw new ServiceException(WholeConstants.CommonStatus.PASSWORD_FORMAT_ERROR); } } if (!user.getPassword().matches(PathConstant.STRING_TWENTYS) || !user.getCheckPass().matches(PathConstant.STRING_TWENTYS)) { throw new ServiceException(WholeConstants.CommonStatus.PASSWORD_FORMAT_ERROR); }else if (!user.getPassword().equals(user.getCheckPass())){ throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_DIFF_PASSWORD); }else { user.setPassword(Md5Util.getStrMd5(user.getPassword())); } //判断用户名称是否重复 if(webUserMapper.selectByUserName(user.getUsername(),user.getId()) > 0) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_NAME_EXIST); } user.setUpdateTime(new Date()); user.setUpdateUserId(ShiroUtil.getUserId()); //web平台修改用户角色 if(user.getUserType().equals(PathConstant.USER_TYPE_WEB)) { if(user.getRoleId() == null) { throw new ServiceException(WholeConstants.CommonStatus.ACCOUNT_ROLE_NULL); } if (userRoleMapper.updateByUserId(user.getId(), user.getRoleId()) < 1) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_UPDATE); } } else if(user.getUserType().equals(PathConstant.USER_TYPE_APP)){ userRoleMapper.deleteByUserId(user.getId()); } if (webUserMapper.updateUser(user) != 1) { throw new ServiceException(WholeConstants.CommonStatus.ERROR_UPDATE); } logService.recordLog(LogType.UPDATE_TYPE.getCode(), "修改用户信息"); return MessageData.buildSuccess(); }
这是一个 Java 方法,使用了 @Transactional 注解,意味着该方法在执行时会被包含在一个事务中。同时,如果该方法抛出 Exception 异常,则会回滚事务。
该方法的功能是更新用户信息。首先会对传入的 User 对象进行校验,包括判断是否为空、是否符合格式要求等。如果校验不通过,会抛出 ServiceException 异常,其中包含错误码。接着会判断用户角色类型,如果是 web 平台用户,则会对其角色进行修改,如果是 app 平台用户,则会删除其角色。最后,会更新用户信息并记录修改操作日志,并返回一个表示成功的 MessageData 对象。