AssetController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.izouma.nineth.web;
  2. import cn.licoy.encryptbody.annotation.encrypt.EncryptBody;
  3. import cn.licoy.encryptbody.enums.EncryptBodyMethod;
  4. import com.fasterxml.jackson.annotation.JsonView;
  5. import com.izouma.nineth.TokenHistory;
  6. import com.izouma.nineth.domain.Asset;
  7. import com.izouma.nineth.domain.DomainOrder;
  8. import com.izouma.nineth.domain.GiftOrder;
  9. import com.izouma.nineth.dto.*;
  10. import com.izouma.nineth.enums.CollectionType;
  11. import com.izouma.nineth.enums.OperationSource;
  12. import com.izouma.nineth.enums.OrderStatus;
  13. import com.izouma.nineth.exception.BusinessException;
  14. import com.izouma.nineth.repo.AssetRepo;
  15. import com.izouma.nineth.repo.CollectionRepo;
  16. import com.izouma.nineth.repo.DomainOrderRepo;
  17. import com.izouma.nineth.repo.OrderRepo;
  18. import com.izouma.nineth.service.*;
  19. import com.izouma.nineth.utils.SecurityUtils;
  20. import com.izouma.nineth.utils.excel.ExcelUtils;
  21. import io.swagger.annotations.ApiOperation;
  22. import lombok.AllArgsConstructor;
  23. import org.springframework.cache.annotation.Cacheable;
  24. import org.springframework.data.domain.Page;
  25. import org.springframework.data.domain.PageImpl;
  26. import org.springframework.data.domain.Pageable;
  27. import org.springframework.security.access.prepost.PreAuthorize;
  28. import org.springframework.web.bind.annotation.*;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.io.IOException;
  31. import java.math.BigDecimal;
  32. import java.time.Duration;
  33. import java.time.LocalDateTime;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Objects;
  38. import java.util.concurrent.ExecutionException;
  39. @RestController
  40. @RequestMapping("/asset")
  41. @AllArgsConstructor
  42. public class AssetController extends BaseController {
  43. private AssetService assetService;
  44. private AssetRepo assetRepo;
  45. private GiftOrderService giftOrderService;
  46. private OrderRepo orderRepo;
  47. private CacheService cacheService;
  48. private UserAssetSummaryService userAssetSummaryService;
  49. private CollectionRepo collectionRepo;
  50. private SysConfigService sysConfigService;
  51. private DomainOrderRepo domainOrderRepo;
  52. //@PreAuthorize("hasRole('ADMIN')")
  53. // @PostMapping("/save")
  54. // public Asset save(@RequestBody Asset record) {
  55. // if (record.getId() != null) {
  56. // Asset orig = assetRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  57. // ObjUtils.merge(orig, record);
  58. // return assetRepo.save(orig);
  59. // }
  60. // return assetRepo.save(record);
  61. // }
  62. //@PreAuthorize("hasRole('ADMIN')")
  63. @PostMapping("/all")
  64. @JsonView(Asset.View.Basic.class)
  65. public Page<Asset> all(@RequestBody PageQuery pageQuery) {
  66. pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
  67. pageQuery.getQuery().putIfAbsent("companyId", 1);
  68. Page<Asset> origin = assetService.all(pageQuery);
  69. List<Asset> content = origin.getContent();
  70. List<Asset> newResult = new ArrayList<>();
  71. content.forEach(asset -> {
  72. if (asset.getType().equals(CollectionType.DOMAIN)) {
  73. String domainName = asset.getName().substring(9);
  74. DomainOrder domainOrder = domainOrderRepo
  75. .findFirstByDomainNameAndOrderStatus(domainName, OrderStatus.FINISH);
  76. if (domainOrder != null)
  77. asset.setEndTime(domainOrder.getEndTime());
  78. }
  79. newResult.add(asset);
  80. });
  81. return new PageImpl<>(newResult, origin.getPageable(), origin.getTotalElements());
  82. }
  83. /**
  84. * 资产查询,除未开启盲盒prefixName相同叠加数量
  85. *
  86. * @param pageQuery 查询条件
  87. * @return 资产叠加后结果
  88. */
  89. @PostMapping({"/userSummary", "/superimposition"})
  90. public List<AssetDTO> userSummary(@RequestBody PageQuery pageQuery) {
  91. pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId());
  92. return assetService.userSummary(pageQuery);
  93. }
  94. @GetMapping("/get/{id}")
  95. @JsonView(Asset.View.Basic.class)
  96. public Asset get(@PathVariable Long id) {
  97. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  98. if (asset.getType().equals(CollectionType.DOMAIN)) {
  99. String domainName = asset.getName().substring(9);
  100. DomainOrder domainOrder = domainOrderRepo
  101. .findFirstByDomainNameAndOrderStatus(domainName, OrderStatus.FINISH);
  102. asset.setEndTime(domainOrder.getEndTime());
  103. }
  104. // orderRepo.findByIdAndDelFalse(asset.getOrderId()).ifPresent(order -> asset.setOpened(order.isOpened()));
  105. return asset;
  106. }
  107. // @PostMapping("/del/{id}")
  108. // public void del(@PathVariable Long id) {
  109. // assetRepo.softDelete(id);
  110. // }
  111. @PreAuthorize("hasRole('ADMIN')")
  112. @GetMapping("/excel")
  113. @ResponseBody
  114. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  115. List<Asset> data = all(pageQuery).getContent();
  116. ExcelUtils.export(response, data);
  117. }
  118. @PostMapping("/publicShow")
  119. @ApiOperation("公开展示")
  120. public void publicShow(@RequestParam Long id) {
  121. assetService.publicShow(id);
  122. }
  123. @PostMapping("/cancelPublic")
  124. @ApiOperation("取消展示")
  125. public void cancelPublic(@RequestParam Long id) {
  126. assetService.cancelPublic(id);
  127. }
  128. @PostMapping("/usePrivilege")
  129. @ApiOperation("使用特权")
  130. public void usePrivilege(@RequestParam Long assetId, @RequestParam Long privilegeId) {
  131. assetService.usePrivilege(assetId, privilegeId);
  132. }
  133. @PostMapping("/consignment")
  134. @ApiOperation("寄售")
  135. public void consignment(@RequestParam Long id, @RequestParam BigDecimal price, @RequestParam String tradeCode,
  136. @RequestParam(defaultValue = "false") boolean safeFlag) {
  137. assetService.consignment(id, price, tradeCode, safeFlag);
  138. }
  139. @PostMapping("/cancelConsignment")
  140. @ApiOperation("取消寄售")
  141. public void cancelConsignment(@RequestParam Long id) {
  142. assetService.cancelConsignment(id);
  143. }
  144. @PostMapping("/gift")
  145. @ApiOperation("转赠")
  146. public GiftOrder gift(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) {
  147. return giftOrderService.gift(SecurityUtils.getAuthenticatedUser().getId(), assetId, toUserId, tradeCode);
  148. }
  149. @PostMapping("/giftWithoutGasFee")
  150. @ApiOperation("转赠(无gas费)")
  151. public GiftOrder giftWithoutGasFee(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) {
  152. return giftOrderService.giftWithoutGasFee(SecurityUtils.getAuthenticatedUser()
  153. .getId(), assetId, toUserId, tradeCode);
  154. }
  155. @GetMapping("/tokenHistory")
  156. @ApiOperation("交易历史")
  157. @EncryptBody(EncryptBodyMethod.AES)
  158. public List<TokenHistory> tokenHistory(@RequestParam(required = false) String tokenId, @RequestParam(required = false) Long assetId) {
  159. return assetService.tokenHistory(tokenId, assetId);
  160. }
  161. @GetMapping("/userHistory")
  162. @ApiOperation("交易历史")
  163. public Page<UserHistory> userHistory(PageQuery pageQuery) {
  164. return assetService.userHistory(SecurityUtils.getAuthenticatedUser().getId(), pageQuery);
  165. }
  166. @GetMapping("/mint")
  167. public String mint(@RequestParam(required = false) LocalDateTime time) {
  168. return assetService.mint(time);
  169. }
  170. @GetMapping("/breakdown")
  171. @ApiOperation("收支总计")
  172. public Map<String, BigDecimal> breakdown() {
  173. return assetService.breakdown(SecurityUtils.getAuthenticatedUser().getId());
  174. }
  175. @PostMapping("/cdn")
  176. @PreAuthorize("hasRole('ADMIN')")
  177. public String cdn() throws ExecutionException, InterruptedException {
  178. new Thread(() -> {
  179. try {
  180. assetService.transferCDN();
  181. } catch (ExecutionException | InterruptedException e) {
  182. e.printStackTrace();
  183. }
  184. }).start();
  185. return "ok";
  186. }
  187. @GetMapping("/assetsForMint")
  188. @JsonView(Asset.View.Basic.class)
  189. public Page<Asset> assetsForMint(@RequestParam Long mintActivityId, @RequestParam(defaultValue = "1") Long companyId, Boolean refresh, Pageable pageable) {
  190. if (Objects.equals(refresh, true)) {
  191. cacheService.clearFmaa();
  192. }
  193. return assetService.findMintActivityAssetsWrap(SecurityUtils.getAuthenticatedUser().getId(),
  194. mintActivityId, companyId, pageable).toPage();
  195. }
  196. @GetMapping("/byTag")
  197. public Page<Asset> byTag(@RequestParam Long tagId, Pageable pageable) {
  198. return assetRepo.byTag(SecurityUtils.getAuthenticatedUser().getId(), tagId, pageable);
  199. }
  200. @ApiOperation("销毁")
  201. @PostMapping("/destroy")
  202. public void destroy(@RequestParam Long id, @RequestParam String tradeCode, @RequestParam OperationSource source) {
  203. assetService.destroy(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode, source);
  204. }
  205. @ApiOperation("元宇宙销毁")
  206. @PostMapping("/metaDestroy")
  207. public void metaDestroy(@RequestBody MetaDestroyParam metaDestroyParam) {
  208. assetService.metaDestroyWithoutTradeCode(metaDestroyParam, SecurityUtils.getAuthenticatedUser()
  209. .getId(), OperationSource.META);
  210. }
  211. @ApiOperation("开盲盒")
  212. @PostMapping("/open")
  213. public void open(@RequestParam Long id) {
  214. if (SecurityUtils.getAuthenticatedUser().getId() == null) {
  215. return;
  216. }
  217. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无盲盒"));
  218. if (!SecurityUtils.getAuthenticatedUser().getId().equals(asset.getUserId())) {
  219. return;
  220. }
  221. if (!asset.isOpened() && CollectionType.BLIND_BOX.equals(asset.getType())) {
  222. asset.setOpened(true);
  223. assetRepo.save(asset);
  224. }
  225. }
  226. @PostMapping("/getRoyalties")
  227. public double getRoyalties(@RequestParam Long id) {
  228. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  229. if (asset.getType().equals(CollectionType.DOMAIN)) {
  230. return 0;
  231. }
  232. return assetService.getRoyalties(asset.getMinterId(), asset.getRoyalties(), SecurityUtils.getAuthenticatedUser()
  233. .getId());
  234. }
  235. @PostMapping("/getServicecharge")
  236. public double getServicecharge(@RequestParam Long id) {
  237. Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  238. if (asset.getType().equals(CollectionType.DOMAIN)) {
  239. return assetService.getDomainServiceCharge(SecurityUtils.getAuthenticatedUser().getId());
  240. }
  241. return assetService.getServicecharge(asset.getServiceCharge(), SecurityUtils.getAuthenticatedUser()
  242. .getId());
  243. }
  244. @GetMapping("/hcChain")
  245. public void hcChain() throws ExecutionException, InterruptedException {
  246. assetService.hcChain();
  247. }
  248. @PostMapping("/lockAsset")
  249. public void lockAsset(@RequestParam Long assetId, @RequestParam Duration duration) {
  250. assetService.lockAsset(SecurityUtils.getAuthenticatedUser().getId(), assetId, duration);
  251. }
  252. @GetMapping("/recal")
  253. public void recal(@RequestParam Long userId) {
  254. userAssetSummaryService.calculateNum(null, userId, 1L);
  255. }
  256. @GetMapping("/topTen")
  257. @Cacheable(value = "transactionTopTen")
  258. public List<TransactionTopTenDTO> transactionTopTen() {
  259. return assetService.transactionTopTen();
  260. }
  261. @GetMapping("/queryFu")
  262. public List<FuAssetDTO> queryFu() {
  263. return assetService.queryFu();
  264. }
  265. @PostMapping("/batchCancelConsignment")
  266. @PreAuthorize("hasRole('ADMIN')")
  267. @ApiOperation("批量下架")
  268. public void cancelConsignment(@RequestParam String search) {
  269. assetService.batchCancelConsignment(search);
  270. }
  271. @PreAuthorize("hasRole('ADMIN')")
  272. @PostMapping("/openTrade")
  273. public void openTrade(@RequestParam String name) {
  274. assetRepo.openTrade(name);
  275. collectionRepo.openTrade(name);
  276. }
  277. }