MetaStoreController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.MetaStore;
  3. import com.izouma.nineth.domain.MetaUserProp;
  4. import com.izouma.nineth.dto.MetaRestResult;
  5. import com.izouma.nineth.dto.PageQuery;
  6. import com.izouma.nineth.enums.MetaPropUsedType;
  7. import com.izouma.nineth.enums.MetaStoreCommodityType;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.MetaStoreRepo;
  10. import com.izouma.nineth.repo.MetaUserPropRepo;
  11. import com.izouma.nineth.service.MetaStoreService;
  12. import com.izouma.nineth.utils.ObjUtils;
  13. import com.izouma.nineth.utils.SecurityUtils;
  14. import com.izouma.nineth.utils.excel.ExcelUtils;
  15. import lombok.AllArgsConstructor;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import java.util.Objects;
  23. @RestController
  24. @RequestMapping("/metaStore")
  25. @AllArgsConstructor
  26. public class MetaStoreController extends BaseController {
  27. private MetaStoreService metaStoreService;
  28. private MetaStoreRepo metaStoreRepo;
  29. private MetaUserPropRepo metaUserPropRepo;
  30. //@PreAuthorize("hasRole('ADMIN')")
  31. @PostMapping("/save")
  32. public MetaStore save(@RequestBody MetaStore record) {
  33. if (Objects.isNull(record.getId())) {
  34. if (MetaStoreCommodityType.META_PROP.equals(record.getCommodityType()) && !checkMetaProp(record.getMetaPropId())) {
  35. throw new BusinessException("商店中已经存在该道具");
  36. }
  37. if (MetaStoreCommodityType.NFT.equals(record.getCommodityType())) {
  38. MetaStore metaStore = metaStoreRepo.findByCommodityTypeAndNameAndDel(MetaStoreCommodityType.NFT, record.getName(), false);
  39. if (Objects.nonNull(metaStore)) {
  40. throw new BusinessException("商店中已经存在该NFT");
  41. }
  42. }
  43. return metaStoreRepo.save(record);
  44. }
  45. MetaStore orig = metaStoreRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  46. ObjUtils.merge(orig, record);
  47. return metaStoreRepo.save(orig);
  48. }
  49. @PostMapping("/{id}/onShelf")
  50. public void onShelf(@PathVariable Long id) {
  51. MetaStore metaStore = metaStoreRepo.findByIdAndDel(id, false);
  52. if (Objects.isNull(metaStore)) {
  53. throw new BusinessException("无记录");
  54. }
  55. if (metaStore.isOnShelf()) {
  56. throw new BusinessException("当前商品已经处于上架状态!");
  57. }
  58. metaStore.setOnShelf(true);
  59. metaStoreRepo.save(metaStore);
  60. }
  61. @PostMapping("/{id}/cancelOnShelf")
  62. public void cancelOnShelf(@PathVariable Long id) {
  63. MetaStore metaStore = metaStoreRepo.findByIdAndDel(id, false);
  64. if (Objects.isNull(metaStore)) {
  65. throw new BusinessException("无记录");
  66. }
  67. if (!metaStore.isOnShelf()) {
  68. throw new BusinessException("当前商品已经处于下架状态!");
  69. }
  70. metaStore.setOnShelf(false);
  71. metaStoreRepo.save(metaStore);
  72. }
  73. //@PreAuthorize("hasRole('ADMIN')")
  74. @PostMapping("/all")
  75. public Page<MetaStore> all(@RequestBody PageQuery pageQuery) {
  76. return metaStoreService.all(pageQuery);
  77. }
  78. @GetMapping("/get/{id}")
  79. public MetaStore get(@PathVariable Long id) {
  80. return metaStoreRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  81. }
  82. @PostMapping("/del/{id}")
  83. public void del(@PathVariable Long id) {
  84. metaStoreRepo.softDelete(id);
  85. }
  86. @GetMapping("/excel")
  87. @ResponseBody
  88. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  89. List<MetaStore> data = all(pageQuery).getContent();
  90. ExcelUtils.export(response, data);
  91. }
  92. @GetMapping("/{metaPropId}/checkMetaProp")
  93. public boolean checkMetaProp(@PathVariable Long metaPropId) {
  94. MetaStore metaStore = metaStoreRepo.findByCommodityTypeAndMetaPropIdAndDel(MetaStoreCommodityType.META_PROP, metaPropId, false);
  95. return Objects.isNull(metaStore);
  96. }
  97. @PostMapping("/{id}/purchase")
  98. public MetaRestResult<Void> purchase(@PathVariable Long id) {
  99. return metaStoreService.purchase(id);
  100. }
  101. @GetMapping("findByType")
  102. public MetaRestResult<List<MetaStore>> findByType(MetaStoreCommodityType commodityType) {
  103. List<MetaStore> metaStores = metaStoreRepo.findAllByCommodityTypeAndOnShelfAndDel(commodityType, true, false);
  104. if (CollectionUtils.isEmpty(metaStores)) {
  105. return MetaRestResult.returnSuccess(metaStores);
  106. }
  107. if (MetaStoreCommodityType.META_PROP.equals(commodityType)) {
  108. Long userId = SecurityUtils.getAuthenticatedUser().getId();
  109. metaStores.forEach(metaStore -> {
  110. MetaUserProp metaUserProp = metaUserPropRepo.findByUserIdAndMetaPropIdAndDel(userId, metaStore.getMetaPropId(), false);
  111. if (Objects.nonNull(metaUserProp) && MetaPropUsedType.PERMANENT.equals(metaUserProp.getUsedType()) && metaUserProp.getNum() >= 1) {
  112. metaStore.setCanPurchase(false);
  113. }
  114. });
  115. }
  116. return MetaRestResult.returnSuccess(metaStores);
  117. }
  118. }