PolicyLawController.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.izouma.zhirongip.web.resource;
  2. import com.izouma.zhirongip.web.BaseController;
  3. import com.izouma.zhirongip.domain.resource.PolicyLaw;
  4. import com.izouma.zhirongip.service.resource.PolicyLawService;
  5. import com.izouma.zhirongip.dto.PageQuery;
  6. import com.izouma.zhirongip.exception.BusinessException;
  7. import com.izouma.zhirongip.repo.resource.PolicyLawRepo;
  8. import com.izouma.zhirongip.utils.ObjUtils;
  9. import com.izouma.zhirongip.utils.excel.ExcelUtils;
  10. import lombok.AllArgsConstructor;
  11. import org.springframework.data.domain.Page;
  12. import org.springframework.security.access.prepost.PreAuthorize;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.time.LocalDate;
  17. import java.util.List;
  18. @RestController
  19. @RequestMapping("/policyLaw")
  20. @AllArgsConstructor
  21. public class PolicyLawController extends BaseController {
  22. private final PolicyLawService policyLawService;
  23. private final PolicyLawRepo policyLawRepo;
  24. @PreAuthorize("hasRole('ADMIN')")
  25. @PostMapping("/save")
  26. public PolicyLaw save(@RequestBody PolicyLaw record) {
  27. if (record.getId() != null) {
  28. PolicyLaw orig = policyLawRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  29. ObjUtils.merge(orig, record);
  30. return policyLawRepo.save(orig);
  31. }
  32. PolicyLaw policyLaw = policyLawRepo.save(record);
  33. LocalDate issuedAt = policyLaw.getIssuedAt();
  34. int year = issuedAt.getYear();
  35. policyLaw.setYear(year);
  36. return policyLawRepo.save(record);
  37. }
  38. //@PreAuthorize("hasRole('ADMIN')")
  39. @PostMapping("/all")
  40. public Page<PolicyLaw> all(@RequestBody PageQuery pageQuery) {
  41. return policyLawService.all(pageQuery);
  42. }
  43. @GetMapping("/get/{id}")
  44. public PolicyLaw get(@PathVariable Long id) {
  45. return policyLawRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  46. }
  47. @PostMapping("/del/{id}")
  48. public void del(@PathVariable Long id) {
  49. policyLawRepo.softDelete(id);
  50. }
  51. @GetMapping("/excel")
  52. @ResponseBody
  53. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  54. List<PolicyLaw> data = all(pageQuery).getContent();
  55. ExcelUtils.export(response, data);
  56. }
  57. }