Helius
2021-05-20 d8fb05f44d8969921b7128d92be282176475ee80
src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java
@@ -1,12 +1,15 @@
package com.xcong.excoin.modules.otc.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.excoin.common.LoginUserUtils;
import com.xcong.excoin.common.exception.GlobalException;
import com.xcong.excoin.common.system.service.CommonService;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
@@ -26,6 +29,7 @@
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
@@ -35,6 +39,7 @@
    private final OtcEntrustOrderDao otcEntrustOrderDao;
    private final CommonService commonService;
    private final MemberWalletCoinDao memberWalletCoinDao;
    private final MemberDao memberDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -93,6 +98,15 @@
            throw new GlobalException("无法出售");
        }
        if (StrUtil.isBlank(orderAddDto.getPassword())) {
            throw new GlobalException("资金密码不能为空");
        }
        MemberEntity memberEntity = memberDao.selectById(member.getId());
        if (!SecureUtil.md5(orderAddDto.getPassword()).equals(memberEntity.getTradePassword())) {
            throw new GlobalException("资金密码错误");
        }
        BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice());
        if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) {
            throw new GlobalException("数量与金额不符");
@@ -137,4 +151,55 @@
        order.setMemberId(member.getId());
        return this.baseMapper.selectOrdderListInPage(order, page);
    }
    @Override
    public void hasPay(Long id) {
        OtcOrder otcOrder = this.baseMapper.selectById(id);
        if (otcOrder == null) {
            throw new GlobalException("订单不存在");
        }
        if (!OtcOrder.STATUS_SUBMIT.equals(otcOrder.getStatus())) {
            throw new GlobalException("状态不正确");
        }
        if (!OtcEntrustOrder.ORDER_TYPE_B.equals(otcOrder.getOrderType())) {
            throw new GlobalException("不是购买单");
        }
        this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, otcOrder.getOrderNo());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void finishOrder(Long id) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcOrder otcOrder = this.baseMapper.selectById(id);
        if (otcOrder == null) {
            throw new GlobalException("订单不存在");
        }
        if (!OtcOrder.STATUS_PAY.equals(otcOrder.getStatus())) {
            throw new GlobalException("状态不正确");
        }
        if (!OtcEntrustOrder.ORDER_TYPE_S.equals(otcOrder.getOrderType())) {
            throw new GlobalException("不是出售单");
        }
        OtcOrder buyOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_B);
        OtcOrder saleOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S);
        MemberWalletCoinEntity buyWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(buyOrder.getMemberId(), "USDT");
        MemberWalletCoinEntity saleWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(saleOrder.getMemberId(), "USDT");
        // 购买者钱包可用新增币
        memberWalletCoinDao.updateBlockBalance(buyWallet.getId(), buyOrder.getCoinAmount(), BigDecimal.ZERO, 0);
        // 出售者钱包冻结减少币
        memberWalletCoinDao.reduceFrozenBalance(saleWallet.getId(), buyOrder.getCoinAmount());
        this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, otcOrder.getOrderNo());
    }
}