SysRoleController.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.example.civilizedcity.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.example.civilizedcity.common.BaseController;
  6. import com.example.civilizedcity.common.Constants;
  7. import com.example.civilizedcity.common.ResponseBean;
  8. import java.time.LocalDateTime;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import com.example.civilizedcity.common.StringUtils;
  12. import com.example.civilizedcity.service.SysUserRoleService;
  13. import io.swagger.annotations.Api;
  14. import io.swagger.annotations.ApiOperation;
  15. import io.swagger.annotations.ApiParam;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.*;
  18. import com.example.civilizedcity.entity.SysRole;
  19. import com.example.civilizedcity.service.SysRoleService;
  20. /**
  21. * 系统角色;(sys_role)表控制层
  22. *
  23. * @author : http://njyunzhi.com
  24. * @date : 2022-12-12
  25. */
  26. @Api(tags = "系统角色对象功能接口")
  27. @RestController
  28. @RequestMapping("/")
  29. public class SysRoleController extends BaseController {
  30. @Autowired
  31. private SysRoleService sysRoleService;
  32. @Autowired
  33. private SysUserRoleService sysUserRoleService;
  34. /**
  35. * 通过ID查询单条数据
  36. *
  37. * @param roleId 主键
  38. * @return 实例对象
  39. */
  40. @ApiOperation("通过ID查询单条数据")
  41. @GetMapping("/sysRole/{id}")
  42. public ResponseBean queryById(@ApiParam("对象ID") @PathVariable String id) throws Exception {
  43. return ResponseBean.success(sysRoleService.getById(id));
  44. }
  45. /**
  46. * 分页查询
  47. *
  48. * @param pageNum 当前页码
  49. * @param pageSize 每页条数
  50. * @return 查询结果
  51. */
  52. @ApiOperation("分页查询")
  53. @GetMapping("/sysRole")
  54. public ResponseBean list(@ApiParam("页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
  55. @ApiParam("单页数据量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
  56. IPage<SysRole> pg = new Page<>(pageNum, pageSize);
  57. QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
  58. // queryWrapper.gt("status", Constants.STATUS_DELETE);
  59. queryWrapper.orderByDesc("create_date");
  60. IPage<SysRole> result = sysRoleService.page(pg, queryWrapper);
  61. return ResponseBean.success(result);
  62. }
  63. /**
  64. * 新增数据
  65. *
  66. * @param sysRole 实例对象
  67. * @return 实例对象
  68. */
  69. @ApiOperation("新增数据")
  70. @PostMapping("/sysRole")
  71. public ResponseBean add(@ApiParam("对象实体") @RequestBody SysRole sysRole) throws Exception {
  72. sysRole.setRoleId(null);
  73. if (StringUtils.isEmpty(sysRole.getName())) {
  74. return ResponseBean.error("角色名称不能为空");
  75. }
  76. long i = sysRoleService.countBy("name", sysRole.getName(), false);
  77. if (i > 0) {
  78. return ResponseBean.error("角色名称已存在");
  79. }
  80. sysRole.setStatus(Constants.STATUS_NORMAL);
  81. sysRole.setCreateDate(LocalDateTime.now());
  82. sysRoleService.save(sysRole);
  83. return ResponseBean.success(sysRole);
  84. }
  85. /**
  86. * 更新数据
  87. *
  88. * @param sysRole 实例对象
  89. * @return 实例对象
  90. */
  91. @ApiOperation("更新数据")
  92. @PutMapping("/sysRole/{id}")
  93. public ResponseBean edit(@ApiParam("对象实体") @RequestBody SysRole sysRole,
  94. @ApiParam("对象ID") @PathVariable String id) throws Exception {
  95. if (StringUtils.isEmpty(sysRole.getName())) {
  96. return ResponseBean.error("角色名称不能为空");
  97. }
  98. SysRole exists = sysRoleService.getByButNot("name", sysRole.getName(), "role_id", id, false);
  99. if (null != exists) {
  100. return ResponseBean.error("角色名称已存在");
  101. }
  102. sysRole.setRoleId(id);
  103. sysRoleService.updateById(sysRole);
  104. return ResponseBean.success(sysRole);
  105. }
  106. /**
  107. * 通过主键删除数据
  108. *
  109. * @param roleId 主键
  110. * @return 是否成功
  111. */
  112. @ApiOperation("通过主键删除数据")
  113. @DeleteMapping("/sysRole/{id}")
  114. public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id) {
  115. sysRoleService.removeById(id);
  116. sysUserRoleService.removeByMap(new HashMap<String, Object>() {{
  117. put("role_id", id);
  118. }});
  119. return ResponseBean.success("success");
  120. }
  121. }