TestController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.ozs.web.controller.tool;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8. import com.alibaba.fastjson.JSON;
  9. import com.fasterxml.jackson.annotation.JsonFormat;
  10. import com.ozs.common.config.PurchaseConfig;
  11. import com.ozs.common.utils.file.FileUploadUtils;
  12. import com.ozs.common.utils.file.FileUtils;
  13. import com.ozs.common.vo.EsMessage;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.PutMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import com.ozs.common.core.controller.BaseController;
  23. import com.ozs.common.core.domain.R;
  24. import com.ozs.common.utils.StringUtils;
  25. import io.swagger.annotations.Api;
  26. import io.swagger.annotations.ApiImplicitParam;
  27. import io.swagger.annotations.ApiImplicitParams;
  28. import io.swagger.annotations.ApiModel;
  29. import io.swagger.annotations.ApiModelProperty;
  30. import io.swagger.annotations.ApiOperation;
  31. import org.springframework.web.multipart.MultipartFile;
  32. /**
  33. * swagger 用户测试方法
  34. *
  35. * @author ruoyi
  36. */
  37. @Api("用户信息管理")
  38. @RestController
  39. @RequestMapping("/test/user")
  40. public class TestController extends BaseController {
  41. private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
  42. {
  43. users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
  44. users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
  45. }
  46. @ApiOperation("获取用户列表")
  47. @GetMapping("/list")
  48. public R<List<UserEntity>> userList() {
  49. List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
  50. return R.ok(userList);
  51. }
  52. @ApiOperation("获取用户详细")
  53. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
  54. @GetMapping("/{userId}")
  55. public R<UserEntity> getUser(@PathVariable Integer userId) {
  56. if (!users.isEmpty() && users.containsKey(userId)) {
  57. return R.ok(users.get(userId));
  58. } else {
  59. return R.fail("用户不存在");
  60. }
  61. }
  62. @ApiOperation("新增用户")
  63. @ApiImplicitParams({
  64. @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
  65. @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
  66. @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
  67. @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
  68. })
  69. @PostMapping("/save")
  70. public R<String> save(UserEntity user) {
  71. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
  72. return R.fail("用户ID不能为空");
  73. }
  74. users.put(user.getUserId(), user);
  75. return R.ok();
  76. }
  77. @ApiOperation("更新用户")
  78. @PutMapping("/update")
  79. public R<String> update(@RequestBody UserEntity user) {
  80. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
  81. return R.fail("用户ID不能为空");
  82. }
  83. if (users.isEmpty() || !users.containsKey(user.getUserId())) {
  84. return R.fail("用户不存在");
  85. }
  86. users.remove(user.getUserId());
  87. users.put(user.getUserId(), user);
  88. return R.ok();
  89. }
  90. @ApiOperation("删除用户信息")
  91. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
  92. @DeleteMapping("/{userId}")
  93. public R<String> delete(@PathVariable Integer userId) {
  94. if (!users.isEmpty() && users.containsKey(userId)) {
  95. users.remove(userId);
  96. return R.ok();
  97. } else {
  98. return R.fail("用户不存在");
  99. }
  100. }
  101. @ApiOperation("测试PDF上传")
  102. @ApiImplicitParam(name = "file", value = "文件", required = true, dataTypeClass = MultipartFile.class)
  103. @PostMapping("/pdfUpload")
  104. public R<String> pdfUpload(MultipartFile file) {
  105. try {
  106. // 上传到服务器,返回一个服务器硬盘地址
  107. String upload = FileUploadUtils.uploadPdfToEs(file,"id");
  108. return R.ok(upload);
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112. return R.fail();
  113. }
  114. @ApiOperation("测试PDF内容搜索")
  115. @ApiImplicitParam(name = "str", value = "字符串", required = true, dataTypeClass = String.class)
  116. @PostMapping("/pdfData")
  117. public R<List> pdfData(String str) {
  118. try {
  119. List<EsMessage> maps = FileUtils.eSearch(str);
  120. List<String> ids = maps.stream().map(EsMessage::getId).collect(Collectors.toList());
  121. return R.ok(maps);
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return R.fail();
  126. }
  127. }
  128. @ApiModel(value = "UserEntity", description = "用户实体")
  129. class UserEntity {
  130. @ApiModelProperty("用户ID")
  131. private Integer userId;
  132. @ApiModelProperty("用户名称")
  133. private String username;
  134. @ApiModelProperty("用户密码")
  135. private String password;
  136. @ApiModelProperty("用户手机")
  137. private String mobile;
  138. public UserEntity() {
  139. }
  140. public UserEntity(Integer userId, String username, String password, String mobile) {
  141. this.userId = userId;
  142. this.username = username;
  143. this.password = password;
  144. this.mobile = mobile;
  145. }
  146. public Integer getUserId() {
  147. return userId;
  148. }
  149. public void setUserId(Integer userId) {
  150. this.userId = userId;
  151. }
  152. public String getUsername() {
  153. return username;
  154. }
  155. public void setUsername(String username) {
  156. this.username = username;
  157. }
  158. public String getPassword() {
  159. return password;
  160. }
  161. public void setPassword(String password) {
  162. this.password = password;
  163. }
  164. public String getMobile() {
  165. return mobile;
  166. }
  167. public void setMobile(String mobile) {
  168. this.mobile = mobile;
  169. }
  170. }