CouponController.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.web.bind.annotation.*;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.IOException;
  17. import java.util.List;
  18. import java.util.stream.Collectors;
  19. @RestController
  20. @RequestMapping("/coupon")
  21. @AllArgsConstructor
  22. public class CouponController extends BaseController {
  23. private CouponService couponService;
  24. private CouponRepo couponRepo;
  25. private CollectionRepo collectionRepo;
  26. //@PreAuthorize("hasRole('ADMIN')")
  27. @PostMapping("/save")
  28. public Coupon save(@RequestBody Coupon record) {
  29. if (record.getId() != null) {
  30. Coupon orig = couponRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  31. ObjUtils.merge(orig, record);
  32. return couponRepo.save(orig);
  33. }
  34. return couponRepo.save(record);
  35. }
  36. //@PreAuthorize("hasRole('ADMIN')")
  37. @PostMapping("/all")
  38. public Page<Coupon> all(@RequestBody PageQuery pageQuery) {
  39. return couponService.all(pageQuery);
  40. }
  41. @GetMapping("/get/{id}")
  42. public Coupon get(@PathVariable Long id) {
  43. Coupon coupon = couponRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  44. if (CollectionUtils.isNotEmpty(coupon.getCollectionIds())) {
  45. List<GeneralDTO> dtos = collectionRepo.findAllByIdIn(coupon.getCollectionIds())
  46. .stream()
  47. .map(collection -> {
  48. GeneralDTO dto = new GeneralDTO();
  49. dto.setId(collection.getId());
  50. dto.setName(collection.getName());
  51. return dto;
  52. })
  53. .collect(Collectors.toList());
  54. coupon.setCollections(dtos);
  55. }
  56. return coupon;
  57. }
  58. @PostMapping("/del/{id}")
  59. public void del(@PathVariable Long id) {
  60. couponRepo.softDelete(id);
  61. }
  62. @GetMapping("/excel")
  63. @ResponseBody
  64. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  65. List<Coupon> data = all(pageQuery).getContent();
  66. ExcelUtils.export(response, data);
  67. }
  68. }