package com.xcong.excoin.modules.otc.service.impl;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.date.DateUnit;
|
import cn.hutool.core.date.DateUtil;
|
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.response.Result;
|
import com.xcong.excoin.common.system.service.CommonService;
|
import com.xcong.excoin.modules.member.dao.MemberDao;
|
import com.xcong.excoin.modules.member.dao.MemberPaymentMethodDao;
|
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
|
import com.xcong.excoin.modules.member.entity.MemberEntity;
|
import com.xcong.excoin.modules.member.entity.MemberPaymentMethodEntity;
|
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
|
import com.xcong.excoin.modules.otc.dao.OtcEntrustOrderDao;
|
import com.xcong.excoin.modules.otc.dao.OtcMarketBussinessDao;
|
import com.xcong.excoin.modules.otc.dto.HasPayDto;
|
import com.xcong.excoin.modules.otc.dto.OrderListDto;
|
import com.xcong.excoin.modules.otc.dto.OtcOrderAddDto;
|
import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder;
|
import com.xcong.excoin.modules.otc.entity.OtcMarketBussiness;
|
import com.xcong.excoin.modules.otc.entity.OtcOrder;
|
import com.xcong.excoin.modules.otc.dao.OtcOrderDao;
|
import com.xcong.excoin.modules.otc.service.OtcOrderService;
|
import com.xcong.excoin.modules.otc.vo.BuyOrderDetailVo;
|
import com.xcong.excoin.modules.otc.vo.OrderListVo;
|
import com.xcong.excoin.modules.otc.vo.SaleOrderDetailVo;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.List;
|
|
@Slf4j
|
@Service
|
@RequiredArgsConstructor
|
public class OtcOrderServiceImpl extends ServiceImpl<OtcOrderDao, OtcOrder> implements OtcOrderService {
|
|
private final OtcMarketBussinessDao otcMarketBussinessDao;
|
private final OtcEntrustOrderDao otcEntrustOrderDao;
|
private final CommonService commonService;
|
private final MemberWalletCoinDao memberWalletCoinDao;
|
private final MemberDao memberDao;
|
private final MemberPaymentMethodDao memberPaymentMethodDao;
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Result buyOrder(OtcOrderAddDto orderAddDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(orderAddDto.getId());
|
if (entrustOrder == null) {
|
throw new GlobalException("委托单不存在");
|
}
|
|
if (member.getId().equals(entrustOrder.getMemberId())) {
|
throw new GlobalException("不能购买自己的委托单");
|
}
|
|
if (!OtcEntrustOrder.ORDER_TYPE_S.equals(entrustOrder.getOrderType())) {
|
throw new GlobalException("无法购买");
|
}
|
|
if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMinAmount()) < 0) {
|
throw new GlobalException("低于最低限额");
|
}
|
|
if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMaxAmount()) > 0) {
|
throw new GlobalException("高于最高限额");
|
}
|
|
if (orderAddDto.getUsdtAmount().compareTo(entrustOrder.getRemainCoinAmount()) > 0) {
|
throw new GlobalException("剩余数量不足");
|
}
|
|
BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice());
|
if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) {
|
throw new GlobalException("数量与金额不符");
|
}
|
|
OtcOrder otcOrder = new OtcOrder();
|
otcOrder.setOrderNo(commonService.generateOrderNo(member.getId()));
|
otcOrder.setUnitPrice(entrustOrder.getUnitPrice());
|
otcOrder.setEntrustOrderId(entrustOrder.getId());
|
otcOrder.setCoinAmount(orderAddDto.getUsdtAmount());
|
otcOrder.setTotalAmount(orderAddDto.getCnyAmount());
|
otcOrder.setMemberId(member.getId());
|
otcOrder.setStatus(OtcOrder.STATUS_SUBMIT);
|
otcOrder.setPayTime(new Date());
|
otcOrder.setEntrustMemberId(entrustOrder.getMemberId());
|
otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_B);
|
|
OtcOrder sale = new OtcOrder();
|
BeanUtil.copyProperties(otcOrder, sale);
|
sale.setMemberId(entrustOrder.getMemberId());
|
sale.setOrderType(OtcEntrustOrder.ORDER_TYPE_S);
|
otcEntrustOrderDao.updateRemainAmount(entrustOrder.getId(), orderAddDto.getUsdtAmount().negate());
|
this.baseMapper.insert(otcOrder);
|
this.baseMapper.insert(sale);
|
|
return Result.ok("购买成功", otcOrder.getId());
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Result saleOrder(OtcOrderAddDto orderAddDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(orderAddDto.getId());
|
if (entrustOrder == null) {
|
throw new GlobalException("委托单不存在");
|
}
|
|
if (member.getId().equals(entrustOrder.getMemberId())) {
|
throw new GlobalException("不能购买自己的委托单");
|
}
|
|
if (!OtcEntrustOrder.ORDER_TYPE_B.equals(entrustOrder.getOrderType())) {
|
throw new GlobalException("无法出售");
|
}
|
|
if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMinAmount()) < 0) {
|
throw new GlobalException("低于最低限额");
|
}
|
|
if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMaxAmount()) > 0) {
|
throw new GlobalException("高于最高限额");
|
}
|
|
if (orderAddDto.getUsdtAmount().compareTo(entrustOrder.getRemainCoinAmount()) > 0) {
|
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("数量与金额不符");
|
}
|
|
MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
|
if (wallet.getAvailableBalance().compareTo(orderAddDto.getUsdtAmount()) < 0) {
|
throw new GlobalException("钱包余额不足");
|
}
|
|
OtcOrder otcOrder = new OtcOrder();
|
otcOrder.setOrderNo(commonService.generateOrderNo(member.getId()));
|
otcOrder.setUnitPrice(entrustOrder.getUnitPrice());
|
otcOrder.setEntrustOrderId(entrustOrder.getId());
|
otcOrder.setCoinAmount(orderAddDto.getUsdtAmount());
|
otcOrder.setTotalAmount(orderAddDto.getCnyAmount());
|
otcOrder.setMemberId(member.getId());
|
otcOrder.setStatus(OtcOrder.STATUS_SUBMIT);
|
otcOrder.setPayTime(new Date());
|
otcOrder.setEntrustMemberId(entrustOrder.getMemberId());
|
otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_S);
|
|
OtcOrder buy = new OtcOrder();
|
BeanUtil.copyProperties(otcOrder, buy);
|
buy.setMemberId(entrustOrder.getMemberId());
|
buy.setOrderType(OtcEntrustOrder.ORDER_TYPE_B);
|
|
otcEntrustOrderDao.updateRemainAmount(entrustOrder.getId(), orderAddDto.getUsdtAmount().negate());
|
this.baseMapper.insert(otcOrder);
|
this.baseMapper.insert(buy);
|
|
memberWalletCoinDao.updateFrozenBalance(member.getId(), wallet.getId(), orderAddDto.getUsdtAmount());
|
return Result.ok("出售成功", otcOrder.getId());
|
}
|
|
@Override
|
public IPage<OrderListVo> findOrderListInPage(OrderListDto orderListDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
Page<OrderListVo> page = new Page<>(orderListDto.getPageNum(), orderListDto.getPageSize());
|
|
OtcOrder order = new OtcOrder();
|
order.setStatus(orderListDto.getStatus());
|
order.setMemberId(member.getId());
|
return this.baseMapper.selectOrdderListInPage(order, page);
|
}
|
|
@Override
|
public void hasPay(HasPayDto hasPayDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcOrder otcOrder = this.baseMapper.selectById(hasPayDto.getId());
|
|
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("不是购买单");
|
}
|
|
if (StrUtil.isBlank(hasPayDto.getName())) {
|
MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), 3, "1");
|
hasPayDto.setName(defualtMethod.getName());
|
}
|
|
this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, hasPayDto.getName(), 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_FINISH, null, otcOrder.getOrderNo());
|
}
|
|
@Override
|
public Result findBuyOrderDetail(Long id) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
member = memberDao.selectById(member.getId());
|
OtcOrder otcOrder = this.baseMapper.selectById(id);
|
if (otcOrder == null) {
|
return Result.fail("订单不存在");
|
}
|
|
OtcOrder buyOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_B);
|
if (buyOrder == null) {
|
return Result.fail("参数错误");
|
}
|
|
BuyOrderDetailVo buyDetail = new BuyOrderDetailVo();
|
buyDetail.setOrderNo(buyOrder.getOrderNo());
|
buyDetail.setUsdtAmount(buyOrder.getCoinAmount());
|
buyDetail.setStatus(buyOrder.getStatus());
|
buyDetail.setTotalAmount(buyOrder.getTotalAmount());
|
buyDetail.setUnitPrice(buyOrder.getUnitPrice());
|
buyDetail.setCreateTime(new Date());
|
buyDetail.setIsMb(member.getIsTrader());
|
|
OtcOrder saleOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S);
|
MemberEntity saleMember = memberDao.selectById(saleOrder.getMemberId());
|
|
buyDetail.setSaleName(saleMember.getName());
|
MemberPaymentMethodEntity defaultMethod = memberPaymentMethodDao.selectDefualtMethod(saleOrder.getMemberId(), 3, "1");
|
buyDetail.setBankName(defaultMethod.getName());
|
buyDetail.setBankNo(defaultMethod.getAccount());
|
buyDetail.setPayName(defaultMethod.getName());
|
buyDetail.setPayTime(buyDetail.getPayTime());
|
|
if (!buyOrder.getMemberId().equals(buyOrder.getEntrustMemberId())) {
|
OtcMarketBussiness otcMb = otcMarketBussinessDao.selectMarketBussinessByMemberId(buyOrder.getEntrustMemberId());
|
buyDetail.setMbId(otcMb.getId());
|
buyDetail.setFinishRatio(otcMb.getFinishRatio());
|
buyDetail.setOrderCnt(otcMb.getBuyCnt());
|
}
|
|
long between = DateUtil.between(buyOrder.getCreateTime(), new Date(), DateUnit.SECOND);
|
buyDetail.setTimes(between);
|
|
return Result.ok(buyDetail);
|
}
|
|
@Override
|
public Result findSaleOrderDetail(Long id) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcOrder otcOrder = this.baseMapper.selectById(id);
|
if (otcOrder == null) {
|
return Result.fail("订单不存在");
|
}
|
|
OtcOrder saleOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S);
|
if (saleOrder == null) {
|
return Result.fail("参数错误");
|
}
|
|
SaleOrderDetailVo saleDetail = new SaleOrderDetailVo();
|
saleDetail.setOrderNo(saleOrder.getOrderNo());
|
saleDetail.setUsdtAmount(saleOrder.getCoinAmount());
|
saleDetail.setStatus(saleOrder.getStatus());
|
saleDetail.setTotalAmount(saleOrder.getTotalAmount());
|
saleDetail.setUnitPrice(saleOrder.getUnitPrice());
|
saleDetail.setCreateTime(new Date());
|
saleDetail.setIsMb(member.getIsTrader());
|
saleDetail.setPayName(saleOrder.getPayName());
|
|
if (!saleOrder.getMemberId().equals(saleOrder.getEntrustMemberId())) {
|
OtcMarketBussiness otcMb = otcMarketBussinessDao.selectMarketBussinessByMemberId(saleOrder.getEntrustMemberId());
|
saleDetail.setMbId(otcMb.getId());
|
saleDetail.setFinishRatio(otcMb.getFinishRatio());
|
saleDetail.setOrderCnt(otcMb.getBuyCnt());
|
}
|
|
return Result.ok(saleDetail);
|
}
|
|
@Override
|
public void cancelOrder(Long id) {
|
OtcOrder otcOrder = this.baseMapper.selectById(id);
|
if (otcOrder == null) {
|
throw new GlobalException("订单不存在");
|
}
|
|
if (!OtcOrder.STATUS_SUBMIT.equals(otcOrder.getStatus())) {
|
throw new GlobalException("不能取消");
|
}
|
|
otcEntrustOrderDao.updateRemainAmount(otcOrder.getEntrustOrderId(), otcOrder.getCoinAmount());
|
this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_CANCEL, null, otcOrder.getOrderNo());
|
}
|
}
|