CouponController.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.Coupon;
  3. import com.izouma.nineth.dto.GeneralDTO;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.CollectionRepo;
  7. import com.izouma.nineth.repo.CouponRepo;
  8. import com.izouma.nineth.service.CouponService;
  9. import com.izouma.nineth.utils.ObjUtils;
  10. import com.izouma.nineth.utils.excel.ExcelUtils;
  11. import lombok.AllArgsConstructor;
  12. import org.apache.commons.collections.CollectionUtils;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  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.stream.Collectors;
  20. @RestController
  21. @RequestMapping("/coupon")
  22. @AllArgsConstructor
  23. public class CouponController extends BaseController {
  24. private CouponService couponService;
  25. private CouponRepo couponRepo;
  26. private CollectionRepo collectionRepo;
  27. @PreAuthorize("hasAnyRole('ADMIN','SAAS')")
  28. @PostMapping("/save")
  29. public Coupon save(@RequestBody Coupon record) {
  30. if (record.getId() != null) {
  31. Coupon orig = couponRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  32. ObjUtils.merge(orig, record);
  33. return couponRepo.save(orig);
  34. }
  35. return couponRepo.save(record);
  36. }
  37. //@PreAuthorize("hasRole('ADMIN')")
  38. @PostMapping("/all")
  39. public Page<Coupon> all(@RequestBody PageQuery pageQuery) {
  40. return couponService.all(pageQuery);
  41. }
  42. @GetMapping("/get/{id}")
  43. public Coupon get(@PathVariable Long id) {
  44. Coupon coupon = couponRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  45. if (CollectionUtils.isNotEmpty(coupon.getCollectionIds())) {
  46. List<GeneralDTO> dtos = collectionRepo.findAllByIdIn(coupon.getCollectionIds())
  47. .stream()
  48. .map(collection -> {
  49. GeneralDTO dto = new GeneralDTO();
  50. dto.setId(collection.getId());
  51. dto.setName(collection.getName());
  52. return dto;
  53. })
  54. .collect(Collectors.toList());
  55. coupon.setCollections(dtos);
  56. }
  57. return coupon;
  58. }
  59. @PostMapping("/del/{id}")
  60. public void del(@PathVariable Long id) {
  61. couponRepo.softDelete(id);
  62. }
  63. @GetMapping("/excel")
  64. @ResponseBody
  65. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  66. List<Coupon> data = all(pageQuery).getContent();
  67. ExcelUtils.export(response, data);
  68. }
  69. }