TransferApplyController.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.free.controller;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.HashMap;
  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.free.config.CustomizationException;
  19. import com.free.config.ExceptionEnum;
  20. import com.free.config.ResponseFormat;
  21. import com.free.entity.TransferApply;
  22. import com.free.mq.MqService;
  23. import com.free.service.TransferApplyService;
  24. import io.swagger.annotations.Api;
  25. import io.swagger.annotations.ApiOperation;
  26. @RestController
  27. @RequestMapping("/ta")
  28. @Api(tags = "转人工申请")
  29. public class TransferApplyController {
  30. @Autowired
  31. private TransferApplyService service;
  32. @Autowired
  33. private MqService mqService;
  34. /** 创建数据 */
  35. @ApiOperation("创建数据")
  36. @PostMapping("")
  37. public Object save(@RequestBody @Valid TransferApply data) {
  38. this.service.save(data);
  39. QueryWrapper qw = new QueryWrapper<>();
  40. qw.eq("id", data.getId());
  41. Map returnData = this.service.getMap(qw);
  42. // 创建完数据后需要通过 mq 给管理员提示
  43. mqService.sendMsgToAdminForApplyCreate(returnData);
  44. return ResponseFormat.success(returnData);
  45. // return ResponseFormat.success();
  46. }
  47. /** 修改数据 */
  48. @ApiOperation("修改数据")
  49. @PostMapping("/{id}")
  50. public Object update(@PathVariable long id, @RequestBody TransferApply data) {
  51. QueryWrapper qw = new QueryWrapper<>();
  52. qw.eq("id", id);
  53. Long num = this.service.count(qw);
  54. if (num <= 0) {
  55. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  56. }
  57. data.setId(id);
  58. this.service.updateById(data);
  59. Object newData = this.service.getById(id);
  60. return ResponseFormat.success(newData);
  61. }
  62. /** 列表查询 */
  63. @ApiOperation("查询数据列表")
  64. @SuppressWarnings({ "unchecked" })
  65. @GetMapping()
  66. public Object list(@RequestParam Map<String, Object> allParams) {
  67. Long skip = null, limit = null;
  68. Map map = new HashMap();
  69. QueryWrapper qw = new QueryWrapper<>();
  70. /** 参数处理处理 */
  71. for (String key : allParams.keySet()) {
  72. Object value = allParams.get(key);
  73. if (key.equals("skip")) {
  74. skip = Long.valueOf(String.valueOf(value));
  75. } else if (key.equals("limit")) {
  76. limit = Long.valueOf(String.valueOf(value));
  77. } else {
  78. if (key.equals("title") || key.equals("platform")) {
  79. qw.like(key, value);
  80. } else {
  81. // 其他为查询条件
  82. qw.eq(key, value);
  83. }
  84. }
  85. }
  86. /** 分页处理 */
  87. if (null != skip && null != limit) {
  88. IPage page = new Page<>(skip, limit);
  89. IPage pageResult = service.page(page, qw);
  90. List data = pageResult.getRecords();
  91. long total = pageResult.getTotal();
  92. map.put("data", data);
  93. map.put("total", total);
  94. } else {
  95. List list = service.list(qw);
  96. map.put("data", list);
  97. }
  98. return ResponseFormat.success(map);
  99. }
  100. /** 根据id查询 */
  101. @ApiOperation("查询数据")
  102. @GetMapping("/{id}")
  103. public Object fetch(@PathVariable long 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 long 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. }