From 0d30c93d000413c6eb34f489ef17688ad4175201 Mon Sep 17 00:00:00 2001 From: KKSU <15274802129@163.com> Date: Mon, 29 Apr 2024 18:18:21 +0800 Subject: [PATCH] 55测试环境 --- src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java | 167 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 144 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java index e76d75b..f2cd874 100644 --- a/src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcOrderServiceImpl.java @@ -1,14 +1,18 @@ package com.xcong.excoin.modules.otc.service.impl; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollUtil; 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.conditions.query.QueryWrapper; 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.contants.AppContants; import com.xcong.excoin.common.exception.GlobalException; import com.xcong.excoin.common.response.Result; import com.xcong.excoin.common.system.service.CommonService; @@ -18,19 +22,19 @@ 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.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.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.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 com.xcong.excoin.rabbit.producer.OtcProducter; +import com.xcong.excoin.utils.RedisUtils; +import com.xcong.excoin.utils.ThreadPoolUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -45,19 +49,25 @@ @Service @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; + private final OtcSettingDao otcSettingDao; + private final RedisUtils redisUtils; + private final OtcProducter otcProducter; @Override @Transactional(rollbackFor = Exception.class) public Result buyOrder(OtcOrderAddDto orderAddDto) { MemberEntity member = LoginUserUtils.getAppLoginUser(); + +// MemberEntity member = memberDao.selectById(445L); OtcEntrustOrder entrustOrder = otcEntrustOrderDao.selectById(orderAddDto.getId()); if (entrustOrder == null) { throw new GlobalException("委托单不存在"); @@ -69,6 +79,13 @@ if (!OtcEntrustOrder.ORDER_TYPE_S.equals(entrustOrder.getOrderType())) { throw new GlobalException("无法购买"); + } + + OtcSetting setting = otcSettingDao.selectById(1L); + String times = redisUtils.getString(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId()); + times = times == null ? "0" :times; + if (setting.getCancellNum() <= Integer.parseInt(times)) { + throw new GlobalException("订单取消次数过多, 限制交易24小时"); } if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMinAmount()) < 0) { @@ -83,9 +100,23 @@ throw new GlobalException("剩余数量不足"); } - BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice()); - if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) { + BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice()).setScale(2, BigDecimal.ROUND_DOWN); + BigDecimal usdt = orderAddDto.getCnyAmount().divide(entrustOrder.getUnitPrice(), 2, BigDecimal.ROUND_DOWN); + if (cny.compareTo(orderAddDto.getCnyAmount()) != 0 && usdt.compareTo(orderAddDto.getUsdtAmount()) != 0) { + log.info("BUY = CNY:{}, CNY_{}, USDT:{}, USDT_{}", cny, orderAddDto.getCnyAmount(), usdt, orderAddDto.getUsdtAmount()); throw new GlobalException("数量与金额不符"); + } + /** + * 最多三单未付款 + */ + QueryWrapper<OtcOrder> objectQueryWrapper = new QueryWrapper<>(); + objectQueryWrapper.eq("member_id",member.getId()); + objectQueryWrapper.lt("status",OtcOrder.STATUS_PAY); + List<OtcOrder> otcOrders = this.baseMapper.selectList(objectQueryWrapper); + if(CollUtil.isNotEmpty(otcOrders)){ + if(otcOrders.size() >= 3){ + throw new GlobalException("最多可同时有三个待付款订单"); + } } OtcOrder otcOrder = new OtcOrder(); @@ -101,24 +132,30 @@ otcOrder.setEntrustMemberId(entrustOrder.getMemberId()); otcOrder.setOrderType(OtcEntrustOrder.ORDER_TYPE_B); - MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(entrustOrder.getMemberId(), 3, "1"); + 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.setBankName(defualtMethod.getBank()); otcOrder.setBankNo(defualtMethod.getAccount()); otcOrder.setCardName(defualtMethod.getName()); + otcOrder.setPayType(defualtMethod.getPaymentType()); OtcOrder sale = new OtcOrder(); BeanUtil.copyProperties(otcOrder, sale); sale.setMemberId(entrustOrder.getMemberId()); - otcOrder.setOppositeMemberId(member.getId()); + 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); + ThreadPoolUtils.sendOrderMsg(); return Result.ok("购买成功", otcOrder.getId()); } @@ -137,6 +174,13 @@ if (!OtcEntrustOrder.ORDER_TYPE_B.equals(entrustOrder.getOrderType())) { throw new GlobalException("无法出售"); + } + + OtcSetting setting = otcSettingDao.selectById(1L); + String times = redisUtils.getString(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId()); + times = times == null ? "0" :times; + if (setting.getCancellNum() <= Integer.parseInt(times)) { + throw new GlobalException("订单取消次数过多, 限制交易24小时"); } if (orderAddDto.getCnyAmount().compareTo(entrustOrder.getLimitMinAmount()) < 0) { @@ -160,8 +204,10 @@ throw new GlobalException("资金密码错误"); } - BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice()); - if (cny.compareTo(orderAddDto.getCnyAmount()) != 0) { + BigDecimal cny = orderAddDto.getUsdtAmount().multiply(entrustOrder.getUnitPrice()).setScale(2, BigDecimal.ROUND_DOWN); + BigDecimal usdt = orderAddDto.getCnyAmount().divide(entrustOrder.getUnitPrice(), 2, BigDecimal.ROUND_DOWN); + if (cny.compareTo(orderAddDto.getCnyAmount()) != 0 && usdt.compareTo(orderAddDto.getUsdtAmount()) != 0) { + log.info("SALE = CNY:{}, CNY_{}, USDT:{}, USDT_{}", cny, orderAddDto.getCnyAmount(), usdt, orderAddDto.getUsdtAmount()); throw new GlobalException("数量与金额不符"); } @@ -189,19 +235,27 @@ buy.setOppositeMemberId(member.getId()); buy.setOrderType(OtcEntrustOrder.ORDER_TYPE_B); - MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), 3, "1"); + MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), null, "1"); if (defualtMethod == null) { throw new GlobalException("未设置默认支付方式"); } - buy.setBankName(defualtMethod.getBank()); + + 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()); + + ThreadPoolUtils.sendOrderMsg(); return Result.ok("出售成功", otcOrder.getId()); } @@ -234,7 +288,7 @@ } if (StrUtil.isBlank(hasPayDto.getName())) { - MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), 3, "1"); + MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(member.getId(), null, "1"); hasPayDto.setName(defualtMethod.getName()); } @@ -271,6 +325,8 @@ memberWalletCoinDao.reduceFrozenBalance(saleWallet.getId(), buyOrder.getCoinAmount()); this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_FINISH, null, otcOrder.getOrderNo()); + otcProducter.sendMarketBussinessMsg(otcOrder.getEntrustOrderId(), OtcOrder.STATUS_FINISH); + otcProducter.sendOrderReturn(buyOrder.getOrderNo()); } @Override @@ -294,16 +350,29 @@ buyDetail.setTotalAmount(buyOrder.getTotalAmount()); buyDetail.setUnitPrice(buyOrder.getUnitPrice()); buyDetail.setCreateTime(buyOrder.getCreateTime()); - buyDetail.setIsMb(member.getIsTrader()); + buyDetail.setIsMb(member.getAccountType() == 1 ? 2 : 1); + buyDetail.setPayName(buyOrder.getPayName()); + buyDetail.setIsOwnEntrust(member.getId().equals(buyOrder.getEntrustMemberId()) ? 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.setSaleMemberId(saleMember.getId()); + buyDetail.setMemberId(member.getId()); buyDetail.setBankName(buyOrder.getBankName()); buyDetail.setBankNo(buyOrder.getBankNo()); - buyDetail.setPayName(buyOrder.getCardName()); + buyDetail.setCardName(buyOrder.getCardName()); + buyDetail.setPayType(buyOrder.getPayType()); buyDetail.setPayTime(buyOrder.getPayTime()); if (!buyOrder.getMemberId().equals(buyOrder.getEntrustMemberId())) { @@ -312,7 +381,6 @@ 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); @@ -336,7 +404,6 @@ } MemberEntity buyMember = memberDao.selectById(saleOrder.getOppositeMemberId()); - SaleOrderDetailVo saleDetail = new SaleOrderDetailVo(); saleDetail.setOrderNo(saleOrder.getOrderNo()); saleDetail.setUsdtAmount(saleOrder.getCoinAmount()); @@ -344,10 +411,22 @@ saleDetail.setTotalAmount(saleOrder.getTotalAmount()); saleDetail.setUnitPrice(saleOrder.getUnitPrice()); saleDetail.setCreateTime(saleOrder.getCreateTime()); - saleDetail.setIsMb(member.getIsTrader()); + saleDetail.setIsMb(member.getAccountType() == 1 ? 2 : 1); saleDetail.setPayName(saleOrder.getPayName()); - saleDetail.setSaleName(buyMember.getName()); + saleDetail.setSaleName(buyMember.getName()); + saleDetail.setSaleMemberId(buyMember.getId()); + saleDetail.setMemberId(member.getId()); + + saleDetail.setPayType(saleOrder.getPayType()); + saleDetail.setIsOwnEntrust(member.getId().equals(saleOrder.getEntrustMemberId()) ? 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()); @@ -365,6 +444,7 @@ @Override public void cancelOrder(Long id) { + MemberEntity member = LoginUserUtils.getAppLoginUser(); OtcOrder otcOrder = this.baseMapper.selectById(id); if (otcOrder == null) { throw new GlobalException("订单不存在"); @@ -380,7 +460,48 @@ memberWalletCoinDao.subFrozenBalance(saleOrder.getMemberId(), wallet.getId(), saleOrder.getCoinAmount()); } + OtcSetting setting = otcSettingDao.selectById(1L); + String times = redisUtils.getString(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId()); + if (StrUtil.isNotBlank(times)) { + int i = Integer.parseInt(times); + i++; + if (i >= setting.getCancellNum()) { + redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId(), i, 86400); + } else { + redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId(), i); + } + } else { + redisUtils.set(AppContants.OTC_ORDER_CANCEL_TIMES + member.getId(), 1, 86400); + } + otcEntrustOrderDao.updateRemainAmount(otcOrder.getEntrustOrderId(), otcOrder.getCoinAmount()); this.baseMapper.updateOrderStatusByOrderNo(OtcOrder.STATUS_CANCEL, null, otcOrder.getOrderNo()); + + otcProducter.sendMarketBussinessMsg(otcOrder.getEntrustOrderId(), OtcOrder.STATUS_CANCEL); + } + + @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("成功"); } } -- Gitblit v1.9.1