FullReductionController.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.izouma.dingdong.web.merchant;
  2. import com.izouma.dingdong.repo.merchant.MerchantRepo;
  3. import com.izouma.dingdong.service.merchant.MerchantService;
  4. import com.izouma.dingdong.utils.SecurityUtils;
  5. import com.izouma.dingdong.web.BaseController;
  6. import com.izouma.dingdong.domain.merchant.FullReduction;
  7. import com.izouma.dingdong.service.merchant.FullReductionService;
  8. import com.izouma.dingdong.dto.PageQuery;
  9. import com.izouma.dingdong.exception.BusinessException;
  10. import com.izouma.dingdong.repo.merchant.FullReductionRepo;
  11. import com.izouma.dingdong.utils.ObjUtils;
  12. import com.izouma.dingdong.utils.excel.ExcelUtils;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.AllArgsConstructor;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.security.access.prepost.PreAuthorize;
  17. import org.springframework.web.bind.annotation.*;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.IOException;
  20. import java.util.List;
  21. @RestController
  22. @RequestMapping("/fullReduction")
  23. @AllArgsConstructor
  24. public class FullReductionController extends BaseController {
  25. private FullReductionService fullReductionService;
  26. private FullReductionRepo fullReductionRepo;
  27. private MerchantRepo merchantRepo;
  28. private MerchantService merchantService;
  29. //@PreAuthorize("hasRole('ADMIN')")
  30. @PostMapping("/save")
  31. public FullReduction save(@RequestBody FullReduction record) {
  32. if (record.getFullAmount().compareTo(record.getMinusAmount()) <= 0) {
  33. throw new BusinessException("减的金额不能大于等于满的金额");
  34. }
  35. if (record.getId() != null) {
  36. FullReduction orig = fullReductionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  37. ObjUtils.merge(orig, record);
  38. return fullReductionRepo.save(orig);
  39. }
  40. return fullReductionRepo.save(record);
  41. }
  42. //@PreAuthorize("hasRole('ADMIN')")
  43. @GetMapping("/all")
  44. public Page<FullReduction> all(PageQuery pageQuery) {
  45. return fullReductionRepo.findAll(toSpecification(pageQuery,FullReduction.class), toPageRequest(pageQuery));
  46. }
  47. @GetMapping("/get/{id}")
  48. public FullReduction get(@PathVariable Long id) {
  49. return fullReductionRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  50. }
  51. @PostMapping("/del/{id}")
  52. public void del(@PathVariable Long id) {
  53. fullReductionRepo.deleteById(id);
  54. }
  55. @GetMapping("/excel")
  56. @ResponseBody
  57. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  58. List<FullReduction> data = all(pageQuery).getContent();
  59. ExcelUtils.export(response, data);
  60. }
  61. @GetMapping("/my")
  62. @ApiOperation("商户下的满减")
  63. public List<FullReduction> my(){
  64. return fullReductionRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()));
  65. }
  66. }