新版仿ok交易所-后端
zyy
2025-08-25 6e3ac72bb8da7d1622acf75c01b09caa341c2383
trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
@@ -1,12 +1,17 @@
package com.yami.trading.service.ico;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.ico.domain.Ico;
import com.yami.trading.bean.ico.domain.UserSubscription;
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;
@@ -16,6 +21,8 @@
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.service.user.WalletExtendService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -23,6 +30,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
@@ -46,6 +54,9 @@
    @Autowired
    MoneyLogService moneyLogService;
    @Autowired
    WalletExtendService walletExtendService;
    /**
     * 新币申购
@@ -79,10 +90,10 @@
            }
            //购买金额
            BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
            Wallet wallet = walletService.saveWalletByPartyId(partyId);
            /*Wallet wallet = walletService.saveWalletByPartyId(partyId);
            if (amount.compareTo(wallet.getMoney()) > 0) {
                throw new YamiShopBindException("余额不足");
            }
            }*/
            if(amount.compareTo(ico.getMinContribution()) < 0 ){
                throw new YamiShopBindException("最低投资额: " + ico.getMinContribution());
            }
@@ -103,7 +114,7 @@
                model.setSubscriptionType(1);
            }
            userSubscriptionService.save(model);
            walletService.update(partyId, Arith.sub(0, amount.doubleValue()));
            /*walletService.update(partyId, Arith.sub(0, amount.doubleValue()));
            // 保存 资金日志
            MoneyLog moneylog = new MoneyLog();
@@ -116,7 +127,7 @@
            moneylog.setSymbol(ico.getSymbol());
            moneylog.setWalletType(ico.getSymbol());
            moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_OPEN);
            moneyLogService.save(moneylog);
            moneyLogService.save(moneylog);*/
            return Result.ok("申请成功" );
        } catch (Exception e) {
            throw new RuntimeException(e);
@@ -124,4 +135,75 @@
    }
    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());
            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) { //中签
                int applyNumber = userSubscription.getSubscribeNums() - model.getBallotNumber();
                BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
                //资金账户
                Wallet wallet = walletService.saveWalletByPartyId(userSubscription.getUserId());
                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(userSubscription.getUserId(), amount.negate().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(userSubscription.getUserId());
                moneylog.setSymbol(ico.getSymbol());
                moneylog.setWalletType(ico.getSymbol());
                moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_OPEN);
                moneyLogService.save(moneylog);
                userSubscription.setStatus(5);
            }
            if (model.getStatus() == 3 || model.getStatus() == 5) {  //转入账户
                //获取币账户
                WalletExtend walletExtend = walletService.saveExtendByPara(userSubscription.getUserId(), ico.getSymbol());
                walletService.updateExtend(userSubscription.getUserId(), walletExtend.getWallettype(), model.getBallotNumber());
            }
            userSubscriptionService.save(userSubscription);
            return Result.ok ( "操作成功" );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}