RoleController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.free.controller.system;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.validation.Valid;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  16. import com.baomidou.mybatisplus.core.metadata.IPage;
  17. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  18. import com.fasterxml.jackson.core.JsonProcessingException;
  19. import com.fasterxml.jackson.databind.ObjectMapper;
  20. import com.fasterxml.jackson.databind.type.CollectionType;
  21. import com.free.annotation.PassToken;
  22. import com.free.config.CustomizationException;
  23. import com.free.config.ExceptionEnum;
  24. import com.free.config.ResponseFormat;
  25. import com.free.entity.system.Role;
  26. import com.free.service.system.RoleService;
  27. import com.free.utils.Utils;
  28. import io.swagger.annotations.Api;
  29. import io.swagger.annotations.ApiOperation;
  30. @RestController
  31. @RequestMapping("/role")
  32. @Api(tags = "角色")
  33. public class RoleController {
  34. @Autowired
  35. private RoleService service;
  36. /** 创建数据 */
  37. @ApiOperation("创建数据")
  38. @PostMapping("")
  39. public Object save(@RequestBody @Valid Role data) {
  40. this.service.save(data);
  41. QueryWrapper qw = new QueryWrapper<>();
  42. qw.eq("id", data.getId());
  43. Map returnData = this.service.getMap(qw);
  44. return ResponseFormat.success(returnData);
  45. // return ResponseFormat.success();
  46. }
  47. /** 列表查询 */
  48. @ApiOperation("查询数据列表")
  49. @SuppressWarnings({ "unchecked" })
  50. @GetMapping()
  51. public Object list(@RequestParam Map<String, Object> allParams) {
  52. Long skip = null, limit = null;
  53. Map map = new HashMap();
  54. QueryWrapper qw = new QueryWrapper<>();
  55. /** 参数处理处理 */
  56. for (String key : allParams.keySet()) {
  57. Object value = allParams.get(key);
  58. if (key.equals("skip")) {
  59. skip = Long.valueOf(String.valueOf(value));
  60. } else if (key.equals("limit")) {
  61. limit = Long.valueOf(String.valueOf(value));
  62. } else {
  63. // 其他为查询条件
  64. qw.eq(key, value);
  65. }
  66. }
  67. /** 分页处理 */
  68. if (null != skip && null != limit) {
  69. IPage page = new Page<>(skip, limit);
  70. IPage pageResult = service.page(page, qw);
  71. List data = pageResult.getRecords();
  72. long total = pageResult.getTotal();
  73. map.put("data", data);
  74. map.put("total", total);
  75. } else {
  76. List list = service.list(qw);
  77. map.put("data", list);
  78. }
  79. return ResponseFormat.success(map);
  80. }
  81. /** 修改数据 */
  82. @ApiOperation("修改数据")
  83. @PostMapping("/{id}")
  84. public Object update(@PathVariable long id, @RequestBody Role data) {
  85. QueryWrapper qw = new QueryWrapper<>();
  86. qw.eq("id", id);
  87. Long num = this.service.count(qw);
  88. if (num <= 0) {
  89. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  90. }
  91. data.setId(id);
  92. this.service.updateById(data);
  93. Object newData = this.service.getById(id);
  94. return ResponseFormat.success(newData);
  95. }
  96. /** 根据id查询 */
  97. @ApiOperation("查询数据")
  98. @GetMapping("/{id}")
  99. public Object fetch(@PathVariable long id) {
  100. Object newData = service.getById(id);
  101. return ResponseFormat.success(newData);
  102. }
  103. /** 根据id删除数据 */
  104. @ApiOperation("删除数据")
  105. @DeleteMapping("/{id}")
  106. public Object delete(@PathVariable long id) {
  107. QueryWrapper qw = new QueryWrapper<>();
  108. qw.eq("id", id);
  109. Long num = service.count(qw);
  110. if (num <= 0) {
  111. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  112. }
  113. service.removeById(id);
  114. return ResponseFormat.success();
  115. }
  116. @PassToken
  117. @ApiOperation("初始化角色")
  118. @PostMapping("/initData")
  119. public Object initData() throws JsonProcessingException {
  120. String filePath = "db/role.json";
  121. ObjectMapper mapper = new ObjectMapper();
  122. String str = Utils.readJsonFile(filePath);
  123. CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
  124. List<Map> list = mapper.readValue(str, javaType);
  125. this.service.initData(list);
  126. return ResponseFormat.success();
  127. }
  128. }