Helius
2021-05-18 9739e12c89e42a2b445801fea0ad958c5f08281c
src/main/java/com/xcong/excoin/modules/otc/service/impl/OtcEntrustOrderServiceImpl.java
@@ -1,14 +1,204 @@
package com.xcong.excoin.modules.otc.service.impl;
import cn.hutool.core.collection.CollUtil;
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.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.OtcEntrustOrder;
import com.xcong.excoin.modules.otc.dao.OtcEntrustOrderMapper;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Service
@RequiredArgsConstructor
public class OtcEntrustOrderServiceImpl extends ServiceImpl<OtcEntrustOrderMapper, OtcEntrustOrder> implements OtcEntrustOrderService {
public class OtcEntrustOrderServiceImpl extends ServiceImpl<OtcEntrustOrderDao, OtcEntrustOrder> implements OtcEntrustOrderService {
    private final OtcMarketBussinessDao otcMarketBussinessDao;
    private final MemberDao memberDao;
    private final MemberPaymentMethodDao memberPaymentMethodDao;
    private final MemberWalletCoinDao memberWalletCoinDao;
    private final OtcOrderDao otcOrderDao;
    private final CommonService commonService;
    @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 (OtcEntrustOrder.ORDER_TYPE_S.equals(addDto.getType())) {
            MemberWalletCoinEntity coinWallet = memberWalletCoinDao.selectWalletCoinBymIdAndCode(member.getId(), "USDT");
            if(coinWallet.getAvailableBalance().compareTo(totalAmount) < 0) {
                throw new GlobalException("可用金额不足");
            }
            memberWalletCoinDao.updateFrozenBalance(member.getId(), coinWallet.getId(), totalAmount);
        }
        OtcMarketBussiness mb = otcMarketBussinessDao.selectMarketBussinessByMemberId(member.getId());
        if (mb == null) {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
        } else {
            otcEntrustOrder.setMbId(mb.getId());
            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);
        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("类型错误");
        }
        OtcMarketBussiness mb = otcMarketBussinessDao.selectMarketBussinessByMemberId(member.getId());
        if (mb == null) {
            otcEntrustOrder.setIsMb(OtcEntrustOrder.IS_MB_N);
        } else {
            otcEntrustOrder.setMbId(mb.getId());
            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(coinWallet.getFrozenBalance()));
            if(coinWallet.getAvailableBalance().compareTo(totalAmount) < 0) {
                throw new GlobalException("可用金额不足");
            }
            coinWallet.setAvailableBalance(coinWallet.getAvailableBalance().subtract(totalAmount));
            coinWallet.setFrozenBalance(totalAmount);
            memberWalletCoinDao.updateById(coinWallet);
        }
        this.baseMapper.updateById(otcEntrustOrder);
    }
    @Override
    public IPage<EntrustListVo> findEntrustListInPage(EntrustOrderListDto dto) {
        Page<EntrustListVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return this.baseMapper.selectEntrustListInPage(dto, page);
    }
    @Override
    public List<EntrustListInfoVo> findOwnEntrustOrder() {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder query = new OtcEntrustOrder();
        query.setStatus(3);
        query.setMemberId(member.getId());
        List<OtcEntrustOrder> otcEntrustOrders = this.baseMapper.selectEntrustOrderByOrderType(query);
        return OtcEntrustOrderMapper.INSTANCE.entrustToListInfoVoList(otcEntrustOrders);
    }
    @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(), wallet.getFrozenBalance());
        }
        otcEntrustOrder = new OtcEntrustOrder();
        otcEntrustOrder.setId(id);
        otcEntrustOrder.setStatus(OtcEntrustOrder.LINE_CANCEL);
        this.baseMapper.updateById(otcEntrustOrder);
    }
    @Override
    public Result findEntrustOrderDetail(Long id) {
        MemberEntity member = LoginUserUtils.getAppLoginUser();
        OtcEntrustOrder otcEntrustOrder = this.baseMapper.selectById(id);
        if (otcEntrustOrder == null) {
            return Result.fail("参数错误");
        }
        if (!member.getId().equals(otcEntrustOrder.getMemberId())) {
            return Result.fail("请求有误");
        }
        EntrustOrderDetailVo detail = OtcEntrustOrderMapper.INSTANCE.entityToOrderDetail(otcEntrustOrder);
        return Result.ok(detail);
    }
}