1
zj
2024-09-29 8bb76f405fb9e5ee135231618c7da357946dc2f8
1
10 files modified
203 ■■■■ changed files
src/main/java/com/nq/pojo/StockSubscribe.java 10 ●●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/StockDzServiceImpl.java 6 ●●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/UserAssetsServices.java 6 ●●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java 111 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/ws/WebSocketClientBeanConfig.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/nq/ws/WebsocketRunClient.java 55 ●●●● patch | view | raw | blame | history
src/main/java/com/nq/ws/WsClientConfig.java 3 ●●●●● patch | view | raw | blame | history
src/main/resources/application.properties 4 ●●●● patch | view | raw | blame | history
src/main/resources/application.yml 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/nq/pojo/StockSubscribe.java
@@ -50,6 +50,16 @@
    private Long orderNumber;
    /**
     * 最小数量
     */
    private Long minNumber;
    /**
     * 杠杆
     */
    private Long lever;
    /**
     * 顯示状态
     */
    private Integer zt;
src/main/java/com/nq/service/impl/StockDzServiceImpl.java
@@ -68,6 +68,12 @@
            return ServerResponse.createByErrorMsg("参数不能为空");
        }
        Long count = stockDzMapper.selectCount(new LambdaQueryWrapper<StockDz>()
                .eq(StockDz::getStockCode,stockCode));
        if(count > 0){
            return ServerResponse.createByErrorMsg("股票代码已存在");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("stock_code",stockCode);
src/main/java/com/nq/service/impl/UserAssetsServices.java
@@ -80,9 +80,11 @@
                    BigDecimal availableBalance = bigAmt.subtract(userAssets.getAmountToBeCovered());
                    if(availableBalance.compareTo(BigDecimal.ZERO) > 0){
                        userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(availableBalance));
                        userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userAssets.getAmountToBeCovered()));
                        userAssets.setAmountToBeCovered(BigDecimal.ZERO);
                    }else{
                        BigDecimal amountToBeCovered = userAssets.getAmountToBeCovered().subtract(bigAmt);
                        userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(bigAmt));
                        userAssets.setAmountToBeCovered(amountToBeCovered);
                    }
                }else{
@@ -106,8 +108,6 @@
            if(type.equals("2")){
                return  userRechargeService.createOrder(userAssets.getUserId(),1,bigAmt.intValue(),"1");
            }else{
                return ServerResponse.createBySuccess();
            }
        }
@@ -190,9 +190,11 @@
               BigDecimal availableBalance = amount.subtract(userAssets.getAmountToBeCovered());
               if(availableBalance.compareTo(BigDecimal.ZERO) > 0){
                   userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(availableBalance));
                   userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userAssets.getAmountToBeCovered()));
                   userAssets.setAmountToBeCovered(BigDecimal.ZERO);
               }else{
                   BigDecimal amountToBeCovered = userAssets.getAmountToBeCovered().subtract(amount);
                   userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(amount));
                   userAssets.setAmountToBeCovered(amountToBeCovered);
               }
           }else{
src/main/java/com/nq/service/impl/UserPositionServiceImpl.java
@@ -1304,8 +1304,8 @@
            if(null == userAssets){
                return ServerResponse.createByErrorMsg("新股转持仓失败");
            }
            userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userPosition.getOrderTotalPrice()));
            userAssets.setFreezeMoney(userAssets.getFreezeMoney()!=null?userAssets.getFreezeMoney().setScale(2,BigDecimal.ROUND_UP):null);
//            userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userPosition.getOrderTotalPrice()));
//            userAssets.setFreezeMoney(userAssets.getFreezeMoney()!=null?userAssets.getFreezeMoney().setScale(2,BigDecimal.ROUND_UP):null);
            userAssetsMapper.updateById(userAssets);
            if (ret > 0) {
                userStockSubscribe.setStatus(5);
src/main/java/com/nq/service/impl/UserStockSubscribeServiceImpl.java
@@ -101,8 +101,27 @@
            if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
                return ServerResponse.createByErrorMsg("请先缴清待补资金", request);
            }
            if(new Date().before(stockSubscribe.getSubscribeTime()) && new Date().after(stockSubscribe.getSubscribeTime()) ){
                return ServerResponse.createByErrorMsg("不在认缴或配售时间之内",request);
            if(userAssets.getAvailableBalance().compareTo(BigDecimal.ZERO) <= 0){
                return ServerResponse.createByErrorMsg("账户余额不足", request);
            }
            if ( model.getApplyNums() == null){
                // 申请数量为空时返回错误信息
                return ServerResponse.createByErrorMsg("申请数量不能为空", request);
            }
            if(model.getApplyNums() < stockSubscribe.getMinNumber() ){
                return ServerResponse.createByErrorMsg("最少申购:" + stockSubscribe.getMinNumber(), request);
            }
            if (model.getApplyNums() > stockSubscribe.getOrderNumber()){
                return ServerResponse.createByErrorMsg("最多可申购:" + stockSubscribe.getOrderNumber(), request);
            }
            Date currentDate = new Date();
            if(currentDate.before(stockSubscribe.getSubscriptionTime())){
                return ServerResponse.createByErrorMsg("未开售",request);
            }
            if(currentDate.after(stockSubscribe.getSubscribeTime())){
                return ServerResponse.createByErrorMsg("已结束",request);
            }
//            //重复申购限制
@@ -117,19 +136,13 @@
            if (stockSubscribe == null) {
                return ServerResponse.createByErrorMsg("新的股票代码不存在",request);
            }
//            if ( model.getApplyNums() == null){
//                // 申请数量为空时返回错误信息
//                return ServerResponse.createByErrorMsg("申请数量不能为空", request);
//            }
//
//            if (model.getApplyNums() > stockSubscribe.getOrderNumber()){
//                // 申请数量大于订单数量时返回错误信息
//                return ServerResponse.createByErrorMsg("最多可申购:" + stockSubscribe.getOrderNumber(), request);
//            }
            //默认数量最大
            model.setApplyNums(stockSubscribe.getOrderNumber().intValue());
            //计算申购金额
            BigDecimal bound;
            if (stockSubscribe.getMinPrice() != null && stockSubscribe.getMinPrice().compareTo(BigDecimal.ZERO) > 0) {
                bound =  new BigDecimal(model.getApplyNums()).multiply(new BigDecimal(stockSubscribe.getLever().toString())).multiply(stockSubscribe.getMinPrice());
            }else{
                bound =  new BigDecimal(model.getApplyNums()).multiply(new BigDecimal(stockSubscribe.getLever().toString())).multiply(stockSubscribe.getPrice());
            }
            if(stockSubscribe.getType() == 1){
                model.setUserId(user.getId());
@@ -146,6 +159,18 @@
                UserStockSubscribe userStockSubscribe = Convert.convert(UserStockSubscribe.class, model);
                userStockSubscribe.setNewStockId(stockSubscribe.getNewlistId());
                ret = userStockSubscribeMapper.insert(userStockSubscribe);
                BigDecimal subtract = userAssets.getAvailableBalance().subtract(bound);
                if(subtract.compareTo(BigDecimal.ZERO) >= 0){
                    userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(bound));
                    userAssets.setAvailableBalance(subtract);
                }else{
                    userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(userAssets.getAvailableBalance()));
                    userAssets.setAvailableBalance(BigDecimal.ZERO);
                    userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().add(subtract.abs()));
                }
                userAssetsMapper.updateById(userAssets);
                if (ret > 0) {
                    return ServerResponse.createBySuccessMsg("申购成功",request);
                } else {
@@ -153,13 +178,13 @@
                }
            }else{
               BigDecimal bound;
               if(stockSubscribe.getMinPrice() != null || stockSubscribe.getMinPrice().compareTo(BigDecimal.ZERO)<0){
                   bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getMinPrice());
               }else{
                   bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice());
               }
//               BigDecimal bound;
//               if(stockSubscribe.getMinPrice() != null || stockSubscribe.getMinPrice().compareTo(BigDecimal.ZERO)<0){
//                   bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getMinPrice());
//               }else{
//                   bound =  new BigDecimal(model.getApplyNums()).multiply(stockSubscribe.getPrice());
//               }
//
               BigDecimal useEnaAmount = iUserAssetsServices.getAvailableBalance(EStockType.IN.getCode(), user.getId());
               if(useEnaAmount.compareTo(bound)<0){
                   return ServerResponse.createByErrorMsg("余额不足,配售失败",request);
@@ -218,23 +243,39 @@
            if((model.getStatus() == 3 && model.getApplyNumber() == null) || (model.getStatus() == 3 && model.getApplyNumber() == 0) ){
                return ServerResponse.createByErrorMsg("中签数量不能小于0");
            }
            if(model.getStatus() == 3 && model.getApplyNumber()>userStockSubscribe.getApplyNums()){
                return  ServerResponse.createByErrorMsg("配置中签数量不能超过申请数量",request);
            }
            //客户中签直接扣除客户账户可用资金
            UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", userStockSubscribe.getUserId());
            if (model.getStatus() == 3 && model.getApplyNumber() != null){
                if(stockSubscribe.getType() == 1){
                    model.setBond((stockSubscribe.getMinPrice() != null ? stockSubscribe.getMinPrice() : stockSubscribe.getPrice()).multiply(BigDecimal.valueOf(model.getApplyNumber())));
                    model.setBond((stockSubscribe.getMinPrice() != null ? stockSubscribe.getMinPrice() : stockSubscribe.getPrice()).multiply(BigDecimal.valueOf(model.getApplyNumber())).multiply(new BigDecimal(stockSubscribe.getLever().toString())));
                    model.setDbMoney(model.getDbMoney());
                    //客户中签直接扣除客户账户可用资金
                    UserAssets userAssets = iUserAssetsServices.assetsByTypeAndUserId("IN", userStockSubscribe.getUserId());
                    if(null == userAssets){
                        return ServerResponse.createByErrorMsg("客户资金账户不存在");
                    }
                    BigDecimal decimal = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(model.getApplyNumber()));//中签总价
                    BigDecimal availableBalance = userAssets.getAvailableBalance().subtract(decimal);//剩余可用余额
                    if(decimal.compareTo(userAssets.getAvailableBalance()) <= 0){
                        userAssets.setAvailableBalance(availableBalance);
                    int applyNumber = userStockSubscribe.getApplyNums() - model.getApplyNumber();
                    if(userAssets.getAmountToBeCovered().compareTo(BigDecimal.ZERO) <= 0){
                        if(applyNumber > 0){
                            //需要退回的资金
                            BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(stockSubscribe.getLever())).multiply(new BigDecimal(applyNumber));
                            userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(refundPrice));
                            userAssets.setFreezeMoney(userAssets.getFreezeMoney().add(refundPrice));
                        }
                    }else{
                        BigDecimal amountToBeCovered = decimal.subtract(userAssets.getAvailableBalance());//待补金额
                        userAssets.setAvailableBalance(BigDecimal.ZERO);
                        userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().add(amountToBeCovered));
                        if(applyNumber > 0){
                            BigDecimal refundPrice = userStockSubscribe.getBuyPrice().multiply(new BigDecimal(stockSubscribe.getLever())).multiply(new BigDecimal(applyNumber));
                            BigDecimal subtract = refundPrice.subtract(userAssets.getAmountToBeCovered());
                            if(subtract.compareTo(BigDecimal.ZERO) <= 0){//如果退回的资金不足补足待补则直接减
                                userAssets.setAmountToBeCovered(userAssets.getAmountToBeCovered().subtract(refundPrice));
                            }else{//如果退回的资金足够填补待补资金还有剩余,则返回余额
                                userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(refundPrice).add(userAssets.getAmountToBeCovered()));
                                userAssets.setAmountToBeCovered(BigDecimal.ZERO);
                                userAssets.setAvailableBalance(subtract);
                            }
                        }
                    }
                    userAssetsMapper.updateById(userAssets);
                    ret = userStockSubscribeMapper.update1(model);
@@ -252,6 +293,12 @@
                    ret = userStockSubscribeMapper.update1(model);
                }
            }else if (model.getStatus() == 2){
                BigDecimal bond = (stockSubscribe.getMinPrice() != null ? stockSubscribe.getMinPrice() : stockSubscribe.getPrice()).multiply(BigDecimal.valueOf(model.getApplyNums())).multiply(new BigDecimal(stockSubscribe.getLever().toString()));
                BigDecimal subtractMoney = bond.subtract(userAssets.getAmountToBeCovered());//需要退回的钱
                userAssets.setAvailableBalance(userAssets.getAvailableBalance().add(subtractMoney.abs()));
                userAssets.setFreezeMoney(userAssets.getFreezeMoney().subtract(subtractMoney));
                userAssets.setAmountToBeCovered(BigDecimal.ZERO);
                userAssetsMapper.updateById(userAssets);
                ret = userStockSubscribeMapper.update1(model);
            }else if (model.getStatus() == 5){
                    return iUserPositionService.newStockToPosition(model.getId());
src/main/java/com/nq/ws/WebSocketClientBeanConfig.java
@@ -22,7 +22,7 @@
//
//        Map<String, WebSocketClient> retMap = new HashMap<>(2);
//        try {
//            WebsocketRunClient websocketRunClient = new WebsocketRunClient(new URI(PropertiesUtil.getProperty("IN_WS_URL")),EStockType.IN);
//            WebsocketRunClient websocketRunClient = new WebsocketRunClient(new URI(PropertiesUtil.getProperty("JS_IN_WS_URL")),EStockType.IN);
//            websocketRunClient.connect();
//            websocketRunClient.setConnectionLostTimeout(0);
//            new Thread(() -> {
src/main/java/com/nq/ws/WebsocketRunClient.java
@@ -9,13 +9,21 @@
//import com.nq.utils.ApplicationContextRegisterUtil;
//import com.nq.utils.redis.RedisKeyUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.message.BasicNameValuePair;
//import org.java_websocket.client.WebSocketClient;
//import org.java_websocket.handshake.ServerHandshake;
//import org.springframework.context.ApplicationContext;
//
//import java.io.IOException;
//import java.lang.reflect.Type;
//import java.net.URI;
//import java.util.Map;
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//
//@Slf4j
//public class WebsocketRunClient  extends WebSocketClient {
@@ -28,20 +36,33 @@
//        this.eStockType = eStockType;
//    }
//
//    private static HttpClient httpClient = HttpClients.createDefault(); // 单例化 HttpClient
//
//
//    private static HttpPost httpPost;
//    static {
//        httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification"); // 初始化 HttpPost
//    }
//
//    @Override
//    public void onOpen(ServerHandshake serverHandshake) {
//        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
//        Timer heartbeatTimer;
//        // 启动心跳定时器
//        heartbeatTimer = new Timer();
//        heartbeatTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
//            }
//        }, 0, 3000); // 每3秒发送一次心跳消息
//    }
//
//    @Override
//    public void onMessage(String s) {
//        try {
//            ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
//            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
//        if(!s.equals("pong") && !s.equals("身份验证成功")){
//            StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
//            liquidationService.RealTimeDataProcess(eStockType,stockDetailBean);
//        }catch (Exception e){
//
//            RedisKeyUtil.setCacheRealTimeStock(eStockType,stockDetailBean);
//        }
//    }
//
@@ -60,4 +81,24 @@
//    public void onError(Exception e) {
//        log.info("websocket 错误");
//    }
//
//    public void sendLoca(String message) {
//        try {
//            // 准备 form-data 参数
//            List<BasicNameValuePair> params = new ArrayList<>();
//            params.add(new BasicNameValuePair("message", message));
//
//            // 设置编码格式为 UTF-8
//            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
//            httpPost.setEntity(entity); // 设置 HttpPost 对象的参数
//
//            // 发送请求
//            HttpResponse response = httpClient.execute(httpPost);
//
//            // 处理响应
//            int statusCode = response.getStatusLine().getStatusCode();
//        } catch (IOException e) {
//            log.error("Http 请求错误", e);
//        }
//    }
//}
src/main/java/com/nq/ws/WsClientConfig.java
@@ -8,6 +8,7 @@
import com.nq.service.impl.MandatoryLiquidationService;
import com.nq.utils.ApplicationContextRegisterUtil;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.RedisKeyUtil;
import com.nq.ws.client.IO;
import com.nq.ws.client.Socket;
import io.socket.emitter.Emitter;
@@ -82,6 +83,8 @@
            @Override
            public void call(Object... args) {
                send(args[0].toString());
                StockRealTimeBean stockDetailBean =  new Gson().fromJson(args[0].toString(), StockRealTimeBean.class);
                RedisKeyUtil.setCacheRealTimeStock(EStockType.IN,stockDetailBean);
            }
        });
src/main/resources/application.properties
@@ -17,7 +17,7 @@
ftp.user=ftp_stock
ftp.pass=123456
ftp.address =/www/wwwroot/ftp_stock/
ftp.server.http.prefix=https://img.ispentagon-institution.com/
ftp.server.http.prefix=https://img.amcfcxn.org/
redis1.ip=localhost
redis1.port=6379
@@ -50,7 +50,7 @@
JS_IN_HTTP_API = http://api-in-2-socket.js-stock.top
JS_IN_HTTP_URL = http://api-in-2.js-stock.top/
JS_IN_WS_URL =ws://api-in-2-ws.js-stock.top
JS_IN_KEY = dYD2k9cb38NbSxFDAMoDgWmOh
JS_IN_KEY = 839tbd9OK88Aby9FbjTvr32hG
US_HTTP_API = http://api-us.js-stock.top/
US_WS_URL = ws://ws-us.js-stock.top
src/main/resources/application.yml
@@ -15,6 +15,8 @@
      min-spare: 100
# 日志配置
logging:
  file:
    encoding: UTF-8
  level:
    com.ruoyi: debug
    org.springframework: warn