RoleController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if(key.equals("name")){
  64. qw.like(key,value);
  65. } else {
  66. // 其他为查询条件
  67. qw.eq(key, value);
  68. }
  69. }
  70. }
  71. /** 分页处理 */
  72. if (null != skip && null != limit) {
  73. IPage page = new Page<>(skip, limit);
  74. IPage pageResult = service.page(page, qw);
  75. List data = pageResult.getRecords();
  76. long total = pageResult.getTotal();
  77. map.put("data", data);
  78. map.put("total", total);
  79. } else {
  80. List list = service.list(qw);
  81. map.put("data", list);
  82. }
  83. return ResponseFormat.success(map);
  84. }
  85. /** 修改数据 */
  86. @ApiOperation("修改数据")
  87. @PostMapping("/{id}")
  88. public Object update(@PathVariable String id, @RequestBody Role data) {
  89. QueryWrapper qw = new QueryWrapper<>();
  90. qw.eq("id", id);
  91. Long num = this.service.count(qw);
  92. if (num <= 0) {
  93. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  94. }
  95. data.setId(id);
  96. this.service.updateById(data);
  97. Object newData = this.service.getById(id);
  98. return ResponseFormat.success(newData);
  99. }
  100. /** 根据id查询 */
  101. @ApiOperation("查询数据")
  102. @GetMapping("/{id}")
  103. public Object fetch(@PathVariable String id) {
  104. Object newData = service.getById(id);
  105. return ResponseFormat.success(newData);
  106. }
  107. /** 根据id删除数据 */
  108. @ApiOperation("删除数据")
  109. @DeleteMapping("/{id}")
  110. public Object delete(@PathVariable String id) {
  111. QueryWrapper qw = new QueryWrapper<>();
  112. qw.eq("id", id);
  113. Long num = service.count(qw);
  114. if (num <= 0) {
  115. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  116. }
  117. service.removeById(id);
  118. return ResponseFormat.success();
  119. }
  120. @PassToken
  121. @ApiOperation("初始化角色")
  122. @PostMapping("/initData")
  123. public Object initData() throws JsonProcessingException {
  124. String filePath = "db/role.json";
  125. ObjectMapper mapper = new ObjectMapper();
  126. String str = Utils.readJsonFile(filePath);
  127. CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
  128. List<Map> list = mapper.readValue(str, javaType);
  129. this.service.initData(list);
  130. return ResponseFormat.success();
  131. }
  132. }