From 9cadad100ba1d3c18966f9df7e1c3213abc0d214 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Tue, 26 Aug 2025 18:34:24 +0800
Subject: [PATCH] ico新增预支付

---
 trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java |  232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 224 insertions(+), 8 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java b/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
index f589961..6fd54b0 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
@@ -1,15 +1,32 @@
 package com.yami.trading.service.ico;
 
 
+import cn.hutool.core.date.DateTime;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.yami.trading.bean.ico.domain.Ico;
-import com.yami.trading.bean.ico.domain.UserSubscriptionRecord;
+import com.yami.trading.bean.ico.domain.UserSubscription;
+import com.yami.trading.bean.item.domain.Item;
+import com.yami.trading.bean.model.MoneyLog;
+import com.yami.trading.bean.model.User;
+import com.yami.trading.bean.model.Wallet;
+import com.yami.trading.bean.model.WalletExtend;
+import com.yami.trading.common.constants.Constants;
 import com.yami.trading.common.domain.Result;
+import com.yami.trading.common.exception.YamiShopBindException;
+import com.yami.trading.common.util.Arith;
+import com.yami.trading.common.util.C2cLock;
 import com.yami.trading.dao.ico.IcoMapper;
+import com.yami.trading.service.MoneyLogService;
+import com.yami.trading.service.WalletService;
+import com.yami.trading.service.user.UserService;
 import lombok.extern.slf4j.Slf4j;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+
+import java.math.BigDecimal;
+import java.util.Date;
 
 /**
  * 新币Service
@@ -20,15 +37,214 @@
 @Slf4j
 public class IcoService extends ServiceImpl<IcoMapper, Ico> {
 
-    /**
-     * 申购
-     * @param model
-     * @return
-     */
-    public Result<String> subscribe(UserSubscriptionRecord model) {
+    @Autowired
+    UserService userService;
 
-        return Result.ok("保存产品成功" );
+    @Autowired
+    WalletService walletService;
+
+    @Autowired
+    UserSubscriptionService userSubscriptionService;
+
+    @Autowired
+    MoneyLogService moneyLogService;
+
+    /**
+     * 新币申购
+     */
+    public Result<String> subscribe(UserSubscription model) {
+        try {
+            if (model == null || model.getIcoProjectId() == null) {
+                throw new YamiShopBindException("参数异常");
+            }
+            String partyId = model.getUserId();
+            Ico ico = this.getById(model.getIcoProjectId());
+            if (ico == null) {
+                throw new YamiShopBindException("新币不存在");
+            }
+
+            User party = userService.getById(partyId);
+            if (!party.isEnabled()) {
+                return Result.succeed("User is locked");
+            }
+            if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) {
+                throw new YamiShopBindException("无权限");
+            }
+            if (!C2cLock.add(partyId)) {
+                throw new YamiShopBindException("Please try again later");
+            }
+
+            if (model.getSubscribeNums() == null || model.getSubscribeNums() == 0) {
+                throw new YamiShopBindException("申请数量不能为空");
+            }
+            //购买金额
+            BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
+            if(amount.compareTo(ico.getMinContribution()) < 0 ){
+                throw new YamiShopBindException("最低投资额: " + ico.getMinContribution());
+            }
+            if(amount.compareTo(ico.getMaxContribution()) > 0 ){
+                throw new YamiShopBindException("最高投资额: " + ico.getMaxContribution());
+            }
+            Date currentDate = new Date();
+            if(currentDate.before(ico.getStartDate())){
+                throw new YamiShopBindException("未开售");
+            }
+            if(currentDate.after(ico.getEndDate())){
+                throw new YamiShopBindException("已结束");
+            }
+
+            model.setStatus(1);
+            model.setUserId(partyId);
+            if (model.getSubscriptionType() == null) { //默认自主申购
+                model.setSubscriptionType(1);
+            }
+
+            //需要先支付
+            if (ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
+                Wallet wallet = walletService.saveWalletByPartyId(partyId);
+                if (amount.compareTo(wallet.getMoney()) > 0) {
+                    throw new YamiShopBindException("余额不足");
+                }
+                walletService.update(partyId, Arith.sub(0, amount.doubleValue()));
+
+                // 保存 资金日志
+                MoneyLog moneylog = new MoneyLog();
+                moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
+                moneylog.setAmountBefore(wallet.getMoney());
+                moneylog.setAmount(amount);
+                moneylog.setAmountAfter(BigDecimal.valueOf(Arith.sub(wallet.getMoney().doubleValue(), amount.doubleValue())));
+                moneylog.setLog("新币购买,申购金额[" + amount + "]");
+                moneylog.setUserId(partyId);
+                moneylog.setSymbol(ico.getSymbol());
+                moneylog.setWalletType(ico.getSymbol());
+                moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
+                moneyLogService.save(moneylog);
+            }
+            userSubscriptionService.save(model);
+            return Result.ok("申请成功" );
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
 
+    public Result <String> updateRecord(UserSubscription model) {
+        try {
+            if( model.getId() == null || model.getStatus() == null){
+                throw new YamiShopBindException("请输入参数");
+            }
+            UserSubscription userSubscription = userSubscriptionService.getById(model.getId());
+            if (userSubscription == null) {
+                throw new YamiShopBindException("记录不存在");
+            }
+            if (model.getStatus() == 3|| model.getStatus() == 2) {
+                userSubscription.setEndTime(DateTime.now());
+            }
+            if (userSubscription.getStatus() == 5) {
+                throw new YamiShopBindException("It's been converted");
+            }
+            if(userSubscription.getStatus() == 3){
+                throw new YamiShopBindException("不能更改申购状态");
+            }
+            Ico ico = this.getById(model.getIcoProjectId());
+            String userId = userSubscription.getUserId();
+            if (model.getStatus() == 3 || model.getStatus() == 5) {
+                if(model.getBallotNumber() == null || model.getBallotNumber() == 0){
+                    throw new YamiShopBindException("中签数量不能小于0");
+                }
+                if(model.getBallotNumber() > userSubscription.getSubscribeNums()){
+                    throw new YamiShopBindException("配置中签数量不能超过申请数量");
+                }
+            }
+            userSubscription.setStatus(model.getStatus());
+            userSubscription.setBallotNumber(model.getBallotNumber());
+            if (model.getStatus() == 3) { //中签
+                //资金账户
+                Wallet wallet = walletService.saveWalletByPartyId(userId);
+                int applyNumber = userSubscription.getSubscribeNums() - model.getBallotNumber();
+                BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
+                //已经预支付
+                if(ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
+                    if(applyNumber > 0) {
+                        //退回资金
+                        BigDecimal refundPrice = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
+                        walletService.update(userId, refundPrice.doubleValue());
+
+                        //保存 资金日志
+                        MoneyLog moneylog = new MoneyLog();
+                        moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
+                        moneylog.setAmountBefore(wallet.getMoney());
+                        moneylog.setAmount(amount);
+                        moneylog.setAmountAfter(wallet.getMoney().add(refundPrice));
+                        moneylog.setLog("新币购买,退回购买金额[" + refundPrice + "]");
+                        moneylog.setUserId(userId);
+                        moneylog.setSymbol(ico.getSymbol());
+                        moneylog.setWalletType(ico.getSymbol());
+                        moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_RT_BUY);
+                        moneyLogService.save(moneylog);
+                    }
+                } else {
+                    BigDecimal subtract = amount.subtract(wallet.getMoney());
+                    if (subtract.compareTo(BigDecimal.ZERO) > 0) {
+                        //放入待补
+                        wallet.setMoney(BigDecimal.ZERO);
+                        wallet.setAmountToBeCovered(subtract);
+                        if (!walletService.updateById(wallet)) {
+                            throw new YamiShopBindException("操作钱包失败!");
+                        }
+                    } else {
+                        //扣除资金
+                        walletService.update(userId, amount.negate().doubleValue());
+                    }
+                    //保存 资金日志
+                    MoneyLog moneylog = new MoneyLog();
+                    moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
+                    moneylog.setAmountBefore(wallet.getMoney());
+                    moneylog.setAmount(amount);
+                    moneylog.setAmountAfter(subtract.compareTo(BigDecimal.ZERO) > 0 ? BigDecimal.ZERO : subtract.negate());
+                    moneylog.setLog("新币购买,申购金额[" + amount + "]");
+                    moneylog.setUserId(userId);
+                    moneylog.setSymbol(ico.getSymbol());
+                    moneylog.setWalletType(ico.getSymbol());
+                    moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
+                    moneyLogService.save(moneylog);
+                }
+                userSubscription.setStatus(5);
+            }
+            if (model.getStatus() == 3 || model.getStatus() == 5) {  //转入账户
+                //获取币账户
+                WalletExtend walletExtend = walletService.saveExtendByPara(userId, ico.getSymbol());
+                walletService.updateExtend(userId, walletExtend.getWallettype(), model.getBallotNumber());
+            }
+            userSubscriptionService.save(userSubscription);
+            return Result.ok ( "操作成功" );
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Item icoToItem(Item item, Ico ico) {
+        item.setName(ico.getName());
+        item.setSymbol(ico.getSymbol());
+        item.setSymbolData(ico.getSymbolData());
+        item.setPips(ico.getPips());
+        item.setPipsAmount(ico.getPipsAmount());
+        item.setAdjustmentValue(ico.getAdjustmentValue());
+        item.setUnitAmount(ico.getUnitAmount());
+        item.setUnitFee(ico.getUnitFee());
+        item.setMarket("");
+        item.setDecimals(ico.getDecimals());
+        item.setMultiple(ico.getMultiple());
+        item.setBorrowingRate(ico.getBorrowingRate());
+        item.setSymbolFullName(ico.getName());
+        item.setType(Item.cryptos);
+        item.setCategory(Item.cryptos);
+        item.setShowStatus("1");
+        item.setTradeStatus("1");
+        item.setQuoteCurrency(ico.getCurrency());
+        item.setCurrencyType(0);
+        item.setStatus(0);
+        item.setTradeType(ico.getIsContractTrading().toString());
+        return item;
+    }
 }

--
Gitblit v1.9.3