AssetController.java 13 KB

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