CardBoxController.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.izouma.tcg.web.card;
  2. import com.izouma.tcg.domain.User;
  3. import com.izouma.tcg.domain.card.CardCase;
  4. import com.izouma.tcg.repo.UserRepo;
  5. import com.izouma.tcg.web.BaseController;
  6. import com.izouma.tcg.domain.card.CardBox;
  7. import com.izouma.tcg.service.card.CardBoxService;
  8. import com.izouma.tcg.dto.PageQuery;
  9. import com.izouma.tcg.exception.BusinessException;
  10. import com.izouma.tcg.repo.card.CardBoxRepo;
  11. import com.izouma.tcg.utils.ObjUtils;
  12. import com.izouma.tcg.utils.excel.ExcelUtils;
  13. import lombok.AllArgsConstructor;
  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.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. @RestController
  23. @RequestMapping("/cardBox")
  24. @AllArgsConstructor
  25. public class CardBoxController extends BaseController {
  26. private final CardBoxService cardBoxService;
  27. private final CardBoxRepo cardBoxRepo;
  28. private final UserRepo userRepo;
  29. //@PreAuthorize("hasRole('ADMIN')")
  30. @PostMapping("/save")
  31. public CardBox save(@RequestBody CardBox record) {
  32. if (record.getId() != null) {
  33. CardBox orig = cardBoxRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  34. ObjUtils.merge(orig, record);
  35. return cardBoxRepo.save(orig);
  36. }
  37. return cardBoxRepo.save(record);
  38. }
  39. //@PreAuthorize("hasRole('ADMIN')")
  40. @PostMapping("/all")
  41. public Page<CardBox> all(@RequestBody PageQuery pageQuery) {
  42. return cardBoxService.all(pageQuery);
  43. }
  44. @GetMapping("/get/{id}")
  45. public CardBox get(@PathVariable Long id) {
  46. return cardBoxRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  47. }
  48. @PostMapping("/del/{id}")
  49. public void del(@PathVariable Long id) {
  50. cardBoxRepo.softDelete(id);
  51. }
  52. @GetMapping("/excel")
  53. @ResponseBody
  54. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  55. List<CardBox> data = all(pageQuery).getContent();
  56. ExcelUtils.export(response, data);
  57. }
  58. @GetMapping("/checkCardBox")
  59. public void checkBox(Long id) {
  60. CardBox cardBox = cardBoxRepo.findById(id).orElseThrow(new BusinessException("无信息"));
  61. if (cardBox.getUserId() != null) {
  62. throw new BusinessException("该卡包已经被预定,取消或退款前无法更改");
  63. }
  64. }
  65. @PostMapping("/changeName")
  66. public void changeName(Long id, String name) {
  67. cardBoxService.changeCardBoxBindName(id, name);
  68. }
  69. @GetMapping("/getBindName/{id}")
  70. public Map<String, Object> getName(@PathVariable Long id) {
  71. CardBox cardBox = cardBoxRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  72. Map<String, Object> result = new HashMap<>();
  73. if (cardBox.getUserId() != null) {
  74. User user = userRepo.findById(cardBox.getUserId()).orElseThrow(new BusinessException("未找到用户"));
  75. result.put("bindName", user.getNickname());
  76. }
  77. if (cardBox.getBindName() != null) {
  78. result.put("bindName", cardBox.getBindName());
  79. }
  80. if (cardBox.getOrderInfoId() != null) {
  81. result.put("orderInfoId", cardBox.getOrderInfoId());
  82. }
  83. return result;
  84. }
  85. @GetMapping("/test")
  86. public void test() {
  87. List<CardBox> all = cardBoxRepo.findAll();
  88. all.forEach(cardBox -> {
  89. if (cardBox.getUserId() != null & cardBox.getOrderInfoId() != null) {
  90. cardBox.setSold(true);
  91. cardBoxRepo.save(cardBox);
  92. }
  93. });
  94. }
  95. }