RaexSnapshotController.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.izouma.nineth.web;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.izouma.nineth.config.MetaConstants;
  4. import com.izouma.nineth.domain.RaexSnapshot;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.dto.SnapshotDTO;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.RaexSnapshotRepo;
  9. import com.izouma.nineth.service.RaexSnapshotService;
  10. import com.izouma.nineth.utils.ObjUtils;
  11. import com.izouma.nineth.utils.excel.ExcelUtils;
  12. import lombok.AllArgsConstructor;
  13. import org.apache.commons.collections.CollectionUtils;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.web.bind.annotation.*;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.IOException;
  18. import java.util.List;
  19. import java.util.Map;
  20. @RestController
  21. @RequestMapping("/raexSnapshot")
  22. @AllArgsConstructor
  23. public class RaexSnapshotController extends BaseController {
  24. private RaexSnapshotService raexSnapshotService;
  25. private RaexSnapshotRepo raexSnapshotRepo;
  26. //@PreAuthorize("hasRole('ADMIN')")
  27. @PostMapping("/save")
  28. public RaexSnapshot save(@RequestBody RaexSnapshot record) {
  29. if (record.getId() != null) {
  30. RaexSnapshot orig = raexSnapshotRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  31. ObjUtils.merge(orig, record);
  32. return raexSnapshotRepo.save(orig);
  33. }
  34. return raexSnapshotRepo.save(record);
  35. }
  36. //@PreAuthorize("hasRole('ADMIN')")
  37. @PostMapping("/all")
  38. public Page<RaexSnapshot> all(@RequestBody PageQuery pageQuery) {
  39. return raexSnapshotService.all(pageQuery);
  40. }
  41. @GetMapping("/get/{id}")
  42. public RaexSnapshot get(@PathVariable Long id) {
  43. return raexSnapshotRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  44. }
  45. @PostMapping("/del/{id}")
  46. public void del(@PathVariable Long id) {
  47. raexSnapshotRepo.softDelete(id);
  48. }
  49. @GetMapping("/excel")
  50. @ResponseBody
  51. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  52. List<RaexSnapshot> data = all(pageQuery).getContent();
  53. ExcelUtils.export(response, data);
  54. }
  55. @GetMapping("/shot")
  56. @ResponseBody
  57. public void shot(HttpServletResponse response, String name, String time) throws IOException {
  58. List<Map<String, Object>> map = raexSnapshotRepo.shot(MetaConstants.LIKE.concat(name).concat(MetaConstants.LIKE), time);
  59. List<SnapshotDTO> snapshotDTOS = mapToList(map);
  60. ExcelUtils.export(response, snapshotDTOS);
  61. }
  62. private List<SnapshotDTO> mapToList(List<Map<String, Object>> map) {
  63. if (CollectionUtils.isEmpty(map)) {
  64. throw new BusinessException("无数据");
  65. }
  66. JSONArray jsonArray = new JSONArray();
  67. jsonArray.addAll(map);
  68. return jsonArray.toJavaList(SnapshotDTO.class);
  69. }
  70. }