| | |
| | | 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.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | 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.dto.OrderAddDto; |
| | | import com.xcong.excoin.modules.otc.dao.*; |
| | | import com.xcong.excoin.modules.otc.dto.HasPayDto; |
| | | import com.xcong.excoin.modules.otc.dto.OrderApealDto; |
| | | import com.xcong.excoin.modules.otc.dto.OrderListDto; |
| | | import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder; |
| | | import com.xcong.excoin.modules.otc.entity.OtcOrder; |
| | | import com.xcong.excoin.modules.otc.dao.OtcOrderDao; |
| | | import com.xcong.excoin.modules.otc.dto.OtcOrderAddDto; |
| | | import com.xcong.excoin.modules.otc.entity.*; |
| | | 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; |
| | |
| | | @RequiredArgsConstructor |
| | | public class OtcOrderServiceImpl extends ServiceImpl<OtcOrderDao, OtcOrder> implements OtcOrderService { |
| | | |
| | | private final OtcMarketBussinessDao otcMarketBussinessDao; |
| | | private final OtcEntrustOrderDao otcEntrustOrderDao; |
| | | private final OtcOrderAppealDao otcOrderAppealDao; |
| | | private final CommonService commonService; |
| | | private final MemberWalletCoinDao memberWalletCoinDao; |
| | | private final MemberDao memberDao; |
| | | private final MemberPaymentMethodDao memberPaymentMethodDao; |
| | | private final OtcBlackListDao otcBlackListDao; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void buyOrder(OrderAddDto orderAddDto) { |
| | | 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("无足够的USDT"); |
| | | throw new GlobalException("剩余数量不足"); |
| | | } |
| | | |
| | | BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice()); |
| | | log.info("--->{}", cny); |
| | | if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) { |
| | | throw new GlobalException("数量与金额不符"); |
| | | } |
| | |
| | | otcOrder.setCoinAmount(orderAddDto.getUsdtAmount()); |
| | | otcOrder.setTotalAmount(orderAddDto.getCnyAmount()); |
| | | otcOrder.setMemberId(member.getId()); |
| | | otcOrder.setOppositeMemberId(entrustOrder.getMemberId()); |
| | | otcOrder.setStatus(OtcOrder.STATUS_SUBMIT); |
| | | otcOrder.setPayTime(new Date()); |
| | | otcOrder.setEntrustMemberId(entrustOrder.getMemberId()); |
| | | otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_B); |
| | | |
| | | MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(entrustOrder.getMemberId(), null, "1"); |
| | | if (defualtMethod == null) { |
| | | throw new GlobalException("对方未设置默认支付方式"); |
| | | } |
| | | if (MemberPaymentMethodEntity.PAYMENTTYPE_CARD.toString().equals(defualtMethod.getPaymentType())) { |
| | | otcOrder.setBankName(defualtMethod.getBank()); |
| | | } else { |
| | | otcOrder.setBankName(defualtMethod.getPaymentQrcode()); |
| | | } |
| | | |
| | | otcOrder.setBankNo(defualtMethod.getAccount()); |
| | | otcOrder.setCardName(defualtMethod.getName()); |
| | | otcOrder.setPayType(defualtMethod.getPaymentType()); |
| | | |
| | | OtcOrder sale = new OtcOrder(); |
| | | BeanUtil.copyProperties(otcOrder, sale); |
| | | sale.setMemberId(entrustOrder.getMemberId()); |
| | | sale.setOppositeMemberId(member.getId()); |
| | | 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 void saleOrder(OrderAddDto orderAddDto) { |
| | | 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())) { |
| | |
| | | otcOrder.setStatus(OtcOrder.STATUS_SUBMIT); |
| | | otcOrder.setPayTime(new Date()); |
| | | otcOrder.setEntrustMemberId(entrustOrder.getMemberId()); |
| | | otcOrder.setOppositeMemberId(entrustOrder.getMemberId()); |
| | | otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_S); |
| | | |
| | | OtcOrder buy = new OtcOrder(); |
| | | BeanUtil.copyProperties(otcOrder, buy); |
| | | buy.setMemberId(entrustOrder.getMemberId()); |
| | | buy.setOppositeMemberId(member.getId()); |
| | | buy.setOrderType(OtcEntrustOrder.ORDER_TYPE_B); |
| | | |
| | | MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), null, "1"); |
| | | if (defualtMethod == null) { |
| | | throw new GlobalException("未设置默认支付方式"); |
| | | } |
| | | |
| | | if (MemberPaymentMethodEntity.PAYMENTTYPE_CARD.toString().equals(defualtMethod.getPaymentType())) { |
| | | buy.setBankName(defualtMethod.getBank()); |
| | | } else { |
| | | buy.setBankName(defualtMethod.getPaymentQrcode()); |
| | | } |
| | | buy.setBankNo(defualtMethod.getAccount()); |
| | | buy.setCardName(defualtMethod.getName()); |
| | | buy.setPayType(defualtMethod.getPaymentType()); |
| | | |
| | | 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 |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void hasPay(Long id) { |
| | | OtcOrder otcOrder = this.baseMapper.selectById(id); |
| | | 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_PAY.equals(otcOrder.getStatus())) { |
| | | if (!OtcOrder.STATUS_SUBMIT.equals(otcOrder.getStatus())) { |
| | | throw new GlobalException("状态不正确"); |
| | | } |
| | | |
| | | if (OtcEntrustOrder.ORDER_TYPE_B.equals(otcOrder.getOrderType())) { |
| | | if (!OtcEntrustOrder.ORDER_TYPE_B.equals(otcOrder.getOrderType())) { |
| | | throw new GlobalException("不是购买单"); |
| | | } |
| | | |
| | | this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, otcOrder.getOrderNo()); |
| | | if (StrUtil.isBlank(hasPayDto.getName())) { |
| | | MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), null, "1"); |
| | | hasPayDto.setName(defualtMethod.getName()); |
| | | } |
| | | |
| | | this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, hasPayDto.getName(), otcOrder.getOrderNo()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | throw new GlobalException("订单不存在"); |
| | | } |
| | | |
| | | if (OtcOrder.STATUS_PAY.equals(otcOrder.getStatus())) { |
| | | if (!OtcOrder.STATUS_PAY.equals(otcOrder.getStatus())) { |
| | | throw new GlobalException("状态不正确"); |
| | | } |
| | | |
| | | if (OtcEntrustOrder.ORDER_TYPE_S.equals(otcOrder.getOrderType())) { |
| | | 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); |
| | |
| | | // 出售者钱包冻结减少币 |
| | | memberWalletCoinDao.reduceFrozenBalance(saleWallet.getId(), buyOrder.getCoinAmount()); |
| | | |
| | | this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_PAY, otcOrder.getOrderNo()); |
| | | 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(buyOrder.getCreateTime()); |
| | | buyDetail.setIsMb(member.getIsTrader()); |
| | | buyDetail.setPayName(buyOrder.getPayName()); |
| | | buyDetail.setIsOwnEntrust(member.getId().equals(buyOrder.getEntrustOrderId()) ? 1 : 2); |
| | | |
| | | OtcOrder saleOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S); |
| | | MemberEntity saleMember = memberDao.selectById(saleOrder.getMemberId()); |
| | | |
| | | OtcBlackList otcBlackList = otcBlackListDao.selectByMemberIdAndBlackMemberId(member.getId(), buyOrder.getEntrustMemberId()); |
| | | if (otcBlackList != null) { |
| | | buyDetail.setIsBlack(1); |
| | | } else { |
| | | buyDetail.setIsBlack(2); |
| | | } |
| | | |
| | | buyDetail.setSaleName(saleMember.getName()); |
| | | |
| | | buyDetail.setBankName(buyOrder.getBankName()); |
| | | buyDetail.setBankNo(buyOrder.getBankNo()); |
| | | buyDetail.setCardName(buyOrder.getCardName()); |
| | | buyDetail.setPayType(buyOrder.getPayType()); |
| | | buyDetail.setPayTime(buyOrder.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()); |
| | | } |
| | | |
| | | if (OtcOrder.STATUS_SUBMIT.equals(buyOrder.getStatus())) { |
| | | long between = DateUtil.between(new Date(), DateUtil.offsetMinute(buyOrder.getCreateTime(), 30), DateUnit.SECOND, false); |
| | | 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("参数错误"); |
| | | } |
| | | MemberEntity buyMember = memberDao.selectById(saleOrder.getOppositeMemberId()); |
| | | |
| | | 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(saleOrder.getCreateTime()); |
| | | saleDetail.setIsMb(member.getIsTrader()); |
| | | saleDetail.setPayName(saleOrder.getPayName()); |
| | | saleDetail.setSaleName(buyMember.getName()); |
| | | saleDetail.setPayType(saleOrder.getPayType()); |
| | | saleDetail.setIsOwnEntrust(member.getId().equals(saleOrder.getEntrustOrderId()) ? 1 : 2); |
| | | |
| | | OtcBlackList otcBlackList = otcBlackListDao.selectByMemberIdAndBlackMemberId(member.getId(), saleOrder.getEntrustMemberId()); |
| | | if (otcBlackList != null) { |
| | | saleDetail.setIsBlack(1); |
| | | } else { |
| | | saleDetail.setIsBlack(2); |
| | | } |
| | | if (!saleOrder.getMemberId().equals(saleOrder.getEntrustMemberId())) { |
| | | OtcMarketBussiness otcMb = otcMarketBussinessDao.selectMarketBussinessByMemberId(saleOrder.getEntrustMemberId()); |
| | | saleDetail.setMbId(otcMb.getId()); |
| | | saleDetail.setFinishRatio(otcMb.getFinishRatio()); |
| | | saleDetail.setOrderCnt(otcMb.getBuyCnt()); |
| | | } |
| | | |
| | | if (OtcOrder.STATUS_SUBMIT.equals(saleOrder.getStatus())) { |
| | | long between = DateUtil.between(new Date(), DateUtil.offsetMinute(saleOrder.getCreateTime(), 30), DateUnit.SECOND, false); |
| | | saleDetail.setTimes(between); |
| | | } |
| | | |
| | | 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("不能取消"); |
| | | } |
| | | |
| | | OtcOrder saleOrder = this.baseMapper.selectOrderByOrderNoAndType(otcOrder.getOrderNo(), OtcEntrustOrder.ORDER_TYPE_S); |
| | | if (!saleOrder.getMemberId().equals(saleOrder.getEntrustMemberId())) { |
| | | MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(saleOrder.getMemberId(), "USDT"); |
| | | memberWalletCoinDao.subFrozenBalance(saleOrder.getMemberId(), wallet.getId(), saleOrder.getCoinAmount()); |
| | | } |
| | | |
| | | otcEntrustOrderDao.updateRemainAmount(otcOrder.getEntrustOrderId(), otcOrder.getCoinAmount()); |
| | | this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_CANCEL, null, otcOrder.getOrderNo()); |
| | | } |
| | | |
| | | @Override |
| | | public Result orderApeal(OrderApealDto orderApealDto) { |
| | | MemberEntity member = LoginUserUtils.getAppLoginUser(); |
| | | Long memberId = member.getId(); |
| | | // long memberId = 446L; |
| | | Long orderId = orderApealDto.getOrderId(); |
| | | OtcOrder otcOrder = this.baseMapper.selectById(orderId); |
| | | if(ObjectUtil.isEmpty(otcOrder)){ |
| | | return Result.fail("订单不存在"); |
| | | } |
| | | String reason = orderApealDto.getReason(); |
| | | if(StrUtil.isEmpty(reason)){ |
| | | return Result.fail("请填写申诉原因"); |
| | | } |
| | | List<String> content = orderApealDto.getContent(); |
| | | OtcOrderAppeal otcOrderAppeal = new OtcOrderAppeal(); |
| | | otcOrderAppeal.setMemberId(memberId); |
| | | otcOrderAppeal.setOrderId(orderId); |
| | | otcOrderAppeal.setReason(reason); |
| | | otcOrderAppeal.setContent(content.toString()); |
| | | otcOrderAppeal.setStatus(OtcOrderAppeal.STATUS_ONE); |
| | | otcOrderAppealDao.insert(otcOrderAppeal); |
| | | return Result.ok("成功"); |
| | | } |
| | | } |