123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.jd.brume.controller
- import com.jd.brume.entity.func.UserFunc
- import com.jd.brume.service.UserService
- import javax.annotation.Resource
- import javax.validation.constraints.NotNull
- import org.springframework.validation.annotation.Validated
- import org.springframework.web.bind.annotation.DeleteMapping
- import org.springframework.web.bind.annotation.GetMapping
- import org.springframework.web.bind.annotation.PostMapping
- import org.springframework.web.bind.annotation.RequestMapping
- import org.springframework.web.bind.annotation.RestController
- import com.baomidou.mybatisplus.core.conditions.Wrapper
- import com.baomidou.mybatisplus.core.toolkit.StringUtils
- import com.baomidou.mybatisplus.core.toolkit.Wrappers
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page
- import com.jd.brume.entity.RoleEntity
- import com.jd.brume.entity.RoleMenuEntity
- import com.jd.brume.entity.func.RoleFunc
- import com.jd.brume.entity.func.RoleMenuFunc
- import com.jd.brume.service.RoleMenuService
- import com.jd.brume.service.RoleService
- import com.jd.brume.util.Result
- import com.jd.brume.util.ResultEnum
- import com.jd.brume.vo.RoleVo
- import com.jd.brume.vo.group.PagingGroup
- import com.jd.brume.vo.group.SaveGroup
- import com.jd.brume.vo.group.UpdateGroup
- /**
- * ???????
- * @author ???
- *
- */
- @Validated
- @RestController
- @RequestMapping('role')
- class RoleController {
-
- @Resource
- RoleService roleService
-
- @Resource
- RoleMenuService roleMenuService
- @Resource
- UserService userService
-
- /**
- * 分页查询
- * @param roleVo
- * @return
- */
- @GetMapping('paging')
- def getRolePaging(@Validated(PagingGroup) RoleVo roleVo) {
- Wrapper<RoleEntity> wrapper = Wrappers.lambdaQuery()
- .like(StringUtils.isNotBlank(roleVo.queryVal), RoleFunc.roleName(), roleVo.queryVal)
- def count = roleService.count(wrapper)
- if (count == 0) return new Result().msg(ResultEnum.NO_DATA_TABLE)
- wrapper.orderByDesc(RoleFunc.createTime())
- def data = roleService.page(new Page(roleVo.page, roleVo.limit, count), wrapper)
- return new Result().layTable([list: data.records, count: data.total])
- }
-
- /**
- * 删除角色
- * @param roleId
- * @return
- */
- @DeleteMapping('delete')
- def deleteRole(@NotNull Integer roleId) {
- def count = userService.count(Wrappers.lambdaQuery().eq(UserFunc.roleId(), roleId))
- if (count > 0){
- return new Result().msg(100, '角色已被用户使用')
- }
- return new Result().ok(roleService.removeById(roleId))
- }
-
- @GetMapping('one')
- def getRole(@NotNull Integer roleId) {
- return new Result().ok([role: roleService.getById(roleId), menus: roleMenuService.lambdaQuery().eq(RoleMenuFunc.roleId(), roleId).list()])
- }
-
- /**
- * 保存角色
- * @param roleVo
- * @return
- */
- @PostMapping('save')
- def saveRole(@Validated(SaveGroup) RoleVo roleVo) {
- def count = roleService.count(Wrappers.lambdaQuery().eq(RoleFunc.roleName(), roleVo.roleName))
- if (count > 0) return new Result().msg(100, '角色名称重复')
- // ??????
- RoleEntity role = new RoleEntity(roleName: roleVo.roleName, description: roleVo.description)
- def status = roleService.save(role)
- // ???????
- if (roleVo.roleMenu) {
- RoleMenuEntity roleMenu
- def arr = []
- for (int i = 0; i < roleVo.roleMenu.size(); i++) {
- def id = roleVo.roleMenu.get(i)
- roleMenu = new RoleMenuEntity(menuId: Integer.valueOf(id), buttonIds: roleVo.roleBtn[String.valueOf(id)], roleId: role.roleId)
- arr.add(roleMenu)
- }
- status = roleMenuService.saveBatch(arr)
- }
- return new Result().ok(status)
- }
-
- /**
- * 修改角色
- * @param roleVo
- * @return
- */
- @PostMapping('update')
- def updateRole(@Validated(UpdateGroup) RoleVo roleVo) {
- def count = roleService.count(Wrappers.lambdaQuery().eq(RoleFunc.roleName(), roleVo.roleName).ne(RoleFunc.roleId(), roleVo.roleId))
- if (count > 0) return new Result().msg(100, '角色名称重复')
- // ?????
- def status = roleService.updateById(new RoleEntity(roleId: roleVo.roleId, roleName: roleVo.roleName, description: roleVo.description))
- // ??????
- status = roleMenuService.remove(Wrappers.lambdaQuery().eq(RoleMenuFunc.roleId(), roleVo.roleId))
- // ???????
- if (roleVo.roleMenu) {
- RoleMenuEntity roleMenu
- def arr = []
- for (int i = 0; i < roleVo.roleMenu.size(); i++) {
- def id = roleVo.roleMenu.get(i)
- roleMenu = new RoleMenuEntity(menuId: Integer.valueOf(id), buttonIds: roleVo.roleBtn[String.valueOf(id)], roleId: roleVo.roleId)
- arr.add(roleMenu)
- }
- status = roleMenuService.saveBatch(arr)
- }
- return new Result().ok(status)
- }
- }
|