TestController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.fdkanfang.web.backend;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import com.fdkanfang.common.constant.MsgCode;
  6. import com.fdkanfang.common.util.AliyunOssUtil;
  7. import com.fdkanfang.common.util.FileUtils;
  8. import com.fdkanfang.common.util.R;
  9. import com.fdkanfang.dao.backend.ResourceMapper;
  10. import com.fdkanfang.dao.backend.UserMapper;
  11. import com.fdkanfang.domain.backend.*;
  12. import com.fdkanfang.domain.dto.HouseDto;
  13. import com.fdkanfang.service.backend.HouseService2;
  14. import com.fdkanfang.service.backend.ImageService2;
  15. import com.fdkanfang.web.mq.config.RabbitConfig;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import lombok.extern.log4j.Log4j2;
  19. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.data.redis.core.RedisTemplate;
  22. import org.springframework.web.bind.annotation.*;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import springfox.documentation.annotations.ApiIgnore;
  25. import javax.validation.Valid;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.time.LocalDateTime;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. /**
  33. * Created by owen on 2020/2/18 0018 10:04
  34. */
  35. @ApiIgnore
  36. @Log4j2
  37. @Api(value = "TestController",tags = "测试")
  38. @RestController
  39. @RequestMapping("test1")
  40. public class TestController {
  41. @Autowired
  42. private UserMapper userMapper;
  43. @Autowired
  44. private ResourceMapper resourceMapper;
  45. @Autowired
  46. private ImageService2 imageService2;
  47. @Autowired
  48. private RabbitTemplate rabbitTemplate;
  49. @Autowired
  50. private HouseService2 houseService2;
  51. @Autowired
  52. private RedisTemplate<String, String> redisTemplate;
  53. // @Value("${image.path}")
  54. private String IMAGE_PATH;
  55. ///kanfang/test/aa.jpg
  56. private static String ossPath = "kanfang/house/";
  57. @GetMapping
  58. public String test1(){
  59. return LocalDateTime.now().toString();
  60. }
  61. @ApiOperation("valid")
  62. @PostMapping("valid")
  63. public R valid(@Valid @RequestBody HouseDto param){
  64. return new R(2000, param.toString());
  65. }
  66. @ApiOperation("mq map接受消息")
  67. @GetMapping("mq")
  68. public R mq(){
  69. HashMap<Object, Object> mqMsgMap = new HashMap<>();
  70. String s = new Date().toString();
  71. log.info("send: {}", s);
  72. mqMsgMap.put("ossUpload", s);
  73. log.info("ossUpload: {}", s);
  74. //发消息到mq
  75. rabbitTemplate.convertAndSend(RabbitConfig.VERTICAL_EXCHANGE, RabbitConfig.VERTICAL_QUEUE_ROUTING, mqMsgMap);
  76. return new R(2000, s);
  77. }
  78. @ApiOperation("保存sceneJson")
  79. @GetMapping("sceneJson/{houseId}")
  80. public R sceneJson(@PathVariable Long houseId){
  81. log.info("id: {}", houseId);
  82. HouseEntity house = houseService2.findById(houseId);
  83. if (house == null) {
  84. return new R(2000, "house为空");
  85. }
  86. String sceneJson = "{\"1-1_cc\": {\"door\": [], \"vertex\": [{\"x\": 0.379, \"y\": -1.2462, \"id\": 0}, {\"x\": 0.379, \"y\": 0.7272, \"id\": 1}, {\"x\": -2.1155, \"y\": 0.7272, \"id\": 2}, {\"x\": -2.1155, \"y\": -0.5785, \"id\": 3}, {\"x\": -0.0038, \"y\": -0.5785, \"id\": 4}, {\"x\": -0.0038, \"y\": -1.2462, \"id\": 5}], \"top\": 1.7748, \"bottom\": -1.6, \"column\": [], \"window\": [], \"wall\": [{\"p1\": 0, \"p2\": 1, \"id\": 0}, {\"p1\": 1, \"p2\": 2, \"id\": 1}, {\"p1\": 2, \"p2\": 3, \"id\": 2}, {\"p1\": 3, \"p2\": 4, \"id\": 3}, {\"p1\": 4, \"p2\": 5, \"id\": 4}, {\"p1\": 5, \"p2\": 0, \"id\": 5}], \"room\": [{\"cameras\": [\"0\"], \"showName\": true, \"subgroup\": 1, \"top\": 1.7748, \"bottom\": -1.6, \"name\": \"\\u623f\\u95f4-1\", \"points\": [0, 1, 2, 3, 4, 5], \"showArea\": true, \"group\": 1}]}, \"2-1_bb\": {\"door\": [], \"vertex\": [{\"x\": 0.2386, \"y\": -0.8504, \"id\": 6}, {\"x\": 0.2386, \"y\": 2.2796, \"id\": 7}, {\"x\": -2.7207, \"y\": 2.2796, \"id\": 8}, {\"x\": -2.7207, \"y\": -0.8504, \"id\": 9}], \"top\": 1.1707, \"bottom\": -1.6, \"column\": [], \"window\": [], \"wall\": [{\"p1\": 6, \"p2\": 7, \"id\": 6}, {\"p1\": 7, \"p2\": 8, \"id\": 7}, {\"p1\": 8, \"p2\": 9, \"id\": 8}, {\"p1\": 9, \"p2\": 6, \"id\": 9}], \"room\": [{\"cameras\": [\"0\"], \"showName\": true, \"subgroup\": 1, \"top\": 1.1707, \"bottom\": -1.6, \"name\": \"\\u623f\\u95f4-1\", \"points\": [6, 7, 8, 9], \"showArea\": true, \"group\": 1}]}}";
  87. house.setSceneJson(sceneJson);
  88. houseService2.update(house);
  89. return new R(2000, house.getId());
  90. }
  91. @GetMapping("mapper")
  92. public R userMapper(){
  93. List<UserEntity> users = userMapper.selectAll();
  94. return new R(2000, users);
  95. }
  96. @GetMapping("redis")
  97. public String redis(){
  98. redisTemplate.boundSetOps("owen1111").add("1","2","3");
  99. return new Date().toString();
  100. }
  101. @ApiOperation("测试参数为空时,查询是否会异常")
  102. @PostMapping("testParam")
  103. public R testParam(@RequestBody UserEntity param){
  104. // 不判断id也不会报错
  105. // User user = userService.findById(param.getId());
  106. return new R(2000, "");
  107. }
  108. @ApiOperation("测试example分页")
  109. @PostMapping("example")
  110. public R example(){
  111. UserEntity user = new UserEntity();
  112. user.setSex(1);
  113. // Page<User> pageAll = userService.findPageAll(Example.of(user));
  114. return new R(2000, "");
  115. }
  116. // @ApiOperation("用户注册/重置密码")
  117. // @GetMapping("table")
  118. // public R table(){
  119. // Map<String, Object> byUserName1 = userRepository.findByUserName1(Long.valueOf("9"));
  120. //
  121. // return new R(2000, byUserName1);
  122. // }
  123. @GetMapping("test")
  124. public String test(){
  125. return new Date() + "";
  126. }
  127. @ApiOperation("测试上传")
  128. @PostMapping(value = "upload", consumes = {"multipart/form-data"})
  129. public R testUpload(@RequestParam("files") MultipartFile[] files){
  130. // 处理上传图片
  131. if (files == null || files.length <= 0) {
  132. log.info("文件为空");
  133. return new R(MsgCode.e_COMMON_3100, MsgCode.msg_COMMON_3100);
  134. }
  135. // 只有新增是才保存图片,修改时另外处理
  136. // 创建目录路径
  137. String directoryTimeName = DateUtil.format(new DateTime(), "yyyyMMdd_HHmmss");
  138. String directoryName = IMAGE_PATH + directoryTimeName;
  139. FileUtil.mkdir(directoryName);
  140. // 准备数据,key:原文件路径 value:oss路径, oss会自动创建目录
  141. HashMap<String, String> imageMap = new HashMap<>();
  142. for (MultipartFile file : files) {
  143. ImageEntity image = new ImageEntity();
  144. String filename = file.getOriginalFilename();
  145. String filePath = directoryName + File.separator + filename;
  146. String ossImagePath = ossPath + directoryTimeName+ "/" + filename;
  147. image.setFileName(filename);
  148. image.setPath(ossImagePath);
  149. image.setLocalPath(filePath);
  150. try {
  151. FileUtils.downloanAndGetResolutionRate(file.getInputStream(), filePath ,false);
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. imageService2.save(image);
  156. imageMap.put(filePath, ossImagePath);
  157. }
  158. // 上传的到阿里云
  159. AliyunOssUtil.uploadMulFiles(imageMap);
  160. return new R(MsgCode.SUCCESS_CODE, MsgCode.msg_SUCCESS);
  161. }
  162. }