AssetController.java 8.7 KB

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