一、业务:根据角色关联菜单
RelationDto
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = GlobalConstant.API_MODEL_IN + “-关联信息”)
public class RelationDto implements Serializable {
private static final long serialVersionUID = 1890569379952399719L;
@NotNull(message = "关联ID" + RegexpConstant.MSG_NOT_NULL)
@ApiModelProperty(value = "关联ID(例如用户ID)", required = true)
private Integer id;
@NotBlank(message = "ID字符串" + RegexpConstant.MSG_NOT_NULL)
@ApiModelProperty(value = "ID字符串,用于批量操作,以逗号分隔(例如用户所关联的角色ID/用户组ID...)", required = true)
private String ids;
}
Controller
@RestController
@RequestMapping(value = "/uac/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - RoleController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RoleController {
@PostMapping(value = "/relation/menus")
@ApiOperation(httpMethod = "POST", value = "根据角色ID添加关联菜单")
public Wrapper addRelationMenus(
@ApiParam(name = "relationDto", value = "关联信息", required = true) @Valid @RequestBody RelationDto relationDto) {
LOGGER.info("addRelationMenus, relationDto:{}", relationDto);
menuRoleService.addRelationMenus(relationDto);
return WrapMapper.ok();
}
}
Service
private static final Logger LOGGER = LoggerFactory.getLogger(MenuRoleServiceImpl.class);
private final MenuRoleMapper menuRoleMapper;
@Autowired
public MenuRoleServiceImpl(MenuRoleMapper menuRoleMapper) {
this.menuRoleMapper = menuRoleMapper;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void addRelationRoles(RelationDto relationDto) {
LOGGER.info("addRelationRoles, relationDto:{}", relationDto);
if (StringUtils.isNotEmpty(relationDto.getIds())) {
List<MenuRole> list = new ArrayList<>();
String[] ids = relationDto.getIds().split(GlobalConstant.SYMBOL_COMMA);
List<Integer> idList = menuRoleMapper.getRelationRoles(relationDto.getId());
MenuRole menuRole;
for (String id : ids) {
if (idList.contains(Integer.parseInt(id))) {
continue;
}
menuRole = new MenuRole();
menuRole.setRoleId(Integer.parseInt(id));
menuRole.setMenuId(relationDto.getId());
list.add(menuRole);
}
if (!list.isEmpty()) {
menuRoleMapper.addRelations(list);
}
}
}
}