From 4d08b2cd312f837a0ea02641d973acd234567797 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 28 May 2020 15:32:39 +0800
Subject: [PATCH] modify

---
 src/main/resources/mapper/platform/TradeSettingDao.xml                                            |   10 +-
 src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java |   48 +++++++++++
 src/main/java/com/xcong/excoin/utils/CoinTypeConvert.java                                         |   50 ++++++++++++
 src/main/java/com/xcong/excoin/modules/contract/parameter/dto/SubmitEntrustDto.java               |    4 +
 src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSkuEntity.java              |    4 
 src/main/java/com/xcong/excoin/modules/platform/dao/TradeSettingDao.java                          |    6 
 src/main/java/com/xcong/excoin/utils/CacheSettingUtils.java                                       |   82 ++++++++++++++++++++
 7 files changed, 192 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/contract/parameter/dto/SubmitEntrustDto.java b/src/main/java/com/xcong/excoin/modules/contract/parameter/dto/SubmitEntrustDto.java
index 3d13c94..bf588cb 100644
--- a/src/main/java/com/xcong/excoin/modules/contract/parameter/dto/SubmitEntrustDto.java
+++ b/src/main/java/com/xcong/excoin/modules/contract/parameter/dto/SubmitEntrustDto.java
@@ -35,4 +35,8 @@
     @ApiModelProperty(value = "币种数量", example = "1")
     private int symbolCnt;
 
+    @NotNull
+    @ApiModelProperty(value = "杠杆倍率", example = "100")
+    private int leverRatio;
+
 }
diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java
index c2e5bef..2f55f16 100644
--- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractEntrustOrderServiceImpl.java
@@ -7,11 +7,17 @@
 import com.xcong.excoin.modules.contract.entity.ContractEntrustOrderEntity;
 import com.xcong.excoin.modules.contract.parameter.dto.SubmitEntrustDto;
 import com.xcong.excoin.modules.contract.service.ContractEntrustOrderService;
+import com.xcong.excoin.modules.member.dao.MemberWalletContractDao;
 import com.xcong.excoin.modules.member.entity.MemberEntity;
+import com.xcong.excoin.modules.member.entity.MemberWalletContractEntity;
+import com.xcong.excoin.utils.CacheSettingUtils;
+import com.xcong.excoin.utils.CoinTypeConvert;
+import com.xcong.excoin.utils.RedisUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
 
 /**
  * @author wzy
@@ -24,19 +30,57 @@
     @Resource
     private ContractEntrustOrderDao contractEntrustOrderDao;
 
+    @Resource
+    private MemberWalletContractDao memberWalletContractDao;
+
+    @Resource
+    private RedisUtils redisUtils;
+
+    @Resource
+    private CacheSettingUtils cacheSettingUtils;
 
     @Override
     public Result addContractEntrustOrder(SubmitEntrustDto submitEntrustDto) {
         MemberEntity memberEntity = LoginUserUtils.getAppLoginUser();
 
+        // 获取最新价
+        BigDecimal newPirce  = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(submitEntrustDto.getSymbol())));
+
+
         // 委托开仓
         if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) {
+            // 开多委托价不能大于当前价
+            if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_MORE) {
+                if (submitEntrustDto.getEntrustPrice().compareTo(newPirce) > -1) {
+                    return Result.fail("委托价不能大于当前价");
+                }
+            }
+
+            // 开空委托价不能小于当前价
+            if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_OPEN_LESS) {
+                if (submitEntrustDto.getEntrustPrice().compareTo(newPirce) < 1) {
+                    return Result.fail("委托价不能小于当前价");
+                }
+            }
+
+            MemberWalletContractEntity walletContract = memberWalletContractDao.selectById(memberEntity.getId());
+            // 委托总额
+            BigDecimal entrustTotalAmount = submitEntrustDto.getEntrustPrice().multiply(BigDecimal.valueOf(submitEntrustDto.getSymbolCnt()));
+
+            if (entrustTotalAmount.compareTo(walletContract.getAvailableBalance()) > -1) {
+                return Result.fail("可用余额不足");
+            }
+
+            BigDecimal lotNumber = cacheSettingUtils.getSymbolSku(submitEntrustDto.getSymbol());
+
+
+
 
         }
 
-        // 委托平仓
+        // 委托平仓 (全仓模式)
         if (submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_MORE || submitEntrustDto.getEntrustType() == ContractEntrustOrderEntity.ENTRUST_TYPE_CLOSE_LESS) {
-
+            return Result.fail("功能暂未开放,敬请期待");
         }
         return null;
     }
diff --git a/src/main/java/com/xcong/excoin/modules/platform/dao/TradeSettingDao.java b/src/main/java/com/xcong/excoin/modules/platform/dao/TradeSettingDao.java
index c633b28..aec081a 100644
--- a/src/main/java/com/xcong/excoin/modules/platform/dao/TradeSettingDao.java
+++ b/src/main/java/com/xcong/excoin/modules/platform/dao/TradeSettingDao.java
@@ -2,20 +2,20 @@
 
 import java.util.List;
 
+import com.xcong.excoin.modules.platform.entity.PlatformSymbolsSkuEntity;
 import org.apache.ibatis.annotations.Param;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.xcong.excoin.modules.platform.entity.PlatformLeverageSettingEntity;
-import com.xcong.excoin.modules.platform.entity.PlatformSymbolsSku;
 import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
 
 public interface TradeSettingDao extends BaseMapper<PlatformTradeSettingEntity> {
 
 	PlatformTradeSettingEntity findTradeSetting();
 
-	PlatformSymbolsSku findSymbolSkubySymbol(@Param("symbol") String symbol);
+	PlatformSymbolsSkuEntity findSymbolSkubySymbol(@Param("symbol") String symbol);
 	
-	List<PlatformSymbolsSku> findAllSymbolSkubySymbol();
+	List<PlatformSymbolsSkuEntity> findAllSymbolSkubySymbol();
 
 	List<PlatformLeverageSettingEntity> findLeverageSetting();
 
diff --git a/src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSku.java b/src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSkuEntity.java
similarity index 85%
rename from src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSku.java
rename to src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSkuEntity.java
index a94171d..ec1f804 100644
--- a/src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSku.java
+++ b/src/main/java/com/xcong/excoin/modules/platform/entity/PlatformSymbolsSkuEntity.java
@@ -13,7 +13,7 @@
  */
 @Data
 @TableName("platform_symbols_sku")
-public class PlatformSymbolsSku implements Serializable {
+public class PlatformSymbolsSkuEntity implements Serializable {
 	/**
 	 * 
 	 */
@@ -27,6 +27,6 @@
 	/**
 	 * 规格
 	 */
-	private String sku;
+	private String lotnumber;
 
 }
diff --git a/src/main/java/com/xcong/excoin/utils/CacheSettingUtils.java b/src/main/java/com/xcong/excoin/utils/CacheSettingUtils.java
new file mode 100644
index 0000000..9145008
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/utils/CacheSettingUtils.java
@@ -0,0 +1,82 @@
+package com.xcong.excoin.utils;
+
+import cn.hutool.core.bean.BeanUtil;
+import com.xcong.excoin.modules.platform.dao.TradeSettingDao;
+import com.xcong.excoin.modules.platform.entity.PlatformSymbolsSkuEntity;
+import com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity;
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author wzy
+ * @date 2020-05-28
+ **/
+@Component
+public class CacheSettingUtils {
+
+
+    /**
+     * 交易设置缓存Key
+     */
+    private final static String TRADE_SETTING_KEY = "trade_setting_key";
+
+    /**
+     * 币种规格缓存key
+     */
+    private final static String TRADE_SYMBOL_SKU_KEY = "trade_symbol_sku_key";
+
+    @Resource
+    private TradeSettingDao tradeSettingDao;
+
+    @Resource
+    private RedisUtils redisUtils;
+
+
+    /**
+     * 获取币种规格
+     *
+     * @param symbol
+     * @return
+     */
+    public BigDecimal getSymbolSku(String symbol) {
+        Object hget = redisUtils.hget(TRADE_SYMBOL_SKU_KEY, symbol);
+        if (hget == null) {
+            List<PlatformSymbolsSkuEntity> symbolSkubySymbol = tradeSettingDao.findAllSymbolSkubySymbol();
+            Map<String, Object> map = new HashMap<String, Object>();
+            if (CollectionUtils.isNotEmpty(symbolSkubySymbol)) {
+                for (PlatformSymbolsSkuEntity symbolSku : symbolSkubySymbol) {
+                    map.put(symbolSku.getName(), symbolSku.getLotnumber());
+                }
+                // 存入redis
+                redisUtils.hmset(TRADE_SYMBOL_SKU_KEY, map);
+            }
+
+            hget = redisUtils.hget(TRADE_SYMBOL_SKU_KEY, symbol);
+        }
+
+        return new BigDecimal(hget.toString());
+    }
+
+    /**
+     * 获取交易设置缓存
+     *
+     * @return
+     */
+    public PlatformTradeSettingEntity getTradeSetting() {
+        Map<Object, Object> hmget = redisUtils.hmget(TRADE_SETTING_KEY);
+
+        if (hmget == null || hmget.size() == 0) {
+            PlatformTradeSettingEntity tradeSetting = tradeSettingDao.findTradeSetting();
+            redisUtils.hmset(TRADE_SETTING_KEY, BeanUtil.beanToMap(tradeSetting));
+            return tradeSetting;
+        }
+        return BeanUtil.mapToBean(hmget, PlatformTradeSettingEntity.class, true);
+    }
+
+}
diff --git a/src/main/java/com/xcong/excoin/utils/CoinTypeConvert.java b/src/main/java/com/xcong/excoin/utils/CoinTypeConvert.java
new file mode 100644
index 0000000..fc2dc6c
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/utils/CoinTypeConvert.java
@@ -0,0 +1,50 @@
+package com.xcong.excoin.utils;
+
+/**
+ * @author wzy
+ * @date 2020-05-28
+ **/
+public class CoinTypeConvert {
+
+    public static String convert(String symbol) {
+        switch (symbol) {
+            case "btcusdt":
+                return "BTC/USDT";
+            case "ethusdt":
+                return "ETH/USDT";
+            case "xrpusdt":
+                return "XRP/USDT";
+            case "ltcusdt":
+                return "LTC/USDT";
+            case "bchusdt":
+                return "BCH/USDT";
+            case "eosusdt":
+                return "EOS/USDT";
+            case "etcusdt":
+                return "ETC/USDT";
+            default:
+                return null;
+        }
+    }
+
+    public static String convertToKey(String symbol) {
+        switch (symbol) {
+            case "BTC/USDT":
+                return "BTC_NEW_PRICE";
+            case "ETH/USDT":
+                return "ETH_NEW_PRICE";
+            case "XRP/USDT":
+                return "XRP_NEW_PRICE";
+            case "LTC/USDT":
+                return "LTC_NEW_PRICE";
+            case "BCH/USDT":
+                return "BCH_NEW_PRICE";
+            case "EOS/USDT":
+                return "EOS_NEW_PRICE";
+            case "ETC/USDT":
+                return "ETC_NEW_PRICE";
+            default:
+                return null;
+        }
+    }
+}
diff --git a/src/main/resources/mapper/platform/TradeSettingDao.xml b/src/main/resources/mapper/platform/TradeSettingDao.xml
index e0e56ba..7537a80 100644
--- a/src/main/resources/mapper/platform/TradeSettingDao.xml
+++ b/src/main/resources/mapper/platform/TradeSettingDao.xml
@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
-<mapper namespace="com.xcong.excoin.modules.coin.dao.platform.TradeSettingDao">	
+<mapper namespace="com.xcong.excoin.modules.platform.dao.TradeSettingDao">
 	
-	<select id="findTradeSetting" resultType="com.xcong.excoin.modules.coin.entity.PlatformTradeSettingEntity">
+	<select id="findTradeSetting" resultType="com.xcong.excoin.modules.platform.entity.PlatformTradeSettingEntity">
 		SELECT * FROM platform_trade_setting
 	</select>
 	
-	<select id="findSymbolSkubySymbol" resultType="com.xcong.excoin.modules.coin.entity.PlatformSymbolsSku">
+	<select id="findSymbolSkubySymbol" resultType="com.xcong.excoin.modules.platform.entity.PlatformSymbolsSkuEntity">
 		select * from platform_symbols_sku
 		 <where>
 			 <if test="symbol!=null and symbol!=''">
@@ -15,11 +15,11 @@
 		 </where>
 	</select>
 
-	<select id="findAllSymbolSkubySymbol" resultType="com.xcong.excoin.modules.coin.entity.PlatformSymbolsSku">
+	<select id="findAllSymbolSkubySymbol" resultType="com.xcong.excoin.modules.platform.entity.PlatformSymbolsSkuEntity">
 		select * from platform_symbols_sku
 	</select>
 	
-	<select id="findLeverageSetting" resultType="com.xcong.excoin.modules.coin.entity.PlatformLeverageSettingEntity">
+	<select id="findLeverageSetting" resultType="com.xcong.excoin.modules.platform.entity.PlatformLeverageSettingEntity">
 		select * from platform_leverage_setting order by value ASC
 	</select>
 	

--
Gitblit v1.9.1