MetaSwitchController.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.MetaSwitch;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.exception.BusinessException;
  5. import com.izouma.nineth.repo.MetaSwitchRepo;
  6. import com.izouma.nineth.service.MetaSwitchService;
  7. import com.izouma.nineth.utils.ObjUtils;
  8. import com.izouma.nineth.utils.excel.ExcelUtils;
  9. import lombok.AllArgsConstructor;
  10. import org.springframework.data.domain.Page;
  11. import org.springframework.web.bind.annotation.*;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import java.util.Objects;
  16. @RestController
  17. @RequestMapping("/metaSwitch")
  18. @AllArgsConstructor
  19. public class MetaSwitchController extends BaseController {
  20. private MetaSwitchService metaSwitchService;
  21. private MetaSwitchRepo metaSwitchRepo;
  22. //@PreAuthorize("hasRole('ADMIN')")
  23. @PostMapping("/save")
  24. public MetaSwitch save(@RequestBody MetaSwitch record) {
  25. MetaSwitch metaSwitch = metaSwitchRepo.findByNameAndDel(record.getName(), false);
  26. if (Objects.nonNull(metaSwitch) && !Objects.equals(metaSwitch.getId(), record.getId())) {
  27. throw new BusinessException("该配置名称已经存在!");
  28. }
  29. if (record.getId() != null) {
  30. MetaSwitch orig = metaSwitchRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  31. ObjUtils.merge(orig, record);
  32. return metaSwitchRepo.save(orig);
  33. }
  34. return metaSwitchRepo.save(record);
  35. }
  36. //@PreAuthorize("hasRole('ADMIN')")
  37. @PostMapping("/all")
  38. public Page<MetaSwitch> all(@RequestBody PageQuery pageQuery) {
  39. return metaSwitchService.all(pageQuery);
  40. }
  41. @GetMapping("/get/{id}")
  42. public MetaSwitch get(@PathVariable Long id) {
  43. return metaSwitchRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  44. }
  45. @PostMapping("/del/{id}")
  46. public void del(@PathVariable Long id) {
  47. metaSwitchRepo.softDelete(id);
  48. }
  49. @PostMapping("/updateStatus/{id}/{status}")
  50. public void updateStatus(@PathVariable Long id, @PathVariable boolean status) {
  51. metaSwitchRepo.updateStatus(id, status);
  52. }
  53. @GetMapping("/excel")
  54. @ResponseBody
  55. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  56. List<MetaSwitch> data = all(pageQuery).getContent();
  57. ExcelUtils.export(response, data);
  58. }
  59. }