package com.xcong.excoin.modules.otc.service.impl;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
import com.alibaba.fastjson.JSONObject;
|
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.MemberAuthenticationDao;
|
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.OtcBlackListDao;
|
import com.xcong.excoin.modules.otc.dao.OtcMarketBussinessDao;
|
import com.xcong.excoin.modules.otc.dao.OtcOrderDao;
|
import com.xcong.excoin.modules.otc.dto.EntrustOrderAddDto;
|
import com.xcong.excoin.modules.otc.dto.EntrustOrderListDto;
|
import com.xcong.excoin.modules.otc.entity.OtcBlackList;
|
import com.xcong.excoin.modules.otc.entity.OtcEntrustOrder;
|
import com.xcong.excoin.modules.otc.dao.OtcEntrustOrderDao;
|
import com.xcong.excoin.modules.otc.entity.OtcMarketBussiness;
|
import com.xcong.excoin.modules.otc.entity.OtcOrder;
|
import com.xcong.excoin.modules.otc.mapper.OtcEntrustOrderMapper;
|
import com.xcong.excoin.modules.otc.service.OtcEntrustOrderService;
|
import com.xcong.excoin.modules.otc.vo.EntrustListInfoVo;
|
import com.xcong.excoin.modules.otc.vo.EntrustListVo;
|
import com.xcong.excoin.modules.otc.vo.EntrustOrderDetailVo;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import netscape.javascript.JSObject;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
@Slf4j
|
@Service
|
@RequiredArgsConstructor
|
public class OtcEntrustOrderServiceImpl extends ServiceImpl<OtcEntrustOrderDao, OtcEntrustOrder> implements OtcEntrustOrderService {
|
|
private final MemberDao memberDao;
|
private final MemberPaymentMethodDao memberPaymentMethodDao;
|
private final OtcMarketBussinessDao otcMarketBussinessDao;
|
private final MemberWalletCoinDao memberWalletCoinDao;
|
private final OtcOrderDao otcOrderDao;
|
private final CommonService commonService;
|
private final OtcBlackListDao otcBlackListDao;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void add(EntrustOrderAddDto addDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcEntrustOrder otcEntrustOrder = OtcEntrustOrderMapper.INSTANCE.entrustOrderDtoToEntity(addDto);
|
otcEntrustOrder.setMemberId(member.getId());
|
|
member = memberDao.selectById(member.getId());
|
if (!MemberEntity.CERTIFY_STATUS_Y.equals(member.getCertifyStatus())) {
|
throw new GlobalException("未实名认证");
|
}
|
|
List<MemberPaymentMethodEntity> payments = memberPaymentMethodDao.selectByMemberId(member.getId());
|
if (CollUtil.isEmpty(payments)) {
|
throw new GlobalException("未绑定收款方式");
|
}
|
|
BigDecimal totalAmount = addDto.getUnitPrice().multiply(addDto.getAmount());
|
otcEntrustOrder.setTotalAmount(totalAmount);
|
|
if (totalAmount.compareTo(addDto.getMax()) < 0) {
|
throw new GlobalException("最大限额应小于总金额");
|
}
|
|
if (OtcEntrustOrder.ORDER_TYPE_S.equals(addDto.getType())) {
|
MemberWalletCoinEntity coinWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
|
// BigDecimal multiply = addDto.getUnitPrice().multiply(coinWallet.getAvailableBalance());
|
if(coinWallet.getAvailableBalance().compareTo(addDto.getAmount()) < 0) {
|
throw new GlobalException("可用金额不足");
|
}
|
|
memberWalletCoinDao.updateFrozenBalance(member.getId(), coinWallet.getId(), addDto.getAmount());
|
}
|
|
if (!MemberEntity.IS_TRADER_Y.equals(member.getIsTrader())) {
|
throw new GlobalException("不是市商");
|
}
|
|
if (member.getIsTrader() == 2) {
|
otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
|
} else {
|
otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_Y);
|
}
|
|
otcEntrustOrder.setRemainCoinAmount(addDto.getAmount());
|
otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_UP);
|
otcEntrustOrder.setEntrustOrderNo(commonService.generateOrderNo(member.getId()));
|
otcEntrustOrder.setId(null);
|
this.baseMapper.insert(otcEntrustOrder);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void modify(EntrustOrderAddDto modifyDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcEntrustOrder otcEntrustOrder = OtcEntrustOrderMapper.INSTANCE.entrustOrderDtoToEntity(modifyDto);
|
BigDecimal remainAmount = otcEntrustOrder.getRemainCoinAmount();
|
otcEntrustOrder.setMemberId(member.getId());
|
|
otcEntrustOrder.setRemainCoinAmount(modifyDto.getAmount());
|
otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_UP);
|
|
List<OtcOrder> orders = otcOrderDao.selectOrderListUnFinish(member.getId(), otcEntrustOrder.getId());
|
if (CollUtil.isNotEmpty(orders)) {
|
throw new GlobalException("存在未完成的订单, 无法编辑");
|
}
|
|
OtcEntrustOrder prevEntity = this.baseMapper.selectById(modifyDto.getId());
|
if (prevEntity == null) {
|
throw new GlobalException("参数错误");
|
}
|
|
if (!prevEntity.getOrderType().equals(modifyDto.getType())) {
|
throw new GlobalException("类型错误");
|
}
|
|
if (member.getIsTrader() == 2) {
|
otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
|
} else {
|
otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_Y);
|
}
|
|
BigDecimal totalAmount = modifyDto.getUnitPrice().multiply(modifyDto.getAmount());
|
otcEntrustOrder.setTotalAmount(totalAmount);
|
if (OtcEntrustOrder.ORDER_TYPE_S.equals(modifyDto.getType())) {
|
MemberWalletCoinEntity coinWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
|
coinWallet.setAvailableBalance(coinWallet.getAvailableBalance().add(remainAmount));
|
if(coinWallet.getAvailableBalance().compareTo(modifyDto.getAmount()) < 0) {
|
throw new GlobalException("可用金额不足");
|
}
|
|
BigDecimal frozen = coinWallet.getFrozenBalance().subtract(remainAmount);
|
coinWallet.setAvailableBalance(coinWallet.getAvailableBalance().subtract(modifyDto.getAmount()));
|
coinWallet.setFrozenBalance(frozen.add(modifyDto.getAmount()));
|
memberWalletCoinDao.updateById(coinWallet);
|
}
|
|
this.baseMapper.updateById(otcEntrustOrder);
|
}
|
|
@Override
|
public IPage<EntrustListVo> findEntrustListInPage(EntrustOrderListDto dto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
Page<EntrustListVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
|
// List<OtcBlackList> blackLists = otcBlackListDao.selectBlackListByMemberId(member.getId());
|
// if (CollUtil.isNotEmpty(blackLists)) {
|
dto.setMemberId(member.getId());
|
// }
|
return this.baseMapper.selectEntrustListInPage(dto, page);
|
}
|
|
@Override
|
public List<EntrustListInfoVo> findOwnEntrustOrder(EntrustOrderListDto orderListDto) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
|
OtcEntrustOrder query = new OtcEntrustOrder();
|
query.setStatus(3);
|
query.setMemberId(member.getId());
|
|
Page<OtcEntrustOrder> page = new Page<>(orderListDto.getPageNum(), orderListDto.getPageSize());
|
IPage<OtcEntrustOrder> result = this.baseMapper.selectOwnEntrustListInPage(query, page);
|
return OtcEntrustOrderMapper.INSTANCE.entrustToListInfoVoList(result.getRecords());
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void cancelEntrustOrder(Long id) {
|
MemberEntity member = LoginUserUtils.getAppLoginUser();
|
OtcEntrustOrder otcEntrustOrder = this.baseMapper.selectById(id);
|
if (otcEntrustOrder == null) {
|
throw new GlobalException("参数错误");
|
}
|
|
if (OtcEntrustOrder.LINE_CANCEL.equals(otcEntrustOrder.getStatus())) {
|
throw new GlobalException("请勿重复撤销");
|
}
|
|
List<OtcOrder> orders = otcOrderDao.selectOrderListUnFinish(member.getId(), otcEntrustOrder.getId());
|
if (CollUtil.isNotEmpty(orders)) {
|
throw new GlobalException("存在未完成的订单, 无法撤销");
|
}
|
|
if (OtcEntrustOrder.ORDER_TYPE_S.equals(otcEntrustOrder.getOrderType())) {
|
MemberWalletCoinEntity wallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
|
memberWalletCoinDao.subFrozenBalance(member.getId(), wallet.getId(), otcEntrustOrder.getRemainCoinAmount());
|
}
|
|
otcEntrustOrder = new OtcEntrustOrder();
|
otcEntrustOrder.setId(id);
|
otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_CANCEL);
|
this.baseMapper.updateById(otcEntrustOrder);
|
}
|
|
@Override
|
public Result findEntrustOrderDetail(Long id) {
|
OtcEntrustOrder otcEntrustOrder = this.baseMapper.selectById(id);
|
if (otcEntrustOrder == null) {
|
return Result.fail("参数错误");
|
}
|
|
MemberPaymentMethodEntity defualtMethod = memberPaymentMethodDao.selectDefualtMethod(otcEntrustOrder.getMemberId(), null, "1");
|
MemberEntity member = memberDao.selectById(otcEntrustOrder.getMemberId());
|
OtcMarketBussiness mb = otcMarketBussinessDao.selectMarketBussinessByMemberId(member.getId());
|
EntrustOrderDetailVo detail = OtcEntrustOrderMapper.INSTANCE.entityToOrderDetail(otcEntrustOrder);
|
detail.setName(member.getName());
|
if (OtcEntrustOrder.ORDER_TYPE_S.equals(otcEntrustOrder.getOrderType())) {
|
detail.setOrderCnt(mb.getSaleOrderCnt());
|
detail.setFinishRatio(mb.getSaleFinishRatio());
|
} else {
|
detail.setOrderCnt(mb.getBuyOrderCnt());
|
detail.setFinishRatio(mb.getFinishRatio());
|
}
|
detail.setPayType(defualtMethod.getPaymentType());
|
return Result.ok(detail);
|
}
|
}
|