diff --git a/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/RemoteClientWalletService.java b/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/RemoteClientWalletService.java new file mode 100644 index 000000000..c6b9c1a0a --- /dev/null +++ b/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/RemoteClientWalletService.java @@ -0,0 +1,16 @@ +package com.ruoyi.client; + +import com.ruoyi.client.domain.ClientUserWallet; +import com.ruoyi.common.core.constant.ServiceNameConstants; +import com.ruoyi.common.core.domain.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +@FeignClient(contextId = "remoteClientWalletService", value = ServiceNameConstants.CLIENT_SERVICE) +public interface RemoteClientWalletService { + + @PostMapping("/wallet/detail") + public R getUserWallet(@RequestBody ClientUserWallet clientUserWallet); + +} diff --git a/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/domain/ClientUserWallet.java b/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/domain/ClientUserWallet.java new file mode 100644 index 000000000..e6d7b5189 --- /dev/null +++ b/ruoyi-api/ruoyi-api-client/src/main/java/com/ruoyi/client/domain/ClientUserWallet.java @@ -0,0 +1,114 @@ +package com.ruoyi.client.domain; + +import com.ruoyi.common.core.annotation.Excel; +import com.ruoyi.common.core.web.domain.BaseEntity; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +import java.math.BigDecimal; + +/** + * 用户资产对象 client_user_wallet + * + * @author ruoyi + * @date 2023-08-29 + */ +public class ClientUserWallet extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 主键 */ + @Excel(name = "主键") + private Long id; + + /** 用户id */ + @Excel(name = "用户id") + private Long userId; + + /** 总金额 */ + @Excel(name = "总金额") + private BigDecimal totalAmount; + + /** 币种 */ + @Excel(name = "币种") + private String currency; + + /** 状态(normal/freeze) */ + @Excel(name = "状态(normal/freeze)") + private String status; + + /** 删除标识 */ + private Long delFlag; + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + public void setUserId(Long userId) + { + this.userId = userId; + } + + public Long getUserId() + { + return userId; + } + public void setTotalAmount(BigDecimal totalAmount) + { + this.totalAmount = totalAmount; + } + + public BigDecimal getTotalAmount() + { + return totalAmount; + } + public void setCurrency(String currency) + { + this.currency = currency; + } + + public String getCurrency() + { + return currency; + } + public void setStatus(String status) + { + this.status = status; + } + + public String getStatus() + { + return status; + } + public void setDelFlag(Long delFlag) + { + this.delFlag = delFlag; + } + + public Long getDelFlag() + { + return delFlag; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("userId", getUserId()) + .append("totalAmount", getTotalAmount()) + .append("currency", getCurrency()) + .append("status", getStatus()) + .append("remark", getRemark()) + .append("delFlag", getDelFlag()) + .append("createTime", getCreateTime()) + .append("createBy", getCreateBy()) + .append("updateTime", getUpdateTime()) + .append("updateBy", getUpdateBy()) + .toString(); + } +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/MarketConstant.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/MarketConstant.java index e022983cd..3b203cbfd 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/MarketConstant.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/MarketConstant.java @@ -28,4 +28,8 @@ public class MarketConstant { public static final Long HOT_PRODUCT = 1L; + public static final String SELL = "sell"; + + public static final String BUY = "buy"; + } diff --git a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/controller/ClientUserWalletController.java b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/controller/ClientUserWalletController.java index 7b1d60a57..f5faae460 100644 --- a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/controller/ClientUserWalletController.java +++ b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/controller/ClientUserWalletController.java @@ -3,6 +3,8 @@ import java.util.List; import java.io.IOException; import javax.servlet.http.HttpServletResponse; + +import com.ruoyi.common.core.domain.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -102,4 +104,15 @@ public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(clientUserWalletService.deleteClientUserWalletByIds(ids)); } + + /** + * 获取用户钱包数据 + * @param clientUserWallet + * @return + */ + @PostMapping("/detail") + public R getUserWallet(@RequestBody ClientUserWallet clientUserWallet) { + return R.ok(clientUserWalletService.getUserWallet(clientUserWallet.getUserId())); + } + } diff --git a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/IClientUserWalletService.java b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/IClientUserWalletService.java index 8404c033f..a53ba4efd 100644 --- a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/IClientUserWalletService.java +++ b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/IClientUserWalletService.java @@ -87,4 +87,11 @@ public interface IClientUserWalletService * @param bizOrderId */ public void balanceRollback(Long userId, String operator, String currency, Long bizOrderId); + + /** + * 获取用户钱包 + * @param userId + * @return + */ + public ClientUserWallet getUserWallet(Long userId); } diff --git a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/impl/ClientUserWalletServiceImpl.java b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/impl/ClientUserWalletServiceImpl.java index 47d97002a..1bc760c5a 100644 --- a/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/impl/ClientUserWalletServiceImpl.java +++ b/ruoyi-modules/ruoyi-client/src/main/java/com/ruoyi/client/service/impl/ClientUserWalletServiceImpl.java @@ -176,6 +176,11 @@ public void balanceRollback(Long userId, String operator, String currency, Long clientUserWalletFlowMapper.insertClientUserWalletFlow(clientUserWalletFlow); } + @Override + public ClientUserWallet getUserWallet(Long userId) { + return getUserWallet(userId, "USD"); + } + private ClientUserWallet getUserWallet(Long userId, String currency) { ClientUserWallet selectParams = new ClientUserWallet(); selectParams.setUserId(userId); diff --git a/ruoyi-modules/ruoyi-market/pom.xml b/ruoyi-modules/ruoyi-market/pom.xml index 2943b6f4f..6cea45ef7 100644 --- a/ruoyi-modules/ruoyi-market/pom.xml +++ b/ruoyi-modules/ruoyi-market/pom.xml @@ -81,6 +81,10 @@ selenium-java 4.11.0 + + com.ruoyi + ruoyi-api-client + diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/controller/client/MarketClientController.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/controller/client/MarketClientController.java index 240eb9f7f..d9ab2ecb7 100644 --- a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/controller/client/MarketClientController.java +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/controller/client/MarketClientController.java @@ -10,6 +10,7 @@ import com.ruoyi.market.crawler.core.model.ProductPriceCache; import com.ruoyi.market.domain.ProductCategory; import com.ruoyi.market.domain.ProductInfo; +import com.ruoyi.market.domain.req.ExchangeOrderCalculateReq; import com.ruoyi.market.domain.req.KLineQueryReq; import com.ruoyi.market.domain.req.ProductQueryReq; import com.ruoyi.market.domain.vo.ProductInfoVo; @@ -81,4 +82,9 @@ public AjaxResult productConfig(@RequestBody ProductQueryReq req) { return AjaxResult.success(marketClientService.selectProductConfig(req.getProductCode())); } + @PostMapping("/exchange/calculate") + public AjaxResult calculate(@RequestBody ExchangeOrderCalculateReq req) { + return AjaxResult.success(marketClientService.calculate(req)); + } + } diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/ProductConfig.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/ProductConfig.java index e51fb572c..915f4fe4c 100644 --- a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/ProductConfig.java +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/ProductConfig.java @@ -38,7 +38,7 @@ public class ProductConfig extends BaseEntity /** 每张数量 */ @Excel(name = "每张数量") - private Double eachSheetNum; + private BigDecimal eachSheetNum; /** 最小倍率 */ @Excel(name = "最小倍率") @@ -120,12 +120,12 @@ public BigDecimal getSpreadRate() { return spreadRate; } - public void setEachSheetNum(Double eachSheetNum) + public void setEachSheetNum(BigDecimal eachSheetNum) { this.eachSheetNum = eachSheetNum; } - public Double getEachSheetNum() + public BigDecimal getEachSheetNum() { return eachSheetNum; } diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/req/ExchangeOrderCalculateReq.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/req/ExchangeOrderCalculateReq.java new file mode 100644 index 000000000..cb83c4106 --- /dev/null +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/req/ExchangeOrderCalculateReq.java @@ -0,0 +1,23 @@ +package com.ruoyi.market.domain.req; + +import lombok.Data; +import lombok.ToString; + +import java.io.Serializable; +import java.math.BigDecimal; + +@Data +@ToString +public class ExchangeOrderCalculateReq implements Serializable { + + private String productCode; + + private String direct; + + private BigDecimal exchangePrice; + + private BigDecimal multiplier; + + private BigDecimal sheetNum; + +} diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ExchangeOrderCalculateVo.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ExchangeOrderCalculateVo.java new file mode 100644 index 000000000..e30710a96 --- /dev/null +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ExchangeOrderCalculateVo.java @@ -0,0 +1,19 @@ +package com.ruoyi.market.domain.vo; + +import lombok.Data; +import lombok.ToString; + +import java.io.Serializable; +import java.math.BigDecimal; + +@Data +@ToString +public class ExchangeOrderCalculateVo implements Serializable { + + private BigDecimal feeAmount; + + private BigDecimal margin; + + private BigDecimal totalBalance; + +} diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ProductConfigVo.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ProductConfigVo.java index 1d35720b2..07df910ec 100644 --- a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ProductConfigVo.java +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/domain/vo/ProductConfigVo.java @@ -1,15 +1,18 @@ package com.ruoyi.market.domain.vo; -import com.ruoyi.common.core.annotation.Excel; import lombok.Data; import lombok.ToString; import java.io.Serializable; +import java.math.BigDecimal; @Data @ToString public class ProductConfigVo implements Serializable { + /** 每张数量 */ + private BigDecimal eachSheetNum; + /** 最小倍率 */ private Double minMultiplier; diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/IMarketClientService.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/IMarketClientService.java index a0722de63..9c5191570 100644 --- a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/IMarketClientService.java +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/IMarketClientService.java @@ -1,6 +1,8 @@ package com.ruoyi.market.service; import com.ruoyi.market.domain.ProductCategory; +import com.ruoyi.market.domain.req.ExchangeOrderCalculateReq; +import com.ruoyi.market.domain.vo.ExchangeOrderCalculateVo; import com.ruoyi.market.domain.vo.HotProductInfoVo; import com.ruoyi.market.domain.vo.ProductConfigVo; import com.ruoyi.market.domain.vo.ProductInfoVo; @@ -40,4 +42,6 @@ public interface IMarketClientService { * @return */ ProductConfigVo selectProductConfig(String productCode); + + ExchangeOrderCalculateVo calculate(ExchangeOrderCalculateReq req); } diff --git a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/impl/MarketClientServiceImpl.java b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/impl/MarketClientServiceImpl.java index 7fc2bc45b..6eab608a4 100644 --- a/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/impl/MarketClientServiceImpl.java +++ b/ruoyi-modules/ruoyi-market/src/main/java/com/ruoyi/market/service/impl/MarketClientServiceImpl.java @@ -1,7 +1,13 @@ package com.ruoyi.market.service.impl; +import com.ruoyi.client.RemoteClientWalletService; +import com.ruoyi.client.domain.ClientUserWallet; +import com.ruoyi.common.core.constant.MarketConstant; import com.ruoyi.common.core.constant.MarketPriceTypeEnum; +import com.ruoyi.common.core.context.SecurityContextHolder; +import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.utils.DateUtils; +import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.bean.BeanUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.redis.service.RedisService; @@ -10,18 +16,19 @@ import com.ruoyi.market.domain.ProductCategory; import com.ruoyi.market.domain.ProductConfig; import com.ruoyi.market.domain.ProductInfo; -import com.ruoyi.market.domain.vo.HotProductInfoVo; -import com.ruoyi.market.domain.vo.ProductConfigVo; -import com.ruoyi.market.domain.vo.ProductInfoVo; -import com.ruoyi.market.domain.vo.ProductPriceVo; +import com.ruoyi.market.domain.req.ExchangeOrderCalculateReq; +import com.ruoyi.market.domain.vo.*; import com.ruoyi.market.service.IMarketClientService; import com.ruoyi.market.service.IProductCategoryService; import com.ruoyi.market.service.IProductConfigService; import com.ruoyi.market.service.IProductInfoService; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -29,6 +36,7 @@ import static com.ruoyi.common.core.constant.MarketConstant.*; +@Slf4j @Service public class MarketClientServiceImpl implements IMarketClientService { @@ -44,6 +52,9 @@ public class MarketClientServiceImpl implements IMarketClientService { @Resource private RedisService redisService; + @Resource + private RemoteClientWalletService remoteClientWalletService; + @Override public List selectProductCategoryList() { ProductCategory params = new ProductCategory(); @@ -124,6 +135,34 @@ public ProductConfigVo selectProductConfig(String productCode) { return productConfigVo; } + @Override + public ExchangeOrderCalculateVo calculate(ExchangeOrderCalculateReq req) { + ProductConfig selectParams = new ProductConfig(); + selectParams.setProductCode(req.getProductCode()); + List productConfigs = iProductConfigService.selectProductConfigList(selectParams); + if (CollectionUtils.isEmpty(productConfigs)) { + return null; + } + ProductConfig productConfig = productConfigs.get(0); + log.info("获取到交易配置:{}", productConfig); + if (StringUtils.equals(BUY, req.getDirect())) { + req.setExchangePrice(req.getExchangePrice().subtract(productConfig.getSpreadRate())); + } else if (StringUtils.equals(SELL, req.getDirect())) { + req.setExchangePrice(req.getExchangePrice().add(productConfig.getSpreadRate())); + } + BigDecimal exchangeAmount = req.getExchangePrice().multiply(req.getSheetNum()).multiply(productConfig.getEachSheetNum()); + log.info("购买总价值为:{}", exchangeAmount); + ExchangeOrderCalculateVo exchangeOrderCalculateVo = new ExchangeOrderCalculateVo(); + exchangeOrderCalculateVo.setFeeAmount(exchangeAmount.multiply(productConfig.getFeeRate()).setScale(productConfig.getPriceUnit(), RoundingMode.HALF_UP)); + exchangeOrderCalculateVo.setMargin(exchangeAmount.divide(req.getMultiplier()).setScale(productConfig.getPriceUnit(), RoundingMode.HALF_UP)); + ClientUserWallet clientUserWalletReq = new ClientUserWallet(); + clientUserWalletReq.setUserId(SecurityContextHolder.getUserId()); + R response = remoteClientWalletService.getUserWallet(clientUserWalletReq); + log.info("获取到用户钱包信息:{}", response.getData()); + exchangeOrderCalculateVo.setTotalBalance(response.getData().getTotalAmount()); + return exchangeOrderCalculateVo; + } + private ProductInfoVo buildProductInfoVo(ProductInfo productInfo, String priceTypeEnum) { ProductInfoVo productInfoVo = new ProductInfoVo(); productInfoVo.setProductCode(productInfo.getProductCode());