AssetController.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.izouma.nineth.web;
  2. import com.fasterxml.jackson.annotation.JsonView;
  3. import com.izouma.nineth.TokenHistory;
  4. import com.izouma.nineth.domain.Asset;
  5. import com.izouma.nineth.domain.GiftOrder;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.dto.UserHistory;
  8. import com.izouma.nineth.enums.CollectionType;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.repo.AssetRepo;
  11. import com.izouma.nineth.repo.OrderRepo;
  12. import com.izouma.nineth.service.AssetService;
  13. import com.izouma.nineth.service.CacheService;
  14. import com.izouma.nineth.service.GiftOrderService;
  15. import com.izouma.nineth.utils.SecurityUtils;
  16. import com.izouma.nineth.utils.excel.ExcelUtils;
  17. import io.swagger.annotations.ApiOperation;
  18. import lombok.AllArgsConstructor;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.data.domain.Pageable;
  21. import org.springframework.security.access.prepost.PreAuthorize;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.IOException;
  25. import java.math.BigDecimal;
  26. import java.time.LocalDateTime;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Objects;
  30. import java.util.concurrent.ExecutionException;
  31. @RestController
  32. @RequestMapping("/asset")
  33. @AllArgsConstructor
  34. public class AssetController extends BaseController {
  35. private AssetService assetService;
  36. private AssetRepo assetRepo;
  37. private GiftOrderService giftOrderService;
  38. private OrderRepo orderRepo;
  39. private CacheService cacheService;
  40. //@PreAuthorize("hasRole('ADMIN')")
  41. // @PostMapping("/save")
  42. // public Asset save(@RequestBody Asset record) {
  43. // if (record.getId() != null) {
  44. // Asset orig = assetRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  45. // ObjUtils.merge(orig, record);
  46. // return assetRepo.save(orig);
  47. // }
  48. // return assetRepo.save(record);
  49. // }
  50. //@PreAuthorize("hasRole('ADMIN')")
  51. @PostMapping("/all")
  52. @JsonView(Asset.View.Basic.class)
  53. public Page<Asset> all(@RequestBody PageQuery pageQuery) {
  54. pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
  55. return assetService.all(pageQuery);
  56. }
  57. @GetMapping("/get/{id}")
  58. @JsonView(Asset.View.Basic.class)
  59. public Asset get(@PathVariable Long id) {
  60. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  61. // orderRepo.findByIdAndDelFalse(asset.getOrderId()).ifPresent(order -> asset.setOpened(order.isOpened()));
  62. return asset;
  63. }
  64. // @PostMapping("/del/{id}")
  65. // public void del(@PathVariable Long id) {
  66. // assetRepo.softDelete(id);
  67. // }
  68. @PreAuthorize("hasRole('ADMIN')")
  69. @GetMapping("/excel")
  70. @ResponseBody
  71. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  72. List<Asset> data = all(pageQuery).getContent();
  73. ExcelUtils.export(response, data);
  74. }
  75. @PostMapping("/publicShow")
  76. @ApiOperation("公开展示")
  77. public void publicShow(@RequestParam Long id) {
  78. assetService.publicShow(id);
  79. }
  80. @PostMapping("/cancelPublic")
  81. @ApiOperation("取消展示")
  82. public void cancelPublic(@RequestParam Long id) {
  83. assetService.cancelPublic(id);
  84. }
  85. @PostMapping("/usePrivilege")
  86. @ApiOperation("使用特权")
  87. public void usePrivilege(@RequestParam Long assetId, @RequestParam Long privilegeId) {
  88. assetService.usePrivilege(assetId, privilegeId);
  89. }
  90. @PostMapping("/consignment")
  91. @ApiOperation("寄售")
  92. public void consignment(@RequestParam Long id, @RequestParam BigDecimal price, @RequestParam String tradeCode) {
  93. assetService.consignment(id, price, tradeCode);
  94. }
  95. @PostMapping("/cancelConsignment")
  96. @ApiOperation("取消寄售")
  97. public void cancelConsignment(@RequestParam Long id) {
  98. assetService.cancelConsignment(id);
  99. }
  100. @PostMapping("/gift")
  101. @ApiOperation("转赠")
  102. public GiftOrder gift(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) {
  103. return giftOrderService.gift(SecurityUtils.getAuthenticatedUser().getId(), assetId, toUserId, tradeCode);
  104. }
  105. @PostMapping("/giftWithoutGasFee")
  106. @ApiOperation("转赠(无gas费)")
  107. public GiftOrder giftWithoutGasFee(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) {
  108. return giftOrderService.giftWithoutGasFee(SecurityUtils.getAuthenticatedUser()
  109. .getId(), assetId, toUserId, tradeCode);
  110. }
  111. @GetMapping("/tokenHistory")
  112. @ApiOperation("交易历史")
  113. public List<TokenHistory> tokenHistory(@RequestParam(required = false) String tokenId, @RequestParam(required = false) Long assetId) {
  114. return assetService.tokenHistory(tokenId, assetId);
  115. }
  116. @GetMapping("/userHistory")
  117. @ApiOperation("交易历史")
  118. public Page<UserHistory> userHistory(PageQuery pageQuery) {
  119. return assetService.userHistory(SecurityUtils.getAuthenticatedUser().getId(), pageQuery);
  120. }
  121. @GetMapping("/mint")
  122. public String mint(@RequestParam(required = false) LocalDateTime time) {
  123. return assetService.mint(time);
  124. }
  125. @GetMapping("/breakdown")
  126. @ApiOperation("收支总计")
  127. public Map<String, BigDecimal> breakdown() {
  128. return assetService.breakdown(SecurityUtils.getAuthenticatedUser().getId());
  129. }
  130. @PostMapping("/cdn")
  131. @PreAuthorize("hasRole('ADMIN')")
  132. public String cdn() throws ExecutionException, InterruptedException {
  133. new Thread(() -> {
  134. try {
  135. assetService.transferCDN();
  136. } catch (ExecutionException | InterruptedException e) {
  137. e.printStackTrace();
  138. }
  139. }).start();
  140. return "ok";
  141. }
  142. @GetMapping("/assetsForMint")
  143. @JsonView(Asset.View.Basic.class)
  144. public Page<Asset> assetsForMint(@RequestParam Long mintActivityId, Boolean refresh, Pageable pageable) {
  145. if (Objects.equals(refresh, true)) {
  146. cacheService.clearFmaa();
  147. }
  148. return assetService.findMintActivityAssetsWrap(SecurityUtils.getAuthenticatedUser().getId(),
  149. mintActivityId, pageable).toPage();
  150. }
  151. @GetMapping("/byTag")
  152. public Page<Asset> byTag(@RequestParam Long tagId, Pageable pageable) {
  153. return assetRepo.byTag(SecurityUtils.getAuthenticatedUser().getId(), tagId, pageable);
  154. }
  155. @ApiOperation("销毁")
  156. @PostMapping("/destroy")
  157. public void destroy(@RequestParam Long id) {
  158. assetService.destroy(id, SecurityUtils.getAuthenticatedUser().getId());
  159. }
  160. @ApiOperation("开盲盒")
  161. @PostMapping("/open")
  162. public void open(@RequestParam Long id) {
  163. if (SecurityUtils.getAuthenticatedUser().getId() == null) {
  164. return;
  165. }
  166. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无盲盒"));
  167. if (!SecurityUtils.getAuthenticatedUser().getId().equals(asset.getUserId())) {
  168. return;
  169. }
  170. if (!asset.isOpened() && CollectionType.BLIND_BOX.equals(asset.getType())) {
  171. asset.setOpened(true);
  172. assetRepo.save(asset);
  173. }
  174. }
  175. @PostMapping("/getRoyalties")
  176. public int getRoyalties(@RequestParam Long id) {
  177. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  178. return assetService.getRoyalties(asset.getMinterId(), asset.getRoyalties(), SecurityUtils.getAuthenticatedUser()
  179. .getId());
  180. }
  181. }