package com.izouma.nineth.web; import cn.licoy.encryptbody.annotation.encrypt.EncryptBody; import cn.licoy.encryptbody.enums.EncryptBodyMethod; import com.fasterxml.jackson.annotation.JsonView; import com.izouma.nineth.TokenHistory; import com.izouma.nineth.domain.Asset; import com.izouma.nineth.domain.DomainOrder; import com.izouma.nineth.domain.GiftOrder; import com.izouma.nineth.dto.*; import com.izouma.nineth.enums.AssetStatus; import com.izouma.nineth.enums.CollectionType; import com.izouma.nineth.enums.OperationSource; import com.izouma.nineth.enums.OrderStatus; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.AssetRepo; import com.izouma.nineth.repo.CollectionRepo; import com.izouma.nineth.repo.DomainOrderRepo; import com.izouma.nineth.repo.OrderRepo; import com.izouma.nineth.service.*; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigDecimal; import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ExecutionException; @RestController @RequestMapping("/asset") @AllArgsConstructor public class AssetController extends BaseController { private AssetService assetService; private AssetRepo assetRepo; private GiftOrderService giftOrderService; private OrderRepo orderRepo; private CacheService cacheService; private UserAssetSummaryService userAssetSummaryService; private CollectionRepo collectionRepo; private SysConfigService sysConfigService; private DomainOrderRepo domainOrderRepo; //@PreAuthorize("hasRole('ADMIN')") // @PostMapping("/save") // public Asset save(@RequestBody Asset record) { // if (record.getId() != null) { // Asset orig = assetRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); // ObjUtils.merge(orig, record); // return assetRepo.save(orig); // } // return assetRepo.save(record); // } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") @JsonView(Asset.View.Basic.class) public Page all(@RequestBody PageQuery pageQuery) { pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId()); pageQuery.getQuery().putIfAbsent("companyId", 1); Page origin = assetService.all(pageQuery); List content = origin.getContent(); List newResult = new ArrayList<>(); content.forEach(asset -> { if (asset.getType().equals(CollectionType.DOMAIN)) { String domainName = asset.getName().substring(9); DomainOrder domainOrder = domainOrderRepo .findFirstByDomainNameAndOrderStatus(domainName, OrderStatus.FINISH); if (domainOrder != null) asset.setEndTime(domainOrder.getEndTime()); } newResult.add(asset); }); return new PageImpl<>(newResult, origin.getPageable(), origin.getTotalElements()); } /** * 资产查询,除未开启盲盒prefixName相同叠加数量 * * @param pageQuery 查询条件 * @return 资产叠加后结果 */ @PostMapping({"/userSummary", "/superimposition"}) public List userSummary(@RequestBody PageQuery pageQuery) { pageQuery.getQuery().put("userId", SecurityUtils.getAuthenticatedUser().getId()); return assetService.userSummary(pageQuery); } @GetMapping("/get/{id}") @JsonView(Asset.View.Basic.class) public Asset get(@PathVariable Long id) { Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录")); if (!asset.isPublicShow() && (SecurityUtils.getAuthenticatedUser() == null || !asset.getUserId().equals(SecurityUtils.getAuthenticatedUser().getId()))) { throw new BusinessException("无记录"); } if (asset.getType().equals(CollectionType.DOMAIN)) { String domainName = asset.getName().substring(9); DomainOrder domainOrder = domainOrderRepo .findFirstByDomainNameAndOrderStatus(domainName, OrderStatus.FINISH); asset.setEndTime(domainOrder.getEndTime()); } // orderRepo.findByIdAndDelFalse(asset.getOrderId()).ifPresent(order -> asset.setOpened(order.isOpened())); return asset; } // @PostMapping("/del/{id}") // public void del(@PathVariable Long id) { // assetRepo.softDelete(id); // } @PreAuthorize("hasRole('ADMIN')") @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @PostMapping("/publicShow") @ApiOperation("公开展示") public void publicShow(@RequestParam Long id) { assetService.publicShow(id); } @PostMapping("/cancelPublic") @ApiOperation("取消展示") public void cancelPublic(@RequestParam Long id) { assetService.cancelPublic(id); } @PostMapping("/usePrivilege") @ApiOperation("使用特权") public void usePrivilege(@RequestParam Long assetId, @RequestParam Long privilegeId) { assetService.usePrivilege(assetId, privilegeId); } @PostMapping("/consignment") @ApiOperation("寄售") public void consignment(@RequestParam Long id, @RequestParam BigDecimal price, @RequestParam String tradeCode, @RequestParam(defaultValue = "false") boolean safeFlag) { assetService.consignment(id, price, tradeCode, safeFlag); } @PostMapping("/cancelConsignment") @ApiOperation("取消寄售") public void cancelConsignment(@RequestParam Long id) { assetService.cancelConsignment(id); } @PostMapping("/gift") @ApiOperation("转赠") public GiftOrder gift(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) { return giftOrderService.gift(SecurityUtils.getAuthenticatedUser().getId(), assetId, toUserId, tradeCode); } @PostMapping("/giftWithoutGasFee") @ApiOperation("转赠(无gas费)") public GiftOrder giftWithoutGasFee(@RequestParam Long assetId, @RequestParam Long toUserId, @RequestParam String tradeCode) { return giftOrderService.giftWithoutGasFee(SecurityUtils.getAuthenticatedUser() .getId(), assetId, toUserId, tradeCode); } @GetMapping("/tokenHistory") @ApiOperation("交易历史") @EncryptBody(EncryptBodyMethod.AES) public List tokenHistory(@RequestParam(required = false) String tokenId, @RequestParam(required = false) Long assetId) { return assetService.tokenHistory(tokenId, assetId); } @GetMapping("/userHistory") @ApiOperation("交易历史") public Page userHistory(PageQuery pageQuery) { return assetService.userHistory(SecurityUtils.getAuthenticatedUser().getId(), pageQuery); } @GetMapping("/mint") public String mint(@RequestParam(required = false) LocalDateTime time) { return assetService.mint(time); } @GetMapping("/breakdown") @ApiOperation("收支总计") public Map breakdown() { return assetService.breakdown(SecurityUtils.getAuthenticatedUser().getId()); } @PostMapping("/cdn") @PreAuthorize("hasRole('ADMIN')") public String cdn() throws ExecutionException, InterruptedException { new Thread(() -> { try { assetService.transferCDN(); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } }).start(); return "ok"; } @GetMapping("/assetsForMint") @JsonView(Asset.View.Basic.class) public Page assetsForMint(@RequestParam Long mintActivityId, @RequestParam(defaultValue = "1") Long companyId, Boolean refresh, Pageable pageable) { if (Objects.equals(refresh, true)) { cacheService.clearFmaa(); } return assetService.findMintActivityAssetsWrap(SecurityUtils.getAuthenticatedUser().getId(), mintActivityId, companyId, pageable).toPage(); } @GetMapping("/byTag") public Page byTag(@RequestParam Long tagId, Pageable pageable) { return assetRepo.byTag(SecurityUtils.getAuthenticatedUser().getId(), tagId, pageable); } @ApiOperation("销毁") @PostMapping("/destroy") public void destroy(@RequestParam Long id, @RequestParam String tradeCode, @RequestParam OperationSource source) { assetService.destroy(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode, source); } @ApiOperation("元宇宙销毁") @PostMapping("/metaDestroy") public void metaDestroy(@RequestBody MetaDestroyParam metaDestroyParam) { assetService.metaDestroyWithoutTradeCode(metaDestroyParam, SecurityUtils.getAuthenticatedUser() .getId(), OperationSource.META); } @ApiOperation("开盲盒") @PostMapping("/open") public void open(@RequestParam Long id) { if (SecurityUtils.getAuthenticatedUser().getId() == null) { return; } Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无盲盒")); if (!SecurityUtils.getAuthenticatedUser().getId().equals(asset.getUserId())) { return; } if (!asset.isOpened() && CollectionType.BLIND_BOX.equals(asset.getType())) { asset.setOpened(true); assetRepo.save(asset); } } @PostMapping("/getRoyalties") public double getRoyalties(@RequestParam Long id) { Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录")); if (asset.getType().equals(CollectionType.DOMAIN)) { return 0; } return assetService.getRoyalties(asset.getMinterId(), asset.getRoyalties(), SecurityUtils.getAuthenticatedUser() .getId()); } @PostMapping("/getServicecharge") public double getServicecharge(@RequestParam Long id) { Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录")); if (asset.getType().equals(CollectionType.DOMAIN)) { return assetService.getDomainServiceCharge(SecurityUtils.getAuthenticatedUser().getId()); } return assetService.getServicecharge(asset.getServiceCharge(), SecurityUtils.getAuthenticatedUser() .getId()); } @GetMapping("/hcChain") public void hcChain() throws ExecutionException, InterruptedException { assetService.hcChain(); } @PostMapping("/lockAsset") public void lockAsset(@RequestParam Long assetId, @RequestParam Duration duration) { assetService.lockAsset(SecurityUtils.getAuthenticatedUser().getId(), assetId, duration); } @GetMapping("/recal") public void recal(@RequestParam Long userId) { userAssetSummaryService.calculateNum(null, userId, 1L); } @GetMapping("/topTen") @Cacheable(value = "transactionTopTen") public List transactionTopTen() { return assetService.transactionTopTen(); } @GetMapping("/queryFu") public List queryFu() { return assetService.queryFu(); } @PostMapping("/batchCancelConsignment") @PreAuthorize("hasRole('ADMIN')") @ApiOperation("批量下架") public void cancelConsignment(@RequestParam String search) { assetService.batchCancelConsignment(search); } @PreAuthorize("hasRole('ADMIN')") @PostMapping("/openTrade") public void openTrade(@RequestParam String name) { assetRepo.openTrade(name); collectionRepo.openTrade(name); } @GetMapping("/getId/{name}") public MetaRestResult getId(@PathVariable String name) { Asset asset = assetRepo.findByNameAndStatusAndCategoryAndDel("RID元宇宙域名 ".concat(name) .concat(".nft"), AssetStatus.NORMAL, "元域名", false); if (Objects.isNull(asset)) { return MetaRestResult.returnError("该域名不存在"); } return MetaRestResult.returnSuccess("查询成功", asset.getUserId()); } }