新版仿ok交易所-后端
zyy
2025-08-26 9cadad100ba1d3c18966f9df7e1c3213abc0d214
ico新增预支付
6 files modified
153 ■■■■■ changed files
trading-order-admin/src/main/java/com/yami/trading/admin/controller/ico/AdminIcoController.java 9 ●●●●● patch | view | raw | blame | history
trading-order-admin/src/main/java/com/yami/trading/api/controller/ApiIcoController.java 3 ●●●● patch | view | raw | blame | history
trading-order-bean/src/main/java/com/yami/trading/bean/ico/domain/Ico.java 6 ●●●●● patch | view | raw | blame | history
trading-order-common/src/main/java/com/yami/trading/common/constants/Constants.java 6 ●●●● patch | view | raw | blame | history
trading-order-common/src/main/java/com/yami/trading/common/domain/Result.java 4 ●●●● patch | view | raw | blame | history
trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java 125 ●●●●● patch | view | raw | blame | history
trading-order-admin/src/main/java/com/yami/trading/admin/controller/ico/AdminIcoController.java
@@ -1,5 +1,6 @@
package com.yami.trading.admin.controller.ico;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
@@ -8,6 +9,7 @@
import com.yami.trading.bean.ico.query.IcoQuery;
import com.yami.trading.bean.item.query.ItemQuery;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.query.QueryWrapperGenerator;
import com.yami.trading.service.ico.IcoService;
import com.yami.trading.service.ico.UserSubscriptionService;
@@ -48,6 +50,13 @@
    @ApiOperation(value = "保存新币")
    @PostMapping("save")
    public  Result <String> save(@Valid @RequestBody Ico ico) {
        if (ico.getId() == null) { //新增校验
            long count = icoService.count(new LambdaQueryWrapper<Ico>()
                    .eq(Ico::getSymbol, ico.getSymbol()));
            if(count > 0){
                throw new YamiShopBindException("代币符号已存在");
            }
        }
        //新增或编辑表单保存
        icoService.saveOrUpdate(ico);
        return Result.ok ( "保存产品成功" );
trading-order-admin/src/main/java/com/yami/trading/api/controller/ApiIcoController.java
@@ -60,7 +60,7 @@
                throw new YamiShopBindException("请稍后再试");
            }
            model.setUserId(partyId);
            icoService.subscribe(model);
            return icoService.subscribe(model);
        } catch (YamiShopBindException e) {
            log.error("错误信息: {}", e.getMessage(), e);
            throw e; // 重新抛出自定义异常
@@ -73,7 +73,6 @@
                rLock.unlock();
            }
        }
        return Result.succeed(null, "ok");
    }
}
trading-order-bean/src/main/java/com/yami/trading/bean/ico/domain/Ico.java
@@ -99,6 +99,12 @@
    @ApiModelProperty("是否开放合约交易 0:否 1 :是")
    private Integer isContractTrading;
    /**
     * 1.先有钱才能申购
     */
    @ApiModelProperty("是否预支付 0:否 1 :是")
    private Integer isPayDown;
    @ApiModelProperty("创建时间")
    private String createdAt;
trading-order-common/src/main/java/com/yami/trading/common/constants/Constants.java
@@ -167,8 +167,12 @@
    /**
     * 新币买入
     */
    public static final String MONEYLOG_CONTENT_NEW_COIN_OPEN = "new_coin_open";
    public static final String MONEYLOG_CONTENT_NEW_COIN_BUY = "new_coin_buy";
    /**
     * 新币买入退回
     */
    public static final String MONEYLOG_CONTENT_NEW_COIN_RT_BUY = "new_coin_rt_buy";
    /**
trading-order-common/src/main/java/com/yami/trading/common/domain/Result.java
@@ -32,6 +32,10 @@
        return of(model, CodeEnum.SUCCESS.getCode(), "");
    }
    public static <T> Result<T> ok(String msg) {
        return of(null, CodeEnum.SUCCESS.getCode(), msg);
    }
    public static <T> Result<T> of(T datas, Integer code, String msg) {
trading-order-service/src/main/java/com/yami/trading/service/ico/IcoService.java
@@ -1,10 +1,7 @@
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;
@@ -22,16 +19,12 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
@@ -56,13 +49,8 @@
    @Autowired
    MoneyLogService moneyLogService;
    @Autowired
    WalletExtendService walletExtendService;
    /**
     * 新币申购
     * @param model
     * @return
     */
    public Result<String> subscribe(UserSubscription model) {
        try {
@@ -91,10 +79,6 @@
            }
            //购买金额
            BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
            /*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());
            }
@@ -114,21 +98,29 @@
            if (model.getSubscriptionType() == null) { //默认自主申购
                model.setSubscriptionType(1);
            }
            userSubscriptionService.save(model);
            /*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_OPEN);
            moneyLogService.save(moneylog);*/
            //需要先支付
            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);
@@ -155,6 +147,7 @@
                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");
@@ -166,40 +159,62 @@
            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));
                //资金账户
                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("操作钱包失败!");
                //已经预支付
                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 {
                    //扣除资金
                    walletService.update(userSubscription.getUserId(), amount.negate().doubleValue());
                    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);
                }
                //保存 资金日志
                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());
                WalletExtend walletExtend = walletService.saveExtendByPara(userId, ico.getSymbol());
                walletService.updateExtend(userId, walletExtend.getWallettype(), model.getBallotNumber());
            }
            userSubscriptionService.save(userSubscription);
            return Result.ok ( "操作成功" );