package com.izouma.tcg.web.card; import com.izouma.tcg.domain.User; import com.izouma.tcg.domain.card.CardCase; import com.izouma.tcg.repo.UserRepo; import com.izouma.tcg.web.BaseController; import com.izouma.tcg.domain.card.CardBox; import com.izouma.tcg.service.card.CardBoxService; import com.izouma.tcg.dto.PageQuery; import com.izouma.tcg.exception.BusinessException; import com.izouma.tcg.repo.card.CardBoxRepo; import com.izouma.tcg.utils.ObjUtils; import com.izouma.tcg.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/cardBox") @AllArgsConstructor public class CardBoxController extends BaseController { private final CardBoxService cardBoxService; private final CardBoxRepo cardBoxRepo; private final UserRepo userRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public CardBox save(@RequestBody CardBox record) { if (record.getId() != null) { CardBox orig = cardBoxRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return cardBoxRepo.save(orig); } return cardBoxRepo.save(record); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return cardBoxService.all(pageQuery); } @GetMapping("/get/{id}") public CardBox get(@PathVariable Long id) { return cardBoxRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { cardBoxRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @GetMapping("/checkCardBox") public void checkBox(Long id) { CardBox cardBox = cardBoxRepo.findById(id).orElseThrow(new BusinessException("无信息")); if (cardBox.getUserId() != null) { throw new BusinessException("该卡包已经被预定,取消或退款前无法更改"); } } @PostMapping("/changeName") public void changeName(Long id, String name) { cardBoxService.changeCardBoxBindName(id, name); } @GetMapping("/getBindName/{id}") public Map getName(@PathVariable Long id) { CardBox cardBox = cardBoxRepo.findById(id).orElseThrow(new BusinessException("无记录")); Map result = new HashMap<>(); if (cardBox.getUserId() != null) { User user = userRepo.findById(cardBox.getUserId()).orElseThrow(new BusinessException("未找到用户")); result.put("bindName", user.getNickname()); } if (cardBox.getBindName() != null) { result.put("bindName", cardBox.getBindName()); } if (cardBox.getOrderInfoId() != null) { result.put("orderInfoId", cardBox.getOrderInfoId()); } return result; } @GetMapping("/test") public void test() { List all = cardBoxRepo.findAll(); all.forEach(cardBox -> { if (cardBox.getUserId() != null & cardBox.getOrderInfoId() != null) { cardBox.setSold(true); cardBoxRepo.save(cardBox); } }); } }