SettingController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. @PostMapping("/del/{id}")
  54. public void del(@PathVariable Long id) {
  55. settingRepo.softDelete(id);
  56. }
  57. @GetMapping("/excel")
  58. @ResponseBody
  59. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  60. List<Setting> data = all(pageQuery).getContent();
  61. ExcelUtils.export(response, data);
  62. }
  63. @PostMapping("/allList")
  64. public List<Setting> allList() {
  65. return settingService.getTree(settingRepo.findAll());
  66. }
  67. @Cacheable(value = "settingList", key = "#flag")
  68. @PostMapping("/byFlag")
  69. public List<Setting> byFlag(int flag) {
  70. List<Setting> tree = settingService.getTree(settingRepo.findAllByFlag(flag));
  71. if (CollUtil.isEmpty(tree)) {
  72. return null;
  73. }
  74. return tree.get(0).getChildren();
  75. }
  76. }