123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package com.example.civilizedcity.controller;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.example.civilizedcity.common.BaseController;
- import com.example.civilizedcity.common.Constants;
- import com.example.civilizedcity.common.ResponseBean;
-
- import java.time.LocalDateTime;
- import java.util.HashMap;
- import java.util.List;
-
- import com.example.civilizedcity.common.StringUtils;
- import com.example.civilizedcity.service.SysUserRoleService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.example.civilizedcity.entity.SysRole;
- import com.example.civilizedcity.service.SysRoleService;
-
- /**
- * 系统角色;(sys_role)表控制层
- *
- * @author : http://njyunzhi.com
- * @date : 2022-12-12
- */
- @Api(tags = "系统角色对象功能接口")
- @RestController
- @RequestMapping("/")
- public class SysRoleController extends BaseController {
-
- @Autowired
- private SysRoleService sysRoleService;
-
- @Autowired
- private SysUserRoleService sysUserRoleService;
-
- /**
- * 通过ID查询单条数据
- *
- * @param roleId 主键
- * @return 实例对象
- */
- @ApiOperation("通过ID查询单条数据")
- @GetMapping("/sysRole/{id}")
- public ResponseBean queryById(@ApiParam("对象ID") @PathVariable String id) throws Exception {
- return ResponseBean.success(sysRoleService.getById(id));
- }
-
- /**
- * 分页查询
- *
- * @param pageNum 当前页码
- * @param pageSize 每页条数
- * @return 查询结果
- */
- @ApiOperation("分页查询")
- @GetMapping("/sysRole")
- public ResponseBean list(@ApiParam("页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
- @ApiParam("单页数据量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
-
- IPage<SysRole> pg = new Page<>(pageNum, pageSize);
- QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
- // queryWrapper.gt("status", Constants.STATUS_DELETE);
- queryWrapper.orderByDesc("create_date");
- IPage<SysRole> result = sysRoleService.page(pg, queryWrapper);
-
- return ResponseBean.success(result);
- }
-
- /**
- * 新增数据
- *
- * @param sysRole 实例对象
- * @return 实例对象
- */
- @ApiOperation("新增数据")
- @PostMapping("/sysRole")
- public ResponseBean add(@ApiParam("对象实体") @RequestBody SysRole sysRole) throws Exception {
- sysRole.setRoleId(null);
-
- if (StringUtils.isEmpty(sysRole.getName())) {
- return ResponseBean.error("角色名称不能为空");
- }
-
- long i = sysRoleService.countBy("name", sysRole.getName(), false);
- if (i > 0) {
- return ResponseBean.error("角色名称已存在");
- }
-
- sysRole.setStatus(Constants.STATUS_NORMAL);
- sysRole.setCreateDate(LocalDateTime.now());
- sysRoleService.save(sysRole);
- return ResponseBean.success(sysRole);
- }
-
- /**
- * 更新数据
- *
- * @param sysRole 实例对象
- * @return 实例对象
- */
- @ApiOperation("更新数据")
- @PutMapping("/sysRole/{id}")
- public ResponseBean edit(@ApiParam("对象实体") @RequestBody SysRole sysRole,
- @ApiParam("对象ID") @PathVariable String id) throws Exception {
-
- if (StringUtils.isEmpty(sysRole.getName())) {
- return ResponseBean.error("角色名称不能为空");
- }
-
- SysRole exists = sysRoleService.getByButNot("name", sysRole.getName(), "role_id", id, false);
- if (null != exists) {
- return ResponseBean.error("角色名称已存在");
- }
-
- sysRole.setRoleId(id);
- sysRoleService.updateById(sysRole);
- return ResponseBean.success(sysRole);
- }
-
- /**
- * 通过主键删除数据
- *
- * @param roleId 主键
- * @return 是否成功
- */
- @ApiOperation("通过主键删除数据")
- @DeleteMapping("/sysRole/{id}")
- public ResponseBean deleteById(@ApiParam("对象ID") @PathVariable String id) {
- sysRoleService.removeById(id);
- sysUserRoleService.removeByMap(new HashMap<String, Object>() {{
- put("role_id", id);
- }});
- return ResponseBean.success("success");
- }
- }
|