SettingController.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.izouma.nineth.web;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.izouma.nineth.domain.Setting;
  4. import com.izouma.nineth.service.CacheService;
  5. import com.izouma.nineth.service.SettingService;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.SettingRepo;
  9. import com.izouma.nineth.utils.ObjUtils;
  10. import com.izouma.nineth.utils.excel.ExcelUtils;
  11. import lombok.AllArgsConstructor;
  12. import org.springframework.cache.annotation.CachePut;
  13. import org.springframework.cache.annotation.Cacheable;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19. import java.util.List;
  20. @RestController
  21. @RequestMapping("/setting")
  22. @AllArgsConstructor
  23. public class SettingController extends BaseController {
  24. private SettingService settingService;
  25. private SettingRepo settingRepo;
  26. private CacheService cacheService;
  27. @PreAuthorize("hasRole('ADMIN')")
  28. @PostMapping("/save")
  29. public Setting save(@RequestBody Setting record) {
  30. if (record.getId() != null) {
  31. Setting orig = settingRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  32. ObjUtils.merge(orig, record);
  33. orig = settingRepo.save(orig);
  34. cacheService.clearSettingList();
  35. return orig;
  36. }
  37. if (record.getParent() == null) {
  38. record.setFlag(settingRepo.nextSort());
  39. }
  40. record = settingRepo.save(record);
  41. cacheService.clearSettingList();
  42. return record;
  43. }
  44. //@PreAuthorize("hasRole('ADMIN')")
  45. @PostMapping("/all")
  46. public Page<Setting> all(@RequestBody PageQuery pageQuery) {
  47. return settingService.all(pageQuery);
  48. }
  49. @GetMapping("/get/{id}")
  50. public Setting get(@PathVariable Long id) {
  51. return settingRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  52. }
  53. @PreAuthorize("hasRole('ADMIN')")
  54. @PostMapping("/del/{id}")
  55. public void del(@PathVariable Long id) {
  56. settingRepo.softDelete(id);
  57. }
  58. @PreAuthorize("hasRole('ADMIN')")
  59. @GetMapping("/excel")
  60. @ResponseBody
  61. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  62. List<Setting> data = all(pageQuery).getContent();
  63. ExcelUtils.export(response, data);
  64. }
  65. @PostMapping("/allList")
  66. public List<Setting> allList() {
  67. return settingService.getTree(settingRepo.findAll());
  68. }
  69. @Cacheable(value = "settingList", key = "#flag")
  70. @PostMapping("/byFlag")
  71. public List<Setting> byFlag(int flag) {
  72. List<Setting> tree = settingService.getTree(settingRepo.findAllByFlag(flag));
  73. if (CollUtil.isEmpty(tree)) {
  74. return null;
  75. }
  76. return tree.get(0).getChildren();
  77. }
  78. }