UserController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.free.controller;
  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.free.annotation.PassToken;
  19. import com.free.config.CustomizationException;
  20. import com.free.config.ExceptionEnum;
  21. import com.free.config.ResponseFormat;
  22. import com.free.dto.UserGetInfoDTO;
  23. import com.free.entity.User;
  24. import com.free.service.UserService;
  25. import com.free.utils.JwtUtil;
  26. import io.swagger.annotations.Api;
  27. import io.swagger.annotations.ApiOperation;
  28. @RestController
  29. @RequestMapping("/user")
  30. @Api(tags = "使用用户")
  31. public class UserController {
  32. @Autowired
  33. private UserService service;
  34. @PassToken
  35. @ApiOperation("用户进入智能客服获取信息")
  36. @PostMapping("/getInfo")
  37. public Object getInfo(@RequestBody UserGetInfoDTO body) {
  38. QueryWrapper qw = new QueryWrapper<>();
  39. qw.eq("uid", body.getUid());
  40. Map user = service.getMap(qw);
  41. if (null == user) {
  42. // 用户首次登录,需要获取用户信息
  43. User newUser = new User();
  44. newUser.setUid(body.getUid());
  45. // TODO:获取用户信息并填充,缺字段再补
  46. newUser.setName("游客");
  47. service.save(newUser);
  48. // 再将新创建的用户赋值回user中,后面统一处理
  49. QueryWrapper nqw = new QueryWrapper<>();
  50. nqw.eq("id", newUser.getId());
  51. user = service.getMap(nqw);
  52. }
  53. // 将用户转换成token
  54. String code = JwtUtil.sign(user);
  55. return ResponseFormat.success(code);
  56. }
  57. /** 创建数据 */
  58. @ApiOperation("创建数据")
  59. @PostMapping("")
  60. public Object save(@RequestBody @Valid User data) {
  61. QueryWrapper oqw = new QueryWrapper<>();
  62. oqw.eq("uid", data.getUid());
  63. Long num = service.count(oqw);
  64. if(num>0) {
  65. throw new CustomizationException(ExceptionEnum.UID_EXIST);
  66. }
  67. this.service.save(data);
  68. QueryWrapper qw = new QueryWrapper<>();
  69. qw.eq("id", data.getId());
  70. Map returnData = this.service.getMap(qw);
  71. return ResponseFormat.success(returnData);
  72. // return ResponseFormat.success();
  73. }
  74. @ApiOperation("修改数据")
  75. @PostMapping("/{id}")
  76. public Object update(@PathVariable String id, @RequestBody User data) {
  77. QueryWrapper qw = new QueryWrapper<>();
  78. qw.eq("id", id);
  79. Long num = this.service.count(qw);
  80. if (num <= 0) {
  81. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  82. }
  83. // 需要清掉uid修改,不允许修改,要改就删了再加
  84. data.setUid(null);
  85. data.setId(id);
  86. this.service.updateById(data);
  87. Object newData = this.service.getById(id);
  88. return ResponseFormat.success(newData);
  89. }
  90. @ApiOperation("查询数据列表")
  91. @SuppressWarnings({ "unchecked" })
  92. @GetMapping()
  93. public Object list(@RequestParam Map<String, Object> allParams) {
  94. Long skip = null, limit = null;
  95. Map map = new HashMap();
  96. QueryWrapper qw = new QueryWrapper<>();
  97. /** 参数处理处理 */
  98. for (String key : allParams.keySet()) {
  99. Object value = allParams.get(key);
  100. if (key.equals("skip")) {
  101. skip = Long.valueOf(String.valueOf(value));
  102. } else if (key.equals("limit")) {
  103. limit = Long.valueOf(String.valueOf(value));
  104. } else {
  105. if (key.equals("name")) {
  106. qw.like(key, value);
  107. } else {
  108. // 其他为查询条件
  109. qw.eq(key, value);
  110. }
  111. }
  112. }
  113. /** 分页处理 */
  114. if (null != skip && null != limit) {
  115. IPage page = new Page<>(skip, limit);
  116. IPage pageResult = service.page(page, qw);
  117. List data = pageResult.getRecords();
  118. long total = pageResult.getTotal();
  119. map.put("data", data);
  120. map.put("total", total);
  121. } else {
  122. List list = service.list(qw);
  123. map.put("data", list);
  124. }
  125. return ResponseFormat.success(map);
  126. }
  127. @ApiOperation("查询数据")
  128. @GetMapping("/{id}")
  129. public Object fetch(@PathVariable String id) {
  130. Object newData = service.getById(id);
  131. return ResponseFormat.success(newData);
  132. }
  133. @ApiOperation("删除数据")
  134. @DeleteMapping("/{id}")
  135. public Object delete(@PathVariable String id) {
  136. QueryWrapper qw = new QueryWrapper<>();
  137. qw.eq("id", id);
  138. Long num = service.count(qw);
  139. if (num <= 0) {
  140. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  141. }
  142. service.removeById(id);
  143. return ResponseFormat.success();
  144. }
  145. }