AssetController.java 8.1 KB

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